0

I have a complex Elasticsearch String query which I don't want to create using Java classes.

Is there a way I can achieve it so as to use it something like:

Query query = getQueryFromString(queryString);
    
co.elastic.clients.elasticsearch.core.SearchResponse searchResponse = null;
    try {

        co.elastic.clients.elasticsearch.core.SearchRequest request = new co.elastic.clients.elasticsearch.core.SearchRequest.Builder()
                .index(indexName)
                .size(pageSize)
                .query(query
                ).build();

        searchResponse = esClient.withTransportOptions(getRequestOptions()).search(request, Object.class);

    } catch (Exception ex) {
        log.error("Some exception occurred while requesting response from elasticsearch", ex);
    }

I went through the documentation but couldn't really find a way to achieve so. Any help would be really appreciated.

Indrapreet
  • 55
  • 8

1 Answers1

1

I tried with Wrapper Query.

Convert query to Base64 and run the code bellow.

Query {"match_all": {}} is ewogICAgIm1hdGNoX2FsbCI6IHt9CiAgfQ==.

var wrapper = WrapperQuery.of(wq -> wq.query("ewogICAgIm1hdGNoX2FsbCI6IHt9CiAgfQ=="));

var request = SearchRequest.of(sr -> sr
    .index("idx_movies")
    .query(Query.of(q -> q.wrapper(wrapper))));

SearchResponse<Movie> response = ClientUtils.getClient().search(request, Movie.class);
rabbitbr
  • 2,991
  • 2
  • 4
  • 17
  • Thanks, @rabbitbr. Can we also do the same for aggregation? Although I understand that it's not a part of the question. – Indrapreet May 16 '23 at 11:43
  • With aggregations it is not possible. In a project I used json files with queries so I didn't have to write in java. In this [post](https://medium.com/@andre.luiz1987/query-template-with-handlebars-elasticsearch-9ecb375c7cfd) there is a part of what I did. Maybe it will help you. – rabbitbr May 16 '23 at 12:50