如何避免在提交对象时扩展映射

时间:2015-06-11 07:53:45

标签: elasticsearch nest

让我们假设我想要索引的下一课:

private class User
{
    public User()
    {
        Id = Guid.NewGuid();
        Added = DateTime.Now;
    }

    public Guid Id { get; protected set; }
    public string LastName { get; set; }
    public DateTime Added { get; protected set; } // Unimportant for search
}

关键是我只需要索引中的IdLastName属性。使用流畅的api一切正常(只会映射指定的属性):

_client.Map<User>(m => m.
    Index("nest_test").
    Properties(p => p.String(s => s.Name(u => u.LastName))));

现在,当我索引对象时,映射将由其余属性扩展。我怎样才能避免这些行为。 (对我来说也很奇怪:MapFromAttributes()映射所有属性,而一个属性甚至没有装饰?!)。

这是一个非常小的例子,但我的一些域对象存在许多关系。我没有尝试过,但我认为在一切都将被提交时我们可以映射这些对象。

1 个答案:

答案 0 :(得分:1)

https://github.com/elastic/elasticsearch-net/issues/1278中的答案复制:

这是由于ES在检测到新字段时的dynamic mapping行为。您可以通过在映射中设置dynamic: falseignore来关闭此行为:

client.Map<Foo>(m => m
    .Dynamic(DynamicMappingOption.Ignore)
    ...
);

client.Map<Foo>(m => m
    .Dynamic(false)
    ...
);

请注意,该属性仍会出现在_source

或者,您可以使用上面提到的fluent ignore属性API,它将完全从映射和_source中排除属性,因为这样做会导致它不进行序列化:

var settings = new ConnectionSettings()
    .MapPropertiesFor<Foo>(m => m
        .Ignore(v => v.Bar)
);

var client = new ElasticClient(settings);

或不太理想,只需在要排除的属性上粘贴Json.NET的[JsonIgnore]属性。