ES dsl查询filter(或must)和should并用时should子句不生效

记录下今天编码时遇到的问题,在filter和should同级并用的查询下,should子句并没有生效,只有filter子句生效。

例如以下dsl

{
    "query": {
        "bool": {
            "filter": [
                {
                    "term": {
                        "status": 3
                    }
                }
            ],
            "should": [
                {
                    "bool": {
                        "filter": [
                            {
                                "bool": {
                                    "must_not": {
                                        "exists": {
                                            "field": "last_recheck_time"
                                        }
                                    }
                                }
                            },
                            {
                                "range": {
                                    "qc_end_time": {
                                        "lte": 1713769642
                                    }
                                }
                            }
                        ]
                    }
                },
                {
                    "range": {
                        "last_recheck_time": {
                            "lte": 1713769642
                        }
                    }
                }
            ]
        }
    }
}

这是因为在filter/must和should并列使用时,当filter/must满足时,should子句默认会被忽略,如果需要filter/must满足的同时,should中的条件也要满足,有两种方法可以修改我们原本的查询。

方法1 使用minimum_should_match

在should同级增加参数minimum_should_match,修改后的dsl如下

{
    "query": {
        "bool": {
            "filter": [
                {
                    "term": {
                        "status": 3
                    }
                }
            ],
            "minimum_should_match":1,
            "should": [
                {
                    "bool": {
                        "filter": [
                            {
                                "bool": {
                                    "must_not": {
                                        "exists": {
                                            "field": "last_recheck_time"
                                        }
                                    }
                                }
                            },
                            {
                                "range": {
                                    "qc_end_time": {
                                        "lte": 1713769642
                                    }
                                }
                            }
                        ]
                    }
                },
                {
                    "range": {
                        "last_recheck_time": {
                            "lte": 1713769642
                        }
                    }
                }
            ]
        }
    }
}

方法2 合并filter和should子句

可将should子句和filter子句合并,将should子句作为filter子句内的一部分,达到想要的查询效果

{
    "query": {
        "bool": {
            "filter": [
                {
                    "term": {
                        "status": 0
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "bool": {
                                    "filter": [
                                        {
                                            "bool": {
                                                "must_not": {
                                                    "exists": {
                                                        "field": "last_recheck_time"
                                                    }
                                                }
                                            }
                                        },
                                        {
                                            "range": {
                                                "qc_end_time": {
                                                    "lte": 0
                                                }
                                            }
                                        }
                                    ]
                                }
                            }
                            ,{
                                "range": {
                                    "last_recheck_time": {
                                        "lte": 0
                                    }
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

参考:

Bool Query | Elasticsearch Guide [5.0] | Elastic

最近更新

  1. TCP协议是安全的吗?

    2024-05-09 20:16:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-09 20:16:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-09 20:16:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-09 20:16:04       20 阅读

热门阅读

  1. 【LeetCode】数组——hashmap的妙用

    2024-05-09 20:16:04       12 阅读
  2. Github 2024-05-06 开源项目日报 Top10

    2024-05-09 20:16:04       11 阅读
  3. Springboot整合Minio,2024版教程

    2024-05-09 20:16:04       11 阅读