目录
Mapping映射入门
什么是mapping映射?
映射:创建索引的时候,可以预先定义字段的类型以及相关属性
elasticsearch会根据json源数据的基础类型猜测你想要的字段映射,将输入的数据转换成可搜索的索引项,mapping就是我们自己定义的字段数据类型,同时告诉elasticsearch如何索引数据以及是否可以被搜索
作用:会让索引建立的更加细致和完善
类型:静态映射和动态映射
内置映射类型
类型 | 字段类型 |
string类型 | text 和 keyword两种 text类型:会进行 分词,抽取词干,建立倒排索引 keyword类型:一个普通字符串,只能完全匹配才能搜索到 |
数字类型 | long,integer,short,byte,double,float |
日期类型 | date |
bool(布尔)类型 | boolean |
binary(二进制)类型 | binary |
复杂类型 | object,nested |
geo(地区)类型 | geo-point,geo-shape |
专业类型 | ip,competion |
keyword 使用
创建Mapping
PUT /book_index
{
"mappings": {
"properties": {
"bookName" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
}
},
"analyzer" : "ik_max_word",
"search_analyzer" : "ik_smart"
}
}
}
}
新增数据
POST /book_index_test/_doc/110000
{
"bookName":"三少爷的剑"
}
查询测试
GET /book_index_test/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"bookName.keyword": "三少爷的剑"
}
}
]
}
}
}
# 查询输出
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 8.873095,
"hits" : [
{
"_index" : "book_index_test",
"_type" : "_doc",
"_id" : "110000",
"_score" : 8.873095,
"_source" : {
"bookName" : "三少爷的剑"
}
}
]
}
}
属性介绍
属性 | 描述 | 适合类型 |
store | 值为true表示存储,为false 表示不存储,默认为false | all |
index | true表示分析,false表示不分析,默认为true | string |
null_value | 如果字段为空,可以设置一个默认值,比如:“NA” | all |
analyzer | 可以设置索引和搜索时用的分析器,默认使用的是 standard分析器,还可以使用whitespace, simple ,english | all |
include_in_all | 默认ES为每个文档定义一个特殊域_all, 它的作用是让每个字段被搜索到,如果不想某个字段被搜索到,可以设置false | all |
format | 时间格式字符串的模式 | date |
更多属性:Mapping parameters | Elasticsearch Guide [8.2] | Elastic
store使用
默认情况下,对字段值进行索引以使其可搜索,但不会存储这些值。这意味着可以查询字段,但无法检索原始字段值。
通常这并不重要。字段值已经是默认存储的源字段的一部分。如果只想检索单个字段或几个字段的值,而不是整个源,那么可以通过源筛选来实现。
在某些情况下,存储字段是有意义的。例如,如果您有一个包含标题、日期和非常大的内容字段的文档,您可能希望只检索标题和日期,而不必从大型源字段中提取这些字段,例如:小说章节内容的文档存储。
创建章节Mapping
PUT /chapter_index_test/_mapping
{
"properties": {
"id": {
"type": "integer",
"store": true
},
"position": {
"type": "integer",
"store": true
},
"title": {
"type": "text",
"store": true
},
"content": {
"type": "text"
}
}
}
新增文档
PUT chapter_index_test_v1/_doc/1
{
"id": 1,
"position": 1,
"title": "第一章",
"content": "创建索引的时候,可以预先定义字段的类型以及相关属性elasticsearch会根据json源数据的基础类型猜测你想要的字段映射,将输入的数据转换成可搜索的索引项,mapping就是我们自己定义的字段数据类型,同时告诉elasticsearch如何索引数据以及是否可以被搜索"
}
查询文档
GET my-chapter_index_test_v3/_search?q=title:第一
{
"stored_fields": [ "id", "position","title"]
}
创建索引
#创建索引(设置字段类型)
PUT jobbole #创建索引设置索引名称
{
"mappings": { #设置mappings映射字段类型
"job": { #表名称
"properties": { #设置字段类型
"title":{ #title字段
"type": "text" #text类型,text类型可以分词,建立倒排索引
},
"salary_min":{ #salary_min字段
"type": "integer" #integer数字类型
},
"city":{ #city字段
"type": "keyword" #keyword普通字符串类型
},
"company":{ #company字段,是嵌套字段
"properties":{ #设置嵌套字段类型
"name":{ #name字段
"type":"text" #text类型
},
"company_addr":{ #company_addr字段
"type":"text" #text类型
},
"employee_count":{ #employee_count字段
"type":"integer" #integer数字类型
}
}
},
"publish_date":{ #publish_date字段
"type": "date", #date时间类型
"format":"yyyy-MM-dd" #yyyy-MM-dd格式化时间样式
},
"comments":{ #comments字段
"type": "integer" #integer数字类型
}
}
}
}
}
#保存文档(相当于数据库的写入数据)
PUT jobbole/job/1 #索引名称/表/id
{
"title":"python分布式爬虫开发", #字段名称:字段值
"salary_min":15000, #字段名称:字段值
"city":"北京", #字段名称:字段值
"company":{ #嵌套字段
"name":"百度", #字段名称:字段值
"company_addr":"北京市软件园", #字段名称:字段值
"employee_count":50 #字段名称:字段值
},
"publish_date":"2017-4-16", #字段名称:字段值
"comments":15 #字段名称:字段值
}
动态映射dynamic mapping
插入几条数据,es会自动为我们建立一个索引,以及对应的mapping,mapping中包含了每个field对应的数据类型,以及如何分词等设置
// 创建文档请求
PUT localhost:9200/blog/_doc/1
{
"title":"内蒙古科右中旗:沃野千里织锦绣---修改操作",
"description":"内蒙古兴安盟科右中旗巴彦呼舒镇乌逊嘎查整洁的村容村貌。近年来,内蒙古自治区兴安盟科尔沁右翼中旗按照“产业兴旺、生态宜居、乡风文明、治理有效、生活富裕”的总要求,坚持科学规划、合理布...国际在线",
"publish_time":"2020-07-08"
}
查看动态映射
GET http://localhost:9200/blog/_mapping
{
"blog" : {
"mappings" : {
"properties" : {
"description" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"id" : {
"type" : "long"
},
"publish_time" : {
"type" : "date"
},
"title" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
手动创建映射
创建索引
PUT http://localhost:9200/book
{
"acknowledged" : true
}
创建映射
PUT localhost:9200/book/_mapping
{
"properties":{
"name":{
"type":"text"
},
"description":{
"type":"text",
"analyzer":"english",
"search_analyzer":"english"
},
"pic":{
"type":"text",
"index":"false"
},
"publish_time":{
"type":"date"
}
}
}
查询映射
GET localhost:9200/blog/_mapping
{
"blog": {
"mappings": {
"properties": {
"description": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"id": {
"type": "long"
},
"publish_time": {
"type": "date"
},
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
映射测试
插入文档
PUT localhost:9200/book/_doc/1
{
"name":"Java核心技术",
"description":"本书由拥有20多年教学与研究经验的资深Java技术专家撰写(获Jolt大奖),是程序员的优选Java指南。本版针对Java SE 9、10和 11全面更新。",
"pic":"item.jd.com",
"publish_time":"2022-04-19"
}
{
"_index" : "book",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
测试查询 localhost:9200/book/_search?q=name:java
GET localhost:9200/book/_search?q=name:java
{
"took": 1126,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.2876821,
"hits": [
{
"_index": "book",
"_type": "_doc",
"_id": "1",
"_score": 0.2876821,
"_source": {
"name": "Java核心技术",
"description": "本书由拥有20多年教学与研究经验的资深Java技术专家撰写(获Jolt大奖),是程序员的优选Java指南。本版针对Java SE 9、10和 11全面更新。",
"pic": "item.jd.com",
"publish_time": "2022-04-19"
}
}
]
}
}
测试查询 localhost:9200/book/_search?q=description:java
GET localhost:9200/book/_search?q=description:java
{
"took": 15,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.45390707,
"hits": [
{
"_index": "book",
"_type": "_doc",
"_id": "1",
"_score": 0.45390707,
"_source": {
"name": "Java核心技术",
"description": "本书由拥有20多年教学与研究经验的资深Java技术专家撰写(获Jolt大奖),是程序员的优选Java指南。本版针对Java SE 9、10和 11全面更新。",
"pic": "item.jd.com",
"publish_time": "2022-04-19"
}
}
]
}
}
测试查询 localhost:9200/book/_search?q=pic:www.jd.com
GET localhost:9200/book/_search?q=pic:item.jd.com
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 0,
"relation": "eq"
},
"max_score": null,
"hits": []
}
}
通过测试发现:name和description都支持全文检索,pic不可作为查询条件。
修改映射
只能在创建index时手动建立mapping,或者新增field mapping,但是不能update field mapping。
因为已有数据按照映射早已分词存储好。如果修改,那这些存量数据怎么办。
新增一个字段mapping
PUT localhost:9200/book/_mapping
{
"properties":{
"ISBN":{
"type":"text",
"fields":{
"raw":{
"type":"keyword"
}
}
}
}
}
修改数据
PUT localhost:9200/book/_doc/1
{
"name":"Java核心技术",
"description":"本书由拥有20多年教学与研究经验的资深Java技术专家撰写(获Jolt大奖),是程序员的优选Java指南。本版针对Java SE 9、10和 11全面更新。",
"pic":"item.jd.com",
"publish_time":"2022-04-19",
"ISBN":"12800420"
}
搜索ISBN
GET localhost:9200/book/_search?q=ISBN:12800420
{
"took": 949,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.2876821,
"hits": [
{
"_index": "book",
"_type": "_doc",
"_id": "1",
"_score": 0.2876821,
"_source": {
"name": "Java核心技术",
"description": "本书由拥有20多年教学与研究经验的资深Java技术专家撰写(获Jolt大奖),是程序员的优选Java指南。本版针对Java SE 9、10和 11全面更新。",
"pic": "item.jd.com",
"publish_time": "2022-04-19",
"ISBN": "12800420"
}
}
]
}
}