GET /_search { "hits" : { "total" : 14, "hits" : [ { "_index": "us", "_type": "tweet", "_id": "7", "_score": 1, "_source": { "date": "2014-09-17", "name": "John Smith", "tweet": "The Query DSL is really powerful and flexible", "user_id": 2 } }, ... 9 RESULTS REMOVED ... ], "max_score" : 1 }, "took" : 4, "_shards" : { "failed" : 0, "successful" : 10, "total" : 10 }, "timed_out" : false }
返回结果中最重要的部分是 hits ,它 包含 total 字段来表示匹配到的文档总数,而且一个 hits 数组包含所查询结果的前十个文档。web
max_score 值是与查询所匹配文档的 _score 的最大值。sql
took 值告诉咱们执行整个搜索请求耗费了多少毫秒。centos
_shards 部分 告诉咱们在查询中参与分片的总数,以及这些分片成功了多少个失败了多少个。正常状况下咱们不但愿分片失败,可是分片失败是可能发生的。若是咱们遭遇到一种灾难级别的故障,在这个故障中丢失了相同分片的原始数据和副本,那么对这个分片将没有可用副原本对搜索请求做出响应。倘若这样,Elasticsearch 将报告这个分片是失败的,可是会继续返回剩余分片的结果。数组
在全部的索引中搜索全部的类型app
在 gb 索引中搜索全部的类型nosql
在 gb 和 us 索引中搜索全部的文档elasticsearch
在任何以 g 或者 u 开头的索引中搜索全部的类型flex
在 gb 索引中搜索 user 类型spa
在 gb 和 us 索引中搜索 user 和 tweet 类型code
在全部的索引中搜索 user 和 tweet 类型
和 SQL 使用 LIMIT 关键字返回单个 page 结果的方法相同
Elasticsearch 接受 from 和 size 参数:
显示应该返回的结果数量,默认是 10
显示应该跳过的初始结果数量,默认是 0
GET /_search?size=5&from=5
GET /_search {} GET /index_2014*/type1,type2/_search {} GET /_search { "from": 30, "size": 10 }
GET /_search { "query": YOUR_QUERY_HERE }
举个例子,你可使用 match 查询语句 来查询 tweet 字段中包含 elasticsearch 的 tweet:
GET /_search { "query": { "match": { "tweet": "elasticsearch" } } }
{ "bool": { "must": { "match": { "email": "business opportunity" }}, "should": [ { "match": { "starred": true }}, { "bool": { "must": { "match": { "folder": "inbox" }}, "must_not": { "match": { "spam": true }} }} ], "minimum_should_match": 1 } }
match_all 查询简单的 匹配全部文档。在没有指定查询方式时,它是默认的查询:
{ "match_all": {}}
高级别全文检索一般用于在全文本字段(如电子邮件正文)上运行全文检索。
他们了解如何分析被查询的字段,并在执行以前将每一个字段的分析器(或search_analyzer)应用于查询字符串。
就是说查询以前会对查询的字符串先作分词处理
{ "match": { "tweet": "About Search" }} match 的operator 操做。必须同时知足 centos 、升、级 GET website/_search { "query": { "match": { "title":{ "query":"centos升级", "operator":"and" } } } }
multi_match 查询能够在多个字段上执行相同的 match 查询:
{ "multi_match": { "query": "full text search", "fields": [ "title", "body" ] } }
match_phrase查询会将查询内容分词,分词器能够自定义,文档中同时知足如下两个条件才会被检索到:
(1)、建立索引插入数据
PUT test PUT test/hello/1 { "content":"World Hello"} PUT test/hello/2 { "content":"Hello World"} PUT test/hello/3 { "content":"I just said hello world"}
(2)、使用match_phrase查询”hello world”
GET test/_search { "query": { "match_phrase": { "content": "hello world" } } } 上面后两个文档匹配,被检索出来;第1个文档的词序与被查询内容不一致,因此不匹配。 { "took": 21, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 0.5753642, "hits": [ { "_index": "test", "_type": "hello", "_id": "2", "_score": 0.5753642, "_source": { "content": "Hello World" } }, { "_index": "test", "_type": "hello", "_id": "3", "_score": 0.5753642, "_source": { "content": "I just said hello world" } } ] } }
range 查询找出那些落在指定区间内的数字或者时间:
{ "range": { "age": { "gte": 20, "lt": 30 } } }
term 查询被用于精确值 匹配,这些精确值多是数字、时间、布尔或者那些 not_analyzed 的字符串:
term 查询对于输入的文本不分析 ,因此它将给定的值进行精确查询。
{ "term": { "age": 26 }} { "term": { "date": "2014-09-01" }} { "term": { "public": true }} { "term": { "tag": "full_text" }}
terms 查询和 term 查询同样,但它容许你指定多值进行匹配。若是这个字段包含了指定值中的任何一个值,那么这个文档知足条件:
{ "terms": { "tag": [ "search", "full_text", "nosql" ] }}
exists 查询和 missing 查询被用于查找那些指定字段中有值 (exists) 或无值 (missing) 的文档。
这与SQL中的 IS_NULL (missing) 和 NOT IS_NULL (exists) 在本质上具备共性:
{ "exists": { "field": "title" } }
bool 查询来实现你的需求。这种查询将多查询组合在一块儿,接收一下的参数
下面的查询用于查找 title 字段匹配 how to make millions
而且不被标识为 spam 的文档。
那些被标识为 starred 或在2014以后的文档,将比另外那些文档拥有更高的排名。
若是 二者 都知足,那么它排名将更高:
{ "bool": { "must": { "match": { "title": "how to make millions" }}, "must_not": { "match": { "tag": "spam" }}, "should": [ { "match": { "tag": "starred" }}, { "range": { "date": { "gte": "2014-01-01" }}} ] } }
若是咱们不想由于文档的时间而影响得分,能够用 filter 语句来重写前面的例子:
{ "bool": { "must": { "match": { "title": "how to make millions" }}, "must_not": { "match": { "tag": "spam" }}, "should": [ { "match": { "tag": "starred" }} ], "filter": { "range": { "date": { "gte": "2014-01-01" }} } } }
GET /gb/tweet/_validate/query { "query": { "tweet" : { "match" : "really powerful" } } } { "valid" : false, "_shards" : { "total" : 1, "successful" : 1, "failed" : 0 } }
GET /cars/transactions/_validate/query?explain { "query": { "match": { "make": "toyota" } } } { "_shards": { "total": 1, "successful": 1, "failed": 0 }, "valid": true, "explanations": [ { "index": "cars", "valid": true, "explanation": "+make:toyota #*:*" } ] }