搜索API

现在,让我们先从一些简单的搜索开始。有两种基本方式运行搜索:一种是通过发送搜索参数REST请求URI,另一种是通过发送他们REST请求主体。请求主体的方法可以更有表现力以及更可读的JSON格式来定义你的搜索。我们会尽力用一个例子请求URI方法,但对于本教程的其余部分,我们将完全使用可请求体的方法。

搜索的REST API用_search作为结尾共容易让人理解。此示例返回银行索引的所有文件:


curl 'localhost:9200/bank/_search?q=*&pretty'

让我们先来剖析搜索调用。我们正在索引(_search结尾)银行索引,并且Q = *参数指示Elasticsearch以匹配索引中的所有文档。追加的pretty参数,只是告诉Elasticsearch返回格式化打印JSON结果。


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":"[email protected]","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":"[email protected]","city":"Dante","state":"TN"}
    }, {
      "_index" : "bank",
      "_type" : "account",

至于返回,我们看到了以下几个部分:

  • took Elasticsearch执行搜索的毫秒时间
  • timed_out 告诉我们是否超时
  • _shards -告诉我们搜索了多少分片,以及成功/失败分片搜索的统计
  • hits 搜索结果
  • hits.total-匹配我们的搜索条件的文档总数
  • hits.hits -搜索结果实际的数组(默认为前10个文件)
  • score和MAX_SCORE -现在忽略这些领域

下面是使用上述替代请求体的方法完全相同的搜索:


curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
  "query": { "match_all": {} }
}'

这里的区别在于,通过替换q = *的URI,我们发布一个JSON格式的查询请求体向_search API。我们将在下一节讨论此JSON查询。

返回(部分示例):


curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
  "query": { "match_all": {} }
}'
{
  "took" : 26,
  "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":"[email protected]","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":"[email protected]","city":"Dante","state":"TN"}
    }, {
      "_index" : "bank",
      "_type" : "account",
      "_id" : "13",

要明白这是很重要的,一旦你获得搜索结果返回即Elasticsearch完全完成了请求,Elasticsearch不保持任何形式的服务器端资源或打开的游标在您的结果中。与此形成鲜明对比的许多其他平台,例如SQL,其中你最初可能会得到您的查询的部分子集,然后你必须不断回到服务器,如果你想获取(或通过分页)剩余部分结果使用某种状态的服务器端游标。

results matching ""

    No results matching ""