Elasticsearch 基础入门
安装
安装 Java
安装 ES
tar zxvf elasticsearch-<version>
启动 ES
./bin/elasticsearch
查看 ES 状态
-k
允许不安全的 https 连接-u
用户名密码,在第一次启动ES时打印,可以通过bin/elasticsearch-reset-password -u elastic
重置密码
curl -k -u elastic:passwd https://localhost:9200
curl -X <VERB> '<PROTOCOL>://<HOST>:<PORT>?<QUERY_STRING>' -d <BODY>
查看文档数量
curl -k -u elastic:passwd https://localhost:9200/_count
端口:
- 集群间使用 9300 端口通信
- RESTful API 使用 9200 端口通信
基础入门
索引员工数据
curl -XPUT -H 'Content-Type: application/json' -k -u elastic:passwd https://localhost:9200/employee/_doc/1 -d \
'{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}'
curl -XPUT -H 'Content-Type: application/json' -k -u elastic:passwd https://localhost:9200/employee/_doc/2 -d \
'{
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests": [ "music" ]
}'
curl -XPUT -H 'Content-Type: application/json' -k -u elastic:passwd https://localhost:9200/employee/_doc/3 -d \
'{
"first_name" : "Douglas",
"last_name" : "Fir",
"age" : 35,
"about": "I like to build cabinets",
"interests": [ "forestry" ]
}'
检索员工数据
curl -k -u elastic:passwd https://localhost:9200/employee/_doc/1?pretty"
- PUT 创建/更新文档
- GET 检索文档
- DELETE 删除文档
- HEAD 检查文档是否存在
curl -I -k -u elastic:passwd https://localhost:9200/employee/_doc/1
简单搜索
检索所有数据,默认返回 10 条
curl -k -u elastic:passwd "https://localhost:9200/employee/_search?pretty"
搜索 last_name 为 Smith 的文档(查询字符串 query-string)
curl -k -u elastic:passwd "https://localhost:9200/employee/_search?pretty&q=last_name:Smith"
查询表达式
curl -H 'Content-Type: application/json' -k -u elastic:passwd "https://localhost:9200/employee/_search?pretty" -d \
'{
"query": {
"match": {
"last_name": "Smith"
}
}
}'
过滤器
过滤器支持高效地执行结构化查询
range 过滤器
curl -H 'Content-Type: application/json' -k -u elastic:passwd "https://localhost:9200/employee/_search?pretty" -d \
'{
"query": {
"bool": {
"must": [{
"match": {
"last_name": "Smith"
}
}],
"filter": [{
"range": {
"age": {"gt": 30}
}
}]
}
}
}'
全文搜索
返回结果会根据相关性排序
curl -H 'Content-Type: application/json' -k -u elastic:passwd "https://localhost:9200/employee/_search?pretty" -d \
'{
"query": {
"match": {
"about": "rock climbing"
}
}
}'
短语搜索
检索包含短语 "rock climbing"
curl -H 'Content-Type: application/json' -k -u elastic:passwd "https://localhost:9200/employee/_search?pretty" -d \
'{
"query": {
"match_phrase": {
"about": "rock climbing"
}
}
}'
高亮搜索
curl -H 'Content-Type: application/json' -k -u elastic:passwd "https://localhost:9200/employee/_search?pretty" -d \
'{
"query": {
"match_phrase": {
"about": "rock climbing"
}
},
"highlight": {
"fields": {
"about": {}
}
}
}'
聚合
根据 interests 分组统计,all_interests 是别名
https://stackoverflow.com/questions/59298209/how-to-fix-elasticsearch-fielddata-is-disabled-on-text-fields-by-default-for-k
curl -H 'Content-Type: application/json' -k -u elastic:passwd "https://localhost:9200/employee/_search?pretty" -d \
'{
"aggs": {
"all_interests": {
"terms": {
"field": "interests.keyword"
}
}
}
}'
先进行搜索,在对结果根据 interests 分组统计
curl -H 'Content-Type: application/json' -k -u elastic:passwd "https://localhost:9200/employee/_search?pretty" -d \
'{
"query": {
"match": {
"last_name": "Smith"
}
},
"aggs": {
"all_interests": {
"terms": {
"field": "interests.keyword"
}
}
}
}'
根据 interests 分组,再计算每组的平均年龄
curl -H 'Content-Type: application/json' -k -u elastic:passwd "https://localhost:9200/employee/_search?pretty" -d \
'{
"aggs": {
"all_interests": {
"terms": {
"field": "interests.keyword"
},
"aggs": {
"avg_age": {
"avg": {"field": "age"}
}
}
}
}
}'