Elasticsearch.Nest 教程系列 4-6 映射:Ignoring properties | 映射时忽略 POCO 上的某些属性


POCO 类属性进行映射时,可以通过以下几种方式忽略部分属性

  • 在使用继承了 ElasticsearchPropertyAttributeBase 的各特性上使用 Ignore 属性:如在使用 TextAttribute 时,使用 Ingore 属性进行属性忽略。
  • 在使用 PropertyNameAttribute 的时候使用Ignore属性。
  • 使用 ConnectionSettings.DefaultMappingFor(Func<ClrTypeMappingDescriptor,
    IClrTypeMapping> selector) 方法的时候进行指定需要忽略的属性。
  • 使用能够被 IElasticsearchSerializer 识别的 Ignore 特性,内建的 SourceSerializer 可以使用 [IgnoreAttribute] 进行忽略。

忽略类中的部分属性

示例:

[ElasticsearchType(RelationName = "company")]
public class CompanyWithAttributesAndPropertiesToIgnore
{
    public string Name { get; set; }

    [Text(Ignore = true)]
    public string PropertyToIgnore { get; set; }

    [PropertyName("anotherPropertyToIgnore", Ignore = true)]
    public string AnotherPropertyToIgnore { get; set; }

    public string FluentMappingPropertyToIgnore { get; set; }

    [Ignore, JsonIgnore]
    public string JsonIgnoredProperty { get; set; }
}

//在 ConnectionSettings 中设置忽略 FluentMappingPropertyToIgnore 属性
var settings = new ConnectionSettings(new Uri(_esSettings.ServerUri))
    .DefaultMappingFor<CompanyWithAttributesAndPropertiesToIgnore>(m => m.Ignore(i => i.FluentMappingPropertyToIgnore))
  • 以上除了 Name 属性外,其他所有属性均会在映射时被忽略掉。

最终结果为:

{
  "ignoremappingtest" : {
    "mappings" : {
      "properties" : {
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

忽略继承的属性

可以在 ConnectionSettings 使用 Ignore 方法来进行忽略,如上方的 FluentMappingPropertyToIgnore 属性。

假设有以下测试用模型:映射 Child 时忽略 Parent 中的 IgnoreMe 属性

public class Parent
{
    public int Id { get; set; }
    public string Description { get; set; }
    public string IgnoreMe { get; set; }
}

public class Child : Parent { }

使用 Ignore 属性:

// ConnectionSettings 中设置忽略 IngoreMe
var settings = new ConnectionSettings(new Uri(_esSettings.ServerUri)).DefaultMappingFor<Child>(m => m.IndexName("test").Ignore(i => i.IgnoreMe))

//创建索引:
var createIndexResponse = client.Indices.Create("test", c => c
        .Map<Child>(m => m
            .AutoMap()
        )
    );

结果如下:

{
  "mappings": {
    "properties": {
      "id": {
        "type": "integer"
      },
      "desc": {
        "fields": {
          "keyword": {
            "ignore_above": 256,
            "type": "keyword"
          }
        },
        "type": "text"
      }
    }
  }
}

覆盖继承的属性

使用 new 关键字来覆盖基类中的同名属性,以让 ES 在进行数据类型推断时,使用跟父类不同的数据类型。

public class ParentWithStringId : Parent
{
    public new string Id { get; set; }
}

最终在创建索引的时候,会覆盖 int 类型的 id,如下:

var createIndexResponse = client.Indices.Create("myindex", c => c
        .Map<ParentWithStringId>(m => m
            .AutoMap()
        )
    );

mappging 信息如下:

{
  "mappings": {
    "properties": {
      "id": {                     
        "type": "text",
        "fields": {
          "keyword": {
            "ignore_above": 256,
            "type": "keyword"
          }
        }
      }
    }
  }
}
  • 以上结果忽略了 IgnoreMe 和 IgnoreMe 属性。
  • 父类中的 id 是 integer 数据类型。