Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Java映射转换为SearchSourceBuilder Elasticsearch 7.2 Java高级API_Java_Rest_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch - Fatal编程技术网 elasticsearch,Java,Rest,elasticsearch" /> elasticsearch,Java,Rest,elasticsearch" />

将Java映射转换为SearchSourceBuilder Elasticsearch 7.2 Java高级API

将Java映射转换为SearchSourceBuilder Elasticsearch 7.2 Java高级API,java,rest,elasticsearch,Java,Rest,elasticsearch,我正在编写一个REST应用程序(javax.ws.rs),它接收来自客户端的搜索请求并将它们提交给Elasticsearch高级API。我想要客户 (主要基于浏览器的javascript)能够使用Elasticsearch REST API指令组合搜索 其余端点的定义如下所示: @Path("list") @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Respons

我正在编写一个REST应用程序(javax.ws.rs),它接收来自客户端的搜索请求并将它们提交给Elasticsearch高级API。我想要客户 (主要基于浏览器的javascript)能够使用Elasticsearch REST API指令组合搜索

其余端点的定义如下所示:

@Path("list")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response list(Map<String, Object> req) {
  ...

我在调用
parseXContent
时收到一个IOException。使用调试器查看字符串
json
,其中包含无法打印的字符。有什么建议吗?

我发现了一个有效的代码模式,它看起来有点复杂,但符合逻辑。任何地方都没有任何文档可以让您找到这一点。这是从张贴在各处留言板上的一些碎片拼凑而成的

    try {

        // convert the Map into a JSON string to parse.  Alternatively
        // you could just take the string directly from the HTTP request
        // but the Map form makes it easy to manipulate. 

        XContentBuilder xcb = XContentFactory.jsonBuilder();
        xcb.map(req);
        String json = Strings.toString(xcb);

        // Create an XContentParser and borrow a NamedXContentRegistry from
        // the SearchModule class.  Without that the parser has no way of
        // knowing the query syntax.

        SearchModule sm = new SearchModule(Settings.EMPTY, false, Collections.emptyList());
        XContentParser parser = XContentFactory.xContent(XContentType.JSON)
                .createParser(new NamedXContentRegistry(sm.getNamedXContents()),
                              LoggingDeprecationHandler.INSTANCE,
                              json);

        // Finally we can create our SearchSourceBuilder and feed it the
        // parser to ingest the request.  This can throw and IllegalArgumentException
        // if something isn't right with the JSON that we started with.

        SearchSourceBuilder ssb = new SearchSourceBuilder();
        ssb.parseXContent(parser);

        // Now create a search request and use it

        SearchRequest sr = new SearchRequest(Log.INDEX);
        sr.source(ssb);
        SearchResponse resp = client.search(sr, RequestOptions.DEFAULT);
我已经用来自客户端的许多不同的JSON查询对此进行了测试,它们似乎都像DirectRESTAPI那样工作。以下是一个例子:

{
    from: 0,
    size: 1000,
    query: {
      match_all: { boost: 1 }
    },
    sort: [
      { timestamp: { 'order': 'asc' } }
    ]
}

希望这篇文章能让其他人从我经历的痛苦搜索中解脱出来。如果有人能提出更好的方法,我将不胜感激。

哪种方法不起作用?你用的是哪种型号的橡皮筋?请具体点。@MichieleegWater我用代码更新了我的文章,我想我很接近了。谢谢你的帮助。
XContentBuilder xcb = XContentFactory.jsonBuilder();
xcb.map(req);
String json = Strings.toString(xcb);

SearchSourceBuilder ssb = new SearchSourceBuilder();
ssb.query(QueryBuilders.wrapperQuery(json));
XContentBuilder xcb = XContentFactory.jsonBuilder();
xcb.map(req);
String json = Strings.toString(xcb);

SearchSourceBuilder ssb = new SearchSourceBuilder();
ssb.query(QueryBuilders.wrapperQuery(json));