简体   繁体   中英

Elasticsearch REST High Level Client combine query builders

I need to create something like an advanced search using ES REST High Level Client (Java).
First, I have a search keyword that searches for all fields.
I use QueryStringQueryBuilder for this.

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

Now, I need to add some more filters using TermsQueryBuilder .

Is it possible to combine the two query builders?

I would like to add the following filters:

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

Is it possible to combine both query builders, to achieve something like an advance search?

Thanks!

Yes, you can do that with a bool query like this:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM