• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
孙龙 程序员
少时总觉为人易,华年方知立业难
博客园    首页    新随笔    联系   管理    订阅  订阅
Elasticsearch 篇之Mapping 设置

1,mapping简介

 

 

2,自定义 mapping

 

 

 

 

 

 

 

3,mapping演示

PUT my_index
{
  "mappings": {
    "doc": {
      "properties": {
        "title": {
          "type": "text"
        },
        "name": {
          "type": "keyword"
        },
        "age": {
          "type": "integer"
        }
      }
    }
  }
}

 

GET my_index/_mapping

{
  "my_index": {
    "mappings": {
      "doc": {
        "properties": {
          "age": {
            "type": "integer"
          },
          "name": {
            "type": "keyword"
          },
          "title": {
            "type": "text"
          }
        }
      }
    }
  }
}

 

PUT my_index/doc/1
{
  "name":"alfred a ",
  "age":9
}
----------------------------
{
  "_index": "my_index",
  "_type": "doc",
  "_id": "1",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 1,
  "_primary_term": 1
}

 

GET my_index/_search
{
  "query":{
    "match": {
      "name": "alfred"
    }
  }
}
-----------------------------------

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "my_index",
        "_type": "doc",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "name": "alfred",
          "age": 9
        }
      }
    ]
  }
}

 

DELETE my_index

  

4,自定义Mapping 之copy_to参数说明

 

 

 

5,自定义Mapping之index参数说明

 

 

 

6,自定义Mapping之index_options参数说明

 

 

 

 

 

 

 

7,mapping文档说明

 -------------------------------

8,数据类型

核心数据类型

字符串型:text、keyword(不会分词)
数值型:long、integer、short、byte、double、float、half_float等
日期类型:date
布尔类型:boolean
二进制类型:binary

 

属性名字 说明
text

用于全文索引,该类型的字段将通过分词器进行分词,最终用于构建索引

keyword 不分词
long 有符号64-bit integer:-2^63 ~ 2^63 - 1
integer 有符号32-bit integer,-2^31 ~ 2^31 - 1
short 有符号16-bit integer,-32768 ~ 32767
byte  有符号8-bit integer,-128 ~ 127
double 64-bit IEEE 754 浮点数
float 32-bit IEEE 754 浮点数
half_float 16-bit IEEE 754 浮点数
boolean true,false
date https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html
binary

该类型的字段把值当做经过 base64 编码的字符串,默认不存储,且不可搜索


范围类型:integer_range、float_range、long_range、double_range、date_range

范围类型例子:

标识一个数据范围而不是一个值  如age:10~20   搜索{"gle":5,"lte":20} 则可以搜索出来数据

支持的数据类型 说明

integer_range

 

float_range

 

long_range

 

double_range

 

date_range

64-bit 无符号整数,时间戳(单位:毫秒)

ip_range

IPV4 或 IPV6 格式的字符串

可选参数:

relation这只匹配模式

INTERSECTS 默认的匹配模式,只要搜索值与字段值有交集即可匹配到

WITHIN 字段值需要完全包含在搜索值之内,也就是字段值是搜索值的子集才搜索出来

CONTAINS 与WITHIN相反,只搜索字段值包含搜索值的文档

例子:

1.添加index

put:127.0.0.1:9200/range_test

{
  "mappings": {
    "_doc": {
      "properties": {
        "count": {
          "type": "integer_range"
        },
        "create_date": {
          "type": "date_range", 
          "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
        }
      }
    }
  }
}

2.添加测试数据

post:127.0.0.1:9200/range_test/_doc/1

{
  "count" : { 
    "gte" : 1,
    "lte" : 100
  },
  "create_date" : { 
    "gte" : "2019-02-1 12:00:00", 
    "lte" : "2019-03-30"
  }
}

3.测试搜索

get:127.0.0.1:9200/range_test/_doc/_search

{
    "query":{
        "term":{
            "count":5
        }
    }
}

5在1~100之间可以搜索出来

{
  "query" : {
    "range" : {
      "create_date" : { 
        "gte" : "2019-02-01",
        "lte" : "2019-03-30",
        "relation" : "within" 
      }
    }
  }
}

 

复杂数据类型

数组类型:array 支持字符串 数值 object对象数组   数组元素必须为相同数据类型
对象类型:object

