iam试图针对我的elasticSearch发送布尔查询,从而过滤掉文档。如何使用Java中的BooleanQueryBuilder Api过滤(A和B和C)和(D或E或F)和G等布尔表达式?我尝试使用filter方法,但没有得到任何结果。例如:
例如,假设A,B,C,D,E,F,G是事件,其中“名称”属性必须与“ Robin”匹配。
private QueryBuilder getQueryBuilder() {
BoolQueryBuilder boolQueryBuilder1 = QueryBuilders
.boolQuery()
.filter(QueryBuilders.matchQuery(D).operator(Operator.OR))
.filter(QueryBuilders.matchQuery(E).operator(Operator.OR))
.filter(F);
BoolQueryBuilder boolQueryBuilder2 = QueryBuilders
.boolQuery()
.filter(QueryBuilders.matchQuery(A).operator(Operator.AND))
.filter(QueryBuilders.matchQuery(B).operator(Operator.AND))
.filter(QueryBuilders.matchQuery(C));
BoolQueryBuilder boolQueryBuilder3 = QueryBuilders
.boolQuery()
.filter(G);
BoolQueryBuilder boolQueryBuilder = QueryBuilders
.boolQuery()
.filter(boolQueryBuilder1)
.filter(boolQueryBuilder2)
.filter(boolQueryBuilder3);
return boolQueryBuilder;
}
我知道这个例子不是很好,但是我希望你能帮助我! 谢谢!
答案 0 :(得分:0)
是这个吗?
private static QueryBuilder getQueryBuilder() {
//condition
MatchQueryBuilder A = QueryBuilders.matchQuery("content", "A");
MatchQueryBuilder B = QueryBuilders.matchQuery("content", "B");
MatchQueryBuilder C = QueryBuilders.matchQuery("content", "C");
MatchQueryBuilder D = QueryBuilders.matchQuery("content", "D");
MatchQueryBuilder E = QueryBuilders.matchQuery("content", "E");
MatchQueryBuilder F = QueryBuilders.matchQuery("content", "F");
MatchQueryBuilder G = QueryBuilders.matchQuery("name", "Robin");
//Maybe you should use TermQuery
//TermQueryBuilder G = QueryBuilders.termQuery("name", "Robin");
//1.(A AND B AND C)
BoolQueryBuilder query1 = QueryBuilders.boolQuery();
query1.must(A).must(B).must(C);
//2.(D OR E ORF)
BoolQueryBuilder query2 = QueryBuilders.boolQuery();
query2.should(D).should(E).should(F);
//3.Last
BoolQueryBuilder lastQuery = QueryBuilders.boolQuery();
lastQuery.must(query1).must(query2).must(G);
//use filter
BoolQueryBuilder filterBuiler = QueryBuilders.boolQuery().filter(lastQuery);
return filterBuiler;
}