Elasticsearch REST高级客户端组合查询构建器

时间:2018-06-05 03:54:54

标签: java spring-boot elasticsearch

我需要使用ES REST高级客户端(Java)创建类似高级搜索的内容 首先,我有一个搜索关键字,用于搜索所有字段 我为此使用了QueryStringQueryBuilder

SearchSourceBuilder ticketInfoSourceBuilder = new SearchSourceBuilder();
ticketInfoSourceBuilder.from(pageable.getOffset());
ticketInfoSourceBuilder.size(pageable.getPageSize());
ticketInfoSourceBuilder.sort(new FieldSortBuilder(sortField).order(sortOrder));
ticketInfoSourceBuilder.query(QueryBuilders.queryStringQuery("ABC1234"));

现在,我需要使用TermsQueryBuilder添加更多过滤器。

是否可以组合两个查询构建器?

我想添加以下过滤器:

"terms" : { "ticket.inquiryType" : ["INQTYP01", "INQTYP06"]}
"terms" : { "ticket.status" : ["NEW", "CLOSED"]}
"terms" : { "ticket.ownership" : ["OWNED", "OTHER_OWNER"]}

是否可以组合两个查询构建器,以实现类似高级搜索的功能?

谢谢!

1 个答案:

答案 0 :(得分:3)

是的,您可以使用bool这样的查询来执行此操作:

QueryBuilder query = QueryBuilders.boolQuery()
   .must(QueryBuilders.queryStringQuery("ABC1234"))
   .filter(QueryBuilders.termsQuery("ticket.inquiryType", "INQTYP01", "INQTYP06"))
   .filter(QueryBuilders.termsQuery("ticket.status", "NEW", "CLOSED"))
   .filter(QueryBuilders.termsQuery("ticket.ownership", "OWNED", "OTHER_OWNER"));
ticketInfoSourceBuilder.query(query);