ElasticSearch字段索引时默认映射为String [英] ElasticSearch Fields Mapping to String by default when indexing

查看:447
本文介绍了ElasticSearch字段索引时默认映射为String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我先解释一下我的情景。
我从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 或任何其他类型:


  1. 您没有真正将这些字段发送为 int ,所以你发送 '10'而不是 10


  2. 您已经插入了至少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:

  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" }
        }
    }
}
'

这篇关于ElasticSearch字段索引时默认映射为String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