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]
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);
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With