更新到Elasticsearch 2.3后无法定位嵌套的地理点。

7
我们使用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'}
                    }
                }
             }
        }
} 

使用elasticsearch-dsl==0.0.11的Python代码

            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,但是没有一个例子有它。

任何帮助将非常感激,谢谢!


path must be addresses and in the geo_distance filter you must have "addresses.location" instead of just "location" - Val
抱歉,我已经尝试过那个了,还有 '.location'。 - marklar
你能试一下下面的答案吗? - Val
1个回答

2
这应该可以正常工作:
        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)

尽快给这个一个尝试! - marklar
如果还是不行,请告诉我,我们会找出解决方法的。 - Val
这似乎已经奏效了,尽管我发誓我之前已经尝试过了!我认为问题的根源是在重新创建索引时没有正确发送映射,但无论如何现在它都可以工作了。非常感谢您花时间解决我的问题! - marklar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接