0

Let me first explain my scenario. I am fetching data from RDBMS and pushing it into ElasticSearch. Fetched Results are in the form of List and i am preparing bulk index request like this:

BulkRequestBuilder bulkRequest = client.prepareBulk(); 
for (Map<String,Object> singleDataRow : ResultSet) 
{ 
       IndexRequest indexRequest = new IndexRequest("testindex","testtype",singleDataRow.getObject("NAME")); 
       bulkRequest.add(indexRequest); 
} 

bulkRequest.execute().actionGet(); 

My Map = includes Map of string to string, string to big decimal, string to big integer etc. eg.

{ BIRTHDATE : 2015-03-05 , NAME : deepankar , AGE : 22 , AMOUNT : 15.5 } 

But when i see the mapping of my testtype in testindex, all mapping of fields are of "type" : "string"

Why the fields does not maps to "type": "string" , or "type" : "long" , and even "type" : "date" as elasticsearch does it by default?

1 Answer 1

2

Elasticsearch will attempt to 'guess' the field type by the first insert, unless you create and map fields beforehand.

There are two possible reasons why your fields are being indexed as string instead of long or any other type:

  1. You're not really sending these fields as int, so you're sending '10' instead of 10

  2. You've already inserted at least 1 document that had a string value for that field, so if you've inserted your first document with AGE: '22' Elasticsearch will set that field to type: string and any future inserts will have a string value.

If you want to make sure, you can delete the current index, re-create it and manually set up mapping before inserting the first document, like so:

curl -XPUT 'http://localhost:9200/testindex/_mapping/testmapping' -d '
{
    "testmapping" : {
        "properties" : {
            "birthdate" : { "type" : "date", "format": "dateOptionalTime" },
            "name" : { "type" : "string" },
            "age" : { "type" : "long" },
            "amount" : { "type" : "double" }
        }
    }
}
'
Sign up to request clarification or add additional context in comments.

5 Comments

thankyou for your reply. Actually i did output the singleDataRow ( MAP<String,Object> ) before calling code indexRequest(singleDataRow); code like this : code System.out.println(singleDataRow.get("AGE").getClass()); code Its returning me : java.math.BigDecimal
Is it possible that the first document you inserted into your index was using strings and not integers/floats? In any case, delete the index, create and and use the mapping API to set field types, then insert your documents.
I dont have any index created. I performed DELETE _all using sense , and running my java code, and when i GET testindex/_mapping : all fields have string as their type!
why mapping is not taking long , float , date automatically? it should make this mapping by default??
I saw your answer. I know that we can provide mapping using mapping api, but my question is why es is indexing data using string as default ?? I have my Map<String,Object> singleDataRow.get("AGE").getClass(); returning as java.Math.BigDecimal so its not that my map includes all values as string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.