当前位置:   article > 正文

elasticsearch 索引文档相关操作_elasticsearch查看索引内容

elasticsearch查看索引内容

一、elasticsearch服务,节点,索引状态查询

1. 查询集群健康状况

GET /_cat/health?v

2. 查询节点信息

GET /_cat/nodes?v

3. 查询索引信息

GET /_cat/indices?v

 二、索引相关操作

1. 创建索引,使用默认配置

PUT /test

默认信息如下,如果没有向索引中插入数据,mappings为空

 2. 创建索引,并设置配置

  1. #设置副本数和分片数
  2. PUT /test
  3. {
  4. "settings": {
  5. "number_of_replicas": 2,
  6. "number_of_shards": 2
  7. }
  8. }

 3. 创建索引,配置mapping信息,这里指定了索引中的字段信息,并设置类型和分词器,只有text类型的数据会被分词,其他的不会分词。index表示是否可以被索引,store表示是否存储,analyzer表示指定分词器,search_analyzer表示搜索的时候使用的分词器

  1. PUT /test
  2. {
  3. "mappings": {
  4. "properties": {
  5. "id": {
  6. "type": "long",
  7. "index": true,
  8. "store": true
  9. },
  10. "name":{
  11. "type": "text",
  12. "analyzer": "ik_smart"
  13. },
  14. "age":{
  15. "type": "integer"
  16. },
  17. "sex":{
  18. "type": "keyword"
  19. },
  20. "address":{
  21. "type": "text",
  22. "analyzer": "ik_smart",
  23. "search_analyzer": "ik_smart"
  24. }
  25. }
  26. }
  27. }

4. 查看索引信息

GET /test

5. 查看索引settings

GET /test/_settings

6. 查看索引mappings

GET /test/_mapping

 7. 删除索引

DELETE /test

8. 对已存在的mapping映射进行修改

  1. #将现在的索引导入到新索引中
  2. POST _reindex
  3. {
  4. "source": {
  5. "index": "goods"
  6. },
  7. "dest": {
  8. "index": "goods_new"
  9. }
  10. }
  11. #删除旧索引
  12. DELETE /goods
  13. #为新索引起别名
  14. PUT /goods_new/_alias/goods
  15. #用旧的索引名可以进行搜索
  16. GET /goods/_search
  17. {
  18. "query": {
  19. "match_all": {}
  20. }
  21. }

三、文档的相关操作

1. PUT方式插入文档(带id),带id多次执行相当于update,每次_version这个字段会增长1

  1. PUT /test/_doc/1
  2. {
  3. "name":"张三",
  4. "age":21,
  5. "address":"湖北武汉光谷步行街"
  6. }

2. POST方式插入文档(不带id),会自动生成id, "_id" : "SryEF4MBkmasSwCPbBDo",

  1. POST /test/_doc
  2. {
  3. "name":"李四",
  4. "age":31,
  5. "address":"深圳南山区生态科技园"
  6. }

3. POST 方式插入文档(带id),执行多次也是update操作,每次_version这个字段会增长1

  1. POST /test/_doc/2
  2. {
  3. "name":"王五",
  4. "age":51,
  5. "address":"江苏南京"
  6. }

4. 文档更新,除了 PUT 和POST 带id操作,还可以用update

  1. POST /test/_update/1
  2. {
  3. "doc": {
  4. "name":"张三三"
  5. }
  6. }

5. 根据id查询文档信息

GET /test/_doc/1

6. 删除文档,注意:删除文档后,再使用同样的id,PUT

DELETE /test/_doc/1

7. 指定mappings的索引,插入不存在的字段会更新mappings,这里插入了gender,city

  1. POST /test/_doc
  2. {
  3. "name":"Jack",
  4. "age":22,
  5. "address":"上海浦东",
  6. "gender":"男",
  7. "city":"上海"
  8. }

8. 批量操作创建 

  1. POST /test/_bulk
  2. {"create":{"_id":"1"}}
  3. {"name":"批量1","age":121,"address":"地址1"}
  4. {"create":{"_id":"2"}}
  5. {"name":"批量2","age":122,"address":"地址2"}
  6. {"create":{"_id":"3"}}
  7. {"name":"批量3","age":123,"address":"地址3"}

