ElasticSearch字段索引时默认映射为String [英] ElasticSearch Fields Mapping to String by default when indexing
问题描述
我从RDBMS获取数据并将其推入ElasticSearch。
提取结果以列表的形式,我正在准备批量索引请求,如下所示:
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();
我的地图=包含字符串到字符串的地图,字符串到大十进制,字符串到大整数等。
例如
{BIRTHDATE:2015-03-05,NAME:deepankar,AGE:22,AMOUNT:15.5}
但是,当我在testindex中看到我的testtype的映射时,字段的所有映射都是type:string
为什么字段不映射到type:string或type:long,甚至type:date作为elasticsearch它是默认情况下的?
Elasticsearch将尝试通过第一个插入来猜测字段类型,除非您创建和映射字段
您的字段被索引为 string
而不是 long
或任何其他类型:
-
您没有真正将这些字段发送为
int
,所以你发送'10'
而不是10
-
您已经插入了至少1个文件,该文档的
字符串 ld,所以如果你已经插入了你的第一个文件,那个 AGE:'22'
Elasticsearch会将该字段设置为type:string
并且任何未来的插入将具有字符串
值。
如果要确保,可以删除当前索引,重新创建它,然后在插入第一个文档之前手动设置映射,如下所示:
curl -XPUT'http:// localhost:9200 / testindex / _mapping / testmapping'-d'
{
testmapping:{
properties :{
birthdate:{type:date,format:dateOptionalTime},
name:{type:string},
年龄:{type:long},
amount:{type:double}
}
}
}
'
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?
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:
You're not really sending these fields as
int
, so you're sending'10'
instead of10
You've already inserted at least 1 document that had a
string
value for that field, so if you've inserted your first document withAGE: '22'
Elasticsearch will set that field totype: string
and any future inserts will have astring
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" }
}
}
}
'
这篇关于ElasticSearch字段索引时默认映射为String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!