有用的链接:
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
基本概念:
- Elasticsearch是一个高度可伸缩的开源全文搜索和分析引擎,RestFul 风格。 提供查询, 聚合等功能。
- Near RealTime (NRT) ElasitcSearch 是一个近乎实时的搜索平台, 倒排索引。
- Node: 一个ElasticSearch 实例被称作一个Node, 一个服务器上可以有多个ES 实例。 多个实例共同构成一个集群。
- Index:索引, 数据库。
- Document:被索引的文档,用JSON表示。
- 索引可以被分片也可以被复制。
Homebrew 安装 Elasticsearch
基本命令: ```bash安装
brew tap elastic/tap brew install elasticsearch-full
启动
brew services start elasticsearch-full
Homebrew 安装 Elasticsearch的路径如下 (同理,kibana, filebeat, logstash ):<br />
<a name="hpjgd"></a>
## 基本操作
<a name="vJid2"></a>
### 增删查改
1. 集群健康
```json
curl -X GET "localhost:9200/_cat/health?v"
集群节点
curl -X GET "localhost:9200/_cat/nodes?v"
集群索引
curl -X GET "localhost:9200/_cat/indices?v"
创建索引
curl -X PUT "localhost:9200/{index_name}?pretty"
创建索引并添加数据
curl -X PUT "localhost:9200/{index_name}/{type}/{id}?pretty" -H 'Content-Type: application/json' -d'{"name": "John Doe"}'
删除索引
curl -X DELETE "localhost:9200/{index_name}?pretty"
修改数据 (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” 字段
脚本
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d' { "script" : "ctx._source.age += 5" } '
删除文档
curl -X DELETE "localhost:9200/{index_name}/{type}/{doc_id}?pretty"
批处理 (使用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不知道该更新哪个文档
加载JSON 数据
curl -H "Content-Type: application/json" -XPOST "localhost:9200/{index_name}/{type}/_bulk?pretty&refresh" --data-binary "@/path/accounts.json"
查询+sort 排序
curl -X GET "localhost:9200/{index_name}/_search" -H 'Content-Type: application/json' -d' { "query": { "match_all": {} }, "sort": [ { "account_number": "asc"/"des"} ] } '
match 指定数量的文档
curl -X GET "localhost:9200/{index_name}/_search" -H 'Content-Type: application/json' -d' { "query": { "match_all": {} }, "from": 10, "size": 10 } '
搜索指定字段
相当于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"]
}
'
匹配指定字段
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 只要又一个匹配就会返回结果组合查询 (用“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
}
}
}
}
}
}
'
- 聚集, 相当于 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"
}
}
}
}
'
先聚集,然后组内求平均 (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" } } } } } } '
按年龄段分组,组内按性别分组,性别组再求平均 (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.ymlES term 和 match 的区别
term 是精准匹配,在搜索前不会对搜索次进行分词处理。如果要匹配多个次可以使用
terms : {title : [‘term 1’, ‘term 2’]} 两个term 之间是or的关系
match 是模糊匹配,
https://www.jianshu.com/p/d5583dff4157