Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unhashable type 'dict' when trying to send an Elasticsearch geo query

I'm trying to get some geodata out of ES via the following snippet:

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

The query however doesn't succeed due to the following error:

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

The location field is correctly mapped to the geo_point type and the query is taken from the official examples. Is there something wrong with the way I wrote the query?

like image 507
alexdeloy Avatar asked Dec 06 '25 03:12

alexdeloy


1 Answers

You are nesting a dict inside a set. Remove outer curly braces to resolve the issue. The error stems from the fact that sets, dicts can't contain unhashable collections like e.g. dict (thanks @Matthias).

body=
        {
            "filtered" : {
                "query" : {
                    "field" : { "text" : "restaurant" }
                },
                "filter" : {
                    "geo_distance" : {
                        "distance" : "12km",
                        "location" : {
                            "lat" : 40,
                            "lon" : -70
                        }
                    }
                }
            }
        }
like image 56
mic4ael Avatar answered Dec 07 '25 16:12

mic4ael