ES的“或“查询

场景:需要查询ES中userId为""或者userId字段不存在的数据

dsl语句:

java 复制代码
{
	"from": 0,
	"size": 30,
	"query": {
		"bool": {
			"should": [{
				"term": {
					"userId": {
						"value": "",
						"boost": 1.0
					}
				}
			},
			{
				"bool": {
					"must_not": [{
						"exists": {
							"field": "userId",
							"boost": 1.0
						}
					}],
					"adjust_pure_negative": true,
					"boost": 1.0
				}
			}],
			"adjust_pure_negative": true,
			"minimum_should_match": "1",
			"boost": 1.0
		}
	},
	"sort": [{
		"eventTime": {
			"order": "desc"
		}
	}]
}

java代码:

java 复制代码
BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
queryBuilder.should(new TermQueryBuilder("userId", ""));
queryBuilder.should(new BoolQueryBuilder().mustNot(new ExistsQueryBuilder("userId")));
//should中的条件满足任一条
queryBuilder.minimumShouldMatch(1);
相关推荐
zc.z2 小时前
JAVA实现:纯PCM格式音频转换成BASE64
java·音视频·pcm
mask哥2 小时前
力扣算法java实现汇总整理(上)
java·算法·leetcode
Aaswk3 小时前
Java Lambda 表达式与流处理
java·开发语言·python
是宇写的啊3 小时前
Spring AOP
java·spring
万邦科技Lafite3 小时前
京东item_get接口实战案例:实时商品价格监控全流程解析
java·开发语言·数据库·python·开放api·淘宝开放平台
青岛前景互联信息技术有限公司4 小时前
OpenClaw 重构智慧消防:AI时代的平台融合实践
大数据·人工智能
梦梦代码精4 小时前
BuildingAI 上部署自定义工作流智能体:5 个实用技巧
大数据·人工智能·算法·开源软件
极客老王说Agent5 小时前
2026智造前瞻:实在Agent生产排期智能助理核心功能与使用方法详解
大数据·人工智能·ai·chatgpt
Mr_pyx5 小时前
Spring AI 入门教程:Java开发者的AI应用捷径
java·人工智能·spring
Zephyr_05 小时前
Leedcode算法题
java·算法