一、elasticsearch服务,节点,索引状态查询
1. 查询集群健康状况
GET /_cat/health?v
2. 查询节点信息
GET /_cat/nodes?v
3. 查询索引信息
GET /_cat/indices?v
二、索引相关操作
1. 创建索引,使用默认配置
PUT /test
默认信息如下,如果没有向索引中插入数据,mappings为空
2. 创建索引,并设置配置
#设置副本数和分片数
PUT /test
{
"settings": {
"number_of_replicas": 2,
"number_of_shards": 2
}
}
3. 创建索引,配置mapping信息,这里指定了索引中的字段信息,并设置类型和分词器,只有text类型的数据会被分词,其他的不会分词。index表示是否可以被索引,store表示是否存储,analyzer表示指定分词器,search_analyzer表示搜索的时候使用的分词器
PUT /test
{
"mappings": {
"properties": {
"id": {
"type": "long",
"index": true,
"store": true
},
"name":{
"type": "text",
"analyzer": "ik_smart"
},
"age":{
"type": "integer"
},
"sex":{
"type": "keyword"
},
"address":{
"type": "text",
"analyzer": "ik_smart",
"search_analyzer": "ik_smart"
}
}
}
}
4. 查看索引信息
GET /test
5. 查看索引settings
GET /test/_settings
6. 查看索引mappings
GET /test/_mapping
7. 删除索引
DELETE /test
8. 对已存在的mapping映射进行修改
#将现在的索引导入到新索引中
POST _reindex
{
"source": {
"index": "goods"
},
"dest": {
"index": "goods_new"
}
}
#删除旧索引
DELETE /goods
#为新索引起别名
PUT /goods_new/_alias/goods
#用旧的索引名可以进行搜索
GET /goods/_search
{
"query": {
"match_all": {}
}
}
三、文档的相关操作
1. PUT方式插入文档(带id),带id多次执行相当于update,每次_version这个字段会增长1
PUT /test/_doc/1
{
"name":"张三",
"age":21,
"address":"湖北武汉光谷步行街"
}
2. POST方式插入文档(不带id),会自动生成id, "_id" : "SryEF4MBkmasSwCPbBDo",
POST /test/_doc
{
"name":"李四",
"age":31,
"address":"深圳南山区生态科技园"
}
3. POST 方式插入文档(带id),执行多次也是update操作,每次_version这个字段会增长1
POST /test/_doc/2
{
"name":"王五",
"age":51,
"address":"江苏南京"
}
4. 文档更新,除了 PUT 和POST 带id操作,还可以用update
POST /test/_update/1
{
"doc": {
"name":"张三三"
}
}
5. 根据id查询文档信息
GET /test/_doc/1
6. 删除文档,注意:删除文档后,再使用同样的id,PUT
DELETE /test/_doc/1
7. 指定mappings的索引,插入不存在的字段会更新mappings,这里插入了gender,city
POST /test/_doc
{
"name":"Jack",
"age":22,
"address":"上海浦东",
"gender":"男",
"city":"上海"
}
8. 批量操作创建
POST /test/_bulk
{"create":{"_id":"1"}}
{"name":"批量1","age":121,"address":"地址1"}
{"create":{"_id":"2"}}
{"name":"批量2","age":122,"address":"地址2"}
{"create":{"_id":"3"}}
{"name":"批量3","age":123,"address":"地址3"}
9. 批量操作更新
POST /test/_bulk
{"update":{"_id":"1"}}
{"doc":{"name":"更新批量1"}}
{"update":{"_id":"2"}}
{"doc":{"name":"更新批量2","age":12}}
10. 批量操作删除
POST /test/_bulk
{"delete":{"_id":"1"}}
{"delete":{"_id":"2"}}
11. 批量组合操作
POST /test/_bulk
{"create":{"_id":"4"}}
{"name":"综合批量操作","age":34}
{"update":{"_id":3}}
{"doc":{"name":"综合操作更新"}}
{"delete":{"_id":"JbzUFoMBkmasSwCPFxAT"}}
四、分词器使用
GET /_analyze
{
"text": "你好啊 中国!",
"analyzer": "ik_smart"
}
GET /_analyze
{
"text": "你好啊 中国!",
"analyzer": "ik_max_word"
}
GET /_analyze
{
"text": "你好啊 中国!",
"analyzer": "standard"
}
GET /_analyze
{
"text": "你好啊 中国!",
"analyzer": "simple"
}
# 根据空格分词
GET /_analyze
{
"text": "你好啊 中国!",
"analyzer": "whitespace"
}
五、搜索(DSL语言高级查询)
索引和数据的准备
PUT /goods
{
"mappings": {
"properties": {
"skuId":{
"type": "long",
"index": true,
"store": true
},
"spuId":{
"type": "long"
},
"skuTitle":{
"type": "text",
"analyzer": "ik_smart"
},
"skuPrice":{
"type": "double"
},
"saleCount":{
"type": "long"
},
"hasStock":{
"type": "boolean"
},
"brandId":{
"type": "long"
},
"categoryId":{
"type": "long"
},
"brandName":{
"type": "keyword",
"index": true,
"store": true
},
"catagoryName":{
"type": "keyword"
},
"attrs":{
"type": "nested",
"include_in_parent":true,
"properties": {
"attrId":{
"type":"long"
},
"attrValue":{
"type":"text"
},
"attrName":{
"type":"text",
"analyzer": "standard"
}
}
}
}
}
}
POST /goods/_bulk
{"create":{"_id":2}}
{ "skuId":1002, "spuId":10002, "skuTitle":"HUAWEI/华为手机畅享50 Pro", "skuPrice":1619, "saleCount":6245, "hasStock": true, "brandId":102,"brandName":"华为", "categoryId":11, "catagoryName":"手机数码", "attrs":[ { "attrId":1, "attrName":"屏幕尺寸", "attrValue":"5.2寸" },{ "attrId":2, "attrName":"相机像素", "attrValue":"800" },{ "attrId":3,"attrName":"电池容量", "attrValue":"4000mAh"},{ "attrId":4, "attrName":"运行内存", "attrValue":"8G" },{ "attrId":5, "attrName":"物理内存", "attrValue":"128G" }]}
{"create":{"_id":3}}
{ "skuId":1002, "spuId":10003, "skuTitle":"HUAWEI/华为手机畅享50 Pro", "skuPrice":1819, "saleCount":4245, "hasStock": true, "brandId":102,"brandName":"华为", "categoryId":11, "catagoryName":"手机数码", "attrs":[ { "attrId":1, "attrName":"屏幕尺寸", "attrValue":"6.2寸" },{ "attrId":2, "attrName":"相机像素", "attrValue":"800" },{ "attrId":3,"attrName":"电池容量", "attrValue":"4000mAh"},{ "attrId":4, "attrName":"运行内存", "attrValue":"8G" },{ "attrId":5, "attrName":"物理内存", "attrValue":"256G" }]}
{"create":{"_id":4}}
{ "skuId":1002, "spuId":10004, "skuTitle":"HUAWEI/华为手机畅享50 Pro", "skuPrice":2019, "saleCount":7285, "hasStock": true, "brandId":102,"brandName":"华为", "categoryId":11, "catagoryName":"手机数码", "attrs":[ { "attrId":1, "attrName":"屏幕尺寸", "attrValue":"6.2寸" },{ "attrId":2, "attrName":"相机像素", "attrValue":"1200" },{ "attrId":3,"attrName":"电池容量", "attrValue":"5000mAh"},{ "attrId":4, "attrName":"运行内存", "attrValue":"16G" },{ "attrId":5, "attrName":"物理内存", "attrValue":"512G" }]}
{"create":{"_id":5}}
{ "skuId":1001, "spuId":10005, "skuTitle":"小米红米手机Redmi 10A 5000mAh大电量大屏", "skuPrice":899, "saleCount":99899, "hasStock": true, "brandId":101,"brandName":"小米", "categoryId":11, "catagoryName":"手机数码", "attrs":[ { "attrId":1, "attrName":"屏幕尺寸", "attrValue":"5.2寸" },{ "attrId":2, "attrName":"相机像素", "attrValue":"800" },{ "attrId":3,"attrName":"电池容量", "attrValue":"5000mAh"},{ "attrId":4, "attrName":"运行内存", "attrValue":"16G" },{ "attrId":5, "attrName":"物理内存", "attrValue":"128G" }]}
{"create":{"_id":6}}
{ "skuId":1001, "spuId":10006, "skuTitle":"小米红米手机Redmi 10A 5000mAh大电量大屏", "skuPrice":1299, "saleCount":10202, "hasStock": true, "brandId":101,"brandName":"小米", "categoryId":11, "catagoryName":"手机数码", "attrs":[ { "attrId":1, "attrName":"屏幕尺寸", "attrValue":"5.2寸" },{ "attrId":2, "attrName":"相机像素", "attrValue":"1200" },{ "attrId":3,"attrName":"电池容量", "attrValue":"5000mAh"},{ "attrId":4, "attrName":"运行内存", "attrValue":"16G" },{ "attrId":5, "attrName":"物理内存", "attrValue":"256G" }]}
{"create":{"_id":7}}
{ "skuId":1001, "spuId":10007, "skuTitle":"小米红米手机Redmi 10A 5000mAh大电量大屏", "skuPrice":1419, "saleCount":4534, "hasStock": true, "brandId":101,"brandName":"小米", "categoryId":11, "catagoryName":"手机数码", "attrs":[ { "attrId":1, "attrName":"屏幕尺寸", "attrValue":"5.2寸" },{ "attrId":2, "attrName":"相机像素", "attrValue":"1200" },{ "attrId":3,"attrName":"电池容量", "attrValue":"5000mAh"},{ "attrId":4, "attrName":"运行内存", "attrValue":"16G" },{ "attrId":5, "attrName":"物理内存", "attrValue":"512G" }]}
{"create":{"_id":8}}
{ "skuId":1003, "spuId":10008, "skuTitle":"OPPO A32大电池大内存", "skuPrice":799, "saleCount":2245, "hasStock": true, "brandId":103,"brandName":"OPPO", "categoryId":11, "catagoryName":"手机数码", "attrs":[ { "attrId":1, "attrName":"屏幕尺寸", "attrValue":"5.2寸" },{ "attrId":2, "attrName":"相机像素", "attrValue":"500" },{ "attrId":3,"attrName":"电池容量", "attrValue":"3000mAh"},{ "attrId":4, "attrName":"运行内存", "attrValue":"4G" },{ "attrId":5, "attrName":"物理内存", "attrValue":"128G" }]}
{"create":{"_id":9}}
{ "skuId":1003, "spuId":10009, "skuTitle":"OPPO A32大电池大内存", "skuPrice":1299, "saleCount":7285, "hasStock": true, "brandId":103,"brandName":"OPPO", "categoryId":11, "catagoryName":"手机数码", "attrs":[ { "attrId":1, "attrName":"屏幕尺寸", "attrValue":"5.2寸" },{ "attrId":2, "attrName":"相机像素", "attrValue":"500" },{ "attrId":3,"attrName":"电池容量", "attrValue":"3000mAh"},{ "attrId":4, "attrName":"运行内存", "attrValue":"6G" },{ "attrId":5, "attrName":"物理内存", "attrValue":"128G" }]}
{"create":{"_id":10}}
{ "skuId":1003, "spuId":10010, "skuTitle":"OPPO A32大电池大内存", "skuPrice":1389, "saleCount":6445, "hasStock": true, "brandId":103,"brandName":"OPPO", "categoryId":11, "catagoryName":"手机数码", "attrs":[ { "attrId":1, "attrName":"屏幕尺寸", "attrValue":"5.2寸" },{ "attrId":2, "attrName":"相机像素", "attrValue":"800" },{ "attrId":3,"attrName":"电池容量", "attrValue":"3000mAh"},{ "attrId":4, "attrName":"运行内存", "attrValue":"8G" },{ "attrId":5, "attrName":"物理内存", "attrValue":"128G" }]}
{"create":{"_id":11}}
{ "skuId":1004, "spuId":10011, "skuTitle":"VIVO Y32t智能游戏大电池手机", "skuPrice":999, "saleCount":9999, "hasStock": true, "brandId":104,"brandName":"VIVO", "categoryId":11, "catagoryName":"手机数码", "attrs":[ { "attrId":1, "attrName":"屏幕尺寸", "attrValue":"6.1寸" },{ "attrId":2, "attrName":"相机像素", "attrValue":"800" },{ "attrId":3,"attrName":"电池容量", "attrValue":"8000mAh"},{ "attrId":4, "attrName":"运行内存", "attrValue":"8G" },{ "attrId":5, "attrName":"物理内存", "attrValue":"128G" }]}
{"create":{"_id":12}}
{ "skuId":1004, "spuId":10012, "skuTitle":"VIVO Y32t智能游戏大电池手机", "skuPrice":1399, "saleCount":25545, "hasStock": true, "brandId":104,"brandName":"VIVO", "categoryId":11, "catagoryName":"手机数码", "attrs":[ { "attrId":1, "attrName":"屏幕尺寸", "attrValue":"6.1寸" },{ "attrId":2, "attrName":"相机像素", "attrValue":"800" },{ "attrId":3,"attrName":"电池容量", "attrValue":"8000mAh"},{ "attrId":4, "attrName":"运行内存", "attrValue":"8G" },{ "attrId":5, "attrName":"物理内存", "attrValue":"512G" }]}
{"create":{"_id":13}}
{ "skuId":1004, "spuId":10013, "skuTitle":"VIVO Y32t智能游戏大电池手机", "skuPrice":1499, "saleCount":99223, "hasStock": true, "brandId":104,"brandName":"VIVO", "categoryId":11, "catagoryName":"手机数码", "attrs":[ { "attrId":1, "attrName":"屏幕尺寸", "attrValue":"6.1寸" },{ "attrId":2, "attrName":"相机像素", "attrValue":"800" },{ "attrId":3,"attrName":"电池容量", "attrValue":"8000mAh"},{ "attrId":4, "attrName":"运行内存", "attrValue":"8G" },{ "attrId":5, "attrName":"物理内存", "attrValue":"1T" }]}
{"create":{"_id":14}}
{ "skuId":1005, "spuId":10014, "skuTitle":"【24期免息/当天发货】Apple/苹果iPhone 13ProMax全网通5G手机苹果13ProMax", "skuPrice":9288, "saleCount":92745, "hasStock": true, "brandId":105,"brandName":"苹果iPhone", "categoryId":11, "catagoryName":"手机数码", "attrs":[ { "attrId":1, "attrName":"屏幕尺寸", "attrValue":"6.7寸" },{ "attrId":2, "attrName":"相机像素", "attrValue":"1200" },{ "attrId":3,"attrName":"电池容量", "attrValue":"3200mAh"},{ "attrId":4, "attrName":"运行内存", "attrValue":"6G" },{ "attrId":5, "attrName":"物理内存", "attrValue":"256G" }]}
{"create":{"_id":15}}
{ "skuId":1005, "spuId":10015, "skuTitle":"【24期免息/当天发货】Apple/苹果iPhone 13ProMax全网通5G手机苹果13ProMax", "skuPrice":10298, "saleCount":11245, "hasStock": true, "brandId":105,"brandName":"苹果iPhone", "categoryId":11, "catagoryName":"手机数码", "attrs":[ { "attrId":1, "attrName":"屏幕尺寸", "attrValue":"6.7寸" },{ "attrId":2, "attrName":"相机像素", "attrValue":"1200" },{ "attrId":3,"attrName":"电池容量", "attrValue":"3200mAh"},{ "attrId":4, "attrName":"运行内存", "attrValue":"6G" },{ "attrId":5, "attrName":"物理内存", "attrValue":"512G" }]}
{"create":{"_id":16}}
{ "skuId":1005, "spuId":10016, "skuTitle":"【24期免息/当天发货】Apple/苹果iPhone 13ProMax全网通5G手机苹果13ProMax", "skuPrice":11298, "saleCount":5624, "hasStock": true, "brandId":105,"brandName":"苹果iPhone", "categoryId":11, "catagoryName":"手机数码", "attrs":[ { "attrId":1, "attrName":"屏幕尺寸", "attrValue":"6.7寸" },{ "attrId":2, "attrName":"相机像素", "attrValue":"1200" },{ "attrId":3,"attrName":"电池容量", "attrValue":"3200mAh"},{ "attrId":4, "attrName":"运行内存", "attrValue":"6G" },{ "attrId":5, "attrName":"物理内存", "attrValue":"1T" }]}
1. 根据id查询文档信息
GET /goods/_doc/1
2. 无条件查询所有文档信息
GET /goods/_search
{
"query": {
"match_all": {}
}
}
3. 设置需要返回的字段
GET /goods/_search
{
"query": {
"match_all": {}
},
"_source": ["catagoryName","brandName","skuPrice","skuTitle"]
}
4. 分页,设置返回的数据条数,从第5个开始,返回2个数据
GET /goods/_search
{
"query": {
"match_all": {}
},
"from": 5,
"size": 2
}
5. 排序,sort 根据 skuPrice这个字段倒序
GET /goods/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"skuPrice": {
"order": "desc"
}
}
]
}
6. match 条件查询,通过match关键词模糊匹配条件内容。例如,这里搜索条件为“苹果手机”,会将苹果手机分词为"苹果"和"手机"两个词进行搜索。
GET /goods/_search
{
"query": {
"match": {
"skuTitle": "苹果手机"
}
}
}
7. prefix 前缀查询,需要使用.keyword,不然查不出来
GET /goods/_search
{
"query": {
"prefix": {
"skuTitle.keyword": {
"value": "手机"
}
}
}
}
8. term 精确查询,对查询条件不分词, 这里搜索“OPPO”,可以得到结果,但是如果是搜索“OPPO 小米”,就没办法得到结果,因为他不会进行分词。
GET /goods/_search
{
"query": {
"term": {
"brandName": {
"value": "OPPO"
}
}
}
}
9. match_phrase,匹配短语,对查询条件不分词,和term有点像,但是term只能匹配keyword
GET /goods/_search
{
"query": {
"match_phrase": {
"skuTitle": "苹果手机"
}
}
}
10. multi_match,多个字段匹配同一个搜索条件,这里的brandName或者skuTitle中包含"华为手机"的分词进行搜索。
GET /goods/_search
{
"query": {
"multi_match": {
"query": "华为手机",
"fields": ["brandName","skuTitle"]
}
}
}
11. terms 匹配多个值,这里的brandName进行匹配“华为手机”,“小米”,由于不会进行分词,所以brandName为“华为”的不会搜索出来。
GET /goods/_search
{
"query": {
"terms": {
"brandName": [
"华为手机",
"小米"
]
}
}
}
12. range 范围查询,查询skuPrice价格在[1000,2000] 范围内的,gte 大于等于 gt 大于 lt小于 lte小于等于。
GET /goods/_search
{
"query": {
"range": {
"skuPrice": {
"gte": 1000,
"lte": 2000
}
}
}
}
13. query_string
GET /goods/_search
{
"query": {
"query_string": {
"default_field": "brandName",
"query": "小米 OR 华为"
}
}
}
14. ids 查询,根据文档id进行查询
GET /goods/_search
{
"query": {
"ids": {
"values": [1,2,3]
}
}
}
15. exists 查询特定字段有值的文档
#先更新,新加入一个fee字段
POST /goods/_update/5
{
"doc": {
"skuTitle":"手机小米红米手机Redmi 10A 5000mAh大电量大屏",
"fee": 10.0
}
}
# 根据fee字段是否有值查询,这里只能查到id为5的刚刚更新的那条记录
GET /goods/_search
{
"query": {
"exists": {
"field": "fee"
}
}
}
16. 组合条件查询 bool,各个条件之间有and,or或者not 的关系。must表示各个条件都要满足,must_not表示不满足所有条件,should表示满足其中一个条件即可,filter表示过滤掉某些条件,不计算相关度评分。
其中must、filter、must_not和should中的子条件是通过term、terms、match、range、ids、exists等查询为参数。
1)使用must,查询skuTitle中包含"小米"并且skuPrice价格在[999,2000] 范围内的
GET /goods/_search
{
"query": {
"bool": {
"must": [
{
"range": {
"skuPrice": {
"gte": 999,
"lte": 2000
}
}
},
{
"match": {
"skuTitle": "小米"
}
}
]
}
}
}
2)must_not , 查询brandName不为“苹果iPhone”,并且价格不小于等于2000
GET /goods/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"brandName": "苹果iPhone"
}
},
{
"range": {
"skuPrice": {
"lte": 2000
}
}
}
]
}
}
}
3)should, 这里就查询skuTitle中包含“华为”,或者skuPrice大于等于8000
GET /goods/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"skuTitle": "华为"
}
},
{
"range": {
"skuPrice": {
"gte": 8000
}
}
}
]
}
}
}
4)filter,过滤结果中skuTitle包含“小米”并且skuPrice小于等于1000,不计算相关性得分
GET /goods/_search
{
"query": {
"bool": {
"filter": [
{
"match": {
"skuTitle": "小米"
}
},
{
"range": {
"skuPrice": {
"lte": 1000
}
}
}
]
}
}
}
5)组合条件查询
GET /goods/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"brandName": "华为"
}
}
],
"must": [
{
"range": {
"saleCount": {
"gte": 5000
}
}
}
],
"filter": [
{
"range": {
"skuPrice": {
"gte": 1000,
"lte": 2000
}
}
}
]
}
}
}
6) 更为复杂的查询,此时查询的是skuPrice小于等于2000,并且 brandName为华为或者saleCount大于等于10000的数据
GET /goods/_search
{
"query": {
"bool": {
"must": [
{
"range": {
"skuPrice": {
"lte": 2000
}
}
},
{
"bool": {
"should": [
{
"match": {
"brandName": "华为"
}
},
{
"range": {
"saleCount": {
"gte": 10000
}
}
}
]
}
}
]
}
}
}
6)fuzzy 模糊搜索,fuzziness:1,表示搜索的value中有一个字可以是不一致的,这里就可以搜索出brandName为“小米”的数据,需要注意的是,这里的字段分词方式要为keyword,不然搜索不出来。
GET /goods/_search
{
"query": {
"fuzzy": {
"brandName": {
"value": "小迷",
"fuzziness": 1
}
}
}
}
7)nested 查询,前提是需要把字段类型设置为nested
#带nested查询
GET /goods/_search
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "attrs",
"query": {
"bool": {
"must": [
{
"match": {
"attrs.attrValue": "8g"
}
},
{
"match": {
"attrs.attrName": "运行内存"
}
}
]
}
}
}
}
]
}
}
}
#不带nested查询
GET /goods/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"attrs.attrValue": "8g"
}
},
{
"match": {
"attrs.attrName": "运行内存"
}
}
]
}
}
}
六、总结
elasticsearch 的基本操作介绍结束了,下一篇将介绍es的聚合操作。