什么是映射

类似于数据库中的表结构定义,主要作用如下:

  • 定义Index下字段名(Field Name)
  • 定义字段的类型,比如数值型,字符串型、布尔型等
  • 定义倒排索引的相关配置,比如是否索引、记录postion等

需要注意的是,在索引中定义太多字段可能会导致索引膨胀,出现内存不足和难以恢复的情况,下面有几个设置:

  • index.mapping.total_fields.limit:一个索引中能定义的字段的最大数量,默认是 1000
  • index.mapping.depth.limit:字段的最大深度,以内部对象的数量来计算,默认是20
  • index.mapping.nested_fields.limit:索引中嵌套字段的最大数量,默认是50

Mapping的数据类型

基本数据类型

属性名字 说明
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 编码的字符串,默认不存储,且不可搜索

Mapping范围数据类型

标识一个数据范围而不是一个值  如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"
}
}
}
}

Mapping复杂数据类型

数组类型 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

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

查询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

几种格式

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
  • 记录字符串hash值 murmur3
  • Percolator

Mapping设置

一个完整的mapping设置

  {
"settings": {
"analysis": {
"analyzer": {
"ik_pinyin_analyzer": {
"type": "custom",
"tokenizer": "ik_smart",
"filter": ["my_pinyin"]#自定义filter
},
"pinyin_analyzer": {
"tokenizer": "shopmall_pinyin"
},
"first_py_letter_analyzer": {
"tokenizer": "first_py_letter"
},
"full_pinyin_letter_analyzer": {
"tokenizer": "full_pinyin_letter"
},
"onlyOne_analyzer": {
"tokenizer": "onlyOne_pinyin"
}
},
"tokenizer": {#自定义分词器
"onlyOne_pinyin": {
"type":"pinyin",
"keep_separate_first_letter": "false",
"keep_first_letter":"false"
},
"shopmall_pinyin": {
"keep_joined_full_pinyin": "true",
"keep_first_letter": "true",
"keep_separate_first_letter": "false",
"lowercase": "true",
"type": "pinyin",
"limit_first_letter_length": "16",
"keep_original": "true",
"keep_full_pinyin": "true",
"keep_none_chinese_in_joined_full_pinyin": "true"
},
"first_py_letter": {
"type": "pinyin",
"keep_first_letter": true,
"keep_full_pinyin": false,
"keep_original": false,
"limit_first_letter_length": 16,
"lowercase": true,
"trim_whitespace": true,
"keep_none_chinese_in_first_letter": false,
"none_chinese_pinyin_tokenize": false,
"keep_none_chinese": true,
"keep_none_chinese_in_joined_full_pinyin": true
},
"full_pinyin_letter": {
"type": "pinyin",
"keep_separate_first_letter": false,
"keep_full_pinyin": false,
"keep_original": false,
"limit_first_letter_length": 16,
"lowercase": true,
"keep_first_letter": false,
"keep_none_chinese_in_first_letter": false,
"none_chinese_pinyin_tokenize": false,
"keep_none_chinese": true,
"keep_joined_full_pinyin": true,
"keep_none_chinese_in_joined_full_pinyin": true
}
},
"filter": {
"my_pinyin": {
"type": "pinyin",
"keep_joined_full_pinyin": true,
"keep_separate_first_letter":true
}
}
} },
"mappings": {
"doc": {#type名字
"properties": {#mapping的属性
"productName": {属性名字
"type": "text",#属性类型
"analyzer": "ik_pinyin_analyzer",#分词器
"fields": {#fields 指定自定义分词器 查询时通过productName.keyword_once_pinyin 可以指定
"keyword_once_pinyin": {
"type": "text",
"analyzer": "onlyOne_analyzer"#指定的自定义分词器
}
}
},
"skuNames": {
"type": "text",
"analyzer": "ik_pinyin_analyzer",
"fields": {
"keyword_once_pinyin": {
"type": "text",
"analyzer": "onlyOne_analyzer"
}
}
},
"regionCode": {
"type": "keyword"
},
"productNameSuggester": {#es6.x搜索建议实现
"type": "completion",
"fields": {
"pinyin": {
"type": "completion",
"analyzer": "pinyin_analyzer"
},
"keyword_pinyin": {
"type": "completion",
"analyzer": "full_pinyin_letter_analyzer"
},
"keyword_first_py": {
"type": "completion",
"analyzer": "first_py_letter_analyzer"
}
}
}
"info": {#es6父子类型设置
"type": "join",
"relations": {
"md_product":[ "sl_customer_character_order_list","ic_product_store_account","sl_customer_product_setting"]
}
}
}
}
}
}

创建mapping

put:http://127.0.0.1:9200/db

{
"mappings": {
"product": {//type
"properties": {
"productName": {//字段
"type": "text"//数据类型
}
}
}
}
}

mapping参数

参数 说明
analyzer 分词器 默认:standard
boost 字段权重默认1 在通过_all字段查询 根据此字段来权重
dynamic 控制字段新增 true(默认 允许新增) false  strict 不能新增文档
index 控制字段是否索引(可搜索) true 是 false否

参考:https://www.jianshu.com/p/e8a9feea683c

