将Elastic&NEST搜索从1.6.2升级到2.3.3。

我们曾经能够在PutMappingDescriptorElasticClient.CreateIndex()之间共享相同的ElasticClient.Map()

但是在2.3.3中,CreateIndex需要TypeMappingDescriptor,而Map需要PutMappingDescriptor

我们如何共享相同的映射配置?

最佳答案

Nest的官方开发人员在其github linked here中回答了这个问题。

基本上,您不使用Func<PutMappingDescriptor<Project>, IPutMappingRequest>,而是直接使用PutMappingDescriptor<Project>。通过更新PutMappingDescriptor<Project>并从那里建立流利的映射。

创建索引需要ITypeMapping,而更新索引需要实现IPutMappingRequestITypeMapping。因此,您可以使用PutMappingDescriptor满足这两个要求。

要创建索引,请使用:

```
client.CreateIndex(“projects”,c => c
.mappings(ms => ms
.Map(m => GetMapping())
)
);

```
您可以忽略在lambda中传递的m并使用您创建的。您可以执行此操作的原因可以在NEST的source code中找到,该文件会创建一个空的TypeMappingDescriptor供您进一步使用:
public MappingsDescriptor Map<T>(Func<TypeMappingDescriptor<T>, ITypeMapping> selector) where T : class => Assign(typeof (T), selector?.Invoke(new TypeMappingDescriptor<T>()));
要更新映射,请执行以下操作:
client.Map(GetMapping());

关于elasticsearch - 是否在NEST 2.3.3中共享索引映射配置?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38088851/

10-11 08:42