es 查询条件不生效

2020-04-19 17:40:34 +08:00
 chenguoyu
{
    "from": 3890,
    "size": 10,
    "query": {
        "bool": {
            "must": [
                {
                    "range": {
                        "receive_log_time": {
                            "from": 1587225600000,
                            "to": 1587288897000,
                            "include_lower": true,
                            "include_upper": true
                        }
                    }
                },
                {
                    "bool": {
                        "must": {
                            "term": {
                                "rule_level": "4"
                            }
                        }
                    }
                }
            ],
            "should": {
                "bool": {
                    "must": {
                        "term": {
                            "rule_level": "5"
                        }
                    }
                }
            }
        }
    }
}

其中 should 下面的条件没有生效,请问有人知道原因吗?

3013 次点击
所在节点    Java
12 条回复
aureole999
2020-04-19 18:09:09 +08:00
should 和 must 并列的话 should 就可以不匹配了。must 那边 rule_level 已经必须是 4 了,should 那边就没有了。
除非你的 rule_level 是个列表,那 rule_level 同时包含 4,5 的会在最前面。
misaka19000
2020-04-19 18:17:10 +08:00
should 我记得有个 minimum_match 属性
misaka19000
2020-04-19 18:19:44 +08:00
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-minimum-should-match.html

如果没有 must,默认值为 1 ;如果有 must,默认值为 0

如果我没记错的话 : )
woniu127
2020-04-19 20:42:08 +08:00
rqrq
2020-04-19 23:25:52 +08:00
这个就等同于 sql 的 or 条件,不确定就写成 sql 语句然后用 /_sql/translate 接口转 dsl
j747677392
2020-04-19 23:36:29 +08:00
should 相当于 or,must 相当于 and 两个同级 should 就没效果了。不知道你要实现什么,一般最外层只用 should 或 must 或 mustnot 其中一个
enrio
2020-04-20 00:21:58 +08:00
你知道自己想查什么吗?
chenguoyu
2020-04-20 09:36:33 +08:00
@enrio #7 我想要的效果是类似于 sql 中
where
(条件 1 and 条件 2)
or
(条件 3 and 条件 4)
and
(条件 5 or 条件 6)
这样的
enrio
2020-04-20 10:03:43 +08:00
@chenguoyu
bool:{
should:[
{
bool:{
must:[条件 1,条件 2]
}
},
{
bool:{
must:[
{
bool:{
must:[条件 3,条件 4]
}
},
{
bool:{
should:[条件 5,条件 6],
mininum_should_match:1
}
}
]
}
}
],
mininum_should_match:1
}
enrio
2020-04-20 10:08:15 +08:00
@chenguoyu 这排版有够恶心
看这个图片,https://pic.downk.cc/item/5e9d03c1c2a9a83be50e60b8.png
Flourite
2020-04-20 10:41:55 +08:00
用 terms
yang2yang
2020-04-20 11:03:01 +08:00
9 楼说的对

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/664013

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX