GET /{index}/_search { "query": { "match_all": {} } }
GET /{index}/_search { "query": { "match": { "FIELD": "TEXT" } } }
GET /{index}/_search { "query": { "multi_match": { "query": "", "fields": [] } } }
GET /{index}/_search { "query": { "range": { "FIELD": { "gte": 10, "lte": 20 } } } }
GET /{index}/_search { "query": { "term": { "FIELD": { "value": "VALUE" } } } }
GET /{index}/_search { "query": { "terms": { "FIELD": [ "VALUE1", "VALUE2" ] } } }
GET /{index}/_search { "query": { "exists": { "field": "" } } }
每一个子查询都会计算一个document针对它的相关度分数,而后bool综合全部分数,合并为一个分数,固然filter是不会计算分数的。
示例:java
{ "bool": { "must": { "match": { "title": "how to make millions" }}, "must_not": { "match": { "tag": "spam" }}, "should": [ { "match": { "tag": "starred" }} ], "filter": { "bool": { "must": [ { "range": { "date": { "gte": "2014-01-01" }}}, { "range": { "price": { "lte": 29.99 }}} ], "must_not": [ { "term": { "category": "ebooks" }} ] } } } } GET /{index}/_search { "query": { "constant_score": { "filter": {}, "boost": 1.2 } } }
通常用在那种特别复杂庞大的搜索下,好比你一会儿写上了上百行的搜索,这个时候能够先用validate api去验证一下搜索是否合法api
GET /employee/_validate/query?explain { "query": { "constant_score": { "filter": {}, "boost": 1.2 } } } { "valid" : false, "error" : "ParsingException[Failed to parse]; nested: IllegalArgumentException[query malformed, empty clause found at [4:18]];; java.lang.IllegalArgumentException: query malformed, empty clause found at [4:18]" } GET /employee/_validate/query?explain { "query": { "constant_score": { "filter": { "term": { "name": "tom" } }, "boost": 1.2 } } } { "_shards" : { "total" : 1, "successful" : 1, "failed" : 0 }, "valid" : true, "explanations" : [ { "index" : "employee", "valid" : true, "explanation" : "(ConstantScore(name:tom))^1.2" } ] }
默认状况下,返回的document是按照_score降序排列的。若是咱们想本身定义排序规则怎么办,此时只须要使用sort便可spa
# 主要语法 "sort": [ { "FIELD": { "order": "desc" } } ] # 总体位置 GET /{index}/_search { "query": { "constant_score": { "filter": { "exists": { "field": "" } }, "boost": 1.2 } }, "sort": [ { "FIELD": { "order": "desc" } } ] }