Mapping,就是对索引库中索引的字段名及其数据类型进行定义,类似于关系数据库中表建立时要定义字段名及其数据类型那样,不过es的 mapping比数据库灵活很多,它可以动态添加字段。一般不需要要指定mapping都可以,因为es会自动根据数据格式定义它的类型,如果
Mapping,就是对索引库中索引的字段名及其数据类型进行定义,类似于关系数据库中表建立时要定义字段名及其数据类型那样,不过es的 mapping比数据库灵活很多,它可以动态添加字段。一般不需要要指定mapping都可以,因为es会自动根据数据格式定义它的类型,如果你需要对某 些字段添加特殊属性(如:定义使用其它分词器、是否分词、是否存储等),就必须手动添加mapping。有两种添加mapping的方法,一种是定义在配 置文件中,一种是运行时手动提交mapping,两种选一种就行了。
先介绍在配置文件中定义mapping,你可以把[mapping名].json文件放到config/mappings/[索引名]目录下,这个目录 要自己创建,一个mapping和一个索引对应,你也可以定义一个默认的mapping,把自己定义的default-mapping.json放到 config目录下就行。json格式如下:
双击代码全选
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
{
"mappings":{
"properties":{
"title":{
"type":"string",
"store":"yes"
},
"description":{
"type":"string",
"index":"not_analyzed"
},
"price":{
"type":"double"
},
"onSale":{
"type":"boolean"
},
"type":{
"type":"integer"
},
"createDate":{
"type":"date"
}
}
}
}
接 下来介绍通过请求添加mapping,下面为一个添加productIndex索引库的mapping的json格式请求。其中productIndex 为索引类型,properties下面的为索引里面的字段,type为数据类型,store为是否存储,"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
{
"productIndex":{
"properties":{
"title":{
"type":"string",
"store":"yes"
},
"description":{
"type":"string",
"index":"not_analyzed"
},
"price":{
"type":"double"
},
"onSale":{
"type":"boolean"
},
"type":{
"type":"integer"
},
"createDate":{
"type":"date"
}
}
}
}
用java api调用的代码如下:
先创建空索引库
双击代码全选
1 client.admin().indices().prepareCreate(
"productIndex"
).execute().actionGet();
put mapping
双击代码全选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
XContentBuilder mapping = jsonBuilder()
.startObject()
.startObject(
"productIndex"
)
.startObject(
"properties"
)
.startObject(
"title"
).field(
"type"
,
"string"
).field(
"store"
,
"yes"
).endObject()
.startObject(
"description"
).field(
"type"
,
"string"
).field(
"index"
,
"not_analyzed"
).endObject()
.startObject(
"price"
).field(
"type"
,
"double"
).endObject()
.startObject(
"onSale"
).field(
"type"
,
"boolean"
).endObject()
.startObject(
"type"
).field(
"type"
,
"integer"
).endObject()
.startObject(
"createDate"
).field(
"type"
,
"date"
).endObject()
.endObject()
.endObject()
.endObject();
PutMappingRequest mappingRequest = Requests.putMappingRequest(
"productIndex"
).type(
"productIndex"
).source(mapping);
client.admin().indices().putMapping(mappingRequest).actionGet();
Mapping,就是对索引库中索引的字段名及其数据类型进行定义,类似于关系数据库中表建立时要定义字段名及其数据类型那样,不过es的 mapping比数据库灵活很多,它可以动态添加字段。一般不需要要指定mapping都可以,因为es会自动根据数据格式定义它的类型,如果
先介绍在配置文件中定义mapping,你可以把[mapping名].json文件放到config/mappings/[索引名]目录下,这个目录 要自己创建,一个mapping和一个索引对应,你也可以定义一个默认的mapping,把自己定义的default-mapping.json放到 config目录下就行。json格式如下:
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
|
{
"mappings":{
"properties":{
"title":{
"type":"string",
"store":"yes"
},
"description":{
"type":"string",
"index":"not_analyzed"
},
"price":{
"type":"double"
},
"onSale":{
"type":"boolean"
},
"type":{
"type":"integer"
},
"createDate":{
"type":"date"
}
}
}
}
|
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
|
{
"productIndex":{
"properties":{
"title":{
"type":"string",
"store":"yes"
},
"description":{
"type":"string",
"index":"not_analyzed"
},
"price":{
"type":"double"
},
"onSale":{
"type":"boolean"
},
"type":{
"type":"integer"
},
"createDate":{
"type":"date"
}
}
}
}
|
先创建空索引库
1 | client.admin().indices().prepareCreate( "productIndex" ).execute().actionGet(); |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
XContentBuilder mapping = jsonBuilder()
.startObject()
.startObject(
"productIndex"
)
.startObject(
"properties"
)
.startObject(
"title"
).field(
"type"
,
"string"
).field(
"store"
,
"yes"
).endObject()
.startObject(
"description"
).field(
"type"
,
"string"
).field(
"index"
,
"not_analyzed"
).endObject()
.startObject(
"price"
).field(
"type"
,
"double"
).endObject()
.startObject(
"onSale"
).field(
"type"
,
"boolean"
).endObject()
.startObject(
"type"
).field(
"type"
,
"integer"
).endObject()
.startObject(
"createDate"
).field(
"type"
,
"date"
).endObject()
.endObject()
.endObject()
.endObject();
PutMappingRequest mappingRequest = Requests.putMappingRequest(
"productIndex"
).type(
"productIndex"
).source(mapping);
client.admin().indices().putMapping(mappingRequest).actionGet();
|