Elasticsearch 7.10 之 Start searching

288 阅读3分钟

将一些数据摄入 Elasticsearch 索引后,您可以通过将请求发送到 _search 端点来进行搜索。要访问全套搜索功能,请使用 Elasticsearch Query DSL 在请求正文中指定搜索条件。您可以在请求 URI 中指定要搜索的索引的名称。

例如,以下请求将检索 bank 索引中按帐号排序的所有文档:

GET /bank/_search
{
  "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
}

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

要在字段中搜索特定 term ,可以使用匹配查询。例如,以下请求搜索 address 字段以查找其中包含 milllane 的客户:

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

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

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

要构造更复杂的查询,可以使用 bool 查询来组合多个查询条件。您可以根据需要(必须匹配),期望(应该匹配)或不期望(必须不匹配)指定条件。

例如,以下请求在 bank 索引中搜索属于 40 岁客户的帐户,但不包括居住在爱达荷州(ID)的任何人:

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

布尔查询中的每个 mustshouldmust_not 元素都称为查询子句。文档满足每个 mustshould 子句中的条件的程度有助于提高文档的相关性得分。分数越高,文档就越符合您的搜索条件。默认情况下,Elasticsearch 返回按这些相关性分数排名的文档。

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

例如,以下请求使用范围过滤器将结果限制为余额在 20000 美元到 30000 美元(含)之间的帐户。

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

详情见官网:www.elastic.co/guide/en/el…

翻译不准请多指教,翻译不易请勿盗用,如使用请标明出处