[英]Elasticsearch find documents by location
我已经在我的Elasticsearch数据库中索引了许多文档,当我查询所有文档时,我看到它们具有如下结构:
GET http://localhost:9200/restaurants/restaurant/_search
输出:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 362,
"max_score": 1,
"hits": [
{
"_index": "restaurants",
"_type": "restaurant",
"_id": "2",
"_score": 1,
"_source": {
"businessName": "VeeTooNdoor Dine",
"geoDescription": "Right next to you2",
"tags": {},
"location": {
"lat": -33.8917007446,
"lon": 151.1369934082
}
}
},
...
]
}
}
现在,我想搜索给定地理位置周围的餐馆,并按照文档[1]使用类似这样的内容:
{
"filtered" : {
"query" : {
"match_all" : {}
},
"filter" : {
"geo_distance" : {
"distance" : "1km",
"location" : {
"lat": -33.8917007446,
"lon": 151.1369934082
}
}
}
}
}
我更改的是match_all
因为我不想特别指定任何搜索字段,所以我只关心地理位置。
当我运行查询时,我收到以下消息:
"error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[olAoWBSJSF2XfTnzEhKIYA][btq][3]: SearchParseException[[btq][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\n \"filtered\" : {\n \"query\" : {\n \"match_all\" : {}\n },\n \"filter\" : {\n .....
现在,我确实在教程页面上注意到以下内容:
更新:自发布本文以来,“ geo enabled”属性的自动映射已禁用。 您必须提供正确的地理属性映射。 请参阅文档。
这让我觉得我必须创建一个指定字段类型的“映射”。 但是,在它所引用的文档中并未提供有关如何实际执行此操作的足够信息。 它显示了JSON的blob,但我不确定这个的正确URL。
此外,我正在使用PHP客户端,并且不确定它是否甚至支持映射,如本教程[2]所示。
我不知何故得到的结论是,对DSL等查询进行了相当多的修改,网上的很多例子都不再适用了,我可能错了。 我正在使用Elasticsearch 1.0.0。
[1] http://www.elasticsearch.org/blog/geo-location-and-search
可能是错误的事情:
1:您的查询显示pin.location
而您的字段只是location
。
2:您的_mapping
location
可能有误
您的映射是否显示如下:
"location": {
"type": "geo_point",
"geohash_precision": 4
}
我能够针对自己的一些数据执行此搜索:
POST /myindex/mydata/_search
{
"query": {
"match_all": {}
},
"filter": {
"geo_distance" : {
"distance" : "100km",
"_latlng_geo" : {
"lat": -33.8917007446,
"lon": 151.1369934082
}
}
}
}
...我的映射片段:
"properties": { .....
"_latlng_geo": {
"type": "geo_point",
"geohash_precision": 4
}
.....
编辑:如何使用Put Mapping API
您可以在创建索引时创建映射,如下所示:
PUT /twitter/
{
"settings" : {
"number_of_shards" : 1
},
"mappings" : {
"tweet":{
"properties":{
"latlng" : {
"type" : "geo_point"
}
}
}
}
}
删除查询最终对我有用:
{
"filter": {
"geo_distance" : {
"distance" : "300km",
"location" : {
"lat" : 45,
"lon" : -122
}
}
}
}
您必须将“location”替换为“restaurant.location ”,因为ElasticSearch将其解释为类似于属性的类型。
{
"filtered" : {
"query" : {
"match_all" : {}
},
"filter" : {
"geo_distance" : {
"distance" : "1km",
"restaurant.location" : {
"lat": -33.8917007446,
"lon": 151.1369934082
}
}
}
}
我希望它对你有所帮助。
如文档中所述:
“我们使用通用的PHP stdClass对象表示一个空对象。JSON现在将正确编码。”
在您的情况下,您应该使用
$searchParams['body']['query']['filtered']['query']['match_all'] = new \stdClass();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.