安装

docker 安装 es

  1. #新建es的config配置⽂件夹
  2. mkdir -p /data/elasticsearch/config
  3. #新建es的data⽬录
  4. mkdir -p /data/elasticsearch/data
  5. #新建es的plugins⽬录
  6. mkdir -p /data/elasticsearch/plugins
  7. #给⽬录设置权限
  8. chmod 777 -R /data/elasticsearch
  9. #写⼊配置到elasticsearch.yml中, 下⾯的 > 表示覆盖的⽅式写⼊, >>表示追加的⽅式写⼊,但是要确
  10. echo "http.host: 0.0.0.0" >> /data/elasticsearch/config/elasticsearch.yml
  11. #安装es
  12. docker run --name elasticsearch -p 9200:9200 -p 9300:9300 \
  13. -e "discovery.type=single-node" \
  14. -e ES_JAVA_OPTS="-Xms128m -Xmx256m" \
  15. -v /data/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
  16. -v /data/elasticsearch/data:/usr/share/elasticsearch/data \
  17. -v /data/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
  18. -d elasticsearch:7.10.1

docker安装kibana

  1. docker run -d --name kibana -e ELASTICSEARCH_HOSTS="http://172.19.141.58:9200" -p 5601:5601 kibana:7.10.1

命令执行

mapping

  1. # 创建/修改 mapping
  2. PUT my_index
  3. {
  4. "mappings": {
  5. "properties": {
  6. "name": {
  7. "type": "text",
  8. "analyzer": "ik_smart"
  9. }
  10. }
  11. }
  12. }
  13. PUT my_index/_mapping
  14. {
  15. "properties": {
  16. "name": {
  17. "type": "text",
  18. "analyzer": "ik_smart"
  19. }
  20. }
  21. }

新增

  1. # 创建索引
  2. POST /users/_doc/1
  3. {
  4. "name": "sni",
  5. "age": 21
  6. }

查询

  1. # 查询所有索引
  2. GET /_cat/indices
  3. # 获取users索引的详情
  4. GET /users
  5. # 获取id=1的索引的数据内容详细信息
  6. GET /users/_doc/1
  7. # 获取id=1的索引的数据内容
  8. GET /users/_source/1

删除

  1. # 删除id=1的数据
  2. DELETE /users/_doc/1
  3. # 删除索引
  4. DELETE /users

修改

  1. # 修改: 数据没有产生变动则不做处理
  2. POST /users/_update/1
  3. {
  4. "doc": {
  5. "age": 22
  6. }
  7. }
  8. # 修改: 数据没有产生变动 也执行处理
  9. POST /users/_doc/1
  10. {
  11. "age": 22
  12. }

搜索-分词

  1. # 根据条件搜索
  2. GET /users/_search
  3. {
  4. "query": {
  5. "match_all": {}
  6. }
  7. }

模糊匹配

不区分大小写

  1. # field = address 模糊匹配 street
  2. GET /users/_search
  3. {
  4. "query": {
  5. "match": {
  6. "address": "street"
  7. }
  8. }
  9. }

精确匹配

  1. GET /users/_search
  2. {
  3. "query": {
  4. "match_phrase": {
  5. "address": "Madison street"
  6. }
  7. }
  8. }

多字段模糊匹配

  1. POST resume/_doc/12
  2. {
  3. "title": "后端⼯程师",
  4. "desc": "多年go语⾔开发经验, 熟悉go的基本语法, 熟悉常⽤的go语⾔库",
  5. "want_learn":"python语⾔"
  6. }
  7. POST resume/_doc/13
  8. {
  9. "title": "go⼯程师",
  10. "desc": "多年开发经验",
  11. "want_learn":"java语⾔"
  12. }
  13. POST resume/_doc/14
  14. {
  15. "title": "后端⼯程师",
  16. "desc": "多年开发经验",
  17. "want_learn":"rust语⾔"
  18. }
  19. # title desc 模糊匹配
  20. GET /resume/_search
  21. {
  22. "query": {
  23. "multi_match": {
  24. "query": "go",
  25. "fields": ["title", "desc"]
  26. }
  27. }
  28. }
  29. # 增加字段权重 ^num
  30. GET /resume/_search
  31. {
  32. "query": {
  33. "multi_match": {
  34. "query": "go",
  35. "fields": ["title^2", "desc"]
  36. }
  37. }
  38. }

match_all

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

搜索

term

精确查询

