Elasticsearch - API 约定



Web 应用中的应用程序编程接口 (API) 是一组函数调用或其他编程指令,用于访问特定 Web 应用程序中的软件组件。例如,Facebook API 帮助开发者通过访问 Facebook 的数据或其他功能(例如生日或状态更新)来创建应用程序。

Elasticsearch 提供了一个 REST API,它通过 HTTP 上的 JSON 进行访问。Elasticsearch 使用一些约定,我们现在将讨论这些约定。

多个索引

大多数 API 操作,主要是搜索和其他操作,都是针对一个或多个索引进行的。这有助于用户只需执行一次查询即可搜索多个位置或所有可用数据。许多不同的表示法用于在多个索引中执行操作。我们将在本章中讨论其中一些。

逗号分隔表示法

POST /index1,index2,index3/_search

请求体

{
   "query":{
      "query_string":{
         "query":"any_string"
      }
   }
}

响应

来自 index1、index2、index3 的 JSON 对象,其中包含 any_string。

_all 关键字用于所有索引

POST /_all/_search

请求体

{
   "query":{
      "query_string":{
         "query":"any_string"
      }
   }
}

响应

来自所有索引的 JSON 对象,其中包含 any_string。

通配符 (* , + , –)

POST /school*/_search

请求体

{
   "query":{
      "query_string":{
         "query":"CBSE"
      }
   }
}

响应

所有以 school 开头的索引中的 JSON 对象,其中包含 CBSE。

或者,您也可以使用以下代码:

POST /school*,-schools_gov /_search

请求体

{
   "query":{
      "query_string":{
         "query":"CBSE"
      }
   }
}

响应

所有以“school”开头但不是 schools_gov 的索引中的 JSON 对象,其中包含 CBSE。

还有一些 URL 查询字符串参数:

  • ignore_unavailable - 如果 URL 中存在的一个或多个索引不存在,则不会发生错误或不会停止操作。例如,schools 索引存在,但 book_shops 不存在。

POST /school*,book_shops/_search

请求体

{
   "query":{
      "query_string":{
         "query":"CBSE"
      }
   }
}

请求体

{
   "error":{
      "root_cause":[{
         "type":"index_not_found_exception", "reason":"no such index",
         "resource.type":"index_or_alias", "resource.id":"book_shops",
         "index":"book_shops"
      }],
      "type":"index_not_found_exception", "reason":"no such index",
      "resource.type":"index_or_alias", "resource.id":"book_shops",
      "index":"book_shops"
   },"status":404
}

考虑以下代码:

POST /school*,book_shops/_search?ignore_unavailable = true

请求体

{
   "query":{
      "query_string":{
         "query":"CBSE"
      }
   }
}

响应(无错误)

所有以 school 开头的索引中的 JSON 对象,其中包含 CBSE。

allow_no_indices

此参数的 true 值将阻止错误,如果带有通配符的 URL 导致没有索引。例如,没有以 schools_pri 开头的索引:

POST /schools_pri*/_search?allow_no_indices = true

请求体

{
   "query":{
      "match_all":{}
   }
}

响应(无错误)

{
   "took":1,"timed_out": false, "_shards":{"total":0, "successful":0, "failed":0},
   "hits":{"total":0, "max_score":0.0, "hits":[]}
}

expand_wildcards

此参数决定是否需要将通配符扩展到打开的索引或关闭的索引,或两者都执行。此参数的值可以是 open 和 closed 或 none 和 all。

例如,关闭索引 schools:

POST /schools/_close

响应

{"acknowledged":true}

考虑以下代码:

POST /school*/_search?expand_wildcards = closed

请求体

{
   "query":{
      "match_all":{}
   }
}

响应

{
   "error":{
      "root_cause":[{
         "type":"index_closed_exception", "reason":"closed", "index":"schools"
      }],
      "type":"index_closed_exception", "reason":"closed", "index":"schools"
   }, "status":403
}

索引名称中的日期数学支持

Elasticsearch 提供了一种根据日期和时间搜索索引的功能。我们需要以特定格式指定日期和时间。例如,accountdetail-2015.12.30 索引将存储 2015 年 12 月 30 日的银行账户详细信息。可以执行数学运算以获取特定日期或日期和时间范围的详细信息。

日期数学索引名称的格式:

<static_name{date_math_expr{date_format|time_zone}}>
/<accountdetail-{now-2d{YYYY.MM.dd|utc}}>/_search

static_name 是表达式的一部分,在每个日期数学索引(如 account detail)中保持不变。date_math_expr 包含确定日期和时间的动态表达式,例如 now-2d。date_format 包含索引中日期的书写格式,例如 YYYY.MM.dd。如果今天的日期是 2015 年 12 月 30 日,则 <accountdetail-{now-2d{YYYY.MM.dd}}> 将返回 accountdetail-2015.12.28。

表达式 解析为
<accountdetail-{now-d}> accountdetail-2015.12.29
<accountdetail-{now-M}> accountdetail-2015.11.30
<accountdetail-{now{YYYY.MM}}> accountdetail-2015.12

我们现在将了解 Elasticsearch 中的一些常用选项,这些选项可用于以指定格式获取响应。

漂亮的结果

我们可以通过附加一个 URL 查询参数(即 pretty = true)来以格式良好的 JSON 对象获取响应。

POST /schools/_search?pretty = true

请求体

{
   "query":{
      "match_all":{}
   }
}

响应

……………………..
{
   "_index" : "schools", "_type" : "school", "_id" : "1", "_score" : 1.0,
   "_source":{
      "name":"Central School", "description":"CBSE Affiliation",
      "street":"Nagan", "city":"paprola", "state":"HP", "zip":"176115",
      "location": [31.8955385, 76.8380405], "fees":2000,
      "tags":["Senior Secondary", "beautiful campus"], "rating":"3.5"
   }
}
………………….

人类可读的输出

此选项可以将统计响应更改为人机可读形式(如果 human = true)或计算机可读形式(如果 human = false)。例如,如果 human = true,则 distance_kilometer = 20KM;如果 human = false,则 distance_meter = 20000,当响应需要由另一个计算机程序使用时。

响应过滤

我们可以通过在 field_path 参数中添加它们来过滤响应到较少的字段。例如,

POST /schools/_search?filter_path = hits.total

请求体

{
   "query":{
      "match_all":{}
   }
}

响应

{"hits":{"total":3}}
广告
© . All rights reserved.