Elasticsearch NEST使用指南:映射和分析

NEST提供了多种映射方法,这里介绍下通过Attribute自定义映射。

一、简单实现

1、定义业务需要的POCO,并指定需要的Attribute

复制代码
[ElasticsearchType(Name = "student")]
public class Student
{
    [Nest.String(Index = FieldIndexOption.NotAnalyzed)]
    public string Id { get; set; }

    [Nest.String(Analyzer = "standard")]
    public string Name { get; set; }

    [Nest.String(Analyzer = "standard")]
    public string Description { get; set; }

    public DateTime DateTime { get; set; }
}
复制代码

2、接着我们通过.AutoMap()来实现映射

复制代码
var descriptor = new CreateIndexDescriptor("db_student")
    .Settings(s => s.NumberOfShards(5).NumberOfReplicas(1))
    .Mappings(ms => ms
        .Map<Student>(m => m.AutoMap())
    );

client.CreateIndex(descriptor);
复制代码

注意:通过.Properties()可以重写通过Attribute定义的映射

二、Attribute介绍

1、StringAttribute

属性名值类型描述
Analyzer string 分析器名称,值包含standard、simple、whitespace、stop、keyward、pattern、language、snowball、custom等,查看分析器更多信息请点击Elasticsearch Analyzers
Boost double 加权值,值越大得分越高
NullValue string 插入文档时,如果数据为NULL时的默认值
Index FieldIndexOption 是否使用分析器,默认使用FieldIndexOption.Analyzed,禁止使用分析器FieldIndexOption.NotAnalyzed

2、NumberAttribute

属性名值类型描述
type NumberType 构造函数参数,指定当前属性的类型,NumberType.Default、Float、Double、Integer、Long、Short、Byte
Boost double 加权值,值越大得分越高
NullValue double 插入文档时,如果数据为NULL时的默认值

3、BooleanAttribute

属性名值类型描述
Boost double 加权值,值越大得分越高
NullValue double 插入文档时,如果数据为NULL时的默认值

4、DateAttribute

属性名值类型描述
Boost double 加权值,值越大得分越高
NullValue string 插入文档时,如果数据为NULL时的默认值
Format string  

5、ObjectAttribute

属性名值类型描述
type string/Type 构造函数参数,指定当前属性的类型T
Dynamic DynamicMapping
我的网站 http://www.a-du.net
原文地址:https://www.cnblogs.com/a-du/p/8031873.html