Geo, Date, and string matching elasticsearch query

Viewed 63

I have an elasticsearch index called tweets, I'm trying to filter these tweets by geo_distance, date, and optional string that might be passed. How should this query be like to meet my requirements

       "query":{
          "bool": {
             "must": [
                {
                   "term": {
                      "text": "ew"
                   }
                },
                {
                   "range": {
                      "date": {
                         "gte": "2009-05-31T02:30:45",
                         "lt": "now"
                      }
                   }
                },
                {
                   "filter": {
                      "geo_distance": {
                         "distance": "12km",
                         "pin.location": [-70, 40]
                      }
                   }
                }
              ]
           }
     }

Thanks in advance

1 Answers

You can use the combination of filter and should clause, to achieve your required result.

filter is used similar to logical AND operator, where the score of the query is ignored, and should is similar to logical OR operator.

Try out this below query:

{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "date": {
              "gte": "2009-05-31T02:30:45",
              "lt": "now"
            }
          }
        },
        {
          "geo_distance": {
            "distance": "12km",
            "pin.location": [
              -70,
              40
            ]
          }
        }
      ],
      "should": {
        "bool": {
          "filter": {
            "term": {
              "text": "ew"
            }
          }
        }
      }
    }
  }
}
Related