1. ES

The Elastic Stack, 包括 Elasticsearch、 Kibana、 Beats 和 Logstash(也称为 ELK Stack)。能够安全可靠地获取任何来源、任何格式的数据,然后实时地对数据进行搜索、分析和可视化。

Elaticsearch,简称为 ES, ES 是一个开源的高扩展的分布式全文搜索引擎, 是整个 ElasticStack 技术栈的核心。

它可以近乎实时的存储、检索数据;本身扩展性很好,可以扩展到上百台服务器,处理 PB 级别的数据。

2. ES安装

官网地址
官方文档
elasticsearch-7-8-下载界面

3. PostMan操作ES

{{es_basic}} —> postman环境变量 —>localhost:9200

3.1 索引操作
3.1.1 创建索引
-PUT {{es_basic}}/shopping

返回结果

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

“acknowledged”: true, 表示索引创建成功!

假设我们已POST方式创建索引

{
    "error": "Incorrect HTTP method for uri [/shopping] and method [POST], allowed: [DELETE, PUT, HEAD, GET]",
    "status": 405
}

该报错信息表示创建索引只能是PUT方法

3.1.2 查询索引
-GET {{es_basic}}/shopping

返回结果

{
    "shopping": {
        "aliases": {},
        "mappings": {},
        "settings": {
            "index": {
                "creation_date": "1648972369147",
                "number_of_shards": "1",
                "number_of_replicas": "1",
                "uuid": "InEwSJbiQcK_jHOKNFwG_w",
                "version": {
                    "created": "7080099"
                },
                "provided_name": "shopping"
            }
        }
    }
}
3.1.3 删除索引
-DELETE {{es_basic}}/shopping

返回结果

{
    "acknowledged": true
}
3.1.4 查看全部索引
-GET {{es_basic}}/_cat/indices?v

返回结果

health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   shopping LFvXy9p8QJi-cLagS6oPtw   1   1          0            0       208b           208b

可以看到我们创建的索引 shopping

3.2 文档操作

文档的操作,存在索引的前提,假设我们已创建好shopping索引

3.2.1 文档创建
-POST {{es_basic}}/shopping/_doc

返回结果

{
    "error": {
        "root_cause": [
            {
                "type": "parse_exception",
                "reason": "request body is required"
            }
        ],
        "type": "parse_exception",
        "reason": "request body is required"
    },
    "status": 400
}

发现创建失败,原因是缺少 body文档, “reason”: “request body is required”

由于es操作基于json文件,我们在创建文档时,添加body 的json 文件

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KLh8t5Dm-1649061268487)(C:\Users\LiuZy\AppData\Roaming\Typora\typora-user-images\image-20220403161937403.png)]

json文件

{
    "title":"小米手机",
    "category":"小米",
    "images":"http://www.gulixueyuan.com/xm.jpg",
    "price":3999.00
}

返回结果

{
    "_index": "shopping",             //索引
    "_type": "_doc",                 // 文档类型
    "_id": "HqGB7n8BI3mVVhLujEv6",  //主键
    "_version": 1,                  //版本
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

​ “result”: “created” 表示文档创建成功,HqGB7n8BI3mVVhLujEv6类似MySQL的唯一主键

创建文档这边不能使用PUT,否则会报错,如下

{
    "error": "Incorrect HTTP method for uri [/shopping/_doc] and method [PUT], allowed: [POST]",
    "status": 405
}

如果想要指定唯一键_id,我们使用PUT、POST都可以

-PUT {{es_basic}}/shopping/_doc/10086
-POST {{es_basic}}/shopping/_doc/10086
3.2.2 文档查询
-GET {{es_basic}}/shopping/_doc/10086

返回结果

{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "10086",
    "_version": 2,
    "_seq_no": 3,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "title": "小米手机_id_put",
        "category": "小米",
        "images": "http://www.gulixueyuan.com/xm.jpg",
        "price": 3999.00
    }
}

查询不存在的id时

{{es_basic}}/shopping/_doc/100863

返回结果

{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "100863",
    "found": false
}

“found”: false 无结果

查询查询该shopping索引下的全部文档

-GET {{es_basic}}/shopping/_doc/_search

返回结果

