Rajiv Srivastava
Rajiv Srivastava

Reputation: 339

How to merge geo distance filter with bool term query

How to use Elasticsearch 1.6/1.7 version geo distance filter with bool term query like this. How and here two merge these two queries

Original query:

{
  "query": {
        "bool": {
          "must": [
            {
              "term": {
                "categories": "tv"
              }
            }
          ],
          "should": [
            {
              "term": {
                "subCategory": "led"
              }
            }
          ],
          "minimum_should_match": 1,
          "boost": 2
        }
      }
    }

I want to search products with above bool query with distance of 10 miles

{
  "filtered": {
    "filter": {
      "geo_distance": {
        "distance": "10km",
        "sellerInfoES.address.sellerLocation": "28.628978,77.21971479999999"
      }
    }
  }
}

Thanks Val! Query is working, I am not getting any query parsing error. However this geo query is not returning and distance range result. I am using Elasticsearch 1.6 and stored sellerLocation as geo_point.Mapping:

{
  "SellerInfoES": {
    "type": "nested",
    "properties": {
      "sellerLocation": {
        "type": "geo_point"
      }
    }
  }
}

This geo_query is not working

{
  "geo_distance": {
    "distance": "100km",
    "sellerLocation": {
      "lat": 28.628978,
      "lon": 77.21971479999999
    }
  }
}

Upvotes: 0

Views: 1619

Answers (1)

Val
Val

Reputation: 217564

You can combine both query/filters like this:

{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "categories": "tv"
              }
            },
            {
              "nested": {
                 "path": "sellerInfoES",
                 "filter": {
                    "geo_distance": {
                       "distance": "10km",
                       "sellerInfoES.sellerLocation": {
                           "lat": "28.628978",
                           "lon":"77.21971479999999"
                       }
                    }
                 }
              }
            }
          ],
          "should": [
            {
              "term": {
                "subCategory": "led"
              }
            }
          ],
          "minimum_should_match": 1,
          "boost": 2
        }
      }
    }
  }
}

Upvotes: 1

Related Questions