# Query in ElasticSearch comparing properties in a document

**URL:** <https://discuss.elastic.co/t/query-in-elasticsearch-comparing-properties-in-a-document/301246>\
**Category:** Elasticsearch\
**Created:** [March 31, 2022, 7:03pm UTC](https://discuss.elastic.co/t/query-in-elasticsearch-comparing-properties-in-a-document/301246 "2022-03-31T19:03:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![TomTom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tomtom/32/3676_2.png) [@TomTom](https://discuss.elastic.co/u/TomTom)\
**Post date:** [March 31, 2022, 7:03pm UTC](https://discuss.elastic.co/t/query-in-elasticsearch-comparing-properties-in-a-document/301246/1 "2022-03-31T19:03:48Z")

</div>

Is it possible in Elasticsearch to search for documents that have two equal/different properties or a property in the document root is equal/different to a property in a nested property?

For example, I have a document that stores orders, inside it I have a property that stores the delivery address ( `delivery_addres` ), and inside that document I have an object with the customer's data, including the address where he lives ( `customer.addres` ).

I would like to search all addresses where the delivery address is different from the customer address where he lives in, but I don't know if Elasticsearch supports this type of search.

My Mapping:

```auto
"mappings": {
    "properties": {
        "delivery_addres": {
            "type": "text",
            "index": true
        }
        "customer": {
            "properties": {
                "addres": {
                    "type": "text",
                    "index": true
                }
            }
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [April 1, 2022, 1:54am UTC](https://discuss.elastic.co/t/query-in-elasticsearch-comparing-properties-in-a-document/301246/2 "2022-04-01T01:54:26Z")

</div>

It doesn't natively, no, it's something you'd need to do in your own code.

---

<div class="post-metadata">

**Author:** ![TomTom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tomtom/32/3676_2.png) [@TomTom](https://discuss.elastic.co/u/TomTom)\
**Post date:** [April 4, 2022, 4:06pm UTC](https://discuss.elastic.co/t/query-in-elasticsearch-comparing-properties-in-a-document/301246/4 "2022-04-04T16:06:59Z")

</div>

Maybe go through existing customer records directly in my database/other index and check customer by customer for all orders where the address sent through the query is different from the property?

**Edit:**  
Could the use of scripts perhaps solve this problem? For example, using Painless to search both the child node and the parent.

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [April 4, 2022, 10:24pm UTC](https://discuss.elastic.co/t/query-in-elasticsearch-comparing-properties-in-a-document/301246/5 "2022-04-04T22:24:54Z")

</div>

Maybe, that's outside my area of knowledge sorry.

---

<div class="post-metadata">

**Author:** ![Iker](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/iker/32/91708_2.png) [@Iker](https://discuss.elastic.co/u/Iker)\
**Post date:** [April 5, 2022, 1:37am UTC](https://discuss.elastic.co/t/query-in-elasticsearch-comparing-properties-in-a-document/301246/6 "2022-04-05T01:37:46Z")

</div>

It's possible with a scripted query; something like this should work:

```auto
{
  "query": {
    "bool": {
      "must": [
        {
          "script": {
            "script": {
              "inline": "doc['delivery_addres'].value.compareTo( doc['customer.addres'].value ) == 0",
              "lang": "painless"
            }
          }
        }
      ]
    }
  }
}

```

---

<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:** [May 3, 2022, 1:38am UTC](https://discuss.elastic.co/t/query-in-elasticsearch-comparing-properties-in-a-document/301246/7 "2022-05-03T01:38:09Z")

</div>

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