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.

1

There are 1 answers

2
rabbitbr On

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