c# – 使用NEST 2.x创建具有多字段映射语法的索引

前端之家收集整理的这篇文章主要介绍了c# – 使用NEST 2.x创建具有多字段映射语法的索引前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我似乎无法在NEST 2.0中获得多字段映射的语法 – 如果这是正确的术语.我发现的每个映射示例似乎都是< = NEST的1.x版本.我是Elasticsearch和NEST的新手,我一直在阅读他们的文档,但NEST文档还没有完全更新2.x. 基本上,我不需要索引或存储整个类型.我只需要索引的一些字段,我需要索引和检索的一些字段,以及一些我不需要索引的字段,仅用于检索. @H_404_2@MyType { // Index this & allow for retrieval. int Id { get; set; } // Index this & allow for retrieval. // **Also**,in my searching & sorting,I need to sort on this **entire** field,not just individual tokens. string CompanyName { get; set; } // Don't index this for searching,but do store for display. DateTime CreatedDate { get; set; } // Index this for searching BUT NOT for retrieval/displaying. string CompanyDescription { get; set; } // Nest this. List<MyChildType> Locations { get; set; } } MyChildType { // Index this & allow for retrieval. string LocationName { get; set; } // etc. other properties. }

我已经能够使用以下示例将整个对象和子进行索引:

@H_404_2@client.Index(item,i => i.Index(indexName));

但是,实际对象比这个要大很多,我真的不需要大部分.我发现了这个,看起来像我想要的那样,但在旧版本中:multi field mapping elasticsearch

我认为“映射”是我想要的,但就像我说的,我是Elasticsearch和NEST的新手,我正在努力学习术语.

要温柔! :)这是我第一次在SO上提问.谢谢!

解决方法

据我所知,你没有任何复杂的类型,你正在尝试映射.因此,您可以轻松使用NEST属性来映射对象.

看一下这个:

@H_404_2@[Nest.ElasticsearchType] public class MyType { // Index this & allow for retrieval. [Nest.Number(Store=true)] int Id { get; set; } // Index this & allow for retrieval. // **Also**,not just individual tokens. [Nest.String(Store = true,Index=Nest.FieldIndexOption.Analyzed,TermVector=Nest.TermVectorOption.WithPositionsOffsets)] string CompanyName { get; set; } // Don't index this for searching,but do store for display. [Nest.Date(Store=true,Index=Nest.NonStringIndexOption.No)] DateTime CreatedDate { get; set; } // Index this for searching BUT NOT for retrieval/displaying. [Nest.String(Store=false,Index=Nest.FieldIndexOption.Analyzed)] string CompanyDescription { get; set; } [Nest.Nested(Store=true,IncludeInAll=true)] // Nest this. List<MyChildType> Locations { get; set; } } [Nest.ElasticsearchType] public class MyChildType { // Index this & allow for retrieval. [Nest.String(Store=true,Index = Nest.FieldIndexOption.Analyzed)] string LocationName { get; set; } // etc. other properties. }

在此声明之后,要在elasticsearch中创建此映射,您需要进行类似于以下的调用

@H_404_2@var mappingResponse = elasticClient.Map<MyType>(m => m.AutoMap());

使用AutoMap()调用NEST将从POCO中读取您的属性并相应地创建映射请求.

另请参见here中的“基于属性的映射”部分.

干杯!

原文链接:https://www.f2er.com/csharp/97206.html

猜你在找的C#相关文章