{
    "took": 38,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "HqGB7n8BI3mVVhLujEv6",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "H6GI7n8BI3mVVhLuwUtc",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "10086",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机_id_put",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            }
        ]
    }
}
3.2.3 文档更新
3.2.3.1 全量更新

主键id与原来存在的文档一致,该操作会覆盖之前的文档,称为全量更新

假设 已存在索引shopping并且id为10086的文档

-POST {{es_basic}}/shopping/_doc/10086

返回结果

{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "10086",
    "_version": 3,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 4,
    "_primary_term": 1
}

如果时首次创建,则是"result": “created”,此时为"result": “updated”,表示被更新

3.2.3.2 局部更新

修改前

-GET {{es_basic}}/shopping/_doc/10086

返回结果

{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "10086",
    "_version": 7,
    "_seq_no": 8,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "title": "小米手机_id_put",
        "category": "小米",
        "images": "http://www.gulixueyuan.com/xm.jpg",
        "price": 3999.00
    }
}

修改

指定body里面的局部属性进行更新

-POST {{es_basic}}/shopping/_update/10086

body

{
    "doc":{
        "title":"为发烧而生",
        "category":"小米粥"
    }
}

doc指定的文档名称

局部更新

-POST {{es_basic}}/shopping/_update/10086

返回结果

{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "10086",
    "_version": 8,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 9,
    "_primary_term": 1
}

_version 会随着每次的更新而递增

我们重新查询

-GET {{es_basic}}/shopping/_doc/10086

返回结果

{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "10086",
    "_version": 8,
    "_seq_no": 9,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "title": "为发烧而生",
        "category": "小米粥",
        "images": "http://www.gulixueyuan.com/xm.jpg",
        "price": 3999.0
    }
}

可见title、category已被更新

3.2.4 文档删除
-DELETE {{es_basic}}/shopping/_doc/10086

返回结果

{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "10086",
    "_version": 9,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 10,
    "_primary_term": 1
}

“result”: “deleted”,文档被删除

再次查询

-GET {{es_basic}}/shopping/_doc/10086

返回结果

{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "10086",
    "found": false
}

可见索引shopping、文档_doc已被删除

3.2.5 文档高级查询

数据准备

-GET {{es_basic}}/shopping/_doc/_search

返回结果

{
    "took": 728,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "HqGB7n8BI3mVVhLujEv6",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "H6GI7n8BI3mVVhLuwUtc",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机_1",
                    "category": "小米_1",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 1
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机_4",
                    "category": "小米_4",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 5
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "99",
                "_score": 1.0,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 8888.00
                }
            }
        ]
    }
}
3.2.5.1 条件查询

URL带参查询:查询category=小米的所有文档

-GET {{es_basic}}/shopping/_search?q=category:小米

返回结果

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": 0.39713606,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "HqGB7n8BI3mVVhLujEv6",
                "_score": 0.39713606,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "H6GI7n8BI3mVVhLuwUtc",
                "_score": 0.39713606,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "55",
                "_score": 0.39713606,
                "_source": {
                    "title": "小米手机_55",
                    "category": "米小",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.3370651,
                "_source": {
                    "title": "小米手机_1",
                    "category": "小米_1",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 1
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "2",
                "_score": 0.3370651,
                "_source": {
                    "title": "小米手机_4",
                    "category": "小米_4",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 5
                }
            }
        ]
    }
}

该查询可以查出category中包含小米的所有文档,但URL传参毕竟不安全

Body带参查询:查询category=小米的所有文档

-GET {{es_basic}}/shopping/_search

Body参数

{
    "query":{
        "match":{
            "category":"小米"
        }
    }
}

返回结果

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": 0.39713606,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "HqGB7n8BI3mVVhLujEv6",
                "_score": 0.39713606,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "H6GI7n8BI3mVVhLuwUtc",
                "_score": 0.39713606,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "55",
                "_score": 0.39713606,
                "_source": {
                    "title": "小米手机_55",
                    "category": "米小",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.3370651,
                "_source": {
                    "title": "小米手机_1",
                    "category": "小米_1",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 1
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "2",
                "_score": 0.3370651,
                "_source": {
                    "title": "小米手机_4",
                    "category": "小米_4",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 5
                }
            }
        ]
    }
}

