更新到 elasticsearch 2.3 后无法定位嵌套的地理点
Unable to locate nested geopoint after updating to elasticsearch 2.3
我们使用 AWS 托管的 Elasticsearch 服务,最近从 1.5 升级到 2.3。我们使用 python 中的 elasticsearch-dsl 包来构建我们的查询并设法迁移了我们的大部分查询,但是无论我尝试什么,geo_distance 都被破坏了。
映射:
{
'company': {
'properties': {
'id': {'type': 'integer'},
'company_number': {'type': 'string'},
'addresses': {
'type': 'nested',
'properties': {
'postcode': {'type': 'string', 'index': 'not_analyzed'},
'location': {'type': 'geo_point'}
}
}
}
}
}
Python 使用 elasticsearch-dsl==0.0.11
的代码
test_location = '53.5411062377, -2.11485504709'
test_distance = "3miles"
location_filter = F("geo_distance",
location=test_location,
distance=test_distance)
query = query.filter("nested",
path="addresses",
filter=location_filter)
库生成的查询:
{'query': {'filtered': {'filter': {'nested': {'filter': {'geo_distance': {'distance': u'3miles', 'location': '53.5411062377, -2.11485504709'}}, 'path': 'addresses'}}, 'query': {'match_all': {}}}}}
我们在新的 2.3 上创建了一个具有相同映射的全新索引。
更新到 elasticsearch-dsl == 2.1.0 并尝试将过滤器转换为查询后:
geo_query = Q({"bool": {
"must": [
{
"geo_distance": {
"distance": "test_distance",
"addresses__location": test_location,
}
},
]
}})
生成以下查询:
{'query': {'bool': {'must': [{'geo_distance': {'distance': '3 miles', u'addresses.location': {'lat': '53.5411062377', 'lon': '-2.11485504709'}}}]}}}
我们得到以下异常:
RequestError: TransportError(400, u'search_phase_execution_exception', u'failed to find geo_point field [addresses.location]')
我尝试将该字段引用为 'location'、'addresses.location'、'addresses' 并使用旧的嵌套查询类型。我不知道映射是否在 2.3 中不再有效,或者我是否构建了错误的查询。
查询
Q({'nested': {'filter': {
'geo_distance': {'distance': u'3miles', 'location': '53.5411062377, -2.11485504709'}
},
'path': 'addresses.location'}})
生成以下错误:
RequestError: TransportError(400, u'search_phase_execution_exception', u'[nested] nested object under path [addresses.location] is not of nested type')
我想我需要添加 lat_lon : True 到映射中才能使地理距离查询正常工作,但是 none 的例子有它。
非常感谢任何帮助,谢谢!
这应该有效:
test_location = '53.5411062377, -2.11485504709'
test_distance = "3miles"
location_query = Q("geo_distance",
addresses__location=test_location,
distance=test_distance)
query = query.filter("nested",
path="addresses",
query=location_query)
我们使用 AWS 托管的 Elasticsearch 服务,最近从 1.5 升级到 2.3。我们使用 python 中的 elasticsearch-dsl 包来构建我们的查询并设法迁移了我们的大部分查询,但是无论我尝试什么,geo_distance 都被破坏了。
映射:
{
'company': {
'properties': {
'id': {'type': 'integer'},
'company_number': {'type': 'string'},
'addresses': {
'type': 'nested',
'properties': {
'postcode': {'type': 'string', 'index': 'not_analyzed'},
'location': {'type': 'geo_point'}
}
}
}
}
}
Python 使用 elasticsearch-dsl==0.0.11
的代码 test_location = '53.5411062377, -2.11485504709'
test_distance = "3miles"
location_filter = F("geo_distance",
location=test_location,
distance=test_distance)
query = query.filter("nested",
path="addresses",
filter=location_filter)
库生成的查询:
{'query': {'filtered': {'filter': {'nested': {'filter': {'geo_distance': {'distance': u'3miles', 'location': '53.5411062377, -2.11485504709'}}, 'path': 'addresses'}}, 'query': {'match_all': {}}}}}
我们在新的 2.3 上创建了一个具有相同映射的全新索引。
更新到 elasticsearch-dsl == 2.1.0 并尝试将过滤器转换为查询后:
geo_query = Q({"bool": {
"must": [
{
"geo_distance": {
"distance": "test_distance",
"addresses__location": test_location,
}
},
]
}})
生成以下查询:
{'query': {'bool': {'must': [{'geo_distance': {'distance': '3 miles', u'addresses.location': {'lat': '53.5411062377', 'lon': '-2.11485504709'}}}]}}}
我们得到以下异常:
RequestError: TransportError(400, u'search_phase_execution_exception', u'failed to find geo_point field [addresses.location]')
我尝试将该字段引用为 'location'、'addresses.location'、'addresses' 并使用旧的嵌套查询类型。我不知道映射是否在 2.3 中不再有效,或者我是否构建了错误的查询。
查询
Q({'nested': {'filter': {
'geo_distance': {'distance': u'3miles', 'location': '53.5411062377, -2.11485504709'}
},
'path': 'addresses.location'}})
生成以下错误:
RequestError: TransportError(400, u'search_phase_execution_exception', u'[nested] nested object under path [addresses.location] is not of nested type')
我想我需要添加 lat_lon : True 到映射中才能使地理距离查询正常工作,但是 none 的例子有它。
非常感谢任何帮助,谢谢!
这应该有效:
test_location = '53.5411062377, -2.11485504709'
test_distance = "3miles"
location_query = Q("geo_distance",
addresses__location=test_location,
distance=test_distance)
query = query.filter("nested",
path="addresses",
query=location_query)