Compare fields

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.

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": {
        }
      }
1 Like

That still produces the same error:

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

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

1 Like

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

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"
          }
        } 
      ]
    }
  }
}
1 Like

Well spotted @colings86 :wink:

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"
          }
        }
      ]
    }
  }
}

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