返回通用的数据,但是body传参安全性较高,推荐使用

Body带参查询:查询所有文档

-GET {{es_basic}}/shopping/_search

Body参数

{
    "query":{
        "match_all":{
        }
    }
}

返回结果

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 6,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "HqGB7n8BI3mVVhLujEv6",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "H6GI7n8BI3mVVhLuwUtc",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机_1",
                    "category": "小米_1",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 1
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机_4",
                    "category": "小米_4",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 5
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "99",
                "_score": 1.0,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 8888.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "55",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机_55",
                    "category": "米小",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            }
        ]
    }
}

Body带参查询:查询category=小米的(指定字段)所有文档

-GET {{es_basic}}/shopping/_search

Body传参

{
    "query":{
        "match":{
            "category":"小米"
        }
    },
    "_source":["title","price"]
}

返回结果

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": 0.39713606,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "HqGB7n8BI3mVVhLujEv6",
                "_score": 0.39713606,
                "_source": {
                    "price": 3999.0,
                    "title": "小米手机"
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "H6GI7n8BI3mVVhLuwUtc",
                "_score": 0.39713606,
                "_source": {
                    "price": 3999.0,
                    "title": "小米手机"
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "55",
                "_score": 0.39713606,
                "_source": {
                    "price": 3999.0,
                    "title": "小米手机_55"
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.3370651,
                "_source": {
                    "price": 1,
                    "title": "小米手机_1"
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "2",
                "_score": 0.3370651,
                "_source": {
                    "price": 5,
                    "title": "小米手机_4"
                }
            }
        ]
    }
}
3.2.5.2 分页查询
-GET {{es_basic}}/shopping/_search

Body参数

{
    "query":{
        "match":{
            "category":"小米"
        }
    },
    "from":0,
    "size":3
}

from:类似于offset偏移量,size:类似于limit

返回参数

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": 0.39713606,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "HqGB7n8BI3mVVhLujEv6",
                "_score": 0.39713606,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "H6GI7n8BI3mVVhLuwUtc",
                "_score": 0.39713606,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "55",
                "_score": 0.39713606,
                "_source": {
                    "title": "小米手机_55",
                    "category": "米小",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            }
        ]
    }
}
3.2.5.3 查询排序

要求所有文档按照加个price降序

{{es_basic}}/shopping/_search

Body参数

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

返回参数

{
    "took": 24,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 6,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "99",
                "_score": null,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 8888.00
                },
                "sort": [
                    8888.0
                ]
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "HqGB7n8BI3mVVhLujEv6",
                "_score": null,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                },
                "sort": [
                    3999.0
                ]
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "H6GI7n8BI3mVVhLuwUtc",
                "_score": null,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                },
                "sort": [
                    3999.0
                ]
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "55",
                "_score": null,
                "_source": {
                    "title": "小米手机_55",
                    "category": "米小",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                },
                "sort": [
                    3999.0
                ]
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "2",
                "_score": null,
                "_source": {
                    "title": "小米手机_4",
                    "category": "小米_4",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 5
                },
                "sort": [
                    5.0
                ]
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "1",
                "_score": null,
                "_source": {
                    "title": "小米手机_1",
                    "category": "小米_1",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 1
                },
                "sort": [
                    1.0
                ]
            }
        ]
    }
}
3.2.5.4 多条件查询

AND

查询category为小小米,价格为3999.00的文档

- GET {{es_basic}}/shopping/_search

Body参数

{
    "query":{
        "bool":{
            "must":[{
                "match":{
                    "category":"小小米"
                }
            },{
                "match":{
                    "price":"3999.00"
                }
            }
            ]
        }
    }
}

bool下面添加条件

返回参数

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 1.5957041,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "HqGB7n8BI3mVVhLujEv6",
                "_score": 1.5957041,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "H6GI7n8BI3mVVhLuwUtc",
                "_score": 1.5957041,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "55",
                "_score": 1.5957041,
                "_source": {
                    "title": "小米手机_55",
                    "category": "米小",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            }
        ]
    }
}

可以看出我们查询的是 “category”:“小小米”,但实际上,只要有包含"小","米"的文档都被匹配出来了,这就是分词的效果,后续介绍

OR + 范围查询

查询category为虾米,或者是华为手机,价格大于3999.00的文档