注意:当条件输入有大写字母时无法搜索出数据

  1. GET /users/_search
  2. {
  3. "query": {
  4. "term": {
  5. "address": "madison street"
  6. }
  7. }
  8. }

范围查询

  1. # 年龄大于等于20 小于等于30
  2. GET user/_search
  3. {
  4. "query":{
  5. "range": {
  6. "age": {
  7. "gte": 20,
  8. "lte": 30
  9. }
  10. }
  11. }
  12. }

exists

  1. # 查询所以存在age字段的数据
  2. GET user/_search
  3. {
  4. "query": {
  5. "exists": {
  6. "field": "age"
  7. }
  8. }
  9. }

fuzzy模糊查询

  1. # fuzzy模糊查询 拼错单词可以纠错 streat -> street
  2. GET /users/_search
  3. {
  4. "query": {
  5. "fuzzy": {
  6. "address": "streat"
  7. }
  8. }
  9. }
  10. # match中开启纠错
  11. GET users/_search
  12. {
  13. "query": {
  14. "match": {
  15. "address": {
  16. "query": "streat",
  17. "fuzziness": 1
  18. }
  19. }
  20. }
  21. }

复合搜索

  1. # must: 必须匹配,查询上下⽂,加分
  2. # should: 应该匹配,查询上下⽂,加分
  3. # must_not: 必须不匹配,过滤上下⽂,过滤
  4. # filter: 必须匹配,过滤上下⽂,过滤
  5. GET /users/_search
  6. {
  7. "query":{
  8. "bool":{
  9. "must":[
  10. ],
  11. "should":[
  12. ],
  13. "must_not":[
  14. ],
  15. "filter":[
  16. ],
  17. }
  18. }
  19. }

示例

  1. GET /users/_search
  2. {
  3. "query":{
  4. "bool":{
  5. "must":[
  6. {
  7. "term": {
  8. "state": "tn"
  9. }
  10. },
  11. {
  12. "range": {
  13. "age": {
  14. "gte": 20,
  15. "lte": 30
  16. }
  17. }
  18. }
  19. ],
  20. "should": [
  21. {
  22. "match": {
  23. "firstname": "Decker"
  24. }
  25. }
  26. ],
  27. "must_not":[
  28. {
  29. "term": {
  30. "gender": "male"
  31. }
  32. }
  33. ],
  34. "filter":[
  35. {
  36. "range": {
  37. "age": {
  38. "gte": 25,
  39. "lte": 30
  40. }
  41. }
  42. }
  43. ]
  44. }
  45. }
  46. }

批量操作

  1. # 批量操作
  2. POST _bulk
  3. { "index" : { "_index" : "test", "_id" : "1" } }
  4. { "field1" : "value1" }
  5. { "delete" : { "_index" : "test", "_id" : "2" } }
  6. { "create" : { "_index" : "test", "_id" : "3" } }
  7. { "field1" : "value3" }
  8. { "update" : {"_id" : "1", "_index" : "test"} }
  9. { "doc" : {"field2" : "value2"} }

批量导入数据

mock数据:https://mxshop-files.oss-cn-hangzhou.aliyuncs.com/accounts.json

  1. # 批量导入
  2. POST /users/_bulk
  3. {"index":{"_id":"1"}}
  4. {"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}
  5. {"index":{"_id":"6"}}
  6. {"account_number":6,"balance":5686,"firstname":"Hattie","lastname":"Bond","age":36,"gender":"M","address":"671 Bristol Street","employer":"Netagy","email":"hattiebond@netagy.com","city":"Dante","state":"TN"}
  7. {"index":{"_id":"13"}}
  8. {"account_number":13,"balance":32838,"firstname":"Nanette","lastname":"Bates","age":28,"gender":"F","address":"789 Madison Street","employer":"Quility","email":"nanettebates@quility.com","city":"Nogal","state":"VA"}
  9. {"index":{"_id":"18"}}
  10. {"account_number":18,"balance":4180,"firstname":"Dale","lastname":"Adams","age":33,"gender":"M","address":"467 Hutchinson Court","employer":"Boink","email":"daleadams@boink.com","city":"Orick","state":"MD"}

批量查询数据

  1. # 批量查询
  2. # index = users
  3. # index = account
  4. GET /_mget
  5. {
  6. "docs": [
  7. {
  8. "_index": "users",
  9. "_id": "1"
  10. },
  11. {
  12. "_index": "account",
  13. "_id": "2"
  14. }
  15. ]
  16. }