{
    "name": "小明",
    "user_info": {
        "student_id": 111,
        "class_info": {
            "class_name": "1年级"
        }
    }
}

被索引形式

{
 "name":"小明",
"user_info.student_id":"111",
"user_info.student_info.class_name":"111"
}

 


嵌套类型:nested object

能够支持数组元素单独的做索引

查询api:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html

聚合api:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-reverse-nested-aggregation.html

排序api:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-reverse-nested-aggregation.html

检索和高亮:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html#nested-inner-hits

 

Nested和Object区别

 put:127.0.0.1:9200/object_test/_doc/1 默认是object类型

{
    "user_name":"小明",
    "subjects":[
        {"subject_name":"地理","id":1},
        {"subject_name":"英语","id":2}
    ]
}

搜索名字为英语id为1的

{
    "query":{
        "bool":{
        "must":[
            {"match":{"subjects.subject_name":"英语"}},
                {"match":{"subjects.id":"1"}}
            ]
            }
    }
}

正常搜索不出来  测试时搜索出来了

因为索引为以下格式

{
 "name":"小明",
"subjects.subject_name":["英语","地理"],
"subjects.subject_id":["1","2"]
}

改为Nested 就不会

 


地理位置数据类型:geo_point、geo_shape

geo_point

几种格式

object对象:"location": {"lat": 41.12, "lon": -71.34}

字符串:"location": "41.12,-71.34"

geohash:"location": "drm3btev3e86"

数组:"location": [ -71.34, 41.12 ]

geo_shape

查询api:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-bounding-box-query.html


专用类型:ip(记录ip地址)、completion(实现自动补全)、token_count(记录分词数)、murmur3(记录字符串hash值)

 

多字段特性


多字段特性(multi-fields),表示允许对同一字段采用不同的配置,比如分词。

常见例子是对人名实现拼音搜索,只需要在人名中新增一个字段pinyin即可。但是这种方式不是十分优雅,multi-fields可以在不改变整体结构的前提下,增加一个子字段:
 

 

 

 

 

9,dynamic-mapping简介

 

 

 

 

10,dynamic日期与数字识别

 

日期的自动识别可以自行配置日期的格式,默认情况下是:

["strict_date_opeional_time", "yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z"]

strict_date_opeional_time 是ISO 标准的日期格式,完整的格式如下:

YYYY-MM-DDhh:mm:ssTZD(eg:1997-07-16y19:20:30+01:00)


dynamic_date_formats:可以自定义日期类型
date_detection:可以关闭日期自动识别机制(默认开启)

 

 

数字自动识别

字符串为数字的时候,默认不会自动识别为整型,因为字符串中出现数字是完全合理的。

numeric_detection 可以开启字符串中数字的自动识别。

 

 

 

11,dynamic-template简介

 

Dynamic Templates 意为 动态模板,它的作用是允许根据es自动识别的数据类型、字段名等来动态设定字段类型。

可以实现的效果如下:

  1. 所有字符串类型都设置为keyword类型,即默认不分词
  2. 所有以message开头的字段都设置为text类型,即分词
  3. 所有以long_开头的字段都设置为long类型
  4. 所有自动匹配为double类型的都设定为float类型,以节省空间

 

 

匹配规则参数

  • match_mapping_type:匹配es自动识别的字段类型,如boolean、long等
  • match、unmatch:匹配字段名
  • path_match、path_unmatch:匹配路径

 

 

 

举例

字段类型匹配

首先PUT一个文档,然后查看mapping:

PUT test_index/doc/1
{
  "name": "Tom"
}

GET test_index/_mapping

 

 可以看到在默认情况下,字符串被识别成为text类型,并且有一个子字段keyword。

现在设置动态模板,要求匹配到string类型的字段设置为keyword:

