ES的“或“查询

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

dsl语句:

{
	"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代码:

BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
queryBuilder.should(new TermQueryBuilder("userId", ""));
queryBuilder.should(new BoolQueryBuilder().mustNot(new ExistsQueryBuilder("userId")));
//should中的条件满足任一条
queryBuilder.minimumShouldMatch(1);

你可能感兴趣的:(elasticsearch,java,大数据)