I'm trying to upgrade my application from ElasticSearch Nest 1.7 to 2.4 and the attribute based mapping looks like it should work, but it doesn't (completely). I have a model class like this:
[DataContract]
[ElasticsearchType(IdProperty = "Id")]
public class Series
{
[DataMember]
[String(Index = FieldIndexOption.Analyzed, Analyzer = "custom_en")]
public string Description { get; set; }
[DataMember]
[String(Index = FieldIndexOption.NotAnalyzed)]
public HashSet<Role> ReleasableTo { get; set; }
}
The equivalent declaration in Nest 1.x was working, and my term query against the field returned the results I was expecting. When I received no results, I checked the mapping, and to my surprise the Index = FieldIndexOption.NotAnalyzed
was not respected. My generated mapping was something like this:
"properties" : {
"description" : {
"type": "string"
}
"releasableTo" : {
"type": "string"
}
}
So neither the field that I had a custom analyzer set was marked properly, nor the the field I needed not to be analyzed was marked properly.
This is the code I used to call initialize everything:
var indexDescriptor = new CreateIndexDescriptor(DefaultIndex)
.Mappings(ms => ms
.Map<Series>(m => m.AutoMap())
)
);
indexDescriptor.Settings(s => s
.NumberOfShards(3)
.NumberOfReplicas(2)
.Analysis(a => a
.CharFilters(c => c.Mapping("&_to_and", mf => mf.Mappings( "&=> and ")))
.TokenFilters(t => t.Stop("en_stopwords", tf=>tf.StopWords(new StopWords(stopwords)).IgnoreCase()))
.Analyzers(z => z
.Custom("custom_en", ca => ca
.CharFilters("html_strip", "&_to_and")
.Tokenizer("standard")
.Filters("lowercase", "en_stopwords")
)
)
)
);
client.CreateIndex(indexDescriptor);
NOTE: client
is the elasticsearch client.
I know the DataContract
attributes don't strictly apply for ElasticSearch, but I also need to serialize these objects to disk for processing. With Nest 1.x there was no conflict, so it didn't cause any problems.
I'm not concerned about the analyzer creation. I'm concerned that the mapping doesn't respect anything more specific than the type.
How do I get Nest 2.x to respect the additional information in the attributes so I don't have to manually map them when declaring the mappings?
So it turns out that the problem with the mapping had to do with other types that were mapped at the same time. There was an invalid response from the index that I didn't catch. It was very frustrating to work through, but the mapping is working correctly now.