在 ElasticSearch 上按距离排序

Sort by distance on ElasticSearch

我在弹性搜索中有这个查询以通过他们的名字获取地点:

GET places/fastfood/_search
{
    "from" : 0, "size" : 10, 
    "query": {
        "bool": {
         "must": [
                { "regexp": { "name": ".*name.*"} },           
                { "term" : { "location_id" : id } }                         
        ]
      }      
    }
}

替换名称和 ID 以匹配我需要的内容。 现在我想获取与名称和 ID 匹配但更接近所选位置的位置。假设我在提供的 location_id(城市、地区)没有麦当劳,我应该去离那个位置最近的地方。

我在每个city/zone位置都有一个字段,那是一个带有纬度、经度的字符串,定义为geo_spot。然后还将纬度和经度定义为长。像这样:

"location": "33.785400,-84.38480",
"latitude": "33.785400",
"longitude": "-84.384800",

所以我尝试添加按位置排序,就像这样,但我收到错误,出了点问题:

GET places/fastfood/_search
{
    "from" : 0, "size" : 10, 
    "query": {
        "bool": {
         "must": [
                { "regexp": { "name": ".*name.*"} },           
                { "term" : { "location_id" : location_id } }                         
        ]
      }      
    },
    "sort": [
    {
      "_geo_distance": {
        "location": { 
          "lat":  26.112,
          "lon": -73.998
        },
        "order":         "asc",
        "unit":          "km", 
        "distance_type": "plane" 
      }
    }
}

我做错了什么?我应该改变什么?

错误是这样的: { "error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[n6y6tjWFRrCP5MXNhEV-YA][places_v1][0]: RemoteTransportException[[Boomer][inet[/x.x.x.x:y]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[places_v1][0]: query[+name:/.hosp./ +location_id:[10 TO 10]],from[0],size[10]: Parse Failure [Failed to parse source [{\r\n \"从\" : 0, \"size\" : 10, \r\n \"query\": {\r\n \"bool\" : {\r\n \"must\": [\r\n { \"regexp\": { \"name\": \".hosp.\"} }, \r\n { \"term\" : { \"location_id\" : 10 } } \r\n ]\r\n } \r\n },\r\n \"sort\": [\r\n {\r\n \"_geo_distance\": {\r\n \"location\": { \r\n \"lat\": 26.112,\r\n \"lon\": -73.998\r\n },\r\n \"order\": \"asc\" ,\r\n \"unit\": \"km\", \r\n \"distance_type\": \"plane\" \r\n }\r\n } \r\n}\n]]];嵌套:JsonParseException[意外的关闭标记“}”:预期的“]”(对于从 [Source: [B@4ac877f6; line: 10, column: 5] 开始的 ARRAY)\ n 在 [来源:未知;行:23,列:2]];}{[n6y6tjWFRrCP5MXNhEV-YA][places_v1][1]:RemoteTransportException[[Boomer][inet[/x.x.x.x: y]][索引:data/read/search[phase/query]]];嵌套:SearchParseException[[places_v1][1]:查询[+na me:/.hosp./ +location_id:[10 TO 10]],from[0],size[10]: 解析失败 [无法解析源 [{ \r\n \"from\" : 0, \"size\" : 10, \r\n \"query\": {\r\n \"bool\": {\r\n \"must\": [\r\n { \"regexp\": { \"name\": \".hosp.\" } }, \r\n { \"term\" : { \"location_id\" : 10 } } \r\n ]\r\n } \r\n },\r\n \"sort\": [\r\n {\r\n \"_geo_distance\": {\r\n \"location\": { \r\n \ "lat\": 26.112,\r\n \"lon\": -73.998\r\n },\r\n \"order\": \"asc\",\r\n \"unit\": \"km\", \r\n \"distance_type\": \"plane\" \r\n }\r\n }\r\n}\n]]];嵌套:JsonParseException[意外关闭标记'}':预期']'(对于从[来源:[B@19f9b52f;行:10,列:5]开始的ARRAY)\n在[来源:未知;行:23,列:2]]; }{[7i0nOGiFRl-OYX8XvL_bSQ][places_v1][2]: SearchParseException[[places_v1][2]: query[+name:/.hosp./ +location_id:[10 TO 10]],from[0],size[10]: 解析失败 [无法解析源 [{\r\n \"from\" : 0, \"size\" : 10, \r\n \"query\": {\r\n \"bool\": {\r\n \"must\": [\r\n { \"regexp\": { \"name\": \".hosp.\"} }, \r\n { \"term\" : { \"location_id\" : 10 } } \r\n ]\r\n } \r\n },\r\n \"sort\": [\r\n {\r\n \"_geo_distance\": {\r\n \"location\": { \r\n \"lat\": 26.112,\r\n \ "lon\": -73.998\r\n },\r\n \"order\": \"asc\",\r\n \"unit\": \"km\", \r\n \"distance_type\": \"plane\" \r\n }\r\n }\r\n}\n]]];嵌套:JsonParseException[意外关闭标记'}':预期']'(对于从[来源:[B@427094b6;行:10,列:5]开始的ARRAY)\n在[来源:[B@427094b6;行:23,列:2]]; }{[n6y6tjWFRrCP5MXNhEV-YA][places_v1][3]:RemoteTransportException[[Boomer][inet[/x.x.x.x:y]][指数:data/read/search[phase/query ]]];嵌套:SearchParseException[[places_v1][3]: query[+name:/.hosp./ +location_id:[10 TO 10]],from [0],size[10]: 解析失败 [无法解析源 [{\r\n \"from\" : 0, \"size\" : 10, \r\n \"query\": {\r\n \"bool\": {\r\n \"must\": [\r\n { \"regexp\": { \"name\" : \".hosp.\"} }, \r\n { \"term\" : { \"location_id\" : 10 } } \r\n ]\r\n } \r\n },\r\n \"sort\": [\r\n {\r\n \"_geo_distance\": {\r\n \"location\": { \r\n \"lat\": 26.112,\r\n \"lon\": -73.998\r\n },\r\n \"order\": \"asc\",\r\n \"unit\": \"km\", \r\n \"distance_type\": \"plane\" \r\n }\r\n }\r\n}\n]]];嵌套:JsonParseException[意外的关闭标记'}':预期的']'(对于从[来源:[B@4f781e6c;行:10,列:5]开始的ARRAY)\n在[来源:未知;行:23,列:2]]; }{[n6y6tjWFRrCP5MXNhEV-YA][places_v1][4]:RemoteTransportException[[Boomer][inet[/x.x.x.x:y]][指数:data/read/search[phase/query ]]];嵌套:SearchParseException[[places_v1][4]: query[+name:/.hosp./ +location_id:[10 TO 10]],from [0],size[10]: 解析失败 [无法解析源 [{\r\n \"from\" : 0, \"size\" : 10, \r\n \"query\": {\r\n \"bool\": {\r\n \"must\": [\r\n { \"regexp\": { \"name\" : \".hosp.\"} }, \r\n { \"term\" : { \"location_id\" : 10 } } \r\n ]\r\n } \r\n },\r\n \"sort\": [\r\n {\r\n \"_geo_distance\": {\r\n \"location\": { \r\n \"lat\": 26.112,\r\n \"lon\": -73.998\r\n },\r\n \"order\": \"asc\",\r\n \"unit\": \"km\", \r\n \"distance_type\": \"plane\" \r\n }\r\n }\r\n}\n]]];嵌套:JsonParseException[意外的关闭标记'}':预期的']'(对于从[来源:[B@586764c7;行:10,列:5]开始的ARRAY)\n在[来源:未知;行:23,列:2]]; }]", "status": 400 }

这是语法错误,您应该使用 ] 而不是 }

来结束排序