Whats the best way to configure mappings programmatically?

I've been using elastic since about 2009 ... and I've always found myself embedding elastic into our app - and configuring it programmatically.

I've always found programmatically configuring the mapping for an index to be quite clunky.
But perhaps I just havent paid close attention to the enhancements over the years and I am now Doing It Wrong.

So, what is the best/cleanest way to configure mappings programmatically?

I really like the way it's handled in the C# library, NEST. You can use attributes on your properties like so:

[ElasticProperty(Index = FieldIndexOption.NotAnalyzed)] public string country_tx { get; set; }

The mappings can be as complicated or simple as you want. You can define custom analyzers here as well.

And you use classes to define your types

[ElasticType(Name = "indexablesearchtable")] class IndexableSearchTable

Creating your index with this mapping is easy:

var indexSettings = new IndexSettings {NumberOfReplicas = 1, NumberOfShards = 5};

                c =>
                        .AddMapping<IndexableSearchTable>(m => m.MapFromAttributes())); 

The right answer for you, however, is also going to depend on your language of choice.

For me its Java... (I'm running elastic embedded)
I dont know of anything like that in the Java API (but I might be wrong).
(like your example, I have generally had to write something to generate the Map representing the mappings - I just wish there was a nicer java api for specifying the mappings)

or you create write into json file and then slurp by the java code? https://github.com/elastic/elasticsearch/blob/master/src/test/java/org/elasticsearch/index/mapper/all/SimpleAllMapperTests.java#L74-L75


Yes, creating a json document - or a map - theyre of the same class. They're both pretty clunky.
I guess the one advantage is that you can compare the json file with what you think it should look like from the documentation.

But fundamentally, there are a fixed number of types, and a fixed number of options for each type - surely we can encapsulate that in a mapping builder api..