Phoenix
Phoenix

Reputation: 8923

Why am I not able to query by geolocation in elastic search?

So I am experimenting with ElasticSearch and this is what I did.

1) Added a geo_point mapping on my index called "test"

curl -XPOST localhost:9200/test -d '{
    "mappings" : {
        "type1" : {
            "_source" : { "enabled" : false },
            "properties" : {
                "location" : { "type" : "geo_point", "index" : "not_analyzed" }
            }
        }
    }
}'

2) Indexed a document under test:

curl -XPUT 'localhost:9200/test/type1/1?pretty' -d '{
 "location" : {
                "lat" : 74,           
                "lon" : 90            
              }                      
}'  

3) Then wrote a query by geolocation filter like this:

curl -XPOST 'localhost:9200/test2/_search?pretty' -d '{
    "filtered" : {
        "query" : {
            "match_all" : {}
},
        "filter" : {
            "geo_distance" : {
                "distance" : "200km",
                "location" : {
                    "lat" : 40,
                    "lon" : -70
                }
            }
        }
    }
}'

For this I get:

"error" : "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[QpGEHtdcReeYmt8X2tG26g][test2][0]: RemoteTransportException[[Jester][inet[/10.58.91.21:9301]][search/phase/query]]; nested: SearchParseException[[test2][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [na]]]; nested: ElasticsearchParseException[Failed to derive xcontent from org.elasticsearch.common.bytes.ChannelBufferBytesReference@60d8bc76];

Upvotes: 1

Views: 543

Answers (1)

Val
Val

Reputation: 217314

First off, in your query (i.e. the 3rd piece of code), localhost:9200/test2/_search?pretty should be localhost:9200/test/_search?pretty, i.e. you're not querying the correct index.

Then your query is simply missing the query keyword (i.e. filtered should be enclosed in a query), it should look like this:

curl -XPOST 'localhost:9200/test/_search?pretty' -d '{
    "query": {
        "filtered": {
            "query": {
                "match_all": {}
            },
            "filter": {
                "geo_distance": {
                    "distance": "200km",
                    "location": {
                        "lat": 40,
                        "lon": -70
                    }
                }
            }
        }
    }
}

Upvotes: 0

Related Questions