【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 @   aaacarrot  阅读(836)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示