Compare fields


(Pjanzen) #1

Hi all,

So I am trying to compare two fields from a specific progam. I am trying it with this.

{
  "query": {
    "bool": {
      "must": [{
        "script": {
          "script": {
            "inline": "doc['email.keyword'].value == params.param1",
            "lang": "painless",
            "params": {
              "param1": "doc['redirect.keyword'].value"
            }
          }
        },
        "match": {
          "program": "get_forwarding_address"
        }
      }]
    }
  }
}

However I am still getting the following error.

{
  "error": {
    "root_cause": [
      {
        "type": "parsing_exception",
        "reason": "[script] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
        "line": 14,
        "col": 9
      }
    ],
    "type": "parsing_exception",
    "reason": "[script] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
    "line": 14,
    "col": 9
  },
  "status": 400
}

Could someone please advice on how to correctly setup this query?

Thanks,
Paul.


(Val Crettaz) #2

In the parameters section, you cannot reference a field from the document.

Why not simply doing this?

      "script": {
        "inline": "doc['email.keyword'].value == doc['redirect.keyword'].value",
        "lang": "painless",
        "params": {
        }
      }

(Pjanzen) #3

That still produces the same error:

 "type": "parsing_exception",
    "reason": "[script] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
    "line": 14,
    "col": 9

(Val Crettaz) #4

If you're using ES 6, you need to replace inline by source. Could that be the reason?


(Pjanzen) #5

Sorry, I must have stated that in the intro, I am using ES 5


(Colin Goodheart-Smithe) #6

You can only put one query in each must clause in the must array, you need to separate your script and match queries into separate objects. However the replies by @val are also true and you would have reached those issues next I suspect.

Try this:

{
  "query": {
    "bool": {
      "must": [
        {
          "script": {
            "script": {
              "source": "doc['email.keyword'].value == doc['redirect.keyword'].value",
              "lang": "painless"
            }
          }
        },
        {
          "match": {
            "program": "get_forwarding_address"
          }
        } 
      ]
    }
  }
}

(Val Crettaz) #7

Well spotted @colings86 :wink:


(Pjanzen) #8

Thanks for the help all,

I have now this and it does exactly what I want :slight_smile:

{
  "_source": [
    "email"
  ],
  "query": {
    "bool": {
      "must": [
        {
          "script": {
            "script": {
              "inline": "doc['email.keyword'].value == doc['redirect.keyword'].value",
              "lang": "painless"
            }
          }
        },
        {
          "match": {
            "program": "get_forwarding_address"
          }
        }
      ]
    }
  }
}

(system) #9

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.