对数据的各种查询.
Exploring Your Data
来自https://www.elastic.co/guide/en/elasticsearch/reference/current/_exploring_your_data.html
下载数据:
1 | [jimo@jimo-pc my-data]$ wget https://raw.githubusercontent.com/elastic/elasticsearch/master/docs/src/test/resources/accounts.json |
导入数据:
1 | [jimo@jimo-pc my-data]$ curl -H "Content-Type: application/json" -XPOST 'localhost:9200/bank/account/_bulk?pretty&refresh' --data-binary "@accounts.json" |
The Search API
1 | [jimo@jimo-pc my-data]$ curl -XGET 'localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty&pretty' |
查询解析:localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty&pretty
以&分开,q=*代表所有,asc升序。
等价与:
1 | curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "match_all": {} }, "sort": [ { "account_number": "asc" } ] } ' |
Introducing the Query Language
1 | 可以传入数量,只查一个,默认size=10 |
Executing Searches
使用_source过滤字段:
1 | [jimo@jimo-pc my-data]$ curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "match_all": {} }, "_source": ["account_number", "balance"] } ' |
精确查找:
1 | [jimo@jimo-pc my-data]$ curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "match": { "account_number": 20 } } } ' |
以空格分开代表OR,不区分大小写:
1 | { |
那要包含这两个则使用:match_phrase
1 | { |
逻辑查询
1 | AND |
Executing Filters
有个_score评分排名项:
1 | "hits" : [ |
可以使用filter来过滤:
1 | { |
Executing Aggregations
聚合类似于SQL的Group By
1 | [jimo@jimo-pc my-data]$ curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d' |
等价于:
1 | SELECT state, COUNT(*) FROM bank GROUP BY state ORDER BY COUNT(*) DESC |
再进行聚合求他们balance的均值:
1 | [jimo@jimo-pc my-data]$ curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d' |
再按均值降序排列:
1 | { |
下面是先对不同年龄段的人聚类,再按性别,再求平均收支的结果:
1 | 操作: |