Elasticsearch http操作
[TOC]
索引
索引创建
PUT http://172.16.20.156:9200/shopping
创建shopping的索引
响应
{
"acknowledged": true,//响应结果
"shards_acknowledged": true,//分片结果
"index": "shopping"//索引名称
}
创建已经存在的索引,则会出错,错误的信息为:
{
"error": {
"root_cause": [
{
"type": "resource_already_exists_exception",
"reason": "index [shopping/6d4rcbZHT_OTPai9B1bt5w] already exists",
"index_uuid": "6d4rcbZHT_OTPai9B1bt5w",
"index": "shopping"
}
],
"type": "resource_already_exists_exception",
"reason": "index [shopping/6d4rcbZHT_OTPai9B1bt5w] already exists",
"index_uuid": "6d4rcbZHT_OTPai9B1bt5w",
"index": "shopping"
},
"status": 400
}
索引查询
查询指定索引
GET http://172.16.20.156:9200/shopping
查询shopping索引
响应
{
"shopping": {//索引名
"aliases": {},//别名
"mappings": {},//映射
"settings": {//设置
"index": {//设置 - 索引
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"number_of_shards": "1",
"provided_name": "shopping",
"creation_date": "1649751434832",
"number_of_replicas": "1",
"uuid": "6d4rcbZHT_OTPai9B1bt5w",
"version": {
"created": "7130099"
}
}
}
}
}
查询所有索引
GET http://172.16.20.156:9200/_cat/indices?v
_cat 表示查看的意思, indices 表示索引,所以整体含义就是查看当前 ES服务器中的所有索引,就好像 MySQL 中的 show tables 的感觉,服务器响应结果如下 :
响应
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open sw_profile_task_log-20220215 zew2Jn3jRXuwLVutwxMZUQ 1 1 0 0 416b 208b
green open shopping 6d4rcbZHT_OTPai9B1bt5w 1 1 0 0 416b 208b
- health:当前服务器健康状态: green(集群完整) yellow(单点正常、集群不完整) red(单点不正常)
- status:索引打开、关闭状态
- index:索引名
- uuid:索引统一编号
- pri:主分片数量
- rep:副本数量
- docs.count:可用文档数量
- docs.deleted:文档删除状态(逻辑删除)
- store.size:主分片和副分片整体占空间大小
- pri.store.size:主分片占空间大小
索引删除
删除指定的索引
DELETE http://172.16.20.156:9200/shopping
删除shopping的索引
响应
{
"acknowledged": true //true表示删除成功
}
文档
创建文档(随机id)
由于没有指定数据唯一性标识(ID),默认情况下, ES 服务器会随机生成一个。
POST http://172.16.20.156:9200/shopping/_doc
给shopping索引添加一个文档
{
"title":"小米手机",
"category":"小米",
"images":"http://www.baidu.com",
"price":1099.00
}
响应
{
"_index": "shopping",//索引
"_type": "_doc",//类型-文档
"_id": "Jz7yHIAB-qNtsTvu7pYE",//唯一标识,可以类比为 MySQL 中的主键,随机生成
"_version": 1,//版本
"result": "created", //结果,这里的 create 表示创建成功
"_shards": {
"total": 2, //分片 - 总数
"successful": 2,//分片成功 - 总数
"failed": 0 //分片失败 - 总数
},
"_seq_no": 0,
"_primary_term": 1
}
创建文档(指定id)
POST http://172.16.20.156:9200/shopping/_doc/10001
如果增加数据时明确数据主键,那么请求方式也可以为 PUT
{
"title":"IQOO 5",
"category":"viv0",
"images":"http://www.baidu.com",
"price":4499.00
}
响应结果同创建文档(随机id)中的响应
全量修改
修改文档的所有数据
POST http://172.16.20.156:9200/shopping/_doc/10001
{
"title":"IQOO 6",
"category":"vivo",
"images":"http://www.baidu1.com",
"price":4699.00
}
响应结果同创建文档(随机id)中的响应,但是result的值为updated,便是数据被更新
局部修改
POST http://172.16.20.156:9200/shopping/_update/10001
修改局部的数据,该接口表示修改索引shopping,文档id:10001中的title与category的字段
{
"doc": {
"title":"安卓手机",
"category":"安卓"
}
}
删除指定文档
DELETE http://172.16.20.156:9200/shopping/_doc/10002
删除指定文档,该接口表示删除索引shopping,文档id:10002的文档数据
响应
{
"_index": "shopping",
"_type": "_doc",
"_id": "10001",
"_version": 4,
"result": "deleted",//删除成功
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 10,
"_primary_term": 1
}
验证是否成功可以通过GET http://172.16.20.156:9200/shopping/_doc/10002来查看是否删除成功
主键查询文档
GET http://172.16.20.156:9200/shopping/_doc/10002
通过主键查询文档,该接口表示查询索引shopping,文档id:10002的文档数据
响应
{
"_index": "shopping",
"_type": "_doc",
"_id": "10002",
"_version": 2,
"_seq_no": 7,
"_primary_term": 1,
"found": true,//假如这个值为false则证明没有找到
"_source": {
"title": "IQOO 5",
"category": "viv0",
"images": "http://www.baidu.com",
"price": 4599.00
}
}
查询某个索引下的所有文档
GET http://172.16.20.156:9200/shopping/_search
查询某个索引下的所有文档,该接口表示查询索引shopping下的所有文档信息
响应
{
"took": 661, //查询花费时间,单位毫秒
"timed_out": false, //是否超时
"_shards": { //分片信息
"total": 1, //总数
"successful": 1, //成功
"skipped": 0, //忽略
"failed": 0 //失败
},
"hits": { //搜索命中结果
"total": { //搜索条件匹配的文档总数
"value": 1, //总命中计数的值
"relation": "eq" //计数规则,eq 表示计数准确,gte 表示计数不准确
},
"max_score": 1.0,//匹配度分值
"hits": [ //命中结果集合
{
"_index": "shopping",
"_type": "_doc",
"_id": "Jz7yHIAB-qNtsTvu7pYE",
"_score": 1.0,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.baidu.com",
"price": 1099.00
}
}
]
}
}
url带参查询
GET http://172.16.20.156:9200/shopping/_search?q=category:苹果
URL带参数形式查询,这很容易让不善者心怀恶意 参数值出现中文会出现乱码情况。为了避免这些情况,我们可用使用带JSON请求体请求进行查询 响应结果同查询某个索引下的所有文档的响应
请求体带参查询
GET http://172.16.20.156:9200/shopping/_search
按条件查询某个索引下符合文档的文档,该接口表示查询索引shopping下的category为苹果的结果 这种方式会根据category的分词效果,查询包含苹、果、苹果的三种数据,下面的同理 "query":这里的 query 代表一个查询对象,里面可以有不同的查询属性 "match_all":查询类型,例如: match_all(代表查询所有), match, term , range 等
range 查询允许以下字符
操作符 | 说明 | 对应符号 |
---|---|---|
gt | 大于 | > |
gte | 大于等于 | >= |
lt | 小于 | < |
lte | 小于等于 | <= |
{
"query":{
"match":{
"category":"苹果"
}
}
}
与http://172.16.20.156:9200/shopping/_search?q=category:苹果 查询的结果一致
带请求体方式的查找所有内容
GET http://172.16.20.156:9200/shopping/_search
查询索引shopping下的所有文档,同
GET http://172.16.20.156:9200/shopping/_search
的查询
{
"query":{
"match_all":{}
}
}
与直接请求http://172.16.20.156:9200/shopping/_search这个的结果一致
分页/指定字段/排序查询/条件查询
GET http://172.16.20.156:9200/shopping/_search
查询索引shopping下category为小米,只是查看title字段,按照price字段进行倒序排列,查看第一页数据,每页显示2条数据
{
"query":{
"match":{
"category":"小米"
}
},
"_source":["title"],//查询指定字段
"sort":{//指定字段排序
"price":{
"order":"desc"
}
}
"from":0,//起始页
"size":2//每页条数
}
完全匹配
GET http://172.16.20.156:9200/shopping/_search
{
"query":{
"match_phrase":{
"category" : "三星"
}
}
}
高亮查询
在进行关键字搜索时,搜索出的内容中的关键字会显示不同的颜色,称之为高亮。 Elasticsearch 可以对查询内容中的关键字部分,进行标签和样式(高亮)的设置。在使用 match 查询的同时,加上一个 highlight 属性: pre_tags:前置标签 post_tags:后置标签 fields:需要高亮的字段 title:这里声明 title 字段需要高亮,后面可以为这个字段设置特有配置, 也可以空
GET http://172.16.20.156:9200/shopping/_search
{
"query":{
"match_phrase":{
"category" : "为"
}
},
"highlight":{
"pre_tags": "<font color='red'>",
"post_tags": "</font>",
"fields":{
"category":{}//<----高亮这字段
}
}
}
响应
{
"took": 128,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.6928279,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "Kz70HIAB-qNtsTvubZbt",
"_score": 1.6928279,
"_source": {
"title": "meta 20 pro",
"category": "华为",
"images": "http://www.baidu.com",
"price": 4099.00
},
"highlight": {
"category": [
"华<em>为</em>"
]
}
}
]
}
}
根据条件删除
POST http://127.0.0.1:9200/shopping/_delete_by_query
删除索引shopping中price为4000的文档
{
"query": {
"match": {
"price": 4000
}
}
}
响应
{
......
"total": 2,
"deleted": 2,
"batches": 1,
"version_conflicts": 0,
"noops": 0,
"retries": {
"bulk": 0,
"search": 0
},
"throttled_millis": 0,
"requests_per_second": -1,
"throttled_until_millis": 0,
"failures": []
}
同时多字段匹配
multi_match 与 match 类似,不同的是它可以在多个字段中查询。
GET http://172.16.20.156:9200/shopping/_search
查询索引shopping下category与title中包含诺果分片的词
{
"query": {
"multi_match": {
"query": "诺果",
"fields": [
"category",
"title"
]
}
}
}
聚合查询
分组
类似与关系型数据库中的 group by
GET http://172.16.20.156:9200/shopping/_search
{
"aggs":{//聚合操作
"price_group":{//名称,随意起名
"terms":{//分组
"field":"price"//分组字段
}
}
}
}
返回结果会附带原始数据的。若不想要不附带原始数据的结果,在 Postman 中,向 ES 服务器发 GET http://127.0.0.1:9200/shopping/_search,附带JSON体如下:
{
"aggs":{
"price_group":{
"terms":{
"field":"price"
}
}
},
"size":0
}
对某个字段取平均值(avg)
若想对所有手机价格求平均值。
GET http://172.16.20.156:9200/shopping/_search
{
"aggs":{
"price_avg":{//名称,随意起名
"avg":{//求平均
"field":"price"
}
}
},
"size":0
}
对某个字段取最大值(max)
对某个字段取最小值(min)
对某个字段求和(sum)
Stats 聚合
对某个字段一次性返回 count, max, min, avg 和 sum 五个指标
GET http://172.16.20.156:9200/shopping/_search
{
"aggs": {
"stats_price": {
"stats": {
"field": "price"
}
}
},
"size": 0
}
响应
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 7,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"stats_price": {
"count": 7,
"min": 1099.0,
"max": 6099.0,
"avg": 3184.714285714286,
"sum": 22293.0
}
}
}
对某个字段的值进行去重之后再取总数
GET http://172.16.20.156:9200/shopping/_search
{
"aggs":{
"distinct_price":{//
"cardinality":{//去重
"field":"price"
}
}
},
"size":0
}
响应
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 7,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"distinct_price": {
"value": 7
}
}
}
先terms分组再进行聚合
GET http://172.16.20.156:9200/shopping/_search
{
"aggs":{
"price_group":{
"terms":{
"field":"price"
},"aggs":{
"sum_price":{
"sum":{
"field":"price"
}
}
}
}
}
}
响应
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 7,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "Jz7yHIAB-qNtsTvu7pYE",
"_score": 1.0,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.baidu.com",
"price": 1099.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "KD7zHIAB-qNtsTvuxpaO",
"_score": 1.0,
"_source": {
"title": "小米10",
"category": "小米",
"images": "http://www.baidu.com",
"price": 2099.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "KT7zHIAB-qNtsTvu55am",
"_score": 1.0,
"_source": {
"title": "小米14",
"category": "小米",
"images": "http://www.baidu.com",
"price": 3099.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "Kj70HIAB-qNtsTvuK5Yl",
"_score": 1.0,
"_source": {
"title": "苹果14",
"category": "苹果",
"images": "http://www.baidu.com",
"price": 6099.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "Kz70HIAB-qNtsTvubZbt",
"_score": 1.0,
"_source": {
"title": "meta 20 pro",
"category": "华为",
"images": "http://www.baidu.com",
"price": 4099.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "10002",
"_score": 1.0,
"_source": {
"title": "IQOO 5",
"category": "viv0",
"images": "http://www.baidu.com",
"price": 4599.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "LD6oIYAB-qNtsTvuy5bM",
"_score": 1.0,
"_source": {
"title": "三星",
"category": "诺基亚",
"images": "http://www.baidu.com",
"price": 1199.00
}
}
]
},
"aggregations": {
"price_group": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 1099.0,
"doc_count": 1,
"sum_price": {
"value": 1099.0
}
},
{
"key": 1199.0,
"doc_count": 1,
"sum_price": {
"value": 1199.0
}
},
{
"key": 2099.0,
"doc_count": 1,
"sum_price": {
"value": 2099.0
}
},
{
"key": 3099.0,
"doc_count": 1,
"sum_price": {
"value": 3099.0
}
},
{
"key": 4099.0,
"doc_count": 1,
"sum_price": {
"value": 4099.0
}
},
{
"key": 4599.0,
"doc_count": 1,
"sum_price": {
"value": 4599.0
}
},
{
"key": 6099.0,
"doc_count": 1,
"sum_price": {
"value": 6099.0
}
}
]
}
}
}
组合查询
bool把各种其它查询通过must(必须 )、 must_not(必须不)、 should(应该)的方式进行组合
小米牌子并且价格为3099元的(must相当于数据库的&&)
GET http://172.16.20.156:9200/shopping/_search
{
"query":{
"bool":{
"must":[{
"match":{
"category":"小米"
}
},{
"match":{
"price":3099.00
}
}]
}
}
}
小米和华为的牌子(should相当于数据库的||)
GET http://172.16.20.156:9200/shopping/_search
{
"query":{
"bool":{
"should":[{
"match":{
"category":"小米"
}
},{
"match":{
"category":"华为"
}
}]
}
}
}
小米和华为的牌子,价格大于2000元并且小于4000的手机
GET http://172.16.20.156:9200/shopping/_search
{
"query":{
"bool":{
"should":[{
"match":{
"category":"小米"
}
},{
"match":{
"category":"华为"
}
}],
"filter":{
"range":{
"price":{
"gt":2000,
"lt":4000
}
}
}
}
}
}
注意:本文归作者所有,未经作者允许,不得转载