一旦您将一些数据放入到Elasticsearch索引中,您就可以通过向_search端点发送请求来搜索它。要访问整套搜索功能,您可以使用Elasticsearch查询DSL来指定请求体中的搜索条件。在请求URI中指定要搜索的索引的名称。

    例如,下面的请求检索按帐号排序的银行索引中的所有文档:

    1. GET /bank/_search
    2. {
    3. "query": { "match_all": {} },
    4. "sort": [
    5. { "account_number": "asc" }
    6. ]
    7. }

    cURL:

    curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "match_all": {} }, "sort": [ { "account_number": "asc" } ] } '
    

    默认情况下,响应的hits部分包含匹配搜索条件的前10个文档:

    {
      "took" : 63,
      "timed_out" : false,
      "_shards" : {
        "total" : 5,
        "successful" : 5,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
            "value": 1000,
            "relation": "eq"
        },
        "max_score" : null,
        "hits" : [ {
          "_index" : "bank",
          "_type" : "_doc",
          "_id" : "0",
          "sort": [0],
          "_score" : null,
          "_source" : {"account_number":0,"balance":16623,"firstname":"Bradshaw","lastname":"Mckenzie","age":29,"gender":"F","address":"244 Columbus Place","employer":"Euron","email":"bradshawmckenzie@euron.com","city":"Hobucken","state":"CO"}
        }, {
          "_index" : "bank",
          "_type" : "_doc",
          "_id" : "1",
          "sort": [1],
          "_score" : null,
          "_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"}
        }, ...
        ]
      }
    }
    

    响应还提供了以下关于搜索请求的信息:

    • took – Elasticsearch运行查询需要多长时间(以毫秒为单位)
    • timed_out – 搜索请求是否超时
    • _shards – 搜索了多少碎片,并对多少碎片成功、失败或跳过进行了细分。
    • max_score – 找到最相关的文档的得分
    • hits.total.value - 找到了多少匹配的文档
    • hits.sort - 文档的排序位置(当不根据相关性得分排序时)
    • hits._score - 文档的相关性评分(在使用match_all时不适用)

    每个搜索请求都是独立的:Elasticsearch在请求中不维护任何状态信息。要翻阅搜索结果,请在您的请求中指定fromsize参数。

    例如,下面的请求从10次到19次:

    GET /bank/_search
    {
      "query": { "match_all": {} },
      "sort": [
        { "account_number": "asc" }
      ],
      "from": 10,
      "size": 10
    }
    

    cURL

    curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "match_all": {} }, "sort": [ { "account_number": "asc" } ], "from": 10, "size": 10 } '
    

    现在您已经了解了如何提交基本的搜索请求,您可以开始构建比match_all更有趣的查询。

    要在字段中搜索特定的术语,可以使用match查询。例如,下面的请求搜索地址字段,以查找地址包含 mill 或 lane 的客户:

    GET /bank/_search
    {
      "query": { "match": { "address": "mill lane" } }
    }
    

    cURL:

    curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "match": { "address": "mill lane" } } } '
    

    要执行短语搜索而不是匹配单个词汇,可以使用match_phrase而不是match。例如,下面的请求只匹配包含短语mill lane的地址:

    GET /bank/_search
    {
      "query": { "match_phrase": { "address": "mill lane" } }
    }
    

    cURL:

    curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "match_phrase": { "address": "mill lane" } } } '
    

    要构造更复杂的查询,可以使用bool查询来组合多个查询条件。您可以指定标准的要求,必须匹配(must match)、期望的应该匹配(should match)或不期望的必须不匹配(must not match)的条件。

    例如,下面的请求在银行索引中搜索属于40岁客户的帐户,但不包括居住在Idaho (ID)的任何人:

    GET /bank/_search
    {
      "query": {
        "bool": {
          "must": [
            { "match": { "age": "40" } }
          ],
          "must_not": [
            { "match": { "state": "ID" } }
          ]
        }
      }
    }
    

    cURL:

    curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "bool": { "must": [ { "match": { "age": "40" } } ], "must_not": [ { "match": { "state": "ID" } } ] } } } '
    

    布尔查询中的每个must、should和must_not元素都被称为查询子句。文档符合每个“必须”或“应该”子句中的标准的程度决定了文档的相关性评分。得分越高,文档就越符合您的搜索条件。默认情况下,Elasticsearch返回根据这些相关性得分排序的文档。

    must_not子句中的条件被视为过滤器。它影响文档是否包含在结果中,但不影响文档的评分方式。您还可以显式地指定任意筛选器来包含或排除基于结构化数据的文档。

    例如,下面的请求使用范围筛选器将结果限制到余额在$20,000到$30,000(包括)之间的帐户。

    GET /bank/_search
    {
      "query": {
        "bool": {
          "must": { "match_all": {} },
          "filter": {
            "range": {
              "balance": {
                "gte": 20000,
                "lte": 30000
              }
            }
          }
        }
      }
    }
    

    cURL:

    curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "bool": { "must": { "match_all": {} }, "filter": { "range": { "balance": { "gte": 20000, "lte": 30000 } } } } } } '