使用NEST 2.x创建具有多字段映射语法的索引 [英] Create index with multi field mapping syntax with NEST 2.x

查看:133
本文介绍了使用NEST 2.x创建具有多字段映射语法的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在NEST 2.0中,我似乎无法获得语法正确的多字段映射 - 如果这是正确的术语。我找到的映射的每个例子似乎都是< = NEST的1.x版本。我是Elasticsearch和NEST的新手,我一直在阅读他们的文档,但是NEST文档还没有完全更新为2.x。



基本上,我不需要索引或存储整个类型。我需要索引的一些字段,我需要索引和检索的一些字段,一些我不需要索引,仅用于检索。

  MyType 
{
//索引此&允许检索。
int Id {get;组; }

//索引此&允许检索。
// **另外**,在我的搜索&排序,我需要排序这个**整个**字段,而不只是个别的标记。
string CompanyName {get;组; }

//不要为此搜索索引,但是要存储以进行显示。
DateTime CreatedDate {get;组; }

//索引此搜索,但不用于检索/显示。
string CompanyDescription {get;组; }

//嵌套这个。
列表< MyChildType>地点{get;组; }
}

MyChildType
{
//索引此&允许检索。
string LocationName {get;组; }

//等属性。
}

我有可以索引整个对象和子对象使用以下内容作为示例:

  client.Index(item,i =&i;指数(INDEXNAME)); 

然而,实际的对象比这个大得多,我真的不需要大部分它。我发现这个,看起来像我想要做的,但是在旧版本中:多现场映射弹性搜索



我认为映射是我要去的,但是像我说的那样,我刚刚到Elasticsearch,NEST和I '尝试学习术语。



要温柔! :)这是我第一次提出一个关于SO的问题。谢谢!

解决方案

据我所见,你没有任何复杂的类型,你正在尝试地图。所以你可以轻松地使用NEST属性来映射你的对象。



查看这个:

 code> [Nest.ElasticsearchType] 
public class MyType
{
// Index this&允许检索。
[Nest.Number(Store = true)]
int Id {get;组; }

//索引此&允许检索。
// **另外**,在我的搜索&排序,我需要排序这个**整个**字段,而不只是个别的标记。
[Nest.String(Store = true,Index = Nest.FieldIndexOption.Analyzed,TermVector = Nest.TermVectorOption.WithPositionsOffsets)]
string CompanyName {get;组; }

//不要为此搜索索引,但是要存储以进行显示。
[Nest.Date(Store = true,Index = Nest.NonStringIndexOption.No)]
DateTime CreatedDate {get;组; }

//索引此搜索,但不用于检索/显示。
[Nest.String(Store = false,Index = Nest.FieldIndexOption.Analyzed)]
string CompanyDescription {get;组; }

[Nest.Nested(Store = true,IncludeInAll = true)]
//嵌套。
列表< MyChildType>地点{get;组; }
}

[Nest.ElasticsearchType]
public class MyChildType
{
// Index this&允许检索。
[Nest.String(Store = true,Index = Nest.FieldIndexOption.Analyzed)]
string LocationName {get;组; }

//等属性。
}

在此声明之后,要在弹性搜索中创建此映射,您需要拨打电话类似于:

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

使用AutoMap()调用NEST将从您的POCO读取您的属性,并相应地创建一个映射请求。 / p>

另请参阅 here



干杯!


I just can't seem to get the syntax correct for multi field mapping in NEST 2.0--if that's the correct terminology. Every example I've found for mapping seems to be <= the 1.x version of NEST. I'm new to Elasticsearch and NEST, and I've been reading their documentation, but the NEST documentation hasn't been completely updated for 2.x.

Basically, I don't need to index or store the entire type. Some fields I need for indexing only, some fields I'll need to index and retrieve, and some I don't need for indexing, just for retrieval.

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.
}

I've have been able to index the entire object and child as-is using the following as an example:

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

However, the actual object is a lot larger than this, and I really don't need most of it. I've found this, which looks like what I think I want to do, but in an older version: multi field mapping elasticsearch

I think "mapping" is what I'm going for, but like I said, I'm new to Elasticsearch and NEST and I'm trying to learn the terminology.

Be gentle! :) It's my first time to ask a question on SO. Thanks!

解决方案

As far as I can see, you don't have any complex types that you are trying map. So you can easily use NEST attributes to map your objects.

Check this out:

[Nest.ElasticsearchType]
public class MyType
{
    // Index this & allow for retrieval.
    [Nest.Number(Store=true)]
    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.
    [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.
}

After this declaration, to create this mapping in elasticsearch you need to make a call similar to:

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

With AutoMap() call NEST will read your attributes from your POCO and create a mapping request accordingly.

Also see "Attribute Based Mapping" section from here.

Cheers!

这篇关于使用NEST 2.x创建具有多字段映射语法的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