# Compare fields

**URL:** <https://discuss.elastic.co/t/compare-fields/114514>\
**Category:** Elasticsearch\
**Created:** [January 8, 2018, 11:46am UTC](https://discuss.elastic.co/t/compare-fields/114514 "2018-01-08T11:46:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![pjanzen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pjanzen/32/13756_2.png) [@pjanzen](https://discuss.elastic.co/u/pjanzen)\
**Post date:** [January 8, 2018, 11:46am UTC](https://discuss.elastic.co/t/compare-fields/114514/1 "2018-01-08T11:46:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)\
**Post date:** [January 8, 2018, 11:51am UTC](https://discuss.elastic.co/t/compare-fields/114514/2 "2018-01-08T11:51:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pjanzen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pjanzen/32/13756_2.png) [@pjanzen](https://discuss.elastic.co/u/pjanzen)\
**Post date:** [January 8, 2018, 11:55am UTC](https://discuss.elastic.co/t/compare-fields/114514/3 "2018-01-08T11:55:30Z")

</div>

That still produces the same error:

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

---

<div class="post-metadata">

**Author:** ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)\
**Post date:** [January 8, 2018, 11:57am UTC](https://discuss.elastic.co/t/compare-fields/114514/4 "2018-01-08T11:57:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pjanzen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pjanzen/32/13756_2.png) [@pjanzen](https://discuss.elastic.co/u/pjanzen)\
**Post date:** [January 8, 2018, 12:00pm UTC](https://discuss.elastic.co/t/compare-fields/114514/5 "2018-01-08T12:00:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [January 8, 2018, 12:00pm UTC](https://discuss.elastic.co/t/compare-fields/114514/6 "2018-01-08T12:00:20Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)\
**Post date:** [January 8, 2018, 12:02pm UTC](https://discuss.elastic.co/t/compare-fields/114514/7 "2018-01-08T12:02:26Z")

</div>

Well spotted @colings86 😉

---

<div class="post-metadata">

**Author:** ![pjanzen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pjanzen/32/13756_2.png) [@pjanzen](https://discuss.elastic.co/u/pjanzen)\
**Post date:** [January 8, 2018, 12:10pm UTC](https://discuss.elastic.co/t/compare-fields/114514/8 "2018-01-08T12:10:49Z")

</div>

Thanks for the help all,

I have now this and it does exactly what I want 🙂

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 5, 2018, 12:10pm UTC](https://discuss.elastic.co/t/compare-fields/114514/9 "2018-02-05T12:10:53Z")

</div>

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