共计 1364 个字符,预计需要花费 4 分钟才能阅读完成。
1. 筹备测试数据,将数据下载到本地:
https://github.com/elastic/el…
数据结构:
{
"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"
}
2. 导入数据到 es
进入到 accounts.json 文件的目录执行导入命令,该命令会主动创立 bank 索引
curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_bulk?pretty&refresh" --data-binary "@accounts.json"
curl "localhost:9200/_cat/indices?v=true"
3. 查问操作
# 查问 bank 索引所有数据, 默认显示 10 条
GET /bank/_search
# 分页查问并依据 account_number 降序
GET /bank/_search
{
"query": {"match_all": {}
},
"sort": [
{
"account_number": {"order": "desc"}
}
],
"from": 10,
"size": 1
}
# 查问 address 蕴含 mill 或 lane
GET /bank/_search
{
"query": {
"match": {"address": "mill lane"}
}
}
# 仅匹配蕴含 mill lane 的内容
GET /bank/_search
{
"query": {
"match_phrase": {"address": "mill lane"}
}
}
#应用 bool 来结构简单查问,age 为 40 但 state 不为 id
GET /bank/_search
{
"query": {
"bool": {
"must": [
{
"match": {"age": "40"}
}
],
"must_not": [
{
"match": {"state": "ID"}
}
]
}
}
}
# 应用 filter 过滤数据
GET /bank/_search
{
"query": {
"bool": {
"must": [
{"match_all": {}
}
],
"filter": {
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}
# 聚合查问
GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_gender": {
"terms": {"field": "gender.keyword"}
}
}
}
#嵌套聚合查问并排序
GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "city.keyword",
"order": {"average_balance": "desc"}
},
"aggs": {
"average_balance": {
"avg": {"field": "balance"}
}
}
}
}
}
正文完
发表至: elasticsearch
2021-02-20