首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在python中将Elasticsearch查询写入elasticsearch-dsl

如何在python中将Elasticsearch查询写入elasticsearch-dsl
EN

Stack Overflow用户
提问于 2019-05-09 00:47:35
回答 1查看 368关注 0票数 0

我正在尝试使用Python3.7中的elasticsearch-dsl库向Elasticsearch编写一个查询。

我想我已经写好了大部分内容,但是我有一个"exist“子句的问题。

这是我想要翻译的查询:

代码语言:javascript
复制
            {
                "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" }
                                    }
                                }}
                                ]
                            }
                        }
                    }
                }
            }

这就是我到目前为止所得到的:

代码语言:javascript
复制
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')]
                        )
                       ]
                    )}
            )

我希望查询能正常运行,但目前我得到了这个错误:

代码语言:javascript
复制
...
must_not=[Q('exists', field='whitelist_country')]
TypeError: unhashable type: 'Bool'
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-11 03:20:21

对于任何有同样问题的人,我是这样解决的:

代码语言:javascript
复制
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')
                        )
                       ]
                    ))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56045487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档