# Question Regarding Dynamic mapping using template

**URL:** <https://discuss.elastic.co/t/question-regarding-dynamic-mapping-using-template/7384>\
**Category:** Elasticsearch\
**Created:** [April 18, 2012, 2:35pm UTC](https://discuss.elastic.co/t/question-regarding-dynamic-mapping-using-template/7384 "2012-04-18T14:35:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rahul\_Sharma](https://avatars.discourse-cdn.com/v4/letter/r/df705f/32.png) [@Rahul\_Sharma](https://discuss.elastic.co/u/Rahul_Sharma)\
**Post date:** [April 18, 2012, 2:35pm UTC](https://discuss.elastic.co/t/question-regarding-dynamic-mapping-using-template/7384/1 "2012-04-18T14:35:28Z")

</div>

Hi,

I use the following code using dynamic template to create index.

client.admin().indices().preparePutTemplate("template1")  
.setTemplate("IndexName")  
.setOrder(0)  
.addMapping("type1", _formXContentBuilderObj_)  
.execute().actionGet();

In  
_formXContentBuilderObj_ method I create ----

```
   b =

```

jsonBuilder().startObject().startObject("type1").startObject("properties");  
b.startObject(internalKey).field("type", "string").field("store",  
"yes").field("index", "not\_analyzed").endObject();  
b.startObject(internalKey + "\_analyzed").field("type",  
"string").field("index", "analyzed").field("store", "no").

```
field("filter", new String[]{"lowercase"}).endObject();
   b.endObject().endObject().endObject();

```

* * *

And then use it to index Documents.  
And I use faceting (termsStats) to do some aggregation.

This works great till I don't change the mapping.  
When I add new mapping (new term), the term stats behaves weired. Though I  
use the "Not analyzed" term for faceting it gives me tokenized result.  
But this does not happen for rest of the old terms.

Am I missing something here. I tried doing forceRefresh but does not help.

Would really appreciate if someone can throw some light.

Thanks  
Rahul

---

<div class="post-metadata">

**Author:** ![Rahul\_Sharma](https://avatars.discourse-cdn.com/v4/letter/r/df705f/32.png) [@Rahul\_Sharma](https://discuss.elastic.co/u/Rahul_Sharma)\
**Post date:** [April 19, 2012, 8:17am UTC](https://discuss.elastic.co/t/question-regarding-dynamic-mapping-using-template/7384/2 "2012-04-19T08:17:15Z")

</div>

Further updates to my experiment:

Realized that when I add a new term in the mapping it ignores the  
"not\_analyzed" part and uses the default analyzer. Not sure why.  
And that's the reason the termsStatsFacet is returning wrong results.

Would appreciate your input.

Thanks  
Rahul

On Wed, Apr 18, 2012 at 8:05 PM, Rahul Sharma  
[rahul.sharma.coder@gmail.com](mailto:rahul.sharma.coder@gmail.com)wrote:

> Hi,
> 
> I use the following code using dynamic template to create index.
> 
> client.admin().indices().preparePutTemplate("template1")  
> .setTemplate("IndexName")  
> .setOrder(0)  
> .addMapping("type1", _formXContentBuilderObj_)  
> .execute().actionGet();
> 
> In  
> _formXContentBuilderObj_ method I create ----
> 
> ```
> b =
> 
> ```
> 
> jsonBuilder().startObject().startObject("type1").startObject("properties");  
> b.startObject(internalKey).field("type", "string").field("store",  
> "yes").field("index", "not\_analyzed").endObject();  
> b.startObject(internalKey + "\_analyzed").field("type",  
> "string").field("index", "analyzed").field("store", "no").
> 
> ```
> field("filter", new String[]{"lowercase"}).endObject();
> b.endObject().endObject().endObject();
> 
> ```
> 
> * * *
> 
> And then use it to index Documents.  
> And I use faceting (termsStats) to do some aggregation.
> 
> This works great till I don't change the mapping.  
> When I add new mapping (new term), the term stats behaves weired. Though I  
> use the "Not analyzed" term for faceting it gives me tokenized result.  
> But this does not happen for rest of the old terms.
> 
> Am I missing something here. I tried doing forceRefresh but does not help.
> 
> Would really appreciate if someone can throw some light.
> 
> Thanks  
> Rahul

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [April 21, 2012, 11:17am UTC](https://discuss.elastic.co/t/question-regarding-dynamic-mapping-using-template/7384/3 "2012-04-21T11:17:19Z")

</div>

Are you trying to update an existing mapping for that field? If so, you  
can't change a field from analyzed to not\_analyzed once it has been created.

On Thu, Apr 19, 2012 at 11:17 AM, Rahul Sharma \<[rahul.sharma.coder@gmail.com](mailto:rahul.sharma.coder@gmail.com)

> wrote:

> Further updates to my experiment:
> 
> Realized that when I add a new term in the mapping it ignores the  
> "not\_analyzed" part and uses the default analyzer. Not sure why.  
> And that's the reason the termsStatsFacet is returning wrong results.
> 
> Would appreciate your input.
> 
> Thanks  
> Rahul
> 
> On Wed, Apr 18, 2012 at 8:05 PM, Rahul Sharma \<  
> [rahul.sharma.coder@gmail.com](mailto:rahul.sharma.coder@gmail.com)\> wrote:
> 
> > Hi,
> > 
> > I use the following code using dynamic template to create index.
> > 
> > client.admin().indices().preparePutTemplate("template1")  
> > .setTemplate("IndexName")  
> > .setOrder(0)  
> > .addMapping("type1", _formXContentBuilderObj_)  
> > .execute().actionGet();
> > 
> > In  
> > _formXContentBuilderObj_ method I create ----
> > 
> > ```
> > b =
> > 
> > ```
> > 
> > jsonBuilder().startObject().startObject("type1").startObject("properties");  
> > b.startObject(internalKey).field("type", "string").field("store",  
> > "yes").field("index", "not\_analyzed").endObject();  
> > b.startObject(internalKey + "\_analyzed").field("type",  
> > "string").field("index", "analyzed").field("store", "no").
> > 
> > ```
> > field("filter", new String[]{"lowercase"}).endObject();
> > b.endObject().endObject().endObject();
> > 
> > ```
> > 
> > * * *
> > 
> > And then use it to index Documents.  
> > And I use faceting (termsStats) to do some aggregation.
> > 
> > This works great till I don't change the mapping.  
> > When I add new mapping (new term), the term stats behaves weired. Though  
> > I use the "Not analyzed" term for faceting it gives me tokenized result.  
> > But this does not happen for rest of the old terms.
> > 
> > Am I missing something here. I tried doing forceRefresh but does not help.
> > 
> > Would really appreciate if someone can throw some light.
> > 
> > Thanks  
> > Rahul

---

<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, 3:31am UTC](https://discuss.elastic.co/t/question-regarding-dynamic-mapping-using-template/7384/4 "2017-07-06T03:31:34Z")

</div>


