elasticsearch 权威指南Mapping(映射)
什么是映射
类似于数据库中的表结构定义,主要作用如下:
- 定义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(映射)的更多相关文章
- 初识Elastic search—附《Elasticsearch权威指南—官方guide的译文》
本文作为Elastic search系列的开篇之作,简要介绍其简要历史.安装及基本概念和核心模块. 简史 Elastic search基于Lucene(信息检索引擎,ES里一个index—索引,一个索 ...
- Elasticsearch权威指南(中文版)
Elasticsearch权威指南(中文版) 下载地址: https://pan.baidu.com/s/1bUGJmwS2Gp0B32xUyXxCIw 扫码下面二维码关注公众号回复100010 获取 ...
- Elasticsearch 权威指南
Elasticsearch 权威指南 http://fuxiaopang.gitbooks.io/learnelasticsearch/content/index.html
- elasticsearch中的mapping映射配置与查询典型案例
elasticsearch中的mapping映射配置与查询典型案例 elasticsearch中的mapping映射配置示例比如要搭建个中文新闻信息的搜索引擎,新闻有"标题".&q ...
- Elasticsearch 权威指南 NESTAPI地址
Elasticsearch 权威指南:http://fuxiaopang.gitbooks.io/learnelasticsearch/content/index.html NEST:http://n ...
- elasticsearch权威指南
elasticsearch权威指南 https://elasticsearch.cn/book/elasticsearch_definitive_guide_2.x/
- 第三百六十四节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的mapping映射管理
第三百六十四节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的mapping映射管理 1.映射(mapping)介绍 映射:创建索引的时候,可以预先定义字 ...
- Elasticsearch: 权威指南(官方教程)
<Elasticsearch 权威指南>中文版 序言 前言 基础入门 深入搜索 处理人类语言 聚合 地理位置 数据建模 管理.监控和部署
- ElasticSearch权威指南学习(映射和分析)
概念 映射(mapping)机制用于进行字段类型确认,将每个字段匹配为一种确定的数据类型(string, number, booleans, date等).+ 分析(analysis)机制用于进行全文 ...
随机推荐
- hibernate基础简单入门1---helloword
1:目录结果 2:实体类(student.java) package com.www.entity; public class Student { private int id; private St ...
- Antenna Placement(二分图的最大匹配)
http://poj.org/problem?id=3020 题意: 一个矩形中,有N个城市'*',现在这n个城市都要覆盖无线,若放置一个基站,它至多可以覆盖相邻的两个城市.问至少放置多少个基站才能使 ...
- css的一些命名规范
网页制作中规范使用DIV+CSS命名规则,可以改善优化功效特别是团队合作时候可以提供合作制作效率,具体DIV CSS命名规则CSS命名大全内容篇. 常用DIV+CSS命名大全集合,即CSS命名规则 D ...
- codevs3370 选学霸(背包dp,并查集)
3372 选学霸 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 老师想从N名学生中选M人当学霸,但有K对人实力相 ...
- P2533 [AHOI2012]信号塔
传送门 据说是一个叫做随机增量法的东西 枚举\(i\),如果不在圆中将它设为圆心 枚举\(j\),如果不在圆中将\((i,j)\)成为新的圆的直径 枚举\(k\),如果不在圆中让\(i,j,k\)组成 ...
- [Swift通天遁地]二、表格表单-(13)实时调整表单元素的显示和隐藏
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- BZOJ 3798 分块打表
思路: 这题思路真是奇妙 先跑个暴力 每隔1e5打个表 块内暴力 打表程序: (开O3 15秒就跑完了) //By SiriusRen #include <bits/stdc++.h> u ...
- Android内存管理(6)onTrimMemory,onLowMemory,MemoryInfo()
转自: http://www.cnblogs.com/sudawei/p/3527145.html 参考: Android Application生命周期学习 Android中如何查看内存(上) An ...
- 设置myeclipse的JSP、HTML的页面编码格式
JSP编码格式: 点击菜单上的window--->preferences 在弹出的对话框中点击MyEclise--->Files and Editors--->JSP, 在Encod ...
- Spark SQL入门案例之人力资源系统数据处理
通过该案例,给出一个比较完整的.复杂的数据处理案例,同时给出案例的详细解析. 人力资源系统的管理内容组织结构图 1) 人力资源系统的数据库与表的构建. 2) 人力资源系统的数据的加载. 3) 人力资源 ...