1

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?

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

I had written query for a single term matching. If you have multiple conditions you can write down a bool query like : "query": { "bool": { "must": [ { "term": { "has_cctv": { "value": 1 } } } ],"must": [ {"term": { "has_instant_bookings": { "value": 1 } }} ] } }
Your comment underneath this post solved it, thank you so much! You might want to update the original answer for anyone else who's having a similar issue can see the answer a little clearer.
2

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"
                }
              }
            }
          ]
        }
      }
    }
  }
}

1 Comment

the sort by distance feature is lost if you lose this , it seems.
1

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.

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.