V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
chenguoyu
V2EX  ›  Java

es 查询条件不生效

  •  
  •   chenguoyu · 2020-04-19 17:40:34 +08:00 · 2994 次点击
    这是一个创建于 1461 天前的主题,其中的信息可能已经有所发展或是发生改变。
    {
        "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 下面的条件没有生效,请问有人知道原因吗?

    12 条回复    2020-04-20 11:03:01 +08:00
    aureole999
        1
    aureole999  
       2020-04-19 18:09:09 +08:00   ❤️ 1
    should 和 must 并列的话 should 就可以不匹配了。must 那边 rule_level 已经必须是 4 了,should 那边就没有了。
    除非你的 rule_level 是个列表,那 rule_level 同时包含 4,5 的会在最前面。
    misaka19000
        2
    misaka19000  
       2020-04-19 18:17:10 +08:00
    should 我记得有个 minimum_match 属性
    misaka19000
        3
    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
        4
    woniu127  
       2020-04-19 20:42:08 +08:00
    rqrq
        5
    rqrq  
       2020-04-19 23:25:52 +08:00
    这个就等同于 sql 的 or 条件,不确定就写成 sql 语句然后用 /_sql/translate 接口转 dsl
    j747677392
        6
    j747677392  
       2020-04-19 23:36:29 +08:00
    should 相当于 or,must 相当于 and 两个同级 should 就没效果了。不知道你要实现什么,一般最外层只用 should 或 must 或 mustnot 其中一个
    enrio
        7
    enrio  
       2020-04-20 00:21:58 +08:00
    你知道自己想查什么吗?
    chenguoyu
        8
    chenguoyu  
    OP
       2020-04-20 09:36:33 +08:00
    @enrio #7 我想要的效果是类似于 sql 中
    where
    (条件 1 and 条件 2)
    or
    (条件 3 and 条件 4)
    and
    (条件 5 or 条件 6)
    这样的
    enrio
        9
    enrio  
       2020-04-20 10:03:43 +08:00   ❤️ 1
    @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
        10
    enrio  
       2020-04-20 10:08:15 +08:00   ❤️ 1
    @chenguoyu 这排版有够恶心
    看这个图片,https://pic.downk.cc/item/5e9d03c1c2a9a83be50e60b8.png
    Flourite
        11
    Flourite  
       2020-04-20 10:41:55 +08:00
    用 terms
    yang2yang
        12
    yang2yang  
       2020-04-20 11:03:01 +08:00
    9 楼说的对
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1062 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 23:06 · PVG 07:06 · LAX 16:06 · JFK 19:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.