您的位置:首页 > 移动开发

Elasticsearch实战系列-mapping 设置

2017-04-01 16:08 771 查看
 


Elasticsearch实战系列-mapping 设置

标签: elasticsearch
2016-02-29 17:49 5316人阅读 评论(0) 收藏 举报


 分类:

Search(3) 


版权声明:本文为博主原创文章,未经博主允许不得转载。

目录(?)[+]

本篇主要讲解Mapping的一些相关配置与需要注意的地方,说到Mapping大家可能觉得有些不解,其实我大体上可以将Elasticsearch理解为一个RDBMS(关系型数据库,比如MySQL),那么index
就相当于数据库实例,type可以理解为表,这样mapping可以理解为表的结构和相关设置的信息(当然mapping有更大范围的意思)。
默认情况不需要显式的定义mapping, 当新的type或者field引入时,Elasticsearch会自动创建并且注册有合理的默认值的mapping(毫无性能压力), 只有要覆盖默认值时才必须要提供mapping定义。

什么是Mapping

先看看官方文档中的定义

A mapping defines the fields within a type, the datatype for each field, and how the field should be handled by Elasticsearch. A mapping is also used to configure metadata associated with the type.

Mapping定义了type中的诸多字段的数据类型以及这些字段如何被Elasticsearch处理,比如一个字段是否可以查询以及如何分词等。

自定义Mapping

下面是一个简单的Mapping定义:
curl -XPUT 'http://localhost:9200/megacorp' -d '
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"employee": {
"properties": {
"first_name": {
"type": "string"
},
"last_name": {
"type": "string"
},
"age": {
"type": "integer"
},
"about": {
"type": "string"
},
"interests": {
"type": "string"
},
"join_time": {
"type": "date",
"format": "dateOptionalTime",
"index": "not_analyzed"
}
}
}
}
}
'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
其中employee是type(相当于关系数据库中的表),在employee中我们定义了first_name、last_name、age、about、interests、join_time这6个属性。
{ "interests": { "type": "string"}
1
1
type表示field的数据类型,上例中interests的type为string表示为普通文本。 

Elasticsearch支持以下数据类型:
文本: string
数字: byte, short, integer, long
浮点数: float, double
布尔值: boolean
Date: date
对于type为 string 的字段,最重要的属性是:index and analyzer。 
1、index 

index 属性控制string如何被索引,它有三个可选值:
analyzed: First analyze the string, then index it. In other words, 

index this field as full text.
not_analyzed:: Index this field, so it is searchable, but index the 

value exactly as specified. Do not analyze it.
no: Don’t index this field at all. This field will not be 

searchable.
对于string类型的filed index 默认值是: analyzed.如果我们想对进行精确查找, 那么我们需要将它设置为: not_analyzed。 

例如:
{ "tag": { "type": "string", "index": "not_analyzed" }
1
1
2、analyzer 

对于 string类型的字段, 我们可以使用 analyzer 属性来指定在搜索阶段和索引阶段使用哪个分词器. 默认, Elasticsearch 使用 standard analyzer, 你也可以指定Elasticsearch内建的其它分词器,比如 whitespace, simple, or english:
例如:
{ "tweet": { "type": "string", "analyzer": "english" }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: