【发布时间】:2015-05-15 13:37:24
【问题描述】:
我们允许客户在创建索引时定义自定义分析器。我们更愿意在 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
here 帖子试图通过实例化 IndexSettings 然后调用 Add("analysis", json) 来做到这一点,但 Add 不是我正在使用的 ElasticSearch 库版本上的函数。
我能想到的选项包括:
- 不知何故使用 ElasticClient.Raw.IndicesCreatePost 或类似的东西
- 通过 IndexSettingsConverter.ReadJson() 将 json 字符串反序列化为 IndexSettings 对象,然后通过 ElasticClient.CreateIndex(ICreateIndexRequest) 应用该对象
这两种机制的文档都非常少。
我绝对尽量避免使用 CreateIndex 的 lambda 函数版本,因为将用户的 json 转换为 lamdba 表达式会很痛苦,而只是立即将它们转换回 NEST 深处的 json。
非常感谢上述 #1 或 #2 的其他选项或具体示例,这是解决此问题的推荐方法。
【问题讨论】:
-
您使用哪个版本的 NEST?
-
我与@mcating 合作。我们使用 Nest 版本 1.1.2。
标签: c# elasticsearch nest