如何在弹性搜索py中通过地理距离进行过滤



使用Python 2.7和elasticsearch-py

给定以下JSON:

[
    {
        "Name":
        "Addresses": [
            "StreetAdd": "xxx",
            "GeoLocation": {
                "lat": xx,
                "long": yy
            }
        ]
    },
    {
        // And so on.
    }   
]

以及以下映射:

mapping = {
    "mappings": {
        "leads": {
            "properties": {
                "Addresses": {
                    "type": "nested",
                    "include_in_parent": "true",
                    "properties": {
                        "GeoLocation": "geo_point"
                    }
                }
            }
        }
    }
}

如何获取纬度40、经度-70之间10公里范围内的位置?我的尝试如下:

search_body = {
    "query" : { 
        "filtered": {
            "query": {
                "match_all" : { }
            },
            "filter": {
                "geo_distance": {
                    "distance": "10km",
                    "Addresses.GeoLocation": {
                        "lat": 40.0,
                        "lon": -70.0
                    }
                }
            }
        }
    },
    "size": 50
}
result = es.search(index=ES_INDEX, body=search_body, sort="Name:asc")
for hit in result["hits"]["hits"]:
    print hit["_source"]["Name"]

然而,这引发了以下错误:

...
C:UsersxxxAppDataLocalContinuumAnaconda2libsite-packageselasticsearchconnectionbase.pyc in _raise_error(self, status_code, raw_data)
    103             pass
    104 
--> 105         raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
    106 
    107 
RequestError: TransportError(400, u'search_phase_execution_exception')

ES还不太熟练,所以我很难想象我应该用什么模式来解决这个问题。

什么东西?

问题出在映射中。这是固定版本的

mapping = {
    "mappings": {
        "leads": {
            "properties": {
                "Addresses": {
                    "type": "nested",
                    "include_in_parent": "true",
                    "properties": {
                        "GeoLocation": {
                            "type":"geo_point" <-- Note the type here
                        }
                    }
                }
            }
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新