附:<a href="http://www.jianshu.com/p/90eea1312a4f"> es安装 </a>
1、导入测试数据
首先下载<a href="https://github.com/bly2k/files/blob/master/accounts.zip?raw=true"> 下载测试数据 accounts.json </a>
<strong>step1、使用ip:port/索引名称/类型/_bulk --data-binary @文件名 语法导入文档</strong>
<pre>curl -XPOST 'localhost:9200/bank/account/_bulk?pretty' --data-binary "@accounts.json"</pre>
<strong>step2、使用_cat/indices?v 语法,可以看到索引bank中已经有了1000条docs</strong>
<pre>curl 'localhost:9200/_cat/indices?v'
<i>health index pri rep docs.count docs.deleted store.size pri.store.size
yellow bank 5 1 1000 0 424.4kb 424.4kb</i></pre>
2、查询语法 "_search"
_search的用法:<code>ip:port/索引名称/_search?q={语法}</code>
例如:
<strong>ex1:查询bank下的所有文档(pretty表示对结果格式化后输出)</strong>
<pre>curl 'localhost:9200/bank/_search?q=*&pretty'</pre>
<strong>查询结果解读</strong>
<pre>
{
"took" : 63,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1000,
"max_score" : 1.0,
"hits" : [ {
"_index" : "bank", "_type" : "account", "_id" : "1", "_score" : 1.0, "_source" : {"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}
}]
}
}
</pre>
- took 执行查询所需要的时间,单位毫秒
- timed_out 查询是否超时
- _shards 执行查询时搜索了多少分配
- hits 查询结果
- hits>total 返回的结果数
- hits>hits 返回的json数组,默认返回10条记录
3、查询的json语法实例
<strong>ex2:match_all 查询bank下的所有文档2</strong>
<pre>curl -XPOST 'localhost:9200/bank/_search?pretty' -d
'{ "query": { "match_all": {} }}'</pre>
<strong>ex3:size 指定返回的结果条数</strong>
<pre>
curl -XPOST 'localhost:9200/bank/_search?pretty' -d
'{ "query": { "match_all": {} }, "size": 1}'
</pre>
<strong>ex4:from 从第几条开始查</strong>
<pre>
curl -XPOST 'localhost:9200/bank/_search?pretty' -d
'{ "query": { "match_all": {} }, "from": 10, "size": 10}'
</pre>
<strong>ex5:sort 根据字段排序</strong>
<pre>
curl -XPOST 'localhost:9200/bank/_search?pretty' -d
'{ "query": { "match_all": {} },
"sort": { "balance": { "order": "desc" } }}'
</pre>
<strong>ex6:显示特定的字段 "_source>account_number"</strong>
<pre>
curl -XPOST 'localhost:9200/bank/_search?pretty' -d
'{
"query": { "match_all": {} },
"_source": ["account_number", "balance"]}'
</pre>
ex7:query>match 等于20
<pre>curl -XPOST 'localhost:9200/bank/_search?pretty' -d
'{ "query": { "match": { "account_number": 20 } }}'</pre>
ex8:query>match 字段中包含单词
curl -XPOST 'localhost:9200/bank/_search?pretty' -d
'{ "query": { "match": { "address": "mill" } }}'
ex9:query>match 字段中包含单词a或者单词b
<pre>curl -XPOST 'localhost:9200/bank/_search?pretty' -d
'{ "query": { "match": { "address": "mill lane" } }}'</pre>
ex10:query>match_phrase 字段中包含单词a和单词b
<pre>curl -XPOST 'localhost:9200/bank/_search?pretty' -d
'{ "query": { "match_phrase": { "address": "mill lane" } }}'</pre>
4、布尔查询
and "must"(且)
<pre>curl -XPOST 'localhost:9200/bank/_search?pretty' -d
'{ "query": { "bool": { "must": [ { "match": { "address": "mill" } },
{ "match": { "address": "lane" } } ] } }}'</pre>
or "should"(或)
<pre>
curl -XPOST 'localhost:9200/bank/_search?pretty' -d
'{ "query": { "bool": { "should": [ { "match": { "address": "mill" } },
{ "match": { "address": "lane" } } ] } }}'
</pre>
!"must_not"(非)
<pre>
curl -XPOST 'localhost:9200/bank/_search?pretty' -d
'{ "query": { "bool": { "must_not": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } }}'
</pre>
must_not match
<pre>
curl -XPOST 'localhost:9200/bank/_search?pretty' -d
'{ "query": {
"bool": { "must": [ { "match": { "age": "40" } } ],
"must_not": [ { "match": { "state": "ID" } } ] } }}'
</pre>
bool查询的过滤语法"filter"
查询int类型值的区间结果 my_min<value<my_max
<pre>
curl -XPOST 'localhost:9200/bank/_search?pretty' -d
'{
"query": { "bool": { "must": { "match_all": {} },
"filter": { "range": { "balance": { "gte": 20000, "lte": 30000 }
} } } }}'
</pre>
5、分组查询
按state分组
<pre>
curl -XPOST 'localhost:9200/bank/_search?pretty' -d
'{ "size": 0,
"aggs": { "group_by_state": { "terms": { "field": "state" } } }}'
</pre>
按state分组,然后求每个分组结果的平均值
<pre>curl -XPOST 'localhost:9200/bank/_search?pretty' -d
'{ "size": 0,
"aggs": { "group_by_state": { "terms": { "field": "state" },
"aggs": { "average_balance": { "avg": { "field": "balance" }
} } } }}'
</pre>
按state分组,然后求每个分组结果的平均值,并按平局值排序
<pre>curl -XPOST 'localhost:9200/bank/_search?pretty' -d
'{ "size": 0,
"aggs": { "group_by_state": { "terms": { "field": "state", "order": { "average_balance": "desc" } },
"aggs": { "average_balance": { "avg": { "field": "balance" }
} } } }}'</pre>
按指定的范围进行分组
<pre>
curl -XPOST 'localhost:9200/bank/_search?pretty' -d
'{ "size": 0, "aggs": { "group_by_age": {
"range": { "field": "age", "ranges": [
{ "from": 20, "to": 30 },
{ "from": 30, "to": 40 },
{ "from": 40, "to": 50 } ] },
"aggs": { "group_by_gender": { "terms": { "field": "gender" },
"aggs": { "average_balance": { "avg": { "field": "balance" }
} } } } } }}'
</pre>
网友评论