Elasticsearch实际数据集搜索操作
1、样本数据集
{"index":{"_id":"1"}} { "account_number": 0, "balance": 26333, "firstname": "perter", "lastname": "man", "age": 28, "gender": "M", "address": "colimbbs street" "employer": "cana", "email": "lijhhk@163.com" "city": "wot" "state": "co" }
加载并创建索引:
curl -XPOST '192.168.110.130:9200/bank/account/_bulk?pretty&refresh' --data-binary "@account.json"
2、Search API
(1)REST请求URI发送搜索参数:
curl -XGET '192.168.110.130:9200/bank/_search?q=*&sort=account_number:asc&pretty' 默认搜索出来10条记录
(2)REST请求主体发送搜索参数:
curl -XGET '192.168.110.130:9200/bank/_search?pretty' -d' { "query":{"match_all":{}}, "sort":[{"account_number":"asc"}] }'
3、Json风格查询语言
(1)查询所有
curl -XGET '192.168.110.130:9200/bank/_search?pretty' -d' { "query":{"match_all":{}} }'
(2)指定size,显示多少条数据
curl -XGET '192.168.110.130:9200/bank/_search?pretty' -d' { "query":{"match_all":{}}, "size":1 }'
(3)指定范围
curl -XGET '192.168.110.130:9200/bank/_search?pretty' -d' { "query":{"match_all":{}}, "from":10, "size":10 }'
(4)排序
curl -XGET '192.168.110.130:9200/bank/_search?pretty' -d' { "query":{"match_all":{}}, "sort":{"balance":{"order":"desc"}} }'
4、搜索演示
(1)指定显示字段
curl -XGET '192.168.110.130:9200/bank/_search?pretty' -d' { "query":{"match_all":{}}, "source":["account_number","balance"] }'
(2)match和match_phrase演示
curl -XGET '192.168.110.130:9200/bank/_search?pretty' -d' { "query":{"match":{"account_number":20}} }'
(3)match搜索包含mill字符串,不区分大小写的信息
curl -XGET '192.168.110.130:9200/bank/_search?pretty' -d' { "query":{"match":{"address":"mill"}} }'
(4)match搜索mill或者lane字符串,不区分大小写的信息
curl -XGET '192.168.110.130:9200/bank/_search?pretty' -d' { "query":{"match":{"address":"mill lane"}} }'
(5)match_phrase搜索包含mill lane字符串,不区分大小写的信息
curl -XGET '192.168.110.130:9200/bank/_search?pretty' -d' { "query":{"match_phrase":{"address":"mill lane"}} }'
(6)bool查询--must(& 并且的关系)
curl -XGET '192.168.110.130:9200/bank/_search?pretty' -d' { "query":{ "bool":{ "must":[ {"match":{"address":"mill"}}, {"match":{"address":"lane"}} ] } } }'
(7)bool查询--should(or 或的关系)
curl -XGET '192.168.110.130:9200/bank/_search?pretty' -d' { "query":{ "bool":{ "should":[ {"match":{"address":"mill"}}, {"match":{"address":"lane"}} ] } } }'
(8)bool查询--must_not(既不包含mill也不包含lane)
curl -XGET '192.168.110.130:9200/bank/_search?pretty' -d' { "query":{ "bool":{ "must_not":[ {"match":{"address":"mill"}}, {"match":{"address":"lane"}} ] } } }'
(9)bool查询--组合使用
curl -XGET '192.168.110.130:9200/bank/_search?pretty' -d' { "query":{ "bool":{ "must":[ {"match":{"age":"40"}} ], "must_not":[ {"match":{"state":"ID"}} ] } } }'
5、过滤器和聚合演示
- 过滤器演示
curl -XGET '192.168.110.130:9200/bank/_search?pretty' -d' { "query":{ "bool":{ "must":{"match_all":{}}, "filter":{ "range":{ "balance":{ "gte":20000, "lte":30000 } } } } } }'
- 聚合演示--按state分组,count递减排序
curl -XGET '192.168.111.130:9200/bank/_search?pretty' -d' { "size":0, "aggs":{ "group_by_state":{ "terms":{ "field":"state.keyword" } } } }
- 聚合演示--按state计算平均账户余额
curl -XGET '192.168.111.130:9200/bank/_search?pretty' -d' { "size":0, "aggs":{ "group_by_state":{ "terms":{ "field":"state.keyword" }, "aggs":{ "average_balance":{ "avg":{ "field":"balance" } } } } } }