我们允许客户在创建索引时定义自定义分析器。我们希望在json中指定此名称,以通过基础的ElasticSearch文档提供最大的灵活性和可理解性。
我想使用对json字符串中定义的分析器,映射器等的任意描述来创建索引。凭感觉,我的命令是
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"] } } } } } }
理想情况下,我的代码看起来像
string json = RetrieveJson(); ElasticSearchClient client = InitializeClient(); client.CreateIndexUsingJson( json ); // this is the syntax I can't figure out
这里的帖子试图通过实例化IndexSettings然后调用Add(“analysis”,json)来实现此目的,但是Add不是我正在使用的ElasticSearch库版本上的函数。
我可以想象的选项包括:
这两种机制的文档都很少。
我绝对在尝试避免使用CreateIndex的lambda函数版本,因为将用户的json转换为lamdba表达式,只是立即将其转换回NEST中的json,这很痛苦。
上面#1或#2的其他选项或具体示例非常受赞赏,这是解决此问题的推荐方法。
最简单的解决方案是原始问题中选项1的实现。
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以避免对安全性和连接细节进行逆向工程。
做出这个结论是费力的,而且如果不处理大量未记录的NEST代码,这是完全不可能的。