# Deducing field type using elasticsearch Java API

**URL:** <https://discuss.elastic.co/t/deducing-field-type-using-elasticsearch-java-api/5929>\
**Category:** Elasticsearch\
**Created:** [November 21, 2011, 5:10pm UTC](https://discuss.elastic.co/t/deducing-field-type-using-elasticsearch-java-api/5929 "2011-11-21T17:10:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vannya](https://avatars.discourse-cdn.com/v4/letter/v/e495f1/32.png) [@vannya](https://discuss.elastic.co/u/vannya)\
**Post date:** [November 21, 2011, 5:10pm UTC](https://discuss.elastic.co/t/deducing-field-type-using-elasticsearch-java-api/5929/1 "2011-11-21T17:10:14Z")

</div>

Hi all,

In my project, I need to deduce the data type of some JSON tree liefs  
based on their value and I want to use the same API/mechanism that  
elasticsearch uses to do this.  
At some point, when indexing documents, elasticsearch will need to do  
this job, and I would like to use the same API/mechanism that  
elasticsearch uses to avoid different type deduction.

Can anyone help?

Thanks in advance!

---

<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:** [November 22, 2011, 12:40pm UTC](https://discuss.elastic.co/t/deducing-field-type-using-elasticsearch-java-api/5929/2 "2011-11-22T12:40:55Z")

</div>

The logic is quite naive, Numeric Java types are mapped to the relevant  
numeric types, and Dates are serialized using the default ISO string format.

On Mon, Nov 21, 2011 at 7:10 PM, vannya [vannyas@gmail.com](mailto:vannyas@gmail.com) wrote:

> Hi all,
> 
> In my project, I need to deduce the data type of some JSON tree liefs  
> based on their value and I want to use the same API/mechanism that  
> elasticsearch uses to do this.  
> At some point, when indexing documents, elasticsearch will need to do  
> this job, and I would like to use the same API/mechanism that  
> elasticsearch uses to avoid different type deduction.
> 
> Can anyone help?
> 
> Thanks in advance!

---

<div class="post-metadata">

**Author:** ![vannya](https://avatars.discourse-cdn.com/v4/letter/v/e495f1/32.png) [@vannya](https://discuss.elastic.co/u/vannya)\
**Post date:** [November 22, 2011, 3:40pm UTC](https://discuss.elastic.co/t/deducing-field-type-using-elasticsearch-java-api/5929/3 "2011-11-22T15:40:36Z")

</div>

Although the logic is naive, is it available in some kind of API that can be used ?

I cannot take the chance of getting the wrong type deduction (or different from the one that elastic will do) and that's why I need to use the exact same code that elastic uses to do his own deduction.

Thanks,

---

<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:** [November 25, 2011, 7:48am UTC](https://discuss.elastic.co/t/deducing-field-type-using-elasticsearch-java-api/5929/4 "2011-11-25T07:48:49Z")

</div>

If you are that concerned, then generate the json yourself (I recommend  
jackson), you can always provide the document in a binary format. Here is  
an example of the "types" supported:  
[https://github.com/elasticsearch/elasticsearch/blob/master/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java#L616](https://github.com/elasticsearch/elasticsearch/blob/master/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java#L616)  
.

On Tue, Nov 22, 2011 at 5:40 PM, vannya [vannyas@gmail.com](mailto:vannyas@gmail.com) wrote:

> Although the logic is naive, is it available in some kind of API that can  
> be  
> used ?
> 
> I cannot take the chance of getting the wrong type deduction (or different  
> from the one that elastic will do) and that's why I need to use the exact  
> same code that elastic uses to do his own deduction.
> 
> Thanks,
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/Deducing-field-type-using-elasticsearch-Java-API-tp3525339p3528065.html](http://elasticsearch-users.115913.n3.nabble.com/Deducing-field-type-using-elasticsearch-Java-API-tp3525339p3528065.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![Jeremie\_BORDIER1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jeremie_bordier1/32/2860_2.png) [@Jeremie\_BORDIER1](https://discuss.elastic.co/u/Jeremie_BORDIER1)\
**Post date:** [November 25, 2011, 8:04am UTC](https://discuss.elastic.co/t/deducing-field-type-using-elasticsearch-java-api/5929/5 "2011-11-25T08:04:42Z")

</div>

Another less complicated way to ensure types is to specify the mapping  
explicitly before indexing. This way you never end up with a wrongly  
interpreted type by ES, and you know what to expect on the client  
side.

Jérémie

On Fri, Nov 25, 2011 at 8:48 AM, Shay Banon [kimchy@gmail.com](mailto:kimchy@gmail.com) wrote:

> If you are that concerned, then generate the json yourself (I recommend  
> jackson), you can always provide the document in a binary format. Here is an  
> example of the "types"  
> supported: [https://github.com/elasticsearch/elasticsearch/blob/master/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java#L616](https://github.com/elasticsearch/elasticsearch/blob/master/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java#L616).
> 
> On Tue, Nov 22, 2011 at 5:40 PM, vannya [vannyas@gmail.com](mailto:vannyas@gmail.com) wrote:
> 
> > Although the logic is naive, is it available in some kind of API that can  
> > be  
> > used ?
> > 
> > I cannot take the chance of getting the wrong type deduction (or different  
> > from the one that elastic will do) and that's why I need to use the exact  
> > same code that elastic uses to do his own deduction.
> > 
> > Thanks,
> > 
> > --  
> > View this message in context:  
> > [http://elasticsearch-users.115913.n3.nabble.com/Deducing-field-type-using-elasticsearch-Java-API-tp3525339p3528065.html](http://elasticsearch-users.115913.n3.nabble.com/Deducing-field-type-using-elasticsearch-Java-API-tp3525339p3528065.html)  
> > Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

--  
Jérémie 'ahFeel' BORDIER

---

<div class="post-metadata">

**Author:** ![vannya](https://avatars.discourse-cdn.com/v4/letter/v/e495f1/32.png) [@vannya](https://discuss.elastic.co/u/vannya)\
**Post date:** [November 25, 2011, 5:33pm UTC](https://discuss.elastic.co/t/deducing-field-type-using-elasticsearch-java-api/5929/6 "2011-11-25T17:33:38Z")

</div>

Hi,

Jérémie, my data set is huge and I don't know it exactly it's fields,  
so I don't know which fields to map... and even if I knew, it wouldn't  
be easier! Thanks for your reply anyway.

Shay, thanks for tour reply.  
My solution was achieved using JsonPrimitive type comparions  
(isBoolean, isString and isNumber), which in the end, does the same  
that your field API: compares a field class against the native Java  
types. I belive my problem is solved now.

Thank you all,

On Nov 25, 8:04 am, Jérémie BORDIER [jeremie.bord...@gmail.com](mailto:jeremie.bord...@gmail.com) wrote:

> Another less complicated way to ensure types is to specify the mapping  
> explicitly before indexing. This way you never end up with a wrongly  
> interpreted type by ES, and you know what to expect on the client  
> side.
> 
> Jérémie
> 
> On Fri, Nov 25, 2011 at 8:48 AM, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:
> 
> > If you are that concerned, then generate the json yourself (I recommend  
> > jackson), you can always provide the document in a binary format. Here is an  
> > example of the "types"  
> > supported: [https://github.com/elasticsearch/elasticsearch/blob/master/modules/el](https://github.com/elasticsearch/elasticsearch/blob/master/modules/el)....
> 
> > On Tue, Nov 22, 2011 at 5:40 PM, vannya [vann...@gmail.com](mailto:vann...@gmail.com) wrote:
> 
> > > Although the logic is naive, is it available in some kind of API that can  
> > > be  
> > > used ?
> 
> > > I cannot take the chance of getting the wrong type deduction (or different  
> > > from the one that elastic will do) and that's why I need to use the exact  
> > > same code that elastic uses to do his own deduction.
> 
> > > Thanks,
> 
> > > --  
> > > View this message in context:  
> > > [http://elasticsearch-users.115913.n3.nabble.com/Deducing-field-type-u](http://elasticsearch-users.115913.n3.nabble.com/Deducing-field-type-u)...  
> > > Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).
> 
> --  
> Jérémie 'ahFeel' BORDIER

---

<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:47am UTC](https://discuss.elastic.co/t/deducing-field-type-using-elasticsearch-java-api/5929/7 "2017-07-06T03:47:27Z")

</div>


