首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Elasticsearch之mapping

这是es系列的第四篇文章了,阅读前三篇有助于小伙伴们理解本篇文章。

1、Elasticsearch总览

2、Elasticsearch安装

3、Elasticsearch上手

回顾

上一篇文章和小伙伴们简单聊了一下关于es的基本操作,包括检查集群状态、创建索引、添加文档、删除索引等。那么本篇我们就再继续完善我们的学习。

首先我们先来来,补充一些上篇文章中遗漏的知识点。

上篇文章我们讨论了如何查询索引,用如下_catapi实现的:

curl -X GET "localhost:9200/_cat/indices?v"

补充

查询

今天我们在介绍几种查询索引以及如何批量的查询的方式,如下,查询单个索引使用的到的是GET的方法,上一篇文章中我们创建可一个名称为triumphxx的索引,我们把它查出来,如下命令

curl -X GET "localhost:9200/triumphxx?pretty" 

所得结果如下,可以看到关于索引triumphxx的相关信息。


{
  "triumphxx" : {
    "aliases" : { },
    "mappings" : { },
    "settings" : {
      "index" : {
        "creation_date" : "1599144296602",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "ii4QVOXTQHGCmLMsSpMBCg",
        "version" : {
          "created" : "7080099"
        },
        "provided_name" : "triumphxx"
      }
    }
  }
}

说明:可以看到返回的结果是关于这个索引的所有的信息,至于aliasesmappingssettings都是什么意思,后面的内容会和大家都有讨论。

由于目前我们只创建了一个索引,接下来我们在创建一个叫做triumph的索引,然后讨论如何批量的查询索引。执行如下命令,进行创建

curl -X PUT "localhost:9200/triumph?pretty"  

这个时候我们我们的就存在了俩个索引,下面如何将俩个索引都查询出来,第一种方式批量获取,将多个索引的名称用,号隔开进行查询,执行如下命令:

curl -X GET "localhost:9200/triumphxx,triumph?pretty"

第二方式,查询所有的索引的方式,执行如下命令

curl -X GET "localhost:9200/_all?pretty"

还有我们前门文章中提到的如下命令也是查询全部的索引

curl -X GET "localhost:9200/_cat/indices?v"

因为目前我们只创建了俩个索引,查询的结果如下显示


{
  "triumph" : {
    "aliases" : { },
    "mappings" : { },
    "settings" : {
      "index" : {
        "creation_date" : "1599144651330",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "cHltoUzzRnOGwCjk4uS0yQ",
        "version" : {
          "created" : "7080099"
        },
        "provided_name" : "triumph"
      }
    }
  },
  "triumphxx" : {
    "aliases" : { },
    "mappings" : { },
    "settings" : {
      "index" : {
        "creation_date" : "1599144296602",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "ii4QVOXTQHGCmLMsSpMBCg",
        "version" : {
          "created" : "7080099"
        },
        "provided_name" : "triumphxx"
      }
    }
  }
}

判断存在

那么我们如何判断一个索引是否存在呢?当然是有方法的。我们可以使用head方法进行判断

curl -I HEAD "localhost:9200/triumph"

返回如下结果,状态码为200表示存在


HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 375

如果不存在,则返回的是404找不到


HTTP/1.1 404 Not Found
content-type: application/json; charset=UTF-8
content-length: 383

开关索引

我们创建完索引后还可以对索引进行关闭的操作,因为我们有时候创建完索引但是由于某种原因不用了而且还不想删除。执行如下POST命令关闭索引

curl -X POST "localhost:9200/triumph/_close?pretty"

返回如下结果,表示关闭成功


{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "indices" : {
    "triumph" : {
      "closed" : true
    }
  }
}

这个时候我们去查询这个索引curl -X GET "localhost:9200/triumph?pretty",返回如下结果


    "triumph": {
        "aliases": {},
        "mappings": {},
        "settings": {
            "index": {
                "verified_before_close": "true",
                "number_of_shards": "1",
                "provided_name": "triumph",
                "creation_date": "1599144651330",
                "number_of_replicas": "1",
                "uuid": "cHltoUzzRnOGwCjk4uS0yQ",
                "version": {
                    "created": "7080099"
                }
            }
        }
    }
}

可以看到返回的结果中:多出一个字段"verified_before_close": "true",这个表示关闭成功

那么既然可以关闭,当然也可以将关闭的索引打开,执行如下命令

curl -X POST "localhost:9200/triumph/_open?pretty"

返回如下结果,表示打开成功,继续查询这个索引,你会发现多出来的关闭的状态已经不见了。


{
  "acknowledged" : true,
  "shards_acknowledged" : true
}

好啦,小伙伴们,我们上篇文章遗留的一些点就说到这里,这篇文章我们主要介绍一下mapping映射的使用。

mapping的介绍与使用

