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

我似乎无法在nesT 2.0中获得多字段映射的语法 – 如果这是正确的术语.我发现的每个映射示例似乎都是< = nesT的1.x版本.我是Elasticsearch和nesT的新手,我一直在阅读他们的文档,但nesT文档还没有完全更新2.x. 基本上,我不需要索引或存储整个类型.我只需要索引的一些字段,我需要索引和检索的一些字段,以及一些我不需要索引的字段,仅用于检索.
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.
}

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

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

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

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

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

解决方法

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

看一下这个:

[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中创建此映射,您需要进行类似于以下的调用

var mappingResponse = elasticclient.Map<MyType>(m => m.AutoMap());

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

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

干杯!

相关文章

原文地址:http://msdn.microsoft.com/en-us/magazine/cc163...
前言 随着近些年微服务的流行,有越来越多的开发者和团队所采...
最近因为比较忙,好久没有写博客了,这篇主要给大家分享一下...
在多核CPU在今天和不久的将来,计算机将拥有更多的内核,Mic...
c语言输入成绩怎么判断等级
字符型数据在内存中的存储形式是什么