按ElasticSearch

时间:2017-03-08 14:55:45

标签: elasticsearch geo

我在弹性搜索中有这个查询来获取他们名字的地方:

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

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

我在每个名为location的城市/区域都有一个字段,这是一个包含纬度,经度,定义为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" 
      }
    }
}

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

错误是这样的: {    "错误":" SearchPhaseExecutionException [无法执行阶段[查询],所有分片都失败; shardFailures {[n6y6tjWFRrCP5MXNhEV-YA] [places_v1] [0]:RemoteTransportException [[Boomer] [inet [/x.x.x.x:y]] [indices:data / read / search [phase / query]]];嵌套:SearchParseException [[places_v1] [0]:query [+ name:/。 hosp。 / + location_id:[10 TO 10]],来自[0],size [10]:Parse Failure [无法解析源[{\ r \ n \"来自\" :0,\"尺寸\" :10,\ r \ n \"查询\":{\ r \ n \" bool \":{\ r \ n \"必须\&# 34;:[\ r \ n {\" regexp \":{\" name \":\"。 hosp。 \ "}},\ r \ n {\" term \" :{\" location_id \" :10}} \ r \ n] \ r \ n} \ r \ n},\ r \ n \" sort \":[\ r \ n {\ r \ n \&#34 ; _geo_distance \":{\ r \ n \" location \":{\ r \ n \" lat \":26.112,\ r \ n \& #34; lon \":-73.998 \ r \ n},\ r \ n \" order \":\" asc \",\ r \ n \" unit \":\" km \",\ r \ n \" distance_type \":\" plane \&#34 ; \ r \ n} \ r \ n} \ r \ n} \ n \ n]];嵌套:JsonParseException [意外关闭标记'}':预期']' (对于ARRAY,从[来源:[B @ 4ac877f6;第10行,第5列]开始)\ n在[来源:UNKNOWN; line:23,column:2]]; } {[n6y6tjWFRrCP5MXNhEV-YA] [places_v1] [1]:RemoteTransportException [[Boomer] [inet [/x.x.x.x:y]] [indices:data / read / search [phase / query]]];嵌套:SearchParseException [[places_v1] [1]:query [+ name:/。 hosp。 / + location_id:[10 TO 10]],来自[0],size [10]:Parse Failure [无法解析源[{\ r \ n \"来自\" :0,\"尺寸\" :10,\ r \ n \"查询\":{\ r \ n \" bool \":{\ r \ n \"必须\&# 34;:[\ r \ n {\" regexp \":{\" name \":\"。 hosp。 \ "}},\ r \ n {\" term \" :{\" location_id \" :10}} \ r \ n] \ r \ n} \ r \ n},\ r \ n \" sort \":[\ r \ n {\ r \ n \&#34 ; _geo_distance \":{\ r \ n \" location \":{\ r \ n \" lat \":26.112,\ r \ n \& #34; lon \":-73.998 \ r \ n},\ r \ n \" order \":\" asc \",\ r \ n \" unit \":\" km \",\ r \ n \" distance_type \":\" plane \&#34 ; \ r \ n} \ r \ n} \ r \ n} \ n \ n]];嵌套:JsonParseException [意外关闭标记'}':预期']' (对于ARRAY,从[来源:[B @ 19f9b52f;第10行,第5列]开始)\ n在[来源:UNKNOWN; line:23,column:2]]; } {[7i0nOGiFRl-OYX8XvL_bSQ] [places_v1] [2]:SearchParseException [[places_v1] [2]:query [+ name:/。 hosp。 / + location_id:[10 TO 10]],从[0]开始,大小[10]:解析失败[无法解析源[{\ r \ n \"来自\" :0,\"尺寸\" :10,\ r \ n \"查询\":{\ r \ n \" bool \":{\ r \ n \"必须\&# 34;:[\ r \ n {\" regexp \":{\" name \":\"。 hosp。 \ "}},\ r \ n {\" term \" :{\" location_id \" :10}} \ r \ n] \ r \ n} \ r \ n},\ r \ n \" sort \":[\ r \ n {\ r \ n \&#34 ; _geo_distance \":{\ r \ n \" location \":{\ r \ n \" lat \":26.112,\ r \ n \& #34; lon \":-73.998 \ r \ n},\ r \ n \" order \":\" asc \",\ r \ n \" unit \":\" km \",\ r \ n \" distance_type \":\" plane \&#34 ; \ r \ n} \ r \ n} \ r \ n} \ n \ n]];嵌套:JsonParseException [意外关闭标记'}':预期']' (对于ARRAY,从[来源:[B @ 427094b6;第10行,第5栏]开始)\ n在[来源:[B @ 427094b6; line:23,column:2]]; } {[n6y6tjWFRrCP5MXNhEV-YA] [places_v1] [3]:RemoteTransportException [[Boomer] [inet [/x.x.x.x:y]] [indices:data / read / search [phase / query]]];嵌套:SearchParseException [[places_v1] [3]:query [+ name:/。 hosp。 / + location_id:[10 TO 10]],来自[0],size [10]:Parse Failure [无法解析源[{\ r \ n \"来自\" :0,\"尺寸\" :10,\ r \ n \"查询\":{\ r \ n \" bool \":{\ r \ n \"必须\&# 34;:[\ r \ n {\" regexp \":{\" name \":\"。 hosp。 \ "}},\ r \ n {\" term \" :{\" location_id \" :10}} \ r \ n] \ r \ n} \ r \ n},\ r \ n \" sort \":[\ r \ n {\ r \ n \&#34 ; _geo_distance \":{\ r \ n \" location \":{\ r \ n \" lat \":26.112,\ r \ n \& #34; lon \":-73.998 \ r \ n},\ r \ n \" order \":\" asc \",\ r \ n \" unit \":\" km \",\ r \ n \" distance_type \":\" plane \&#34 ; \ r \ n} \ r \ n} \ r \ n} \ n \ n]];嵌套:JsonParseException [意外关闭标记'}':预期']' (对于ARRAY,从[来源:[B @ 4f781e6c;第10行,第5列]开始)\ n在[来源:UNKNOWN; line:23,column:2]]; } {[n6y6tjWFRrCP5MXNhEV-YA] [places_v1] [4]:RemoteTransportException [[Boomer] [inet [/x.x.x.x:y]] [indices:data / read / search [phase / query]]];嵌套:SearchParseException [[places_v1] [4]:query [+ name:/。 hosp。 / + location_id:[10 TO 10]],来自[0],size [10]:Parse Failure [无法解析源[{\ r \ n \"来自\" :0,\"尺寸\" :10,\ r \ n \"查询\":{\ r \ n \" bool \":{\ r \ n \"必须\&# 34;:[\ r \ n {\" regexp \":{\" name \":\"。 hosp。 \ "}},\ r \ n {\" term \" :{\" location_id \" :10}} \ r \ n] \ r \ n} \ r \ n},\ r \ n \" sort \":[\ r \ n {\ r \ n \&#34 ; _geo_distance \":{\ r \ n \" location \":{\ r \ n \" lat \":26.112,\ r \ n \& #34; lon \":-73.998 \ r \ n},\ r \ n \" order \":\" asc \",\ r \ n \" unit \":\" km \",\ r \ n \" distance_type \":\" plane \&#34 ; \ r \ n} \ r \ n} \ r \ n} \ n \ n]];嵌套:JsonParseException [意外关闭标记'}':预期']' (对于ARRAY,从[来源:[B @ 586764c7;第10行,第5列]开始)\ n在[来源:UNKNOWN; line:23,column:2]]; }]&#34 ;,    "状态":400 }

1 个答案:

答案 0 :(得分:1)

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