就如我们es系列的第一篇所说的映射是什么:可以理解为传统数据库的表结构,每个文档的的每个字段是什么类型,通过映射来定义,它是对于索引文档的一种约束。

创建mapping

首先我们创建一个movie的索引,并且为这个索引创建mapping,顺序执行如下命令

curl -X PUT "localhost:9200/movie?pretty" 

{
  "movie" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "films" : {
          "type" : "text"
        },
        "name" : {
          "type" : "text"
        },
        "release_area" : {
          "type" : "text"
        },
        "theme" : {
          "type" : "keyword"
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1599147483046",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "aKSuEAQeRwiCuLsBUlA3OQ",
        "version" : {
          "created" : "7080099"
        },
        "provided_name" : "movie"
      }
    }
  }
}

执行完成后,返回如下结果,表示创建索引成功

{    "acknowledged": true}

再次查询我们可以看到,返回的索引信息,已经包含了映射的信息


{
  "movie" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "films" : {
          "type" : "text"
        },
        "name" : {
          "type" : "text"
        },
        "release_area" : {
          "type" : "text"
        },
        "theme" : {
          "type" : "keyword"
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1599147483046",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "aKSuEAQeRwiCuLsBUlA3OQ",
        "version" : {
          "created" : "7080099"
        },
        "provided_name" : "movie"
      }
    }
  }
}

也可以执行如下命令,进行单独的查询索引的mapping信息

curl -X GET "localhost:9200/movie/_mapping?pretty" 

返回如下结果,就是我们刚刚创建的映射信息


{
  "movie" : {
    "mappings" : {
      "properties" : {
        "films" : {
          "type" : "text"
        },
        "name" : {
          "type" : "text"
        },
        "release_area" : {
          "type" : "text"
        },
        "theme" : {
          "type" : "keyword"
        }
      }
    }
  }
}

那么我们如果想查看某几个索引的映射怎么查呢?其实就像批量查询索引的方式是一样的,如下命令

curl -X GET "localhost:9200/movie,triumphxx,triumph/_mapping?pretty" 

可以看到,其中有俩个索引我们没有设置映射的所以为空


{
  "triumph" : {
    "mappings" : { }
  },
  "triumphxx" : {
    "mappings" : { }
  },
  "movie" : {
    "mappings" : {
      "properties" : {
        "films" : {
          "type" : "text"
        },
        "name" : {
          "type" : "text"
        },
        "release_area" : {
          "type" : "text"
        },
        "theme" : {
          "type" : "keyword"
        }
      }
    }
  }
}

当然也可以使用查询全部的映射的命令,小伙伴们可以自己去尝试,这里作者就不把执行结果贴出来了。


curl -X GET "localhost:9200/_mapping?pretty" 
# 或者
curl -X GET "localhost:9200/_all/_mapping?pretty" 

增加字段

比如我们给movie索引添加一个字段叫做release_time,那我们该如何做呢,其实和刚开始创建索引是一样,只需要在mapping的结构中增加你的字段即可,如下:

curl -X PUT "localhost:9200/movie/_mapping" -H 'Content-Type:application/json' -d'

映射体加上你的字段即可


{
		"properties": {
				"name": {
						"type":"text"
				},
				"release_area": {
						"type":"text"
				},
				"films": {
				    "type":"text"
				},
				"theme": {
				 		"type":"keyword"
				},
        "release_time": {
            "type": "text" 
        }
		}
}

小伙伴们可能注意到到了,type作者搞了俩个类型textkeyword,这俩个是什么意思呢,这里先简单介绍一下,text类型的数据是可以被分词的,就是讲一句话按一定的规则进行拆分,而keyword是不可以进行拆分的。细节到后面的分词器,作者会详细的和大家聊这部分的内容。

修改字段

看了上面的增加字段,有的小伙伴可能就会问了,那我能修改字段的类型吗?这里是不可以,es规定一旦一个字段类型确定了以后是不能够修改的,除非删除后重新创建。如果强制修改会报如下错误:错误很清晰的说明,类型不可以修改。


{
    "error": {
        "root_cause": [
            {
                "type": "illegal_argument_exception",
                "reason": "mapper [release_time] of different type, current_type [text], merged_type [keyword]"
            }
        ],
        "type": "illegal_argument_exception",
        "reason": "mapper [release_time] of different type, current_type [text], merged_type [keyword]"
    },
    "status": 400
}

小结

好啦,小伙伴们,我们今天主要补充了一些索引的相关知识,以及映射mapping的创建以及其他的一些操作,让我们清楚了什么是索引的映射以及他的作用是什么。接下来,我们就会讨论,基于这个映射我们对文档的CRUD的操作

  • 发表于:
  • 本文为 InfoQ 中文站特供稿件
  • 首发地址https://www.infoq.cn/article/d6f379c87e5f647453c29caf7
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券