Elasticsearch(二) mapping创建和解析(转)
mapping创建
1 PUT my_index{ 2 "settings": { 3 "number_of_shards ": 5, 4 "number_of_replicas": 1 5 }, 6 "mappings": { 7 "my_doc": { 8 "properties": { 9 "title": { 10 "type": "text", 11 "normalizer": "my_normalizer" 12 }, 13 "name": { 14 "type": "text", 15 "analyzer": "standard", 16 "boost": 2 17 }, 18 "age": { 19 "type": "integer" 20 }, 21 "created": { 22 "type": "date", 23 "format": "strict_date_optional_time||epoch_millis" 24 } 25 } 26 } 27 } 28 }
curl写法 (后面类似以下写法,不再列出)
curl -X PUT "localhost:9200/my_index" -H 'Content-Type: application/json' -d' { # 与上面一致 }
- my_index index名称
- setting 配置
- my_doc type名
- properties 字段配置
mapping参数
- 字段中的type(es数据类型)
1.text 字符串,分词,全文索引
2.keyword 关键字,不分词,适合id,email等这种不分词的字段
3.numeric 数字类型有integer、long、short、byte、double、float等类型
4.date 时间类型
5.boolean 布尔类型
6.binary 接收base64编码的字符串
7.rang 具体有integer_range,float_range,long_range,double_range,date_range,ip_range,可存储范围数据,如下插入
1 PUT index/type/id 2 { 3 "field_name" : { 4 "gte" : 10, 5 "lte" : 20 6 } 7 }
8.数组类型,es实际上不存在array类型,es每个类型都支持转成数组类型,也就是不管定义成integer还是text等都可以以数组形式存进去,如果需要存integer数组,那只需要将这个字段定义成integer就可以了
9.object 对象类型 json格式
10.nested 嵌套类型,object嵌套数组
11.geo_point 经纬度 可存入对象json,字符串,数组
12.ip 可存ipv4 ipv6地址
13.token_count integet类型,统计词个数
1 PUT my_index 2 { 3 "mappings": { 4 "_doc": { 5 "properties": { 6 "name": { 7 "type": "text", 8 "fields": { 9 "length": { 10 "type": "token_count", 11 "analyzer": "standard" 12 } 13 } 14 } 15 } 16 } 17 } 18 } 19 20 PUT my_index/_doc/2 21 { "name": "Rachel Alice Williams" } 22 23 # 查询name字段有三个词的数据 24 GET my_index/_search 25 { 26 "query": { 27 "term": { 28 "name.length": 3 29 } 30 } 31 }
14.join 用于在同个索引下创建父子关系的类型
# 定义父子关系 question父 answer子 PUT my_index { "mappings": { "_doc": { "properties": { "my_join_field": { "type": "join", "relations": { "question": "answer" } } } } } } # 插入父数据 PUT my_index/_doc/1 { "text": "This is a question", "my_join_field": { "name": "question" } } # 插入子数据 routing指向根节点 parent指向直接父节点 PUT my_index/_doc/2?routing=1 { "text": "This is an answer", "my_join_field": { "name": "answer", "parent": "1" } }
其他数据类型还有Alias,mapper-murmur3,mapper-annotated-text,Percolator type,Completion,Geo-Shape datatype
详细参考官方文档 https://www.elastic.co/guide/en/elasticsearch/reference/6.x/mapping-types.html
- analyzer 分析器,作用是分词和词条标准化(比如dogs标准化为dog,大写标准化小写),默认是standard分析器,还可以在创建mapping时配置自定义分析器,并使用自定义分析器。
1 PUT my_index 2 { 3 "settings":{ 4 "analysis":{ 5 "analyzer":{ 6 "my_stop_analyzer":{ 7 "type":"custom", 8 "tokenizer":"standard", 9 "filter":[ 10 "lowercase", 11 "english_stop" 12 ] 13 } 14 }, 15 "filter":{ 16 "english_stop":{ 17 "type":"stop", 18 "stopwords":"_english_" 19 } 20 } 21 } 22 }, 23 "mappings":{ 24 "_doc":{ 25 "properties":{ 26 "title": { 27 "type":"text", 28 "analyzer":"my_analyzer" 29 } 30 } 31 } 32 } 33 }
- normalizer 与analyzer类似,但是分词结果都是单一词
- boost 设置查询相关性权重,默认是1
- coerce 是否强制转换,默认为true,比如integer类型参数可以直接传递字符串数字,会自动转为数字。设置为false则不会强转,则参数值传递字符串会报错。
- copy_to 可将字段指向一个组别,之后的查询可直接查询该组别,查询范围会从组别包含的字段中进行查询,多个值用空格隔开,类似_all。
1 PUT my_index 2 { 3 "mappings": { 4 "_doc": { 5 "properties": { 6 "first_name": { 7 "type": "text", 8 "copy_to": "full_name" 9 }, 10 "last_name": { 11 "type": "text", 12 "copy_to": "full_name" 13 } 14 } 15 } 16 } 17 } 18 19 PUT my_index/_doc/1 20 { 21 "first_name": "John", 22 "last_name": "Smith" 23 } 24 25 GET my_index/_search 26 { 27 "query": { 28 "match": { 29 "full_name": { 30 "query": "John Smith", 31 "operator": "and" 32 } 33 } 34 } 35 }
查询结果为
1 "_source": { 2 "first_name": "John", 3 "last_name": "Smith" 4 }
- doc_values 一般与keyword结合使用,默认为true,即查询可通过该字段进行排序和聚合查询,但是设置为fasle则不可通过该字段排序和聚合查询,但是相应会比较节省内存空间。
- dynamic 默认为true,es默认可以动态新增字段,改为false则不可动态插入不存在的字段名,该参数与properties参数同级
1 PUT my_index 2 { 3 "mappings": { 4 "_doc": { 5 "dynamic": false, 6 "properties": { 7 "user":{ 8 "type":"text" 9 } 10 } 11 } 12 }
- enabled 默认为true,设置为false则该字段不可被索引
- eager_global_ordinals 设置为true可以提高查询速度,但会降低更新速度,只可用在keyword类型使用,text类型只能在设置fileddata参数为ture时使用
- fileddata 与text配合使用,默认text类型不可支持排序和聚合查询,设置fileddata后即可,实现效果与新增keyword类型fields一致。
1 PUT my_index 2 { 3 "mappings": { 4 "_doc": { 5 "properties": { 6 "my_field": { 7 "type": "text", 8 "fields": { 9 "keyword": { 10 "type": "keyword" 11 } 12 } 13 } 14 } 15 } 16 } 17 }
- format 一般与日期类型一起使用,格式化日期
1 PUT my_index 2 { 3 "mappings": { 4 "_doc": { 5 "properties": { 6 "date": { 7 "type": "date", 8 "format": "yyyy-MM-dd" 9 } 10 } 11 } 12 } 13 }
- ignore_malformed 忽略异常插入的数据,默认为false,即插入异常数据会报错,比如integer类型插入非数字字符串,则会报错,设置为true则允许错误数据,可以插入。
1 PUT my_index 2 { 3 "mappings": { 4 "_doc": { 5 "properties": { 6 "number_one": { 7 "type": "integer", 8 "ignore_malformed": true 9 }, 10 "number_two": { 11 "type": "integer" 12 } 13 } 14 } 15 } 16 }
- ignore_above 限制字段字符串长度,只能和keyword类型一起用,插入的数据超过限制数的数据不报错,但是不会被存储,搜索不到
1 PUT my_index 2 { 3 "mappings": { 4 "_doc": { 5 "properties": { 6 "message": { 7 "type": "keyword", 8 "ignore_above": 20 9 } 10 } 11 } 12 } 13 }
- index 默认为true,设置为false则该字段不会存索引,即不可被搜索到。
- fields 为一个字段设定一个子字段,一般是由于当前字段数据类型不满足某种查询时使用,比如text类型的字段,想要排序就需要设置keyword类型的fields 通过此field进行排序,如下
1 PUT my_index 2 { 3 "mappings": { 4 "_doc": { 5 "properties": { 6 "city": { 7 "type": "text", 8 "fields": { 9 "raw": { 10 "type": "keyword" 11 } 12 } 13 } 14 } 15 } 16 } 17 } 18 19 PUT my_index/_doc/1 20 { 21 "city": "New York" 22 } 23 24 GET my_index/_search 25 { 26 "query": { 27 "match": { 28 "city": "york" 29 } 30 }, 31 "sort": { 32 "city.raw": "asc" 33 }, 34 "aggs": { 35 "Cities": { 36 "terms": { 37 "field": "city.raw" 38 } 39 } 40 } 41 }
- norms 规范化,默认为ture,如果该字段不参与计分,则可以设置为false以节省硬盘空间
- null_value 指定该字段为null或空数组时对应的索引值,默认null不可被索引,使用方式如下
1 PUT my_index 2 { 3 "mappings": { 4 "_doc": { 5 "properties": { 6 "status_code": { 7 "type": "keyword", 8 "null_value": "NULL" 9 } 10 } 11 } 12 } 13 } 14 15 PUT my_index/_doc/1 16 { 17 "status_code": null 18 } 19 20 PUT my_index/_doc/2 21 { 22 "status_code": [] 23 } 24 25 GET my_index/_search 26 { 27 "query": { 28 "term": { 29 "status_code": "NULL" 30 } 31 } 32 }
- position_increment_gap 词间差距,当一个字段有多个值,默认该值为100,如下,因为Abraham 和 Lincoln分别在数组的两个词中,因此他们的伪差距为100,所以搜索不出来
1 PUT my_index/_doc/1 2 { 3 "names": [ "John Abraham", "Lincoln Smith"] 4 } 5 6 GET my_index/_search 7 { 8 "query": { 9 "match_phrase": { 10 "names": { 11 "query": "Abraham Lincoln" 12 } 13 } 14 } 15 }
通过此方式修改该字段伪差距
1 PUT my_index 2 { 3 "mappings": { 4 "_doc": { 5 "properties": { 6 "names": { 7 "type": "text", 8 "position_increment_gap": 0 9 } 10 } 11 } 12 } 13 }
修改为0后,便可跨词搜索出来,但是必须和Abraham 和 Lincoln一样是相邻的词
- properties 配置type字段或子字段,自字段可如下配置嵌套类型和object
1 PUT my_index 2 { 3 "mappings": { 4 "_doc": { 5 "properties": { 6 "manager": { 7 "properties": { 8 "age": { "type": "integer" }, 9 "name": { "type": "text" } 10 } 11 }, 12 "employees": { 13 "type": "nested", 14 "properties": { 15 "age": { "type": "integer" }, 16 "name": { "type": "text" } 17 } 18 } 19 } 20 } 21 } 22 }
- search_analyzer 配置字段查询分析器,与analyzer一样,但只用在搜索时,同时存在则搜索以这个为主(注 es新增数据索引和搜索都是通过指定分析器进行分词)
- similarity 配置字段相关度计算算法,默认是BM25,还可配置classic(TF/IDF算法),boolean(不计算相关度,只看查询内容是否完全匹配)
- store 是否存储源数据,默认是true,即会存储源数据,设置为false则该字段不保存数据,一般用于映射
- term_vector 词条向量,默认为false 参数有以下几个
- no 默认,不存储词条向量
- yes 分词对应的字段会被存储
- with_positions 分词和每个词的位置会被存储
- with_offsets 分词和分词的起始和结束字符偏移量会被存储
- with_positions_offsets 分词 位置和向量会被存储
Meta FIelds 元字段
- _all _all字段会将其他字段的值连接成一个大字符串,使用空格分隔,然后进行分析和索引,但是不进行存储,即可通过_all字段进行搜索所有字段服务的值,但查不到该字段的值,设置为true,则可通过查询_all查询所有。该字段中的值都会被当做字符串处理,比如日期类型2018-09-10会被当做字符串分割为2018 09 10 三个词。需要注意的是6.0以上版本该字段已经弃用,如果需要实现类似效果,可在需要用的字段配置move_to参数。例子如下
1 GET my_index/_search 2 { 3 "query": { 4 "match": { 5 "_all": "Tom Terry" 6 } 7 } 8 }
- _field_names 索引包含除null之外的任何值的文档中每个字段的名称,可通过指定字段名查询,使用如下,已禁用。
1 GET my_index/_search 2 { 3 "query": { 4 "terms": { 5 "_field_names": [ "name" ] 6 } 7 } 8 }
- _ignored 6.4版本后新增的元字段,通过此字段可查询到之前被忽略的异常而插入的数据信息(注:字段通过配置ignore_malformed参数,可忽略异常插入错误数据,比如将非数字的字符串插入数字类型的字段)
GET _search { "query": { "exists": { "field": "_ignored" } } }
- _id 通过该字段可以做id查询 如下查询id为1 2的数据
1 GET my_index/_search 2 { 3 "query": { 4 "terms": { 5 "_id": [ "1", "2" ] 6 } 7 } 8 }
- _index 通过此元字段可以进行index层面的排序、聚合、查询等。如下例子,指定查询index,通过index聚合查询。
GET index_1,index_2/_search { "query": { "terms": { "_index": ["index_1", "index_2"] } }, "aggs": { "indices": { "terms": { "field": "_index", "size": 10 } } } }
- _meta 用于存储一些特定信息 如下存储类信息。
1 PUT my_index 2 { 3 "mappings": { 4 "user": { 5 "_meta": { 6 "class": "MyApp::User", 7 "version": { 8 "min": "1.0", 9 "max": "1.3" 10 } 11 } 12 } 13 } 14 }
- _routing 指定分片路由字段,分片计算方式如下,默认的_routing 是_id,即通过id值计算路由
shard_num = hash(_routing) % num_primary_shards
通过以下方式可以指定路由键为user1,插入数据
1 PUT my_index/_doc/1?routing=user1&refresh=true 2 { 3 "title": "This is a document" 4 } 5 6 GET my_index/_doc/1?routing=user1
通过以下方式请求可以指定只在user1和user2路由键相关联的分片上查找
1 GET my_index/_search?routing=user1,user2 2 { 3 "query": { 4 "match": { 5 "title": "document" 6 } 7 } 8 }
如果带路由键插入,但是不带路由键查询,会根据id计算分片,查找不到可能会导致遍历所有分片,因此可以在创建mapping时指定必须带路由键操作。
1 PUT my_index2 2 { 3 "mappings": { 4 "_doc": { 5 "_routing": { 6 "required": true 7 } 8 } 9 } 10 }
- _source 源数据,如果enabled设置为false则不存储数据,配置如下
1 PUT tweets 2 { 3 "mappings": { 4 "_doc": { 5 "_source": { 6 "enabled": false 7 } 8 } 9 } 10 }
一般用于配置某些字段不存储数据 配置如下,includes表示存储数据的字段,excludes标识不存储数据的字段。
PUT logs
{
"mappings": {
"_doc": {
"_source": {
"includes": [
"*.count",
"meta.*"
],
"excludes": [
"meta.description",
"meta.other.*"
]
}
}
}
}
PUT logs/_doc/1
{
"requests": {
"count": 10,
"foo": "bar"
},
"meta": {
"name": "Some metric",
"description": "Some metric description",
"other": {
"foo": "one",
"baz": "two"
}
}
}
- _type 与_id类似,可通过type类型进行查询,排序,聚合等。6.0版本后废弃
- _uid 唯一id,即在同个index中,uid可以唯一标识任意type中的数据。用法与_type _id类似。6.0版本后废弃
1 GET my_index/_search 2 { 3 "query": { 4 "terms": { 5 "_uid": [ "_doc#1", "_doc#2" ] 6 } 7 }
原文:https://blog.csdn.net/asd804171023/article/details/82837675
感谢您的阅读,您的支持是我写博客动力。

浙公网安备 33010602011771号