vlambda博客
学习文章列表

基于go的elasticsearch简易教程(1)

elasticsearch7入门指南

安装

  • docker安装

 
   
   
 
  1. docker pull elasticsearch:7.14.0

  2. docker run -d -p 9310:9300 -p 9210:9200 -e "discovery.type=single-node" elasticsearch:7.14.0

  3. docker ps

  • 测试

 
   
   
 
  1. ~ curl http://127.0.0.1:9210

  2. {

  3. "name" : "4888120ad527",

  4. "cluster_name" : "docker-cluster",

  5. "cluster_uuid" : "Z572AGx6T7K-WKRCYBcRWA",

  6. "version" : {

  7. "number" : "7.14.0",

  8. "build_flavor" : "default",

  9. "build_type" : "docker",

  10. "build_hash" : "dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1",

  11. "build_date" : "2021-07-29T20:49:32.864135063Z",

  12. "build_snapshot" : false,

  13. "lucene_version" : "8.9.0",

  14. "minimum_wire_compatibility_version" : "6.8.0",

  15. "minimum_index_compatibility_version" : "6.0.0-beta1"

  16. },

  17. "tagline" : "You Know, for Search"

  18. }

  • 安装es的客户端工具kibana

 
   
   
 
  1. docker pull kibana:7.14.0

  2. // 本地配置映射到容器

  3. docker run -d --name kibana -v /Users/liang.zhang02/adjutantz/es/kibana/kibana.yml:/usr/share/kibana/config/kibana.yml -p 5611:5601 kibana:7.14.0

  • 访问kibana

http://127.0.0.1:5611

索引

  • 索引常见操作

 
   
   
 
  1. # 查询索引

  2. GET /_cat/indices?v


  3. # 创建索引

  4. PUT /products


  5. # 创建副本为0的索引

  6. PUT /orders

  7. {

  8. "settings": {

  9. "number_of_shards": 1,

  10. "number_of_replicas": 0

  11. }

  12. }


  13. # 删除索引

  14. DELETE /products

映射

  • es常见数据类型

名称 类型
字符串 keyword(不分词),text(分词)
整数 integer
小数 float,double(精度范围更广)
布尔 boolean
日期 date
  • 常见操作

 
   
   
 
  1. # 创建

  2. PUT /products

  3. {

  4. "settings": {

  5. "number_of_replicas": 0,

  6. "number_of_shards": 1

  7. },

  8. "mappings": {

  9. "properties": {

  10. "id":{

  11. "type": "integer"

  12. },

  13. "title":{

  14. "type": "keyword"

  15. },

  16. "description":{

  17. "type": "text"

  18. },

  19. "price":{

  20. "type": "float"

  21. },

  22. "created_at":{

  23. "type": "date"

  24. }

  25. }

  26. }

  27. }


  28. # 查看

  29. GET /products/_mapping

文档

  • 常见操作

 
   
   
 
  1. # 新增文档,指定ID

  2. POST /products/_doc/1

  3. {

  4. "id": 1,

  5. "title": "大麦茶",

  6. "description": "好喝的大麦茶",

  7. "price": 4.50,

  8. "created_at": "2022-10-02"

  9. }


  10. # 新增文档,自动生成ID【C0_whn8BeuVBNeP_QdhB】

  11. POST /products/_doc

  12. {

  13. "id": 2,

  14. "title": "乌龙茶",

  15. "description": "好喝的乌龙茶",

  16. "price": 4.50,

  17. "created_at": "2022-10-02"

  18. }


  19. # 根据ID查询文档

  20. GET /products/_doc/1


  21. # 根据ID查询文档

  22. GET /products/_doc/C0_whn8BeuVBNeP_QdhB


  23. # 删除文档

  24. DELETE /products/_doc/1


  25. # 更新文档【先删除,再添加】

  26. PUT /products/_doc/C0_whn8BeuVBNeP_QdhB

  27. {

  28. "title": "乌龙茶场馆"

  29. }


  30. # 更新指定字段

  31. POST /products/_doc/C0_whn8BeuVBNeP_QdhB/_update

  32. {

  33. "doc":{

  34. "id": 2,

  35. "title": "乌龙茶场馆",

  36. "description": "好喝的乌龙茶2",

  37. "price": 5.50

  38. }

  39. }


  40. # 批量添加文档

  41. POST /products/_doc/_bulk

  42. {"index":{"_id":"5"}}

  43. {"id":5, "title":"5-title","description":"5-desc","price":8.88}

  44. {"index":{"_id":"6"}}

  45. {"id":6, "title":"6-title","description":"6-desc","price":9.99}


  46. # 批量增删查改,注意update需要"doc"

  47. POST /products/_doc/_bulk

  48. {"index":{"_id":7}}

  49. {"id":7, "title":"7-title","description":"7-desc","price":77.77}

  50. {"update":{"_id":5}}

  51. {"doc":{"title":"5-title-update"}}

  52. {"delete":{"_id":6}}

