Home > Software engineering >  Set json string custom query to an elastic search request, java RestHighLevelClient
Set json string custom query to an elastic search request, java RestHighLevelClient

Time:11-24

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.

CodePudding user response:

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);
  • Related