es搜索核心与实战Day03(Mapping)
一.Mapping
- Mapping类似数据库中的schema的定义,作用如下
1. 定义索引中的字段的名称
2.定义字段的数据类型,例如字符串,数字,布尔…
3.字段,倒排索引的相关配置,(Analyzed or Not Analyzed,Analyzer)
-
Mapping 会把JSON文档映射成Lucene所需要的扁平格式
-
一个Mapping属于一个索引的Type
1. 每个文档都属于一个Type,
2. 一个Type有一个Mapping定义
3. 7.0开始,不需要在Mapping定义中指定type信息
二.字段的数据类型
- 简单类型
1.Text / Keyword
2.Date
3. Integer / Floating
4.BooleanI
5.IPv4 & IPv6
- 复杂类型 -对象和嵌套对象
- 对象类型 /嵌套类型
- 特殊类型
- geo_ point & geo_ shape / percolator
//写入文件查看mapping
PUT mapping_test/_doc/1
{
"firstName":"Chan",
"lastName":"Jackie",
"loginDate":"2018-07-24T10:29:48.1032"
}
//查看mapping文件
GET mapping_test/_mapping
//Delete index
DELETE mapping_test
//dynamic mapping,推断字段的类型
PUT mapping_text/_doc/1
{
"uid":"123",
"isVip":"false",
"isAdmin":"true",
"age":19,
"heigh":180
}
//查看Dynamic
GET mapping_text/_mapping
‘true’ | ’false‘ | ‘strict’ | |
---|---|---|---|
文档可索引 | YES | YES | NO |
字段可索引 | YES | NO | NO |
Mapping被更新 | YES | NO | NO |
3.定义mapping
PUT movies
{
"mappings": {
"doc":{
"dynamic" :"false"
}
}
}
- 当dynamic被设置成false的时候,存在新增字段的数据写入,该数据可以被索引,但是新增字段被丢弃
- 当设置成Strict模式时候,数据写入直接报错
如何定义一个Mapping
PUT movies
{
"mappings": {
//define your mappings here
}
}
Index -控制当前字段是否被索引。默认为true。如果设置成false, 该字段不可被搜索
PUT users
{
"mappings" : {
"properties" : {
"firstName" : {
"type" : "text"
},
"lastName" : {
"type" : "text"
//对NULL值实现搜索
"null_value":"NULL"
},
"mobile" : {
"type" : "text" ,
//该字段不可被搜索
"index": false
}
}
}
}
4.数组类型
//数组类型
PUT users/_doc/1
{
"name":"onebirds",
"interests":"reading"
}
PUT users/_doc/1
{
"name":"twobirds",
"interests":["reading","music"]
}
POST users/_search
{
"query":{
"match_all":{}
}
}
5.char_filter
//自动去除html标签
POST _analyze
{
"tokenizer": "keyword",
"char_filter": ["html_strip"],
"text": "<b>hello world</b>"
}
//使用char fillter进行替换
POST _analyze
{
"tokenizer": "standard",
"char_filter": [
{
"type":"mapping",
"mappings":["- => _"]
}
],
"text": "123-456, I-test! test-990 650-555-1234"
}
//char fillter替换表情符号
POST _analyze
{
"tokenizer": "standard",
"char_filter": [
{
"type":"mapping",
"mappings":[":)=>happy",":(=>sad"]
}
],
"text":["I am felling :)","Feeling :( today"]
}
//正则表达式
GET _analyze
{
"tokenizer": "standard",
"char_filter": [
{
"type":"pattern_replace",
"pattern":"http://(.*)",
"replacement":"$1"
}
],
"text":"http://www.elastic.co"
}
6.whitespace与stop
//whitespace与stop
GET _analyze
{
"tokenizer":"whitespace",
"filter":["stop"],
"text":["The rain in Spain falls mainly on the plain."]
}
//remove加入lowercase后,The被当成stopword删除
GET _analyze
{
"tokenizer":"whitespace",
"filter":["lowercase","stop"],
"text":["The rain in Spain falls mainly on the plan."]
}