目录:
1.创建es集群并运行
2.给文档添加索引
3.使用es查询语句检索文档
4.使用bucket、metric聚合分析结果
单机部署多实例
下载
wget "https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.5.1-linux-x86_64.tar.gz" -O elasticsearch-7.5.1-linux-x86_64.tar.gz
启动多个实例
cd elasticsearch-7.5.1/bin
./elasticsearch -d
./elasticsearch -Epath.data=data2 -Epath.logs=log2 -d
./elasticsearch -Epath.data=data3 -Epath.logs=log3 -d
验证
curl 127.0.0.1:9200/_cat/health?v
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1578280878 03:21:18 elasticsearch green 3 3 0 0 0 0 0 0 - 100.0%
创建索引
PUT /customer/_doc/1
{
"name": "John Doe"
}
GET /customer/_doc/1
批处理文档,建立索引
wget "http://github.com/elastic/elasticsearch/blob/master/docs/src/test/resources/accounts.json?raw=true"
curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_bulk?pretty&refresh" --data-binary "@accounts.json"
GET /_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open bank OzbbV0o2SySxOpp_bvjF7A 1 1 1000 0 924.8kb 462.4kb
green open .kibana_task_manager_1 5F536zhkQPCRqwVvgabgaw 1 1 2 1 95.3kb 51.6kb
green open .apm-agent-configuration jKGK8Oe7TOS-QjUrhpwmGA 1 1 0 0 566b 283b
green open .kibana_1 i_n9c2m9SfGsRcZsG35XMQ 1 1 6 4 65.9kb 17.1kb
green open customer hqgmxIFRT2qZsUaOXbsaBQ 1 1 1 0 7kb 3.5kb
查询
GET /bank/_search
GET /bank/_search
{
"query": {"match_all":{}},
"sort":[
{"account_number": "asc"}],
"from":10,
"size":10
}
GET /bank/_search
{
"query": { "match": { "address": "mill lane" } }
}
GET /bank/_search
{
"query": { "match_phrase": { "address": "mill lane" } }
}
GET /bank/_search
{
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}
GET /bank/_search
{
"query":{
"bool":{
"must":{"match_all":{}},
"filter":{
"range":{
"balance":{
"gte": 20000,
"lte": 30000
}
}
}
}
}
}
分析
根据省份分组聚合
GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword"
}
}
}
}
省份分组结合账号平均存款
GET /bank/_search
{
"size": 0,
"aggs":{
"group_by_state":{
"terms":{
"field": "state.keyword"
},
"aggs":{
"average_balance":{
"avg":{
"field":"balance"
}
}
}
}
}
}
省份分组结合账号平均存款,并按平均存款倒序排列
GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword",
"order": {
"average_balance": "desc"
}
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}