Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the query via JSON to an Elasticsearch SearchRequest?

Elasticsearch: 6.1.2

I have an input query via JSON and would like to use the high level Java API to construct a search request using that query data.

String jsonQuery = "..."
SearchRequest searchRequest = new SearchRequest()
SearchSourceBuilder builder = ?
searchRequest.source(builder);

I tried to construct the builder via:

SearchSourceBuilder.fromXContent(XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, query));

but that yields:

Caused by: org.elasticsearch.ElasticsearchException: namedObject is not supported for this parser at org.elasticsearch.common.xcontent.NamedXContentRegistry.parseNamedObject(NamedXContentRegistry.java:129) ~[elasticsearch-6.1.2.jar:6.1.2] at org.elasticsearch.common.xcontent.support.AbstractXContentParser.namedObject(AbstractXContentParser.java:402) ~[elasticsearch-6.1.2.jar:6.1.2] at org.elasticsearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder(AbstractQueryBuilder.java:313) ~[elasticsearch-6.1.2.jar:6.1.2] at org.elasticsearch.search.builder.SearchSourceBuilder.parseXContent(SearchSourceBuilder.java:1003) ~[elasticsearch-6.1.2.jar:6.1.2] at org.elasticsearch.search.builder.SearchSourceBuilder.fromXContent(SearchSourceBuilder.java:115) ~[elasticsearch-6.1.2.jar:6.1.2]

like image 559
Jotschi Avatar asked Jan 23 '18 10:01

Jotschi


2 Answers

I'm now generating the SearchSourceBuilder this way:

String query = "..."
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
SearchModule searchModule = new SearchModule(Settings.EMPTY, false, Collections.emptyList());
try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(new NamedXContentRegistry(searchModule
            .getNamedXContents()), query)) {
    searchSourceBuilder.parseXContent(parser);
}
like image 149
Jotschi Avatar answered Nov 19 '22 14:11

Jotschi


I too was facing the same issue. We had been updating from ES 1.4.4 to ES 6.4.2 and I had used Java High Level Rest Client provided by ES to do create/update/delete operations in ES. I couldn't find an easy way to convert a query JSON used in prev ES version to an appropriate searchRequest using the High Level Rest Client. The below is what I ended up using.

import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.Request;

public static Response executeGetRequest(RestHighLevelClient client,
            String api, String body) throws Exception {

            Request request = new Request("GET", api);
            if(body != null && !body.isEmpty()) {
                request.setJsonEntity(body);
            }            
            Response response = client.getLowLevelClient()
                    .performRequest(request);
            if (response == null) {
                throw new Exception(
                        "Get request have failed");
            }
            return response;        
    }
like image 36
Rahul Kosambi Avatar answered Nov 19 '22 16:11

Rahul Kosambi