有用的链接:

https://docs.es.shiyueshuyi.xyz/#/intro/datain
https://www.cnblogs.com/cjsblog/p/9439331.html
https://www.cnblogs.com/niceyoo/p/12936325.html
ElasticSearch 搜索报错问题 No mapping found for [column] in order to sort on:
https://blog.csdn.net/caojianwei1992/article/details/88971733
ElasticSearch 官方文档:
https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html

ES 可视化工具

https://cloud.tencent.com/developer/article/1638819

基本概念:

  1. Elasticsearch是一个高度可伸缩的开源全文搜索和分析引擎,RestFul 风格。 提供查询, 聚合等功能。
  2. Near RealTime (NRT) ElasitcSearch 是一个近乎实时的搜索平台, 倒排索引。
  3. Node: 一个ElasticSearch 实例被称作一个Node, 一个服务器上可以有多个ES 实例。 多个实例共同构成一个集群。
  4. Index:索引, 数据库。
  5. Document:被索引的文档,用JSON表示。
  6. 索引可以被分片也可以被复制。

    Homebrew 安装 Elasticsearch

    基本命令: ```bash

    安装

    brew tap elastic/tap brew install elasticsearch-full

启动

brew services start elasticsearch-full

  1. Homebrew 安装 Elasticsearch的路径如下 (同理,kibana, filebeat, logstash ):<br />![截屏2022-01-03 上午1.33.15.png](https://cdn.nlark.com/yuque/0/2022/png/22970599/1641144810892-19446494-4cb8-46d0-9c97-3b7aa076b46a.png#clientId=u79d4c732-bfc3-4&from=drop&id=uca23dce4&margin=%5Bobject%20Object%5D&name=%E6%88%AA%E5%B1%8F2022-01-03%20%E4%B8%8A%E5%8D%881.33.15.png&originHeight=784&originWidth=1564&originalType=binary&ratio=1&size=183703&status=done&style=none&taskId=u1200a3f7-e265-40f5-922a-4c2909974f3)
  2. <a name="hpjgd"></a>
  3. ## 基本操作
  4. <a name="vJid2"></a>
  5. ### 增删查改
  6. 1. 集群健康
  7. ```json
  8. curl -X GET "localhost:9200/_cat/health?v"
  1. 集群节点

    1. curl -X GET "localhost:9200/_cat/nodes?v"
  2. 集群索引

    1. curl -X GET "localhost:9200/_cat/indices?v"
  3. 创建索引

    1. curl -X PUT "localhost:9200/{index_name}?pretty"
  4. 创建索引并添加数据

    1. curl -X PUT "localhost:9200/{index_name}/{type}/{id}?pretty" -H 'Content-Type: application/json' -d'{"name": "John Doe"}'
  5. 删除索引

    curl -X DELETE "localhost:9200/{index_name}?pretty"
    
  6. 修改数据 (POST 用于更新文档,PUT 用于添加数据)

    curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
    {
    "doc": { "name": "Jane Doe", "age": 20 }
    }
    '
    

    注意文档要是JSON格式,且前面一定要加上 “doc” 字段

  7. 脚本

    curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
    {
    "script" : "ctx._source.age += 5"
    }
    '
    
  8. 删除文档

    curl -X DELETE "localhost:9200/{index_name}/{type}/{doc_id}?pretty"
    
  9. 批处理 (使用bulk API)

    curl -X POST "localhost:9200/{index_name}/{type}/_bulk?pretty" -H 'Content-Type: application/json' -d'
    {"index":{"_id":"1"}}
    {"name": "John Doe" }
    {"index":{"_id":"2"}}
    {"name": "Jane Doe" }
    '
    

    注意一定要申明 {“index” : {“_id” : “num”}}, 否则ES不知道该更新哪个文档

  10. 加载JSON 数据

    curl -H "Content-Type: application/json" -XPOST "localhost:9200/{index_name}/{type}/_bulk?pretty&refresh" --data-binary "@/path/accounts.json"
    
  11. 查询+sort 排序

    curl -X GET "localhost:9200/{index_name}/_search" -H 'Content-Type: application/json' -d'
    {
    "query": { "match_all": {} },
    "sort": [
    { "account_number": "asc"/"des"}
    ]
    }
    '
    
  12. match 指定数量的文档

    curl -X GET "localhost:9200/{index_name}/_search" -H 'Content-Type: application/json' -d'
    {
    "query": { "match_all": {} },
    "from": 10,
    "size": 10
    }
    '
    
  13. 搜索指定字段

