1

I have the following query in Elasticsearch:

{
  "script_fields": {
    "travel_time": {
      "script": {
        "inline": "doc['DateTo'].value - doc['DateFrom'].value"
      }
    }
  }, 
  "stored_fields": [
    "_source"
    ], 
  "query": {
    "bool": { 
      "filter": {
        "exists": {
          "field": "DateTo"
        }
      }
    }
  }
}

How can I add DateFrom into exists filter?

2
  • which version of elasticsearch do you use? Commented Oct 19, 2017 at 14:53
  • @TobiasGassmann: 5.5 Commented Oct 19, 2017 at 14:53

1 Answer 1

1

You can add multiple exists criteria:

  "query": {
    "bool": { 
      "filter": [
       {
         "exists": {
           "field": "DateFrom"
         }
       },
       {
         "exists": {
           "field": "DateTo"
         }
       },
       {
         "script": {
           "script": {
             "inline": "doc['DateTo'].value - doc['DateFrom'].value > 0"
           }
         }
       }
      ]
    }
  }
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, ok. Can I also add the filter that "doc['DateTo'].value - doc['DateFrom'].value" should be a positive numeric value?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.