ES API公约

1,多个索引

大多数indexAPI支持多个索引操作,如:(1)test1,test2,test3 (2)_all (3)通配符:test*.-test2

多索引API都支持以下url查询字符串参数:

ignore_unavailable 控制是否忽略任何指定的索引不可用。(true/false)
allow_no_indices 控制如果通配符表达式结果没有具体的索引,是否失败。(true/false)
expand_wildcards 控制什么样的具体指数通配符指数表达式扩展到。

单索引API不支持多索引,如:Document APIssingle-index alias APIs 

2.索引名(index names)中date math的支持

格式:<static_name{date_math_expr{date_format|time_zone}}>

索引名称的数学表达及对应的最终索引名称:当前日期 :2024.03.22

ExpressionResolves to

<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

 

特殊符号URI编码:

< > / { } | + : ,
%3C %3E %2F %7B %7D %7C %2B %3A %2C

# GET /<logstash-{now/d}>/
_search 执行索引时需要将所有的特殊符号进行URI编码 curl -XGET 'localhost:9200/%3Clogstash-%7Bnow%2Fd%7D%3E/_search?pretty' -H 'Content-Type: application/json' -d' { "query" : { "match": { "test": "data" } } } '
# GET /<logstash-{now/d-2d}>,<logstash-{now/d-1d}>,<logstash-{now/d}>/_search
curl -XGET 'localhost:9200/%3Clogstash-%7Bnow%2Fd-2d%7D%3E%2C%3Clogstash-%7Bnow%2Fd-1d%7D%3E%2C%3Clogstash-%7Bnow%2Fd%7D%3E/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query" : {
    "match": {
      "test": "data"
    }
  }
}
'

3.API中的常规操作

?pretty=true JSON可读格式
?format=yaml yaml可读格式
?human=false 默认为true适合人阅读,false返回计算机结果。eg:"exists_time": "1h" or "size": "1kb"< - >"exists_time_in_millis": 3600000 or "size_in_bytes": 1024
date  math  
filter_path
支持通配符
 减少返回字段,结果中只保留自己关心的字段。eg:

curl -XGET 'localhost:9200/_search?q=elasticsearch&filter_path=took,hits.hits._id,hits.hits._score&pretty'

结果:

{
  "took" : 3,
  "hits" : {
    "hits" : [
      {
        "_id" : "0",
        "_score" : 1.6375021
      }
    ]
  }
}

curl -XGET 'localhost:9200/_cluster/state?filter_path=metadata.indices.*.stat*&pretty'

结果:

{
  "metadata" : {
    "indices" : {
      "twitter": {"state": "open"}
    }
  }
}

flat_setting=true

返回结果扁平化

默认值为false
 

curl -XGET 'localhost:9200/twitter/_settings?flat_settings=true&pretty'

结果:

{

  "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"
    }
  }
}
curl -XGET 'localhost:9200/twitter/_settings?flat_settings=false&pretty'
{
  "twitter" : {
    "settings" : {
      "index" : {
        "number_of_replicas": "1",
        "number_of_shards": "1",
        "creation_date": "1474389951325",
        "uuid": "n6gzFZTgS664GUfx0Xrpjw",
        "version": {
          "created": ...
        },
        "provided_name" : "twitter"
      }
    }
  }
}

error_trace=true

捕获异常

curl -XPOST 'localhost:9200/twitter/_search?size=surprise_me&error_trace=true&pretty'

结果:

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "Failed to parse int parameter [size] with value [surprise_me]",
        "stack_trace": "Failed to parse int parameter [size] with value [surprise_me]]; nested: IllegalArgumentException..."//不然没有这句
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "Failed to parse int parameter [size] with value [surprise_me]",
    "stack_trace": "java.lang.IllegalArgumentException: Failed to parse int parameter [size] with value [surprise_me]
    at org.elasticsearch.rest.RestRequest.paramAsInt(RestRequest.java:175)...",
    "caused_by": {
      "type": "number_format_exception",
      "reason": "For input string: "surprise_me"",
      "stack_trace": "java.lang.NumberFormatException: For input string: "surprise_me"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)..."
    }
  },
  "status": 400
}

4.基于URL的访问控制---------不理解

许多用户使用基于URL访问控制的代理来保护对Elasticsearch索引的访问。对于多重搜索, 多重获取批量请求,用户可以选择在URL中指定一个索引,也可以在请求主体中的每个请求中指定一个索引。这可以使基于URL的访问控制具有挑战性。(什么鬼)

为防止用户覆盖URL中指定的索引,请将此设置添加到elasticsearch.yml文件中:

    rest.action.multi.allow_explicit_index:false

默认值是true,但是当设置false为时,Elasticsearch将拒绝在请求正文中指定具有显式索引的请求
原文地址:https://www.cnblogs.com/zhxdxf/p/8335807.html