相当于SELECT account_number, balance FROM bank

curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
{
  "query": { "match_all": {} },
  "_source": ["account_number", "balance"]
}
'
  1. 匹配指定字段

    curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
    {
    "query": { "match": { "account_number": 20 } }
    }
    '
    
    curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
    {
    "query": { "match": { "address": "mill lane" } }
    }
    '
    

    相当于SELECT * FROM bank WHERE address LIKE ‘%mill’ OR address LIKE ‘%lane%’
    mill 或者 lane 只要又一个匹配就会返回结果

  2. 组合查询 (用“bool” 来组合多个查询条件) ```json curl -X GET “localhost:9200/bank/_search” -H ‘Content-Type: application/json’ -d’ { “query”: { “bool”: { “must”: [

    { "match": { "address": "mill" } },
    { "match": { "address": "lane" } }
    

    ] } } } ‘

curl -X GET “localhost:9200/bank/_search” -H ‘Content-Type: application/json’ -d’ { “query”: { “bool”: { “must”: [ { “match”: { “age”: “40” } } ], “must_not”: [ { “match”: { “state”: “ID” } } ] } } } ‘

curl -X GET “localhost:9200/bank/_search” -H ‘Content-Type: application/json’ -d’ { “query”: { “bool”: { “should”: [ { “match”: { “address”: “mill” } }, { “match”: { “address”: “lane” } } ] } } } ‘

must 相当于 and, should 相当于 or

17.  过滤器和range 使用:
```json
curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "must": { "match_all": {} },
      "filter": {
        "range": {
          "balance": {
            "gte": 20000,
            "lte": 30000
          }
        }
      }
    }
  }
}
'
  1. 聚集, 相当于 SQL 里的聚集函数,例如分组,求和,求平均之类。

terms 指定聚集的字段

curl -X GET "localhost:9200/{index_name}/_search" -H 'Content-Type: application/json' -d'
{
  "size": 0,
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "state.keyword"
      }
    }
  }
}
'
  1. 先聚集,然后组内求平均 (avg 函数), 嵌套聚合

    curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
    {
    "size": 0,
    "aggs": {
    "group_by_state": {
      "terms": {
        "field": "state.keyword"
      },
      "aggs": {
        "average_balance": {
          "avg": {
            "field": "balance"
          }
        }
      }
    }
    }
    }
    '
    
  2. 按年龄段分组,组内按性别分组,性别组再求平均 (field, rangs, terms, aggs, avg 函数)

    curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
    {
    "size": 0,
    "aggs": {
    "group_by_age": {
      "range": {
        "field": "age",
        "ranges": [
          {
            "from": 20,
            "to": 30
          },
          {
            "from": 30,
            "to": 40
          },
          {
            "from": 40,
            "to": 50
          }
        ]
      },
      "aggs": {
        "group_by_gender": {
          "terms": {
            "field": "gender.keyword"
          },
          "aggs": {
            "average_balance": {
              "avg": {
                "field": "balance"
              }
            }
          }
        }
      }
    }
    }
    }
    '
    

    Elasticsearch-head 解决跨域问题

    进入目录:cd /usr/local/etc/elasticsearch/
    修改yml文件:open -e elasticsearch.yml
    image.png

    ES term 和 match 的区别

    term 是精准匹配,在搜索前不会对搜索次进行分词处理。如果要匹配多个次可以使用
    terms : {title : [‘term 1’, ‘term 2’]} 两个term 之间是or的关系
    match 是模糊匹配,
    https://www.jianshu.com/p/d5583dff4157