文章目录
写在前面
我已经不记得这是第几次学习ElasticSearch(es)了,初学的时候,知道有这么个东西,感觉用不到,后来可以简单的用命令来操作,再后来就在自己的项目中集成了es,发现es的坑还是挺深的,不得已又来学习了
初来乍到创建索引
很多文章都有这么一个比喻,把索引比作数据库,把类型比作数据库的表,把文档比作表中每一行数据,但是在es7.x中,对类型已经慢慢弃用,不管怎么滴吧,先来体验一下安装创建索引。**没安装es的小伙伴可以先看这篇文章ElasticSearch、kibana、head、ik分词器在Windows下的安装
es是rest风格的,所以可以通过PUT命令来创建索引
创建user_index索引
PUT /user_index
复制代码
就这样就创建了一个索引,是不是可简单,我觉得也简单,还有简单的呢。
查看user_index索引信息
GET /user_index
复制代码
删除user_index索引
DELETE /user_index
复制代码
如果世界这么简单就好了。。。
索引有个概念叫做分片和备份,意思就是,我们如果要存10条数据,我们会把这10条数据分成若干片,备份的话,就是可以给每个分片进行备份,并把相同的片放到不同的节点上(防止单点故障问题)。
创建并指定分片数和备份数
PUT /user_index
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
}
}
复制代码
通过settings来指定分片数为3,备份数为2,是不是有一丢丢的复杂了,其实世界还没有这么罢休,还有呢,我们在创建索引的时候,还可以指定文档的属性(这里没有指定类型,默认是_doc),例如integer,long,text,keyword,date等。
创建并指定properties
PUT /user_index
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
},
"mappings": {
"properties": {
"id": {
"type": "long"
},
"name": {
"type": "keyword"
},
"context": {
"type": "text"
},
"age": {
"type": "integer"
},
"date": {
"type": "date"
}
}
}
}
复制代码
这个看着就有点舒服了吧,其实还不行,世界还没有这么简单“世界还小,我愿意陪你到天荒地老”,继续来,在我们创建索引的时候,可以指定分词器,ik分词器。
创建并指定字段分词器
PUT /user_index
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
},
"mappings": {
"properties": {
"context": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
复制代码
简单的查询数据
查询有一个关键字叫做_search
。
首先我们创建一个索引,然后插入几条数据,默认的类型格式是_doc(官方即将抛弃类型)
PUT /user_index
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
},
"mappings": {
"properties": {
"name": {
"type": "keyword"
},
"age": {
"type": "integer"
},
"date": {
"type": "date",
"format" : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd"
}
}
}
}
复制代码
PUT /user_index/_doc/1
{
"name": "张三",
"age": 18,
"date": "2020-12-04"
}
PUT /user_index/_doc/2
{
"name": "张四",
"age": 22,
"date": "2020-12-04"
}
PUT /user_index/_doc/3
{
"name": "李三",
"age": 20,
"date": "2020-12-04"
}
复制代码
查询user_index索引的全部数据
GET /user_index/_search
复制代码
再来一个查询全部数据的
GET /user_index/_search
{
"query": {"match_all": {}}
}
复制代码
查询指定属性
这里通过match匹配,属于模糊匹配,该匹配会分词,term匹配属于精确匹配,分词的时候分text类型,keyword类型不进行分词。
GET /user_index/_search
{
"query": {
"match": {
"name": "张三"
}
}
}
复制代码
根据范围查询
GET /user_index/_search
{
"query": {
"range": {
"age": {
"gte": 20,
"lte": 25
}
}
}
}
复制代码
对查询的结果进行分页
GET /user_index/_search
{
"query": {
"range": {
"age": {
"gte": 15,
"lte": 25
}
}
},
"from": 0,
"size": 2
}
复制代码
对查询的结果进行排序
GET /user_index/_search
{
"query": {"match_all": {}},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
复制代码
自认为高大上查询
学习es查询,不学bool查询感觉是没有灵魂的,bool查询有四个子语句,分别是must,filter,should,must_not
must查询
GET /user_index/_search
{
"query": {
"bool": {
"must": {
"term" : {
"name": "张三"
}
}
}
}
}
复制代码
filter查询
GET /user_index/_search
{
"query": {
"bool": {
"filter": {
"term" : {
"name": "张三"
}
}
}
}
}
复制代码
should查询
GET /user_index/_search
{
"query": {
"bool": {
"should": {
"term" : {
"name": "张三"
}
}
}
}
}
复制代码
must_not查询
GET /user_index/_search
{
"query": {
"bool": {
"must_not": {
"term" : {
"name": "张三"
}
}
}
}
}
复制代码
写在最后
有些东西,我只是做了演示,没有对其底层原理进行分析,这也是我学习的一个方法,我一般学一个新东西,会去反复学习,层层递进。