Whats the best way to configure mappings programmatically?


#1

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?


(Joshua P) #2

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};

client.CreateIndex(
                c =>
                    c.Index("stable_test")
                        .InitializeUsing(indexSettings)
                        .AddMapping<IndexableSearchTable>(m => m.MapFromAttributes())); 

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


#3

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)


(Jason Wee) #4

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

jason


#5

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..


(system) #6