Php弹性搜索按地理距离排序

时间:2020-10-06 08:26:17

标签: php laravel elasticsearch

我正在尝试按公里数排序 我的字段类型是 "location": { "properties": { "lat": { "type": "float" }, "lon": { "type": "float" } } },

在我的索引中,其存储类似"location": { "lon": 72.9814762, "lat": 33.7013063 },

和查询中

2 个答案:

答案 0 :(得分:0)

@apokryfos是正确的,您必须将数据映射为地理位置。 之后,您就可以对_geo_distance进行排序

"sort": [
    {
      "_geo_distance": {
        "coords": {
          "lat": -27.87,
          "lon": -54.43
        },
        "order": "asc",
        "unit": "km",
        "mode": "min",
        "distance_type": "arc",
        "ignore_unmapped": true
      }
    }
  ]

答案 1 :(得分:0)

您必须手动进行映射。请在下面找到映射示例(适用于ES 7.4.0)。第一个命令创建索引,第二个命令映射2个字段(一个类型为geo_point的字段),第三个命令检索映射信息。

> curl -X PUT -H "Content-Type:application/json" 
> "http://localhost:9200/my_helloworld_location"
> 
> curl -X PUT -H "Content-Type:application/json" 
> "http://localhost:9200/my_helloworld_location/_mapping/doc?include_type_name=true"
> -d ' {     "properties":{     "locationName":{  
>     "type":"text",
>     "fields":{  
>     "keyword":{  
>       "ignore_above":20,
>       "type":"keyword"
>     }    }   },    "location":{  
>     "type":"geo_point",
>     "fields":{  
>     "keyword":{  
>       "ignore_above":20,
>       "type":"keyword"
>     }    }   }  }   }'  
> 

> curl -X GET -H "Content-Type:application/json" 
> "http://localhost:9200/my_helloworld_location/_mapping?pretty=true"