我正在尝试使用Python3.7中的elasticsearch-dsl库向Elasticsearch编写一个查询。
我想我已经写好了大部分内容,但是我有一个"exist“子句的问题。
这是我想要翻译的查询:
{
"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'发布于 2019-05-11 03:20:21
对于任何有同样问题的人,我是这样解决的:
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')
)
]
))https://stackoverflow.com/questions/56045487
复制相似问题