- GET {{es_basic}}/shopping/_search

Body参数

{
    "query":{
        "bool":{
            "should":[{
                "match":{
                    "category":"虾米"
                }
            },{
                "match":{
                    "category":"华为"
                }
            }],
            "filter":{
            "range":{
                "price":{
                    "gt":3333
                }
            }
        }
        }
    }
}

返回参数

{
    "took": 16,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4,
            "relation": "eq"
        },
        "max_score": 3.902842,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "99",
                "_score": 3.902842,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 8888.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "HqGB7n8BI3mVVhLujEv6",
                "_score": 0.19856803,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "H6GI7n8BI3mVVhLuwUtc",
                "_score": 0.19856803,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "55",
                "_score": 0.19856803,
                "_source": {
                    "title": "小米手机_55",
                    "category": "米小",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            }
        ]
    }
}
3.2.5.5 全文检索

查询所有匹配"category":"小华"的文档

{{es_basic}}/shopping/_search

Body参数

{
    "query":{
        "match":{
            "category":"小华"
        }
    }
}

返回结果

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 6,
            "relation": "eq"
        },
        "max_score": 1.951421,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "99",
                "_score": 1.951421,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 8888.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "HqGB7n8BI3mVVhLujEv6",
                "_score": 0.19856803,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "H6GI7n8BI3mVVhLuwUtc",
                "_score": 0.19856803,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "55",
                "_score": 0.19856803,
                "_source": {
                    "title": "小米手机_55",
                    "category": "米小",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.16853255,
                "_source": {
                    "title": "小米手机_1",
                    "category": "小米_1",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 1
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "2",
                "_score": 0.16853255,
                "_source": {
                    "title": "小米手机_4",
                    "category": "小米_4",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 5
                }
            }
        ]
    }
}

该检索的方式,可以检索出所有含有"小","华"文字的文档,如果要完全匹配,见如下

3.2.5.6 完全匹配

查询完全匹配"为"的 文档

-GET {{es_basic}}/shopping/_search

Body参数

{
    "query":{
        "match_phrase":{
            "category":"为"
        }
    }
}

match_phrase 字段表示完全匹配,即只匹配文档category包含"为"的所有文档,如果是匹配"小华",则是匹配文档category包含"小华"的所有文档

返回参数

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.951421,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "99",
                "_score": 1.951421,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 8888.00
                }
            }
        ]
    }
}
3.2.5.7 高亮字段

如果我们想要在返回的字段中高亮某些字段

-GET {{es_basic}}/shopping/_search

Body参数

{
    "query":{
        "match_phrase":{
            "category":"为"
        }
    },
    "highlight":{
        "fields":{
            "category":{}   //高亮category字段
        }
    }
}

返回参数

{
    "took": 82,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.951421,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "99",
                "_score": 1.951421,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 8888.00
                },
                "highlight": {
                    "category": [
                        "华<em>为</em>"
                    ]
                }
            }
        ]
    }
}
3.2.5.8 分组查询

类似于MySQL的分组group by查询,我们将shopping索引下的price字段分组

-GET {{es_basic}}/shopping/_search

Body参数

{
    "aggs":{               //聚合操作
        "price_group":{    //名字可以任意取
            "terms":{      //分组
                "field":"price"   //分组字段
            }
        }
    }
}

返回结果

{
    "took": 22,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 6,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "HqGB7n8BI3mVVhLujEv6",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "H6GI7n8BI3mVVhLuwUtc",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机_1",
                    "category": "小米_1",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 1
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机_4",
                    "category": "小米_4",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 5
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "99",
                "_score": 1.0,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 8888.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "55",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机_55",
                    "category": "米小",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            }
        ]
    },
    "aggregations": {
        "price_group": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 3999.0,
                    "doc_count": 3
                },
                {
                    "key": 1.0,
                    "doc_count": 1
                },
                {
                    "key": 5.0,
                    "doc_count": 1
                },
                {
                    "key": 8888.0,
                    "doc_count": 1
                }
            ]
        }
    }
}

price_group就是我们分组后的一个结果集,如果想要在分组求和后不带上hits原始数据,可以在聚合操作同级,加上 “size”:0

3.2.5.9 聚合函数求平均值
{{es_basic}}/shopping/_search

