查询结构:
_search |---query | |----match_all | |----match | | |----field ({ "match": { "tweet": "About Search" }}) | | | |----match_phrase | |----multi_match ({"multi_match":{"query":"full text search","fields":["title","body"]}}) | |----constant_score | |----filter | |----bool | | |----must | | |----must_not | | |----should | | |----filter | |----range ({"range":{"age":{"gte":20,"lt":30}}}) | |----term ({ "term": { "tag":"full_text"}}) | |----terms ({ "terms": { "tag": [ "search", "full_text", "nosql" ] }}) | |----exists ({"exists":{"field":"title"}}) | |----missing ({"missing":{"field":"title"}}) |---sort
ES提供两种查询方式,一种是通过REST request URI,另一种是REST request body.后一种的表现能力更好
curl 'localhost:9200/bank/_search?q=*&pretty' { "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"} }, { "_index" : "bank", "_type" : "account", "_id" : "6", "_score" : 1.0, "_source" : {"account_number":6,"balance":5686,"firstname":"Hattie","lastname":"Bond","age":36,"gender":"M","address":"671 Bristol Street","employer":"Netagy","email":"hattiebond@netagy.com","city":"Dante","state":"TN"} }, { "_index" : "bank", "_type" : "account",
- took – 执行搜索耗时,单位毫秒
- timed_out – 搜索是否超时
- _shards – 告知有多少的分片被查询及成功和失败的分片数
- hits – 搜索结果
- hits.total – 有多少条记录符合查询条件
- hits.hits – 实际返回的查询结果数据,默认返回10条记录
- _score and max_score - 先不解释
REST request body方式的查询
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match_all": {} } }'
QUERY DSL(查询语言)
- size – 查询返回数量,默认为10
- from – 起始位置,默认为0
- sort – 排序
- query – 查询条件
- _source – 返回的字段
- size –
示例
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match_all": {} }, "from": 10, "size": 10, "sort": { "balance": { "order": "desc" } }, "_source": ["account_number", "balance"] }'