How to avoid to extend the mapping on commiting an object

145 views Asked by At

Let's assume folowing class which I want to index:

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
}

The point is that I need just the Id and LastName property in the index. Using the fluent api everything works fine (just the specified properties will be mapped):

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

Now, when I index an object the mapping will be extended by the remaining properties. How can I avoid these behavior. (Also odd for me: MapFromAttributes() maps all of the properties, while not even one property is decorated?!).

This is a very little example but some of my domain objects stand in many-many-relations. I didn't try it but I don't think it's possible to map these objects when everything will be committed.

1

There are 1 answers

1
Greg Marzouka On BEST ANSWER

Copied from the answer in https://github.com/elastic/elasticsearch-net/issues/1278:

This is due to the dynamic mapping behavior of ES when it detects new fields. You can turn this behavior off by setting dynamic: false or ignore in your mapping:

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

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

Keep in mind though, the property will still be present in _source.

Alternatively, you can use the fluent ignore property API mentioned above, which will exclude the property entirely from the mapping and _source since doing so will cause it to not undergo serialization:

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

var client = new ElasticClient(settings);

or less ideal, just stick Json.NET's [JsonIgnore] attribute on the properties you want to exclude.