简体   繁体   English

ElasticSearch-使用NEST(.net)添加\\更新SearchTemplate

[英]ElasticSearch - add\update SearchTemplate using NEST (.net)

i got this elasticsearch instance, and im trying to implement something like a "dynamic" searchtemplate. 我得到了这个elasticsearch实例,并且我试图实现类似“动态”搜索模板的功能。

Case: I got a userinterface where we can adjust the boosting of certain types. 案例:我有一个用户界面,我们可以在其中调整某些类型的增强。 ex. 例如 "Boost hits in Articles by 10", "Boost hits on the product page by 20" “在文章中将点击率提高10”,“在产品页面上将点击率提高20”

Today: I can write up the query itself using fluent DSL, which would be generated everytime someone searches. 今天:我可以使用流利的DSL编写查询本身,该查询将在每次有人搜索时生成。

Future: I would love to have this in a searchtemplate that would just get updated everytime a change is made, thereby just passing in the template name and a query string - instead of having to look up the current boosting values on every request. 未来:我希望将其包含在一个每次进行更改时都会更新的searchtemplate中,从而仅传递模板名称和查询字符串-而不是必须在每个请求中查找当前的boosting值。

My research: So the nest client supports adding searchtemplates to the ES instance, but it seems like it only supports "inline" scripts and i cant seem to find a way to parse my existing query written in "Fluent DSL" to the JSON equivalent. 我的研究:嵌套客户端支持将搜索模板添加到ES实例,但是似乎它仅支持“内联”脚本,而且我似乎找不到找到将以“ Fluent DSL”编写的现有查询解析为JSON等效方法的方法。

Fallback idea: Writing the script manually using json \\ query DSL. 后备想法:使用json \\ query DSL手动编写脚本。

Any ideas \\ solutions out there? 有什么想法\\解决方案吗? :) :)

Version: ElasticSearch\\ElasticSearch.NET\\NEST v5.2 版本:ElasticSearch \\ ElasticSearch.NET \\ NEST v5.2

As pointed out in the comments, there is a serializer on the NEST client, you can also use the NewtonSoft JSON library as pointet out in this answer - but as also stated, to get the exact JSON use the Nest Client serializer, which wraps NewtonSoft JSON with the correct configuration. 正如评论中指出的那样,NEST客户端上有一个序列化程序,您也可以使用NewtonSoft JSON库作为此答案中的要点-但也如前所述,要使用Nest Client序列化程序来获得确切的JSON,Nest Client序列化程序包装了NewtonSoft具有正确配置的JSON。

Here is an example on the implementation i did using Object initializer syntax, as the one stated in the answer mentioned above uses an old library. 这是我使用对象初始化器语法实现的一个示例,因为上面提到的答案中所述的使用了一个旧库。

var searchRequest = new SearchRequest()
        {
            Query = query
        };
        var myBytes = _client.Serializer.SerializeToBytes(searchRequest);
        var jsonSearchTemplate = Encoding.UTF8.GetString(myBytes);

As extra information, this is how you add it to your cluster: 作为额外的信息,这是将其添加到群集中的方式:

//Define your template ID, this is later used when doing the search.
var templateRequest = new PutSearchTemplateDescriptor("my_template");

//Add your parsed json as the inline script
templateRequest.Template(jsonSearchTemplate);

var response = _client.PutSearchTemplate(templateRequest);

How to query using the template: (I also got multiple parameters that needs to be sent in) 如何使用模板进行查询:(我还需要发送多个参数)

var response =
            _client.SearchTemplate<MyIndexObject>(
                e =>
                    e.Index($"myIndex")
                        .Id("my_template")
                        .Params(new Dictionary<string, object>()
                        {
                            {"query_string", "Obamacare"},
                            {"min_should_match", "70%"}
                        }));

Hope this helps anyone else out there struggling with the same thing. 希望这可以帮助其他人在同一件事上苦苦挣扎。

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

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