python-3.x - 如何使用 elasticsearch-dsl-py 创建“或”条件过滤器?
问题描述
下面的查询是我想使用 elasticsearch-dsl-py 构建的,但我不知道该怎么做。
GET /my_index/_search
{
"query": {
"filtered": {
"filter": {
"bool": {
"or": {
"filters": [
{
"term": {
"status": "a"
},
"term": {
"x_status": "a"
},
}
]
}
}
}
}
}
}
我只想以 SQL 格式执行如下查询
select * from my_index where status = "a" or x_status="a"
解决方案
我不确定您正在运行哪个版本的 ES,但只知道很久以前在版本 5中filtered
已将其替换。因此您的查询可以这样重写:bool
GET /my_index/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"status": "a"
}
},
{
"term": {
"x_status": "a"
}
}
]
}
}
}
使用elasticsearch-dsl-py
,这转换为:
s = Search()
s = s.query('bool', should=[Q('term', status='a'), Q('term', x_status='a')])
推荐阅读
- ssl - 如何修复“javax.net.ssl.SSLHandshakeException:链验证失败”
- symfony - Symfony 4重定向循环以登录表单多个角色
- vb.net - 忽略空白文本框
- mysql - 如何准确选择用户名和密码注意大小写字母 - mysql
- python - After using @ray decorator can't add data to dictionary
- mysql - Laravel NULL 日期是有效的“今天”日期
- ios - pod install error [!] 无法添加 URL 名为“master-1”的源
- c# - Session.clear 会话放弃不起作用
- odoo - 如何在 ubuntu 14.04 中安装 odoo?开发 odoo 模块(如 html、css 等)的基本要求是什么
- android - 如何确保 .gitignore 适用于同一存储库中的多个 Android 项目