Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elastic search geo point query filter

I am trying to use geo_point for distance but it always shows location type double not geo_point how can I set location mapped to geo_point .

Actually I have to find all records within 5km sorted.

 "pin" : {
             "properties" : {
             "location" : {
             "properties" : {
             "lat" : {
             "type" : "double"
             },
                 "lon" : {
                 "type" : "double"
                 }
              }
              },
                  "type" : {
                  "type" : "string"
                  }
              }
              },

and when I am trying searches with this query below to find result within 5km of delhi lat long :

 {
          "query": {
            "filtered": {
              "query": {
                "match_all": {}
              },
              "filter": {
                "pin": {
                  "distance": "5",
                  "distance_unit": "km",
                  "coordinate": {
                    "lat": 28.5402707,
                    "lon": 77.2289643
                  }
                }
              }
            }
          }
        }

It shows me error query_parsing_exception and No query registered for [pin]. I cannot figure out the problem. It always throws this exception

  {

            "error": {
                "root_cause": [
                    {
                        "type": "query_parsing_exception",
                        "reason": "No query registered for [pin]",
                        "index": "find_index",
                        "line": 1,
                        "col": 58
                    }
                ],
                "type": "search_phase_execution_exception",
                "reason": "all shards failed",
                "phase": "query",
                "grouped": true,
                "failed_shards": [
                    {
                        "shard": 0,
                        "index": "find_index",
                        "node": "DtpkEdLCSZCr8r2bTd8p5w",
                        "reason": {
                            "type": "query_parsing_exception",
                            "reason": "No query registered for [pin]",
                            "index": "find_index",
                            "line": 1,
                            "col": 58
                        }
                    }
                ]
            },
            "status": 400

        }

Please help me to figure out this problem. how can I set geo_point and solve this exception error and status 400 and all_shards_failed error

like image 288
deepak singh chaudhary Avatar asked Dec 06 '25 07:12

deepak singh chaudhary


1 Answers

You simply need to map your pin type like this, i.e. using the geo_point data type:

# 1. first delete your index
DELETE shouut_find_index

# 2. create a new one with the proper mapping
PUT shouut_find_index
{
  "mappings": {
    "search_shouut": {
      "properties": {
        "location": {
          "type": "geo_point"
        },
        "type": {
          "type": "string"
        }
      }
    }
  }
}

Then you can index a document like this

PUT shouut_find_index/search_shouut/1
{ 
   "location": {
      "lat": 28.5402707,
      "lon": 77.2289643
   },
   "type": "dummy"
}

And finally your query can look like this

POST shouut_find_index/search_shouut/_search
{
  "query": {
    "filtered": {
      "filter": {
        "geo_distance": {
          "distance": "5km",
          "location": {
            "lat": 28.5402707,
            "lon": 77.2289643
          }
        }
      }
    }
  }
}
like image 200
Val Avatar answered Dec 08 '25 20:12

Val



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!