PUT test_index
{
  "mappings": {
    "doc": {
      "dynamic_templates": [
        {
          "strings_as_keywords": {
            "match_mapping_type": "string",
            "mapping": {
              "type": "keyword"
            }
          }
        }
      ] 
    }
  }

  

重新创建文档并查看mapping:

 

 name字段的类型变成了 keyword类型

字段匹配

现在想将以message开头的字段且为string的匹配称为text类型,其余为keyword:

PUT test_index
{
  "mappings": {
    "doc": {
      "dynamic_templates": [
        {
          "message_as_text": {
            "match_mapping_type": "string",
            "match": "message",
            "mapping": {
              "type": "text"
            }
          }
        },
        {
          "strings_as_keywords": {
            "match_mapping_type": "string",
            "mapping": {
              "type": "keyword"
            }
          }
        }
      ] 
    }
  }
}

 

Dynamic Templates 的匹配顺序是从上到下执行的,匹配到一个后后面的规则就会跳过

然后创建一个文档并查看mapping:

PUT test_index/doc/1
{
  "name": "john",
  "message": "good boy"
}

GET test_index/_mapping

 

 可以看到message被设置为了text类型,name还是keyword

 

double设定为float

 

 

12,自定义mapping的建议

 

一般步骤
自定义mapping 的步骤:

写一条文档到es的临时索引中,获取es自动生成的mapping
修改第一步得到的mapping,自定义相关配置
使用第2步的mapping创建实际的索引
实际举例
假设我得到了需要存入es的文档,首先将文档写入临时的index中:

实际举例

假设我得到了需要存入es的文档,首先将文档写入临时的index中:

PUT test_index/doc/1
{
  "referre": "-",
  "response_code": "200",
  "remote_ip": "172.0.0.1",
  "method": "POST",
  "username": "-",
  "http_version": "1.1",
  "body_sent": {
    "bytes": "0"
  },
  "url": "/helloworld"
}

然后查看es自动生成的mapping:

GET test_index/_mapping

现在希望将bytes设置为整型,url设置为text类型,其他都使用keyword(将上一步的输出复制过来就好):

PUT product_index
{
    "mappings": {
      "doc": {
        "properties": {
          "body_sent": {
            "properties": {
              "bytes": {
                "type": "long"
              }
            }
          },
          "http_version": {
            "type": "keyword"
          },
          "method": {
            "type": "keyword"
          },
          "referre": {
            "type": "keyword"
          },
          "remote_ip": {
            "type": "keyword"
          },
          "response_code": {
            "type": "keyword"
          },
          "url": {
            "type": "text"
          },
          "username": {
            "type": "keyword"
          }
        }
      }
    }
  }

这样直接将测试index的mapping复制过来进行修改,不会遗漏字段,修改完成设置一个index的名称就行了。

 然后就可以向实际的索引中写入文档了:

PUT product_index/doc/1
{
  "referre": "-",
  "response_code": "200",
  "remote_ip": "172.0.0.1",
  "method": "POST",
  "username": "-",
  "http_version": "1.1",
  "body_sent": {
    "bytes": "0"
  },
  "url": "/helloworld"
}

然后查看一下实际索引的mapping:

GET product_index/_mapping

 

13,索引模板

 

什么是索引模板

索引模板,index template,主要用于在新建索引时自动应用预先设置的配置,简化索引创建的步骤。

模板中可以设定索引的配置以及mapping,可以有多个模板,根据order设置,order大的覆盖小的范围。

 

 模板加载顺序根据 order 从小到大加载,后面的大order的模板的配置将会覆盖小 order配置。

获取与删除的API 如下:

 

 

举例

这里设置了两个索引模板:

PUT _template/test_template
{
  "index_patterns": ["te*", "bar*"],
  "order": 0,
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "doc": {
      "_source": {
        "enabled": false
      },
      "properties": {
        "name": {
          "type": "keyword"
        }
      }
    }
  }
}

PUT _template/test_template2
{
  "index_patterns": ["test*"],
  "order": 1,
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "doc": {
      "_source": {
        "enabled": true
      }
    }
  }
}

  然后先创建一个foo_index,并获取一下它的mapping:

PUT foo_index
GET foo_index/_mapping

 

 因为这个index没有被任何一个模板匹配到,所以它的mapping是空的

再创建一个bar_index,并获取一下它的mapping:

PUT bar_index
GET bar_index/_mapping

 

 

这个索引匹配到了test_template

再创建一个test_index,并获取一下它的index配置:

PUT test_index
GET test_index/

 

 这个索引匹配到了test_template2模板,这使得索引的”_source”: {“enabled”: true}

 

本文来自博客园,作者:孙龙-程序员,转载请注明原文链接:https://www.cnblogs.com/sunlong88/p/12830588.html

posted on 2020-05-05 14:14  孙龙-程序员  阅读(4001)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3