0

In Elasticsearch, when I search by geo-distance to a point, can I at the same time filter by another attribute, such as a number being within a range, so that both filters need to be true for the result to come back?

1 Answer 1

2

Sure, use bool query, where you can specify multiple clauses in must and (or) filter blocks. Be aware that clauses in must block will contribute to the relevance score and clauses in filter block will not (read more about query and filter context).

For example, query that at same time search by geo-distance with contribution to score and filter an age being within a range without contribution to score:

{
  "query": { 
    "bool": { 
      "must": [
        { 
          "geo_distance": {
            "distance": "100km",
            "pin.location": {
              "lat": 38.889248,
              "lon": -77.050636
            }
          }
        }
      ],
      "filter": [ 
        { 
          "range": {
            "age": {
              "gte": 18,
              "lte": 65
            }
          }
        }
      ]
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

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.