API约定与规范

多索引

在查询语句可以指定一个索引或者多个索引,多个索引用“,”连接即可,elasticsearch也支持用通配符的方式指定多个索引,索引前缀加“-”表示把该索引排除:

test1,test2,test3
test*,-test3
_all 表示所有的索引

多索引api的url同时也支持以下参数:

  • ignore_unavailable 取值为true/false 表示是否忽略不可用的索引(关闭的索引或者不存在的索引)
  • allow_no_indices 取值为true/false 表示如果没有匹配到索引是否会返回错误
  • expand_wildcards 控制通配符的匹配对象,其中取值open:只匹配到状态为open的索引 close:只匹配状态为close的索引 (open,closed) :表示匹配所有索引 none:关闭通配符功能 all:同(open,closed)

https://www.cnblogs.com/xing901022/p/5281769.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-index.html

索引名称支持日期表达式

有的时候查询的数据分布在多个索引中,这个时候需要跨索引搜索,虽然通配符可以模糊的指定索引但如果索引指定过多会给elasticsearch集群造成过大的负载压力,因此对于带日期的索引可以通过date math的方式指定索引。
date math index name的形式如下:

<static_name{date_math_expr{date_format|time_zone}}>
  • static_name:is the static text part of the name
  • date_math_expr is a dynamic date math expression that computes the
    date dynamically
  • date_format is the optional format in which the computed date should
    be rendered. Defaults to YYYY.MM.dd.
  • time_zone is the optional time zone . Defaults to utc.

假如今天是2024年3月22号

表达式 解析值
<logstash-{now/d}> logstash-2024.03.22
<logstash-{now/M}> logstash-2024.03.01
<logstash-{now/M{YYYY.MM}}> logstash-2024.03
<logstash-{now/M-1M{YYYY.MM}}> logstash-2024.02
<logstash-{now/d{YYYY.MM.dd|+12:00}}> logstash-2024.03.23

如果需要用“{}”符号,需要用“\”进行转义,如:

<elastic\\{ON\\}-{now/M}> 解析值为: elastic{ON}-2024.03.01

如果查询过去三天的logstash日志,则:

# GET /<logstash-{now/d-2d}>,<logstash-{now/d-1d}>,<logstash-{now/d}>/_search

通用配置

pretty result

  • 请求参数加上?pretty=true可以使查询结果可读性提高

  • 请求参数加上?format=yaml可以使查询结果以yaml的格式显示

可读性输出

请求参数加上?human=false,默认为false

日期表达式

表达式的开始时间(可以是字母now或者是字符串 + “||”),表达式可以使以下的多种组合:

扫描二维码关注公众号,回复: 5235763 查看本文章
  • +1h - add one hour
  • -1d - subtract one day
  • /d - round down to the nearest day

https://www.elastic.co/guide/en/elasticsearch/reference/6.1/common-options.html#date-math

响应过滤

请求的filter_path参数可以用来缩减响应内容,具体请参考:

https://www.elastic.co/guide/en/elasticsearch/reference/6.1/common-options.html#common-options-response-filtering

平铺设置

请求的flat_settings参数可以用来平铺字段(主要对setting内容有效)
如:

GET twitter/_settings?flat_settings=true

返回:

{
  "twitter" : {
    "settings": {
      "index.number_of_replicas": "1",
      "index.number_of_shards": "1",
      "index.creation_date": "1474389951325",
      "index.uuid": "n6gzFZTgS664GUfx0Xrpjw",
      "index.version.created": ...,
      "index.provided_name" : "twitter"
    }
  }
}

GET twitter/_settings?flat_settings=false

返回:

{
  "twitter" : {
    "settings" : {
      "index" : {
        "number_of_replicas": "1",
        "number_of_shards": "1",
        "creation_date": "1474389951325",
        "uuid": "n6gzFZTgS664GUfx0Xrpjw",
        "version": {
          "created": ... },
        "provided_name" : "twitter"
      }
    }
  }
}

参数值

  • elasticsearch中“false”表示布尔型false,“true”表示布尔型true
  • elasticsearch除了支持json的数字表达同时也支持字符串数字,“2”、“2.3”

其他参考:

https://www.elastic.co/guide/en/elasticsearch/reference/6.1/common-options.html#_parameters

猜你喜欢

转载自blog.csdn.net/fsz9065/article/details/78941961