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]

Jotschi
  • 3,270
  • 3
  • 31
  • 52

3 Answers3

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);
}
Jotschi
  • 3,270
  • 3
  • 31
  • 52
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;        
    }
Rahul Kosambi
  • 113
  • 1
  • 7
0

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

Aman B
  • 2,276
  • 1
  • 18
  • 26
  • createParser only accepts NamedXContentRegistry - Your suggestion does not compile since the value is JsonXContent – Jotschi Jan 23 '18 at 12:50
  • Seems like it is available in v6.2 https://github.com/elastic/elasticsearch/blob/6.2/server/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContent.java – Aman B Jan 23 '18 at 14:41
  • You can look at the test cases here https://github.com/elastic/elasticsearch/blob/6.2/server/src/test/java/org/elasticsearch/search/builder/SearchSourceBuilderTests.java – Aman B Jan 23 '18 at 14:42
  • That is an entirely different createParser method, from ESTestCase. – Jaap Mar 14 '18 at 10:20