9. 批量操作更新

  1. POST /test/_bulk
  2. {"update":{"_id":"1"}}
  3. {"doc":{"name":"更新批量1"}}
  4. {"update":{"_id":"2"}}
  5. {"doc":{"name":"更新批量2","age":12}}

10. 批量操作删除

  1. POST /test/_bulk
  2. {"delete":{"_id":"1"}}
  3. {"delete":{"_id":"2"}}

11. 批量组合操作

  1. POST /test/_bulk
  2. {"create":{"_id":"4"}}
  3. {"name":"综合批量操作","age":34}
  4. {"update":{"_id":3}}
  5. {"doc":{"name":"综合操作更新"}}
  6. {"delete":{"_id":"JbzUFoMBkmasSwCPFxAT"}}

四、分词器使用

  1. GET /_analyze
  2. {
  3. "text": "你好啊 中国!",
  4. "analyzer": "ik_smart"
  5. }
  6. GET /_analyze
  7. {
  8. "text": "你好啊 中国!",
  9. "analyzer": "ik_max_word"
  10. }
  11. GET /_analyze
  12. {
  13. "text": "你好啊 中国!",
  14. "analyzer": "standard"
  15. }
  16. GET /_analyze
  17. {
  18. "text": "你好啊 中国!",
  19. "analyzer": "simple"
  20. }
  21. # 根据空格分词
  22. GET /_analyze
  23. {
  24. "text": "你好啊 中国!",
  25. "analyzer": "whitespace"
  26. }

五、搜索(DSL语言高级查询)

 索引和数据的准备

  1. PUT /goods
  2. {
  3. "mappings": {
  4. "properties": {
  5. "skuId":{
  6. "type": "long",
  7. "index": true,
  8. "store": true
  9. },
  10. "spuId":{
  11. "type": "long"
  12. },
  13. "skuTitle":{
  14. "type": "text",
  15. "analyzer": "ik_smart"
  16. },
  17. "skuPrice":{
  18. "type": "double"
  19. },
  20. "saleCount":{
  21. "type": "long"
  22. },
  23. "hasStock":{
  24. "type": "boolean"
  25. },
  26. "brandId":{
  27. "type": "long"
  28. },
  29. "categoryId":{
  30. "type": "long"
  31. },
  32. "brandName":{
  33. "type": "keyword",
  34. "index": true,
  35. "store": true
  36. },
  37. "catagoryName":{
  38. "type": "keyword"
  39. },
  40. "attrs":{
  41. "type": "nested",
  42. "include_in_parent":true,
  43. "properties": {
  44. "attrId":{
  45. "type":"long"
  46. },
  47. "attrValue":{
  48. "type":"text"
  49. },
  50. "attrName":{
  51. "type":"text",
  52. "analyzer": "standard"
  53. }
  54. }
  55. }
  56. }
  57. }
  58. }
  1. POST /goods/_bulk
  2. {"create":{"_id":2}}
  3. { "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" }]}
  4. {"create":{"_id":3}}
  5. { "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" }]}
  6. {"create":{"_id":4}}
  7. { "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" }]}
  8. {"create":{"_id":5}}
  9. { "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" }]}
  10. {"create":{"_id":6}}
  11. { "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" }]}
  12. {"create":{"_id":7}}
  13. { "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" }]}
  14. {"create":{"_id":8}}
  15. { "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" }]}
  16. {"create":{"_id":9}}
  17. { "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" }]}
  18. {"create":{"_id":10}}
  19. { "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" }]}
  20. {"create":{"_id":11}}
  21. { "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" }]}
  22. {"create":{"_id":12}}
  23. { "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" }]}
  24. {"create":{"_id":13}}
  25. { "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" }]}
  26. {"create":{"_id":14}}
  27. { "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" }]}
  28. {"create":{"_id":15}}
  29. { "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" }]}
  30. {"create":{"_id":16}}
  31. { "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. 无条件查询所有文档信息

  1. GET /goods/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. }
  6. }

3. 设置需要返回的字段

  1. GET /goods/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. },
  6. "_source": ["catagoryName","brandName","skuPrice","skuTitle"]
  7. }

