xsong 的个人博客

浪费时间是一桩大罪过!

  menu
3 文章
0 浏览
1 当前访客
ღゝ◡╹)ノ❤️

Elasticsearch基础

Elasticsearch

1. 安装

安装链接

  • 点击链接安装,然后解压即可

2. 基础语法

2.1 索引

  • 创建students索引: put: host/students
  • 查看单个索引:get: host/students
{
    "shopping": {//索引名
        "aliases": {},//别名
        "mappings": {},//映射
        "settings": {//设置
            "index": {//设置 - 索引
                "creation_date": "1617861426847",//设置 - 索引 - 创建时间
                "number_of_shards": "1",//设置 - 索引 - 主分片数量
                "number_of_replicas": "1",//设置 - 索引 - 主分片数量
                "uuid": "J0WlEhh4R7aDrfIc3AkwWQ",//设置 - 索引 - 主分片数量
                "version": {//设置 - 索引 - 主分片数量
                    "created": "7080099"
                },
                "provided_name": "shopping"//设置 - 索引 - 主分片数量
            }
        }
    }
}

  • 查询全部的索引,以及索引的状态信息:get: host/_cat/indices?v

image-20220627165849192

表头含义
health当前服务器健康状态: green(集群完整) yellow(单点正常、集群不完整) red(单点不正常)
status索引打开、关闭状态
index索引名
uuid索引统一编号
pri主分片数量
rep副本数量
docs.count可用文档数量
docs.deleted文档删除状态(逻辑删除)
store.size主分片和副分片整体占空间大小
pri.store.size主分片占空间大小
  • 删除索引:``DELETE: host/studets`

2.2 文档

  • 创建文档(不指定主键,他会自己生成一个主键 _id):POST : host/shop/_doc请求的JSON格式为:
{
    "shop_name": "西瓜",
    "price": 20
}
  • 创建文档(指定主键):POST/PUT : host/shop/_doc/1请求的JSON格式同上.

2.3 查询

  • 主键查询:GET: host/shop/_doc/1
  • 查看所有数据:GET: host/shop/_search

2.3.1 条件查询

  • URL带参查询方式:GET host/shop/_search?q=shop_name:西瓜 格式是后面有个q参数,然后里面加上条件,多条件的时候使用 AND 或者 OR 连接

  • 我们主要使用的是请求体带参查询 同样是GET请求,但是需要有json体

    • 单条件查询使用"match";(多条件详见bool查询)
    {
        "query":
        {
            "match":
            {
                "shop_name":"西瓜"
            }
        }
    }
    
    • 查询所有文档内容用 "match_all"
    {
        "query":
        {
            "match_all":{}
        }
    }
    
    • 指定查询的字段使用_source:[]
    {
        "query":
        {
            "match_all":{}
        }
        "_source":["shop_name":"西瓜","price":20]
    }
    

2.3.2 分页查询

使用from 和 size 进行分页查询

{
	"query":{
		"match_all":{}
	},
	"from":0,
	"size":2
}

2.3.3 查询排序

{
	"query":{
		"match_all":{}
	},
	"sort":{
		"price":{
			"order":"desc"
		}
	}
}

2.3.4 bool查询

must:与关系,相当于关系型数据库中的 and。

should:或关系,相当于关系型数据库中的 or。

must_not:非关系,相当于关系型数据库中的 not。

filter:过滤条件。

range:条件筛选范围。

多条件查询
  • 多条件查询 must相当于数据库的&& should相当于数据库的||
{
	"query":{
		"bool":{
			"must(should)":[{
				"match":{
					"shop_name":"西瓜"
				}
			},{
				"match":{
					"price":20
				}
			}]
		}
	}
}
范围查询
  • gt: > 大于(greater than)

  • lt: < 小于(less than)

  • gte: >= 大于或等于(greater than or equal to)

  • lte: <= 小于或等于(less than or equal to)

    {
        "query":
        {
            "bool":
            {
                "should":
                {
                    "match":
                    {
                        "shop_name":西瓜
                    }
                }
            },
            "filter":
            {
                "range":
                {
                    "gt":10
                }
            }
        }
    }
    

2.3.5 全文检索

  • 全文检索比较强,我输入“西南”,他能返回西瓜和南瓜,只要是我查询的字段包含这里面的字的时候他都返回
{
    "query":
    {
        "match":
        {
            "shop_name":"西南"
        }
    }
}

2.3.6 完全匹配(模糊查询)

跟全文检索不同的是全文检索使用的是 match 而 完全匹配使用的是 match_phrase

{
	"query":
    {
        "match_phrase":
        {
            "shop_name":"西南"
        }
    }
}

2.3.7 高亮查询

使用highlight 来进行处理

{
	"query":
    {
        "match":
        {
            "shop_name":"西南"
        }
    },
    "highlight":
    {
        "fields":
        {
            "shop_name":{}
        }

    }
}

2.3.8 聚合查询

聚合查询使用的是aggs

  • 分组操作

    {
        "aggs":
        {
            "price_group": //名字随便起,分组的名字
            {
                "terms"://分组操作
                {
                    "field":"price" //要分组的字段
                }
            }
        },
        "size":0 //不加这个他会先显示所有数据,再显示分组的
    }
    
  • 求平均值操作:跟上面同理,只是terms操作变成了 avg操作

    {
        "aggs":
        {
            "price_avg": //名字随便起,分组的名字
            {
                "avg":
                {
                    "field":"price" //要分组的字段
                }
            }
        },
        "size":0 //不加这个他会先显示所有数据,再显示分组的
    }
    

2.4 修改操作

  • 全局修改:POST: host/shop/_doc/1请求的json格式为
{
    “name” : "黄瓜",
    "price": 30,
    "weight":20
}
  • 局部修改:POST: host/shop/_update/1请求的json格式为
{
    "doc":{
        "name" : "南瓜"
    }
}
  • 删除一个文档:DELETE: host/shop/_doc/1,删除一个文档不会立即从磁盘上移除,它只是被标记成已删除(逻辑删除)。
{
    "_index": "shop",
    "_type": "_doc",
    "_id": "1",
    "_version": 4,
    "result": "deleted", ------标记删除成功
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 4,
    "_primary_term": 1
}

2.5 映射

  • 创建一个新的索引

  • 发送映射请求:PUT:/host/user/_mapping

    {
        "properties":{
            "name":{
                "type":"text", //这个类型可以全文检索
                "index":true
            },
            "sex":{
                 "type":"keyword", //这个类型不能全文检索
                 "index":true
            }
        }
    }
    

标题:Elasticsearch基础
作者:xsong
地址:https://xsong.top/articles/2022/06/28/1656404166291.html