Skip to main content
 首页 » 编程设计

python之尝试发送 Elasticsearch 地理查询时无法散列类型 'dict'

2024年10月24日10birdshome

我试图通过以下代码片段从 ES 中获取一些地理数据:

result = es.search( 
    index="loc", 
    body={ 
        { 
            "filtered" : { 
                "query" : { 
                    "field" : { "text" : "restaurant" } 
                }, 
                "filter" : { 
                    "geo_distance" : { 
                        "distance" : "12km", 
                        "location" : { 
                            "lat" : 40, 
                            "lon" : -70 
                        } 
                    } 
                } 
            } 
        } 
    } 
) 

但是由于以下错误,查询没有成功:

"lon" : -70 
TypeError: unhashable type: 'dict' 

location 字段正确映射到 geo_point 类型,查询取自 official examples .我编写查询的方式有问题吗?

请您参考如下方法:

您正在将 dict 嵌套在 set 中。删除外部花括号以解决该问题。该错误源于这样一个事实,即集合,字典不能包含不可散列的集合,例如字典(感谢@Matthias)。

body= 
        { 
            "filtered" : { 
                "query" : { 
                    "field" : { "text" : "restaurant" } 
                }, 
                "filter" : { 
                    "geo_distance" : { 
                        "distance" : "12km", 
                        "location" : { 
                            "lat" : 40, 
                            "lon" : -70 
                        } 
                    } 
                } 
            } 
        }