Query in ElasticSearch comparing properties in a document

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:

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

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

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.

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

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

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

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