# Whats the best way to configure mappings programmatically?

**URL:** <https://discuss.elastic.co/t/whats-the-best-way-to-configure-mappings-programmatically/1320>\
**Category:** Elasticsearch\
**Created:** [May 26, 2015, 2:23pm UTC](https://discuss.elastic.co/t/whats-the-best-way-to-configure-mappings-programmatically/1320 "2015-05-26T14:23:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mooky](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@mooky](https://discuss.elastic.co/u/mooky)\
**Post date:** [May 26, 2015, 2:23pm UTC](https://discuss.elastic.co/t/whats-the-best-way-to-configure-mappings-programmatically/1320/1 "2015-05-26T14:23:09Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Joshua\_P](https://avatars.discourse-cdn.com/v4/letter/j/c89c15/32.png) [@Joshua\_P](https://discuss.elastic.co/u/Joshua_P)\
**Post date:** [May 26, 2015, 7:58pm UTC](https://discuss.elastic.co/t/whats-the-best-way-to-configure-mappings-programmatically/1320/2 "2015-05-26T19:58:27Z")

</div>

I really like the way it's handled in the C# library, [NEST](http://nest.azurewebsites.net/nest/quick-start.html). 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.

---

<div class="post-metadata">

**Author:** ![mooky](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@mooky](https://discuss.elastic.co/u/mooky)\
**Post date:** [June 1, 2015, 4:32pm UTC](https://discuss.elastic.co/t/whats-the-best-way-to-configure-mappings-programmatically/1320/3 "2015-06-01T16:32:38Z")

</div>

> [@Joshua\_P](#):
>
> 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)

---

<div class="post-metadata">

**Author:** ![Jason\_Wee](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Jason\_Wee](https://discuss.elastic.co/u/Jason_Wee)\
**Post date:** [June 2, 2015, 7:27am UTC](https://discuss.elastic.co/t/whats-the-best-way-to-configure-mappings-programmatically/1320/4 "2015-06-02T07:27:33Z")

</div>

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](https://github.com/elastic/elasticsearch/blob/master/src/test/java/org/elasticsearch/index/mapper/all/SimpleAllMapperTests.java#L74-L75)

jason

---

<div class="post-metadata">

**Author:** ![mooky](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@mooky](https://discuss.elastic.co/u/mooky)\
**Post date:** [June 2, 2015, 1:22pm UTC](https://discuss.elastic.co/t/whats-the-best-way-to-configure-mappings-programmatically/1320/5 "2015-06-02T13:22:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 12:10am UTC](https://discuss.elastic.co/t/whats-the-best-way-to-configure-mappings-programmatically/1320/6 "2017-07-06T00:10:14Z")

</div>


