Search code examples
elasticsearchelasticsearch-dsl

How to query elasticsearch for a field value being greater than and less than another field?


I have a data collection with two fields y_hat and y.

I want to query it from elasticsearch where y_hat is larger than y value.

How do I do the query?


Solution

  • You can use Script Query from Elasticsearch. Below query will return document only if field y_hat value is grater then field y value.

    {
      "query": {
        "bool": {
          "filter": {
            "script": {
              "script": """
                return doc['y_hat'].value > doc['y'].value;
              """
            }
          }
        }
      }
    }