我想使用Elasticsearch Java API创建查询,但不知道如何创建OR子句?我想查询的是;

SELECT *
FROM USERS
WHERE (user.name = "admin") AND (user.message LIKE "test*") AND (user.age = "30" OR user.status = "major")

我已经创建了如下查询,但是我不知道如何创建像SQL查询这样的OR子句;
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

boolQueryBuilder.must(QueryBuilders.matchQuery("name", "admin"));
boolQueryBuilder.must(QueryBuilders.matchQuery("message", "test*"));

boolQueryBuilder.must(QueryBuilders.matchQuery("age", "30"));
boolQueryBuilder.must(QueryBuilders.matchQuery("status","major"));

最佳答案

您只需要在另一个bool/should查询中捕获OR条件

BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

boolQueryBuilder.must(QueryBuilders.matchQuery("name", "admin"));
boolQueryBuilder.mustNot(QueryBuilders.matchQuery("message", "test*"));

BoolQueryBuilder orQuery = QueryBuilders.boolQuery();
orQuery.should(QueryBuilders.matchQuery("age", "30"));
orQuery.should(QueryBuilders.matchQuery("status","major"));
orQuery.minimumNumberShouldMatch(1);
boolQueryBuilder.must(orQuery);

PS:不确定为什么要为第二个约束添加一个mustNot

10-08 13:56