查看当前索引的映射

http://127.0.0.1:9200/blogs2/product/_mapping

{
"blogs2": {
"mappings": {
"product": {
"properties": {
"price": {
"type": "long"
},
"productName": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"remark": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"tags": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}

自定义映射

作用定义数据类型 比如数字映射成text 大于小于范围搜索就会无效 还有明确哪些fullText需要分词哪些不需要分词

确切值(Exact values)和全文本(FullText)

es支持很多种数据类型但是主要分为2大类
确切值就是能够确定的值 比如id 日期  通过=就能查询到我们想要的数据

而全文本是需要进行相似度匹配 返回最佳匹配

elasticsearch 权威指南Mapping(映射)的更多相关文章

  1. 初识Elastic search—附《Elasticsearch权威指南—官方guide的译文》

    本文作为Elastic search系列的开篇之作,简要介绍其简要历史.安装及基本概念和核心模块. 简史 Elastic search基于Lucene(信息检索引擎,ES里一个index—索引,一个索 ...

  2. Elasticsearch权威指南(中文版)

    Elasticsearch权威指南(中文版) 下载地址: https://pan.baidu.com/s/1bUGJmwS2Gp0B32xUyXxCIw 扫码下面二维码关注公众号回复100010 获取 ...

  3. Elasticsearch 权威指南

    Elasticsearch 权威指南 http://fuxiaopang.gitbooks.io/learnelasticsearch/content/index.html

  4. elasticsearch中的mapping映射配置与查询典型案例

    elasticsearch中的mapping映射配置与查询典型案例 elasticsearch中的mapping映射配置示例比如要搭建个中文新闻信息的搜索引擎,新闻有"标题".&q ...

  5. Elasticsearch 权威指南 NESTAPI地址

    Elasticsearch 权威指南:http://fuxiaopang.gitbooks.io/learnelasticsearch/content/index.html NEST:http://n ...

  6. elasticsearch权威指南

    elasticsearch权威指南 https://elasticsearch.cn/book/elasticsearch_definitive_guide_2.x/

  7. 第三百六十四节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的mapping映射管理

    第三百六十四节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的mapping映射管理 1.映射(mapping)介绍 映射:创建索引的时候,可以预先定义字 ...

  8. Elasticsearch: 权威指南(官方教程)

    <Elasticsearch 权威指南>中文版 序言 前言 基础入门 深入搜索 处理人类语言 聚合 地理位置 数据建模 管理.监控和部署

  9. ElasticSearch权威指南学习(映射和分析)

    概念 映射(mapping)机制用于进行字段类型确认,将每个字段匹配为一种确定的数据类型(string, number, booleans, date等).+ 分析(analysis)机制用于进行全文 ...

随机推荐

  1. luoguP2939 [USACO09FEB]改造路Revamping Trails

    约翰一共有N)个牧场.由M条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场1出发到牧场N去给奶牛检查身体. 通过每条小径都需要消耗一定的时间.约翰打算升级其中K条小径,使之成为高 速公路. ...

  2. bzoj4561

    扫描线 想法挺妙 搞了很长很长时间... http://www.cppblog.com/superlong/archive/2010/08/06/122427.html #include<bit ...

  3. Android开发中常用的一些小技巧(转载)

    http://www.jb51.net/article/61135.htm Activity.startActivities() 常用于在应用程序中间启动其他的Activity. TextUtils. ...

  4. pcntl研究

    虽说php用于并发计算有点山寨,但总比没有强把.(有问题请指正) 下面是pcntl多线程的例子.(只能用于cli模式,而且只能用于linux环境) <?php $starttime=microt ...

  5. Ubuntu中安装部署Intel CS WebRTC

    1环境要求 组件 版本要求 OS CentOS* 7.4, Ubuntu 14.04/16.04 LTS Node 8.11.* (推荐8.11.1) MongoDB 2.4.9 Boost 1.65 ...

  6. $P2935 [USACO09JAN]最好的地方Best Spot$

    P2935 [USACO09JAN]最好的地方Best Spot Floyd的水题(黄题) 海星. 这可能是我第一道发的Floyd的博客 inline void Floyd(){ ;k<=n;k ...

  7. POJ 3855 计算几何·多边形重心

    思路: 多边形面积->任选一个点,把多边形拆成三角,叉积一下 三角形重心->(x1+x2+x3)/3,(y1+y2+y3)/3 多边形重心公式题目中有,套一下就好了 计算多边形重心方法: ...

  8. UNIX环境高级编程--7

    进程环境main函数:    C程序总是从main函数开始执行.main函数原型是:    int main(int argc, char *argv[]);    当内核执行C程序时(使用一个exe ...

  9. IIS添加映射配置

    这种问题主要出现在使用应用程序级别的地址重写.如果你将一个动态的地址重写成虚拟的其它扩展名或者不带扩展名的地址,通常在IIS5.1和II6.0中,访问这样一个实际不存在的地址,首先会被Web服务器返回 ...

  10. vue项目中使用百度地图的方法

    1.在百度地图申请密钥: http://lbsyun.baidu.com/  将 <script type="text/javascript" src="http: ...