Set json string custom query to an elastic search request, java RestHighLevelClient

0

I'm using the RestHighLevelClient and I'm facing some trouble.

From front end, I will receive a json String like that:

{"query":{"term":{"something.keyword":"something"}}} 

and I need to add that String to a SearchRequest or, better, create a SearchRequest from the json above

How can I do that without creating a parser and create programmatically the QueryBuilder to add to the searchRequest?

EDIT: I've already tried the wrapper query, but executing this query:

{
  "query": {
    "wrapper": {
      "query": "eyJxdWVyeSI6eyJ0ZXJtIjp7ImV2ZW50LmtpbmQua2V5d29yZCI6ImV2ZW50In19fSA="
    }
  }
}

I have this response:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "parsing_exception",
        "reason" : "unknown query [query]",
        "line" : 1,
        "col" : 10
      }
    ],
    "type" : "parsing_exception",
    "reason" : "unknown query [query]",
    "line" : 1,
    "col" : 10,
    "caused_by" : {
      "type" : "named_object_not_found_exception",
      "reason" : "[1:10] unknown field [query]"
    }
  },
  "status" : 400
}

EDIT 2:

Sorry, the wrapper works just perfectly! I had to remove the "query" from the string, my fault.

elasticsearch java
2021-11-22 13:45:41
1

1

As Val suggested, you can write the SearchRequest this way:

SearchRequest searchRequest = new SearchRequest("indexName");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(QueryBuilders.wrapperQuery("your json goes here"));
searchRequest.source(searchSourceBuilder);
2021-11-23 12:40:25

With wrapper I have some errors: query: String query = "{\"query\":{\"term\":{\"something.kind.keyword\":\"something\"}}} " response: { "error" : { "root_cause" : [ { "type" : "parsing_exception", "reason" : "unknown query [query]", "line" : 1, "col" : 10 } ], "type" : "parsing_exception", "reason" : "unknown query [query]", "line" : 1, "col" : 10, "caused_by" : { "type" : "named_object_not_found_exception", "reason" : "[1:10] unknown field [query]" } }, "status" : 400 }
Pikappa

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................