如何将地图转换为SearchSourceBuilder? [英] How do I convert a map to a SearchSourceBuilder?

查看:553
本文介绍了如何将地图转换为SearchSourceBuilder?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Elasticsearch 2.x中,我们使用 source(Map)初始化 SearchRequest 的源:

In Elasticsearch 2.x, we were using source(Map) to initialise a SearchRequest's source:

SearchRequest searchRequest = new SearchRequest();
searchRequest.source((Map<?,?>) request.get("search_request"));

在Elasticsearch 5中,所有 source(...)方法不见了,取而代之的是采用 SearchSourceBuilder 的方法。

In Elasticsearch 5, all source(...) methods are gone, replaced by one taking SearchSourceBuilder. That much is documented.

但是我如何将 Map 转换为 SearchSourceBuilder ?那里似乎没有任何有用的工厂方法,我已经在寻找其他采用 Map 的方法了,似乎没有什么可以跳过的。

But how on earth do I convert a Map to a SearchSourceBuilder? There don't seem to be any useful factory methods in there and I have already scoured for other methods taking Map and nothing seems to jump out.

推荐答案

您突出显示的问题 。。。

The issue you highlight has been reported but this is not possible anymore because of this.

您可以阅读有关此重大更改的全文,但是用两个词来说,在ES 2.x中,协调节点(即接收查询的节点)将委托查询的解析对于每个分片,不仅浪费资源(主要是CPU周期),还存在其他一些缺点,因为不可能在单个位置优化查询。

You can read the full story relating to this big change, but in two words, in ES 2.x the coordinating node (i.e. the one receiving the query) would delegate the parsing of the query to each shard and not only that was a waste of resources (mainly CPU cycles) but there were also a few other drawbacks in that it wasn't possible to optimize the query in a single place.

在ES 5中,他们决定协调节点将执行一次ONCE解析,然后将已解析的查询发送到每个分片。如果您阅读了我链接到的博客文章,您将会发现这应该是一个很大的改进。当然,这意味着您不再能够使用 SearchRequest.source(Map)方法。

In ES 5, they decided that the coordinating node would do the parsing ONCE and then send the parsed query to each shard. If you read that blog post I linked to, you'll see that this is supposed to be a big improvement. Of course, that means that you're not able to use the SearchRequest.source(Map) method anymore.

更新

source(Map) $ c>方法如下:

The original source code of the source(Map) method looked like this:

public SearchRequest source(Map source) {
    try {
        XContentBuilder builder = XContentFactory.contentBuilder(Requests.CONTENT_TYPE);
        builder.map(source);
        return source(builder);
    } catch (IOException e) {
        throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
    }
}

没有什么可以阻止您使用该代码进行转换在您的应用程序代码中。

Nothing prevents you from having that code for doing the transformation in your application code.

我尚未对其进行测试,但是您应该能够创建 SearchSourceBuilder

I haven't tested it but then you should be able to create a SearchSourceBuilder like this:

SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

// from Map to XContent
XContentBuilder builder = ... // see above
// from XContent to JSON
String json = new String(builder.getBytes(), "UTF-8");
// use JSON to populate SearchSourceBuilder
JsonXContent parser = createParser(JsonXContent.jsonXContent, json));
sourceBuilder.parseXContent(new QueryParseContext(parser));

这篇关于如何将地图转换为SearchSourceBuilder?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