4. 分页,设置返回的数据条数,从第5个开始,返回2个数据

  1. GET /goods/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. },
  6. "from": 5,
  7. "size": 2
  8. }

5. 排序,sort 根据 skuPrice这个字段倒序

  1. GET /goods/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. },
  6. "sort": [
  7. {
  8. "skuPrice": {
  9. "order": "desc"
  10. }
  11. }
  12. ]
  13. }

6. match 条件查询,通过match关键词模糊匹配条件内容。例如,这里搜索条件为“苹果手机”,会将苹果手机分词为"苹果"和"手机"两个词进行搜索。

  1. GET /goods/_search
  2. {
  3. "query": {
  4. "match": {
  5. "skuTitle": "苹果手机"
  6. }
  7. }
  8. }

7. prefix 前缀查询,需要使用.keyword,不然查不出来

  1. GET /goods/_search
  2. {
  3. "query": {
  4. "prefix": {
  5. "skuTitle.keyword": {
  6. "value": "手机"
  7. }
  8. }
  9. }
  10. }

8. term 精确查询,对查询条件不分词, 这里搜索“OPPO”,可以得到结果,但是如果是搜索“OPPO 小米”,就没办法得到结果,因为他不会进行分词。

  1. GET /goods/_search
  2. {
  3. "query": {
  4. "term": {
  5. "brandName": {
  6. "value": "OPPO"
  7. }
  8. }
  9. }
  10. }

9. match_phrase,匹配短语,对查询条件不分词,和term有点像,但是term只能匹配keyword

  1. GET /goods/_search
  2. {
  3. "query": {
  4. "match_phrase": {
  5. "skuTitle": "苹果手机"
  6. }
  7. }
  8. }

10. multi_match,多个字段匹配同一个搜索条件,这里的brandName或者skuTitle中包含"华为手机"的分词进行搜索。

  1. GET /goods/_search
  2. {
  3. "query": {
  4. "multi_match": {
  5. "query": "华为手机",
  6. "fields": ["brandName","skuTitle"]
  7. }
  8. }
  9. }

11. terms 匹配多个值,这里的brandName进行匹配“华为手机”,“小米”,由于不会进行分词,所以brandName为“华为”的不会搜索出来。

  1. GET /goods/_search
  2. {
  3. "query": {
  4. "terms": {
  5. "brandName": [
  6. "华为手机",
  7. "小米"
  8. ]
  9. }
  10. }
  11. }

12. range 范围查询,查询skuPrice价格在[1000,2000] 范围内的,gte 大于等于 gt 大于 lt小于 lte小于等于。

  1. GET /goods/_search
  2. {
  3. "query": {
  4. "range": {
  5. "skuPrice": {
  6. "gte": 1000,
  7. "lte": 2000
  8. }
  9. }
  10. }
  11. }

13. query_string

  1. GET /goods/_search
  2. {
  3. "query": {
  4. "query_string": {
  5. "default_field": "brandName",
  6. "query": "小米 OR 华为"
  7. }
  8. }
  9. }

14. ids 查询,根据文档id进行查询

  1. GET /goods/_search
  2. {
  3. "query": {
  4. "ids": {
  5. "values": [1,2,3]
  6. }
  7. }
  8. }

15. exists 查询特定字段有值的文档

  1. #先更新,新加入一个fee字段
  2. POST /goods/_update/5
  3. {
  4. "doc": {
  5. "skuTitle":"手机小米红米手机Redmi 10A 5000mAh大电量大屏",
  6. "fee": 10.0
  7. }
  8. }
  9. # 根据fee字段是否有值查询,这里只能查到id为5的刚刚更新的那条记录
  10. GET /goods/_search
  11. {
  12. "query": {
  13. "exists": {
  14. "field": "fee"
  15. }
  16. }
  17. }

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] 范围内的

  1. GET /goods/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": [
  6. {
  7. "range": {
  8. "skuPrice": {
  9. "gte": 999,
  10. "lte": 2000
  11. }
  12. }
  13. },
  14. {
  15. "match": {
  16. "skuTitle": "小米"
  17. }
  18. }
  19. ]
  20. }
  21. }
  22. }

