赞
踩
es中的文档等价于java中的对象,那么在java中有字段(比如string, int, long等类型), 同理在es索引中的具体字段也是有类型的.
PUT /testDocument/article/1
{
"title" : "elasticsearchshi是是什么",
"author" : "zhangsan",
"titleScore" : 60
}
如上这种操作没有指明索引类型, es会自动根据类型识别字段.那么如何知道es自动识别的字段类型呢?
kibana中的命令GET/testDocument/article/_mapping
然后再插入一条数据
PUT /testDocument/article/2
{
"title" : "elasticsearchshi是是什么",
"author" : "zhangsan",
"titleScore" : 66.666
}
查询数据: GET /doucment/aricle/2
class Article{
private String title;
private String author;
private ? titleScore; //《什么类型合适》?如果使用double类型,那么后面肯定会有数据格式转换的异常 doublelong
}
# 先删除原来的document DELETE testDocument # 然后添加新数据,设置mapping PUT testDocument { "mappings": { "article" : { "properties": { "title" : {"type": "text"} , "author" : {"type": "text"} , "titleScore" : {"type": "double"} } } } } # 查询索引数据类型 get testDocument/article/_mapping
PUT school
{
"mapping": {
"logs": {
"properties": {"messages" : {"type": "text"}}
}
}
}
GET /school/_mapping/logs
POST /school/_mapping/logs
{
"properties": {"number" : {"type": "text"}}
}
GET /school/_mapping/logs
上图说明mapping映射设置成功~
语法:
GET /{index}/_mapping/{type}/field/{field}
GET /school/_mapping/logs/field/number
问题: 使用es默认生成mapping, 使用java api统计数据的, 发现数据不准确.
解决: 发现程序员A在创建索引的时候,使用的默认mapping,导致精度损失。为了解决这个问题,重新构建一个新的库,这个库设置某个字段为double。
PUT document1
{
"mappings": {
"article" : {
"properties":
{
"title" : {"type": "text"} ,
"author" : {"type": "text"} ,
"titleScore" : {"type": "double"}
}
}
}
}
POST _reindex
{
"source": {
"index": "document"
},
"dest": {
"index": "document1"
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。