Body参数

{
    "aggs":{
        "price_avg":{
            "avg":{
                "field":"price"
            }
        }
    },
    "size":0
}

返回结果

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 6,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "price_avg": {
            "value": 3481.8333333333335
        }
    }
}
3.3 映射关系

前面我们创建索引,类似在MySQL中创建了数据库,但是数据库里面的表结构,却是没有定义,就是有哪些字段,类型,长度,约束等,这个就称之为mapping(映射)。

  1. 先创建一个索引
-PUT {{es_basic}}/user

返回结果

{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "user"
}
  1. 创建字段的约束关系
-PUT {{es_basic}}/user/_mapping

Body参数

{
    "properties":{
        "name":{
            "type":"text",
            "index":true
        },
        "sex":{
            "type":"keyword",
            "index":true
        },
        "tel":{
            "type":"keyword",
            "index":false
        }
    }
}

返回参数

{
    "acknowledged": true
}
  1. 增加一条数据
-PUT {{es_basic}}/user/_create/1001

Body参数

{
    "name":"LiuZy",
    "sex":"男",
    "tel":"10086"
}

返回结果

{
    "_index": "user",
    "_type": "_doc",
    "_id": "1001",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}
  1. 查询name里面带"L"的数据
- GET {{es_basic}}/user/_search

Body参数

{
    "query":{
        "match":{
            "name":"L"
        }
    }
}

返回参数

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 0,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    }
}

发现啥都没有,这是由于分词的问题,没有将L从LiuZy被分出去

重新PUT一条数据

{
    "name":"小米",
    "sex":"男的",
    "tel":"10086"
}

我们查询查询name里面带"小"的数据

Body参数

{
    "query":{
        "match":{
            "name":"小"
        }
    }
}

返回结果

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 0.43445712,
        "hits": [
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "1002",
                "_score": 0.43445712,
                "_source": {
                    "name": "小米",
                    "sex": "男",
                    "tel": "10086"
                }
            },
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "1004",
                "_score": 0.43445712,
                "_source": {
                    "name": "小米",
                    "sex": "男的",
                    "tel": "10086"
                }
            }
        ]
    }
}
  1. 查询sex里面带"男"的数据
-GET {{es_basic}}/user/_search

Body参数

{
    "query":{
        "match":{
            "sex":"男"
        }
    }
}

返回结果

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 0.4700036,
        "hits": [
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "1001",
                "_score": 0.4700036,
                "_source": {
                    "name": "LiuZy",
                    "sex": "男",
                    "tel": "10086"
                }
            },
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "1002",
                "_score": 0.4700036,
                "_source": {
                    "name": "小米",
                    "sex": "男",
                    "tel": "10086"
                }
            }
        ]
    }
}

此时没有匹配出"男的", 由于sex是 “type”:“keyword”,类型,所以需要精确匹配才能得出数据!

  1. 查询sex里面带"男"的数据
-GET {{es_basic}}/user/_search

Body参数

{
    "query":{
        "match":{
            "tel":"100"
        }
    }
}

返回结果

{
    "error": {
        "root_cause": [
            {
                "type": "query_shard_exception",
                "reason": "failed to create query: Cannot search on field [tel] since it is not indexed.",
                "index_uuid": "bnVBtstJReGuhnlrYS4xCg",
                "index": "user"
            }
        ],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [
            {
                "shard": 0,
                "index": "user",
                "node": "8T45xezcTZ28X6Gfbs_SDw",
                "reason": {
                    "type": "query_shard_exception",
                    "reason": "failed to create query: Cannot search on field [tel] since it is not indexed.",
                    "index_uuid": "bnVBtstJReGuhnlrYS4xCg",
                    "index": "user",
                    "caused_by": {
                        "type": "illegal_argument_exception",
                        "reason": "Cannot search on field [tel] since it is not indexed."
                    }
                }
            }
        ]
    },
    "status": 400
}

查询报错的原因是 sex的索引 “index”:false,表示不能被查询


后续学习使用JavaAPI操作以上数据,待更新

参考和学习

CSDN:https://blog.csdn.net/u011863024/article/details/115721328
B站:https://www.bilibili.com/video/BV1hh411D7sb?p=17

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