ElasticSearch NEST:通过指定json通过ElasticClient创建索引 [英] ElasticSearch NEST: Create an index through ElasticClient by specifying json

查看:385
本文介绍了ElasticSearch NEST:通过指定json通过ElasticClient创建索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们允许客户在创建索引时定义自定义分析器.我们希望在json中指定此名称,以通过基础的ElasticSearch文档提供最大的灵活性和可理解性.

We allow the client to define custom analyzers at the time they create an index. We would prefer to specify this in json to provide maximum flexibility and understandability via the underlying ElasticSearch documentation.

我想使用对json字符串中定义的分析器,映射器等的任意描述来创建索引.凭感觉,我的命令是

I would like to create an index using an arbitrary description of analyzers, mappers, etc., defined in a json string. Using sense, my command is

PUT /my_index
{
    "settings": 
    {
        "analysis": 
        {
            "char_filter" : 
            {
                "my_mapping" : 
                {
                    "type" : "mapping",
                    "mappings" : [".=>,", "'=>,"]
                }
            },
            "analyzer": 
            {
                "my_analyzer": 
                {
                    "type":         "custom",
                    "tokenizer":    "standard",
                    "filter":       ["lowercase" ],
                    "char_filter" : ["my_mapping"]
                }
            }
         }
      }
   }
}

理想情况下,我的代码看起来像

Ideally, my code would look something like

string json = RetrieveJson();
ElasticSearchClient client = InitializeClient();
client.CreateIndexUsingJson( json ); // this is the syntax I can't figure out

帖子此处尝试通过实例化IndexSettings然后调用Add("analysis ,json),但Add不是我正在使用的ElasticSearch库版本上的函数.

The post here attempts to do this by instantiating an IndexSettings then calling Add( "analysis", json ), but Add is not a function on the ElasticSearch library version I'm using.

我可以想象的选项包括:

The options I can imagine include:

  1. 以某种方式使用ElasticClient.Raw.IndicesCreatePost或类似的方式
  2. 通过IndexSettingsConverter.ReadJson()将json字符串反序列化为IndexSettings对象,然后通过ElasticClient.CreateIndex(ICreateIndexRequest)应用它

这两种机制的文档都很少.

Both of these mechanisms have very scant documentation.

我绝对要避免使用CreateIndex的lambda函数版本,因为将用户的json转换为lamdba表达式,只是立即在NEST中将其转换回json是很痛苦的.

I'm absolutely trying to avoid the lambda function versions of CreateIndex, since it would be miserable to translate the user's json into lamdba expressions, only to immediately translate them back into json deep in NEST.

非常感谢上面#1或#2的其他选项或具体示例,这是解决此问题的推荐方法.

Other options or concrete examples of #1 or #2 above are very much appreciated, as is a recommended approach to solving this problem.

推荐答案

最简单的解决方案是原始问题中选项#1的实现.

Easiest solution was an implementation of Option #1 from the original question.

public void CreateIndex(string indexName, string json)
{
    ElasticClient client = GetClient();
    var response = _client.Raw.IndicesCreatePost(indexName, json);
    if (!response.Success || response.HttpStatusCode != 200)
    {
        throw new ElasticsearchServerException(response.ServerError);
    }
}

修改了转换器,JsonReaders和JsonSerializers之后,我发现IndexSettingsConverter似乎没有正确地将任意设置json反序列化为有效的IndexSettings对象.感觉到一个兔子洞,我接受了Manolis的建议,并弄清楚了如何直接对ElasticClient.IElasticsearchClient应用任意json以避免对安全性和连接细节进行逆向工程.

After tinkering around with converters and JsonReaders and JsonSerializers, I found that the IndexSettingsConverter didn't seem to properly deserialize arbitrary settings json into a valid IndexSettings object. Sensing a rabbit hole, I took Manolis' suggestion and figured out how to apply the arbitrary json directly against the ElasticClient.IElasticsearchClient to avoid having to reverse-engineer security and connection details.

为得出这个结论付出了巨大的努力,如果不处理大量未记录的NEST代码,这是完全不可能的.

Painful effort to come to this conclusion, and completely impossible without working through a whole lot of undocumented NEST code.

这篇关于ElasticSearch NEST:通过指定json通过ElasticClient创建索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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