映射(Mapping)相当于数据表的表结构。ElasticSearch中的映射(Mapping)用来定义一个文档,可以定义所包含的字段以及字段的类型、分词器及属性等等。
映射可以分为动态映射和静态映射。
# 静态映射:若映射(mapping)已经创建,写入数据时,elasticsearch会根据映射和写入数据的key进行对比,然后写入相应的映射中;
# 动态映射:如果mapping没有创建,elasticsearch将会根据写入的数据的key自行创建相应的mapping,并写入数据。
PUT /myindex/article/1
{
"post_date": "2018-05-10",
"title": "Java",
"content": "java is the best language",
"author_id": 119
}
# 可以使用_mapping查询索引的mapping
GET /myindex/article/_mapping
# 返回结果
{
"myindex": {
"mapping": {
"article": {
"properties": {
"author_id": {
"type": "long"
},
"content": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"post_date": {
"type": "date"
},
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
# 手动创建mapping
PUT /lib6
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 0
},
"mappings": {
"books": {
"properties": {
"title": {"type": "text"},
"name": {"type": "text", "ayalyzer": "standard"},
"publish_date": {"type": "date", "index": false},
"price": {"type": "double"},
"number": {"type": "integer"}
}
}
}
}
由上可见elasticsearch时支持数据类型的。
字符串:string,string类型包含 text 和 keyword。
数指型:long、integer、short、byte、double、float
日期型:date
布尔型:boolean
二进制型:binary
PUT /myindex/article/2
{
"post_date": "2018-05-12",
"title": "html",
"content": "I like html",
"author_id": 120
}
PUT /myindex/article/3
{
"post_date": "2018-05-16",
"title": "es",
"content": "Es is distributed document store",
"author_id": 110
}
# 查不出数据
GET /myindex/article/_search?q=post_date:2018
# 返回结果
{
"took": 198,
"time_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
# 查出数据
GET /myindex/article/_search?q=post_date:2018-05-10
# 返回结果
{
"took": 84,
"time_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "myindex",
"_type": "article",
"_id": "1",
"_score": 1,
"_source": {
"post_date": "2018-05-10",
"title": "Java",
"content": "java is the best language",
"author_id": 119
}
}
]
}
}
# 查出数据
GET /myindex/article/_search?q=content:html
# 返回结果
{
"took": 45,
"time_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "myindex",
"_type": "article",
"_id": "2",
"_score": 0.2876821,
"_source": {
"post_date": "2018-05-12",
"title": "html",
"content": "I like html",
"author_id": 120
}
}
]
}
}
除字符串类型以外,其他类型必须要进行精确查询,因为除字符串外其他类型不会进行分词。
数组类型(Array datatype),数组类型不需要专门指定数组元素的type,例如:
字符型数组:[“one”,“two”]
整型数组*:[1, 2]
数组型数组:[1, [2, 3]] 等价于 [1, 2, 3]
对象数组:[{“name”: “Mary”, “age”: 12}, {“name”: “John”, “age”: 10}]
对象类型(Object datatype):object 用于单个Json对象
# object类型
PUT /lib/person/1
{
"name": "Tom",
"age": 25,
"birthday": "1985-12-12",
"address": {
"country": "china",
"province": "hubei",
"city": "wuhan"
}
}
# 底层存储格式——符合面向对象思想
{
"name": ["Tom"],
"age": [25],
"birthday": ["1985-12-12"],
"address.country": ["china"],
"address.province": ["hubei"],
"address.city": ["wuhan"]
}
嵌套类型(Nested datatype):nested 用于Json数组
{
"persons": [
{"name": "lisi", "age": 27},
{"name": "wangwu", "age": 26},
{"name": "zhangsan", "age": 23}
]
}
# 底层存储
{
"persons": ["lisi", "wangwu", "zhangsan"],
"persons": [27, 26, 23]
}
地理坐标类型(Geo-point datatype):geo_point 用于经纬度坐标
地理形状类型(Geo-Shape datatype):geo_shape 用于类似于多边形的复杂形状
IPv4 类型(IPv4 datatype):ip 用于IPv4 地址
Completion 类型(Completion datatype):completion 提供自动补全建议
Token count 类型(Token count datatype):token_count 用于统计做子标记的字段的index数目,该值会一直增加,不会因为过滤条件而减少
mapper-murmur3 类型:通过插件,可以通过_murmur3_来计算index的哈希值
附加类型(Attachment datatype):采用mapper-attachments插件,可支持_attachments_索引,例如 Microsoft office 格式,Open Documnet 格式, ePub,HTML等
"enabled":true (缺省)| false
"index": true(缺省)| false
"index_options": "docs"
"norms": {"enable": true, "loading": "lazy"}
"doc_value": true(缺省)| false
"fielddata": {"format": "disabled"}
"store": false(默认)| true
"coerce: true(缺省)| false"
multifields:灵活使用多字段解决多样的业务需求
dynamic:控制mapping的自动更新
"dynamic": true(缺省)| false | strict
data_detection:true(缺省)| false
dynamic和data_detection的详解:Elasticsearch dynamic mapping(动态映射) 策略
"analyzer": "ik"
"boost": 1.23
"fields": {"raw": {"type": "string", "index": "not_analyzed"}}
"ignore_above": 100
"include_in_all": true
"null_value": "NULL"
"position_increament_gap": 0
"search_analyzer": "ik"
"similarity": "BM25"
"trem_vector": "no"