How to set the query via JSON to an Elasticsearch SearchRequest?
Asked Answered
L

3

10

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]

Laxation answered 23/1, 2018 at 10:9 Comment(0)
L
10

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);
}
Laxation answered 23/1, 2018 at 12:51 Comment(4)
TFFY @Laxation You just made my Friday.Aquaplane
@Aquaplane I'm no longer using this approach. Using plain JSON and REST is far more flexible.Laxation
Oh sure, if you want to refactor the world. It is, after all, Friday.Aquaplane
I see a lot of examples using new NamedXContentRegistry(ClusterModule.getNamedXWriteables()) as the registry but it doesn't work for me. This works. Thank you very much.Hypersthene
H
1

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;        
    }
Happenstance answered 16/1, 2019 at 6:24 Comment(0)
E
0

In createParser method replace the first parameter NamedXContentRegistry.EMPTY with JsonXContent.jsonXContent

Erleena answered 23/1, 2018 at 12:30 Comment(4)
createParser only accepts NamedXContentRegistry - Your suggestion does not compile since the value is JsonXContentLaxation
Seems like it is available in v6.2 github.com/elastic/elasticsearch/blob/6.2/server/src/main/java/…Erleena
You can look at the test cases here github.com/elastic/elasticsearch/blob/6.2/server/src/test/java/…Erleena
That is an entirely different createParser method, from ESTestCase.Bangs

© 2022 - 2025 — McMap. All rights reserved.