Aymen Ragoubi
Aymen Ragoubi

Reputation: 396

ElasticSearch bool query request

I am trying to translate this query to java code (HighLevelRestClient):

{
   "query":{
     "bool": {
       "must": [
         {
           "query_string": {
              "query": "((\"PUT\" OR \"POST\") AND \"REST\" AND (\"BRI\" OR \"BRIEXT\" OR \"TMP\"))"
            }
         }
       ]
     }
   }
}

I tried this request with postman and it works fine, And I want to try it with the elasticSearch resthighlevel client.

QueryBuilder matchQueryBuilder = QueryBuilders.boolQuery().must();

I don't know what to put inside the must.

Could someone help me with this?

Upvotes: 0

Views: 1024

Answers (1)

Eirini Graonidou
Eirini Graonidou

Reputation: 1566

You could use the queryStringQuery method , as noted in the official documentation

The following code snippet uses the high level RestClient

String query =  "((\"PUT\" OR \"POST\") AND \"REST\" AND (\"BRI\" OR 
 \"BRIEXT\" OR \"TMP\"))";

QueryBuilder matchQueryBuilder = QueryBuilders.boolQuery().must(new 
QueryStringQueryBuilder(query));
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(matchQueryBuilder);

SearchRequest searchRequest = new SearchRequest();
// set the indices you want to search in.
searchRequest.indices("your-index");
searchRequest.source(searchSourceBuilder);

// execute the query
SearchResponse response = hlRestClient.search(searchRequest, header);
SearchHits hits = response.getHits();

Upvotes: 1

Related Questions