1

I am having a condition as below: I have "SearchCriteria1", "SearchCriteria2" and so on till "SearchCriteria..n". This criteria is dynamic and can go upto 10 search criterias.

For each of the above criteria I can have sub-criterias as "SearchCriteria11" , "SearchCriteria12", "SearchCriteria13"

So basically as structured below

SearchCriteria1 --> SearchCriteria11, SearchCriteria12, SearchCriteria13

SearchCriteria2 --> SearchCriteria21, SearchCriteria22, SearchCriteria23

I want this to be searched in elastic search as

(SearchCriteria1 OR SearchCriteria11 OR SearchCriteria12 OR SearchCriteria13) AND (SearchCriteria2 OR SearchCriteria21 OR SearchCriteria22 OR SearchCriteria23) 

This query I needed to be searched across 3 fields in the document , for example: "eventsummary", "address" and "eventdescription"

I am using the following maven dependency

        <dependency>
            <groupId>co.elastic.clients</groupId>
            <artifactId>elasticsearch-java</artifactId>
            <version>8.9.0</version>
        </dependency>

I have tried the below code but does not work as inside the loop the query gets overridden.

Query esContextQuery = null, edescContextQuery = null;
for (int i = 0; i < searchCriteria.length; i++) {
      String primCriteria = searchCriteria[i];
      MatchQuery.of(m ->           m.field("eventdescription").query(primCriteria).field("eventsummary").query(primCriteria))._toQuery();
        
if (searchContextCriteria != null && searchContextCriteria.length > 0) {
    for (String contextSkill : searchContextCriteria) {
        MatchQuery.of(m -> m.field("eventdescription").query(contextSkill))._toQuery();
     }
}

if (searchContextCriteria != null && searchContextCriteria.length > 0) {
    for (String contextSkill : searchContextCriteria) {
            edescContextQuery = MatchQuery.of(m->m.field("eventsummary").query(contextSkill))._toQuery();
        }
    }
}

Post which I have framed the boolean for the AND across field

SearchResponse<Event> responseSelected = esClient.search(s -> s.index("events").query(
                        q -> q.bool(b -> b.must(query1).must(query2))),
                        Event.class);

I am using a low level client for connecting to Elastic coded below

RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();

        // Create the transport with a Jackson mapper
    ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());

        // And create the API client
    ElasticsearchClient esClient = new ElasticsearchClient(transport);

The loop overrides the query and I am unsure/ ignorant of how to frame the above explained criteria in the loop .Any help would be really helpful.

Dwai
  • 119
  • 1
  • 15
  • I got the answer from the link mentioned below, that bool (must) takes in a array/List of Queries https://stackoverflow.com/questions/13352146/how-do-i-combine-multiple-queries-in-elasticsearch – Dwai Aug 31 '23 at 17:07

0 Answers0