加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 18:00:00 所属栏目:百科 来源:网络整理
导读:我似乎无法在NEST 2.0中获得多字段映射的语法 – 如果这是正确的术语.我发现的每个映射示例似乎都是 = NEST的1.x版本.我是Elasticsearch和NEST的新手,我一直在阅读他们的文档,但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中的“基于属性的映射”部分.

干杯!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读