DSL(Domain Specific Language)

  • 查询文档所有match_all

 
   
   
 
  1. # 查询所有

  2. GET /products/_doc/_search

  3. {

  4. "query":{

  5. "match_all":{}

  6. }

  7. }


  8. # 简化,比较常用的方式

  9. GET /products/_search

  10. {

  11. "query": {

  12. "match_all": {}

  13. }

  14. }

  • 条件搜索term

 
   
   
 
  1. # term 匹配

  2. # keyword:不分词,全部匹配

  3. # text:分词:中文单个字,英文单词

  4. GET /products/_search

  5. {

  6. "query": {

  7. "term": {

  8. "description": {

  9. "value": "麦"

  10. }

  11. }

  12. }

  13. }


  14. # keyword

  15. GET /products/_search

  16. {

  17. "query": {

  18. "term": {

  19. "title": {

  20. "value": "大麦茶"

  21. }

  22. }

  23. }

  24. }


  25. # integer

  26. GET /products/_search

  27. {

  28. "query": {

  29. "term": {

  30. "id": {

  31. "value": "17"

  32. }

  33. }

  34. }

  35. }


  36. # double

  37. GET /products/_search

  38. {

  39. "query": {

  40. "term": {

  41. "price": {

  42. "value": "8.99"

  43. }

  44. }

  45. }

  46. }


  47. ### 总结

  48. # 1. elasticsearch中除了text会分词,其余的均不分词,即需要全部匹配

  49. # 2. elasticsearch标准库中英文是以单词分词,中文是以单个字分词

  • 范围查询range

 
   
   
 
  1. GET /products/_search

  2. {

  3. "query": {

  4. "range": {

  5. "price": {

  6. "gt": 5,

  7. "lte": 10

  8. }

  9. }

  10. }

  11. }

  • 前缀查询prefix

 
   
   
 
  1. # 前缀查询,keyword不分词,可实现类似于like {{前缀}}%

  2. GET /products/_search

  3. {

  4. "query": {

  5. "prefix": {

  6. "title": {

  7. "value": "乌龙"

  8. }

  9. }

  10. }

  11. }


  12. # 前缀查询,text分词,在标准库是单个中文字分词

  13. GET /products/_search

  14. {

  15. "query": {

  16. "prefix": {

  17. "description": {

  18. "value": "好"

  19. }

  20. }

  21. }

  22. }

  • 通配符查询wildcard

 
   
   
 
  1. # 通配符查询

  2. # ?代表匹配一个,*代表匹配多个

  3. GET /products/_search

  4. {

  5. "query": {

  6. "wildcard": {

  7. "description": {

  8. "value": "appl?"

  9. }

  10. }

  11. }

  12. }


  13. GET /products/_search

  14. {

  15. "query": {

  16. "wildcard": {

  17. "description": {

  18. "value": "ap*"

  19. }

  20. }

  21. }

  22. }

  • 通过id查询

 
   
   
 
  1. # ids查询

  2. GET /products/_search

  3. {

  4. "query": {

  5. "ids": {

  6. "values": [17,7,5]

  7. }

  8. }

  9. }

  • 相关性查询

 
   
   
 
  1. # ids查询

  2. GET /products/_search

  3. {

  4. "query": {

  5. "ids": {

  6. "values": [17,7,5]

  7. }

  8. }

  9. }