简体   繁体   English

将 json 字符串自定义查询设置为弹性搜索请求,java RestHighLevelClient

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

I'm using the RestHighLevelClient and I'm facing some trouble.我正在使用 RestHighLevelClient 并且遇到了一些麻烦。

From front end, I will receive a json String like that:从前端,我会收到一个 json 字符串,如下所示:

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

and I need to add that String to a SearchRequest or, better, create a SearchRequest from the json above我需要将该字符串添加到 SearchRequest,或者更好的是,从上面的 json 创建一个 SearchRequest

How can I do that without creating a parser and create programmatically the QueryBuilder to add to the searchRequest?如何在不创建解析器并以编程方式创建 QueryBuilder 以添加到 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:编辑2:

Sorry, the wrapper works just perfectly, I had to remove the "query" from the string.抱歉,包装器工作得很好,我不得不从字符串中删除“查询”。 my fault.我的错。

As Val suggested, you can write the SearchRequest this way:正如 Val 所建议的,您可以这样编写 SearchRequest:

SearchRequest searchRequest = new SearchRequest("indexName);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(QueryBuilders.wrapperQuery("your json goes here"));
searchRequest.source(searchSourceBuilder);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM