【ES】同时使用should和must 导致只有must生效

参数如下:

{
    "size": 10000,
    "query": {
        "bool": {
            "must": [
                {
                    "range": {
                        "dateTime": {
                            "from": "2022-09-21",
                            "to": "2022-09-22",
                            "include_lower": true,
                            "include_upper": true,
                            "boost": 1.0
                        }
                    }
                }
            ],
            "should": [
                {
                    "match": {
                        "device": "01223344"
                    }
                }
            ]
        }
    }
}

通过查询,should 是不生效的。

解决方案

解决方案一:加上"minimum_number_should_match": 1 这个设置should语句的满足条件值。

解决方案二:将should嵌在must语句中。

方案一:

{
    "size": 10000,
    "query": {
        "bool": {
            "must": [
                {
                    "range": {
                        "dateTime": {
                            "from": "2022-09-21",
                            "to": "2022-09-22",
                            "include_lower": true,
                            "include_upper": true,
                            "boost": 1.0
                        }
                    }
                }
            ],
            "should": [
                {
                    "match": {
                        "device": "01223344"
                    }
                }
            ]
            "minimum_should_match": 1
        }
    }
}

方案二:

{
    "query": {
        "bool": {
            "must": [
                {
                    "range": {
                        "dateTime": {
                            "from": "2022-09-20T23:30:00",
                            "to": "2022-09-21"
                        }
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "term": {
                                     "device": "01223344"
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

参考: https://blog.csdn.net/zhengchanmin/article/details/113723890

posted @ 2023-08-05 16:01  aaacarrot  阅读(188)  评论(0编辑  收藏  举报