2)must_not , 查询brandName不为“苹果iPhone”,并且价格不小于等于2000

  1. GET /goods/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must_not": [
  6. {
  7. "match": {
  8. "brandName": "苹果iPhone"
  9. }
  10. },
  11. {
  12. "range": {
  13. "skuPrice": {
  14. "lte": 2000
  15. }
  16. }
  17. }
  18. ]
  19. }
  20. }
  21. }

3)should, 这里就查询skuTitle中包含“华为”,或者skuPrice大于等于8000

  1. GET /goods/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "should": [
  6. {
  7. "match": {
  8. "skuTitle": "华为"
  9. }
  10. },
  11. {
  12. "range": {
  13. "skuPrice": {
  14. "gte": 8000
  15. }
  16. }
  17. }
  18. ]
  19. }
  20. }
  21. }

4)filter,过滤结果中skuTitle包含“小米”并且skuPrice小于等于1000,不计算相关性得分

  1. GET /goods/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "filter": [
  6. {
  7. "match": {
  8. "skuTitle": "小米"
  9. }
  10. },
  11. {
  12. "range": {
  13. "skuPrice": {
  14. "lte": 1000
  15. }
  16. }
  17. }
  18. ]
  19. }
  20. }
  21. }

5)组合条件查询

  1. GET /goods/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must_not": [
  6. {
  7. "match": {
  8. "brandName": "华为"
  9. }
  10. }
  11. ],
  12. "must": [
  13. {
  14. "range": {
  15. "saleCount": {
  16. "gte": 5000
  17. }
  18. }
  19. }
  20. ],
  21. "filter": [
  22. {
  23. "range": {
  24. "skuPrice": {
  25. "gte": 1000,
  26. "lte": 2000
  27. }
  28. }
  29. }
  30. ]
  31. }
  32. }
  33. }

6) 更为复杂的查询,此时查询的是skuPrice小于等于2000,并且 brandName为华为或者saleCount大于等于10000的数据

  1. GET /goods/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": [
  6. {
  7. "range": {
  8. "skuPrice": {
  9. "lte": 2000
  10. }
  11. }
  12. },
  13. {
  14. "bool": {
  15. "should": [
  16. {
  17. "match": {
  18. "brandName": "华为"
  19. }
  20. },
  21. {
  22. "range": {
  23. "saleCount": {
  24. "gte": 10000
  25. }
  26. }
  27. }
  28. ]
  29. }
  30. }
  31. ]
  32. }
  33. }
  34. }

6)fuzzy 模糊搜索,fuzziness:1,表示搜索的value中有一个字可以是不一致的,这里就可以搜索出brandName为“小米”的数据,需要注意的是,这里的字段分词方式要为keyword,不然搜索不出来。

  1. GET /goods/_search
  2. {
  3. "query": {
  4. "fuzzy": {
  5. "brandName": {
  6. "value": "小迷",
  7. "fuzziness": 1
  8. }
  9. }
  10. }
  11. }

7)nested 查询,前提是需要把字段类型设置为nested

  1. #带nested查询
  2. GET /goods/_search
  3. {
  4. "query": {
  5. "bool": {
  6. "must": [
  7. {
  8. "nested": {
  9. "path": "attrs",
  10. "query": {
  11. "bool": {
  12. "must": [
  13. {
  14. "match": {
  15. "attrs.attrValue": "8g"
  16. }
  17. },
  18. {
  19. "match": {
  20. "attrs.attrName": "运行内存"
  21. }
  22. }
  23. ]
  24. }
  25. }
  26. }
  27. }
  28. ]
  29. }
  30. }
  31. }
  32. #不带nested查询
  33. GET /goods/_search
  34. {
  35. "query": {
  36. "bool": {
  37. "must": [
  38. {
  39. "match": {
  40. "attrs.attrValue": "8g"
  41. }
  42. },
  43. {
  44. "match": {
  45. "attrs.attrName": "运行内存"
  46. }
  47. }
  48. ]
  49. }
  50. }
  51. }

六、总结

elasticsearch 的基本操作介绍结束了,下一篇将介绍es的聚合操作。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/503780
推荐阅读
相关标签
  

闽ICP备14008679号