Как написать запрос Elasticsearch вasticsearch-dsl в Python - PullRequest
0 голосов
/ 08 мая 2019

Я пытаюсь написать запрос в Elasticsearch, используя библиотекуasticsearch-dsl в Python 3.7.

Я думаю, что мне удалось написать большую часть, но у меня возникла проблема с предложением "существовать".

Это запрос, который я хочу перевести:

            {
                "query": {
                    "constant_score": {
                        "filter": {
                            "bool": {
                                "must": { 
                                    "term": { "locale": "$locale" }
                                },
                                "must_not": {
                                    "term": { "blacklist_country": "$country_code" }
                                },
                                "should": [
                                { "term": { "whitelist_country": "$country_code" } },
                                { "bool": {
                                    "must_not": {
                                        "exists": { "field": "whitelist_country" }
                                    }
                                }}
                                ]
                            }
                        }
                    }
                }
            }

И вот что у меня есть:

q = Q('constant_score',
            filter={Q('bool',
                must=[Q('term', locale=locale)],
                must_not=[Q('term', blacklist_country=country_code)],
                should=[Q('term', whitelist_country=country_code),
                        Q('bool',
                            must_not=[Q('exists', field='whitelist_country')]
                        )
                       ]
                    )}
            )

Я ожидаю, что запрос будет выполнен правильно, но в настоящее время я получаю эту ошибку:

...
must_not=[Q('exists', field='whitelist_country')]
TypeError: unhashable type: 'Bool'

1 Ответ

0 голосов
/ 10 мая 2019

Для тех, у кого такая же проблема, я решил ее так:

search = Search(using=client_es, index="articles") \
            .query('constant_score', filter=Q('bool',
                must=Q('term', locale=locale),
                must_not=Q('term', blacklist_country=country_code),
                should=[Q('term', whitelist_country=country_code),
                        Q('bool',
                            must_not=Q('exists', field='whitelist_country')
                        )
                       ]
                    ))
...