Alex Garrett
Alex Garrett

Reputation: 155

ElasticSearch filtering with geo distance

I'm attempting to filter data with both geo distance and fields like 'has_cctv' or 'has_instant_bookings'.

{
  "query" : {
    "filtered" : {
      "filter" : {
        "geo_distance": {
          "distance": 10000,
          "lat_lng": {
            "lat": "51.5073509",
            "lon": "-0.1277583"
          }
        }
      }
    }
  }
}

I've tried many combinations of filtering using terms but can't seem to get past errors. For example:

{
  "query" : {
    "filtered" : {
      "filter" : {
        "terms": [
          {"term": {"has_cctv": 1}}
        ],
        "geo_distance": {
          "distance": 10000,
          "lat_lng": {
            "lat": "51.5073509",
            "lon": "-0.1277583"
          }
        }
      }
    }
  }
}

This gives me '[terms] filter does not support [has_cctv] within lookup element'. Could this be a problem with my query, or a problem with the way the data is stored?

Upvotes: 1

Views: 2841

Answers (3)

Bharvi Dixit
Bharvi Dixit

Reputation: 69

Here goes the correct query:

POST _search
{
   "query": {
      "filtered": {
         "query": {
            "term": {
               "has_cctv": {
                  "value": 1
               }
            }
         },
         "filter": {
            "geo_distance": {
               "distance": 10000,
               "lat_lng": {
                  "lat": "51.5073509",
                  "lon": "-0.1277583"
               }
            }
         }
      }
   }
}

Just make sure that lat_lng is stored as geo_point

Thanks Bharvi

Upvotes: 2

Andrei Stefan
Andrei Stefan

Reputation: 52368

Or you could use an and filter and group the two filters together. And a comparison between bool filter and and/or/not filters: http://www.elasticsearch.org/blog/all-about-elasticsearch-filter-bitsets/

{
  "query": {
    "filtered": {
      "filter": {
        "and": {
          "filters": [
            {
              "term": {
                "has_cctv": "1"
              }
            },
            {
              "geo_distance": {
                "distance": 10000,
                "lat_lng": {
                  "lat": "51.5073509",
                  "lon": "-0.1277583"
                }
              }
            }
          ]
        }
      }
    }
  }
}

Upvotes: 2

dadoonet
dadoonet

Reputation: 14512

Two errors.

As you have more than one filter, you need to add a bool filter and put each filter in a must clause. Then you don't need a terms filter here but a term filter.

Upvotes: 1

Related Questions