0

I have a question regarding the last version of Elasticsearch.

Until the previous version I was using the following Java API search:

SearchRequestBuilder request = client.prepareSearch(index).setSource(jsonQuery)

Now setSource is deprecated and it is recommended to use setQuery(QueryBuilder) instead.

Is it possible to pass the whole json query as before?

This is the query syntax I was using and I would like to keep if it is possible:

"{"
+ "\"query\": {"
+ "    \"bool\": {"
+ "      \"filter\": { ... }"
+ "     }"
+ "  },"
+ "  \"fields\": ["
+ "    \"xxx\","
+ "  ],"
+ "  \"size\": 1000"
+ "}";

I do not want to split body, fields and size using:

setQuery(QueryBuilders.wrapperQuery(jsonQuery)).setSize(size).storedFields(fields)
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Ribas
  • 137
  • 1
  • 11

1 Answers1

0

Yes, you can use QueryBuilders.wrapperQuery() which will create an instance of WrapperQueryBuilder in order to achieve this:

SearchRequestBuilder request = client.prepareSearch(index)
    .setQuery(QueryBuilders.wrapperQuery(jsonQuery))
    .setSize(1000)
    .fields("xxx1", "xxx2");
Val
  • 207,596
  • 13
  • 358
  • 360
  • Thank you for the answer Val. I have already tried this implementation but in this way I am not able to include the stored fields from jsonQuery since I am defining the body. – Ribas Nov 17 '16 at 13:21
  • Not sure I understand, can you elaborate? Update your question with the query you were doing previously and what won't work with this solution. – Val Nov 17 '16 at 13:25
  • Please update your question and format your code, it'll be more legible and easier to help you. – Val Nov 17 '16 at 13:31
  • Indeed, you'll need to specify the `fields` and `size` differently, directly on the request object. – Val Nov 17 '16 at 14:09
  • I hoped there was still a way to declare all the query from a String. Anyway thank you for your help – Ribas Nov 17 '16 at 14:15