3

I have created and index the same as the example tutorials, in here...

https://www.elastic.co/guide/en/elasticsearch/reference/2.0/geo-point.html

in specific writing the following:

curl -PUT 'localhost:9200/my_index?pretty' -d '
{
  "mappings": {
    "my_type": {
      "properties": {
        "location": {
          "type": "geo_point"
        }
      }
    }
  }
}'

I have also added two points as data

curl -PUT 'localhost:9200/my_index/my_type/1?pretty' -d'
{
  "text": "first geo-point",
  "location": { 
    "lat": 41.12,
    "lon": -71.34
  }
}'

curl -PUT 'localhost:9200/my_index/my_type/1?pretty' -d'
{
  "text": "second geo-point",
  "location": { 
    "lat": 41.13,
    "lon": -71.35
  }
}'

The example geo bounding box query on the page works (i.e):

curl -XGET 'localhost:9200/my_index/_search?pretty' -d'
{
  "query": {
    "geo_bounding_box": { 
      "location": {
        "top_left": {
          "lat": 42,
          "lon": -72
        },
        "bottom_right": {
          "lat": 40,
          "lon": -74
        }
      }
    }
  }
}'

But the example from this page (https://www.elastic.co/guide/en/elasticsearch/reference/2.0/query-dsl-geo-bounding-box-query.html) doesn't work:

What I have tried looks like the following:

curl -XGET 'localhost:9200/my_index/_search?pretty' -d'
{
    "bool" : {
        "must" : {
            "match_all" : {}
        },
        "filter" : {
            "geo_bounding_box" : {
                "my_type.location" : {
                    "top_left" : {
                        "lat" : 42,
                        "lon" : -72
                    },
                    "bottom_right" : {
                        "lat" : 40,
                        "lon" : -74
                    }
                }
            }
        }
    }
}'

The error I get is as follows:

"error" : {
    "root_cause" : [ {
      "type" : "search_parse_exception",
      "reason" : "failed to parse search source. unknown search element [bool]",
      "line" : 3,
      "col" : 5
    } ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [ {
      "shard" : 0,
      "index" : "my_index",
      "node" : "0qfvkynhTRyjHFRurBLJeQ",
      "reason" : {
        "type" : "search_parse_exception",
        "reason" : "failed to parse search source. unknown search element [bool]",
        "line" : 3,
        "col" : 5
      }
    } ]
  },
  "status" : 400
}

I hope its just a simple error, so would like to know what am i doing wrong?

1 Answer 1

7

You need to specify that the whole thing is a query:

curl -XGET 'localhost:9200/my_index/_search?pretty' -d'
{
   "query": {
      "bool" : {
          "must" : {
              "match_all" : {}
          },
          "filter" : {
              "geo_bounding_box" : {
                  "my_type.location" : {
                        "top_left" : {
                          "lat" : 42,
                          "lon" : -72
                      },
                      "bottom_right" : {
                          "lat" : 40,
                          "lon" : -74
                      }
                  }
              }
          }
      }
   }
}'

However as far as I understand using bool with must and filter is the old way of doing things. In previous versions, geo queries were thought of as "filters", so you had to first run a match_all query to return all the results, and then filter using the geo bounding box. In Elasticssearch 2.0+, there is no separation between filters and queries - everything is a query. So you can run the geo query directly:

curl -XGET 'localhost:9200/my_index/_search?pretty' -d'
{
  "query": {
    "geo_bounding_box": { 
      "location": {
        "top_left": {
          "lat": 42,
          "lon": -72
        },
        "bottom_right": {
          "lat": 40,
          "lon": -74
        }
      }
    }
  }
}'
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.