[Elasticsearch 实战][银行账户] 批量导入数据、搜索与聚合
- ElasticSearch
- 2019-11-21
- 151热度
- 0评论
导航
如果还没有 es 的环境,请先看 Elasticsearch 与 Kibana 下载安装。
以下整理自官方文档。
我们将使用 bulk API 批量导入银行账户的数据到 es 中,执行一些常见的搜索和聚合操作。
1 导入demo数据
- 下载 accounts.json 到本地(可用 wget),里面包含大量的银行账户数据,形如:
{ "account_number": 0, "balance": 16623, "firstname": "Bradshaw", "lastname": "Mckenzie", "age": 29, "gender": "F", "address": "244 Columbus Place", "employer": "Euron", "email": "bradshawmckenzie@euron.com", "city": "Hobucken", "state": "CO" }
- 使用如下命令,将数据导入到 es 里的 bank 索引中,并检查结果:
curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_bulk?pretty&refresh" --data-binary "@accounts.json" curl "localhost:9200/_cat/indices?v"
返回结果如下:
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open bank l7sSYV2cQXmu6_4rJWVIww 5 1 1000 0 128.6kb 128.6kb
2 搜索操作
(推荐在 Kibana 中输入语句)
2.1 按账户编号升序,列出所有账户
GET /bank/_search { "query": { "match_all": {} }, "sort": [ { "account_number": "asc" } ] }
2.2 搜索结果分页
GET /bank/_search { "query": { "match_all": {} }, "sort": [ { "account_number": "asc" } ], "from": 10, "size": 10 }
2.3 关键词匹配
空格分割的单词,任意一个匹配上都行。
GET /bank/_search { "query": { "match": { "address": "mill lane" } } }
2.4 词组匹配
空格分割的单词,整体作为一个词组来匹配。
GET /bank/_search { "query": { "match_phrase": { "address": "mill lane" } } }
2.5 搜索非Idaho州(ID)的,开户人年龄为40岁的账户
GET /bank/_search { "query": { "bool": { "must": [ { "match": { "age": "40" } } ], "must_not": [ { "match": { "state": "ID" } } ] } } }
2.6 搜索存款数额在 2w-3w 的账户
GET /bank/_search { "query": { "bool": { "must": { "match_all": {} }, "filter": { "range": { "balance": { "gte": 20000, "lte": 30000 } } } } } }
3 聚合操作
3.1 统计每个州的账户数量
GET /bank/_search { "size": 0, "aggs": { "group_by_state": { "terms": { "field": "state.keyword" } } } }
3.2 统计每个州账户的平均存款,降序排列
GET /bank/_search { "size": 0, "aggs": { "group_by_state": { "terms": { "field": "state.keyword", "order": { "average_balance": "desc" } }, "aggs": { "average_balance": { "avg": { "field": "balance" } } } } } }