elasticsearch笔记-002-API通用可选参数

[toc]

1. API通用可选参数

1.1. 美观、格式化结果数据

  • ?pretty=true 格式化为json
  • ?format=yaml 结果展示为 yml
  • ?human=false 关闭 1h/1kb等人性化转换

1.2 响应过滤:filter_path

1.2.1 响应过滤用法

原来请求:

GET /bank/_doc/1

原响应:

{
  "_index" : "bank",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 3,
  "_seq_no" : 1001,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "account_number" : 1,
    "balance" : 39225,
    "firstname" : "Amber",
    "lastname" : "Duke",
    "age" : 32,
    "gender" : "M",
    "address" : "880 Holmes Lane",
    "employer" : "Pyrami",
    "email" : "[email protected]",
    "city" : "Brogan",
    "state" : "IL"
  }
}

加上响应过滤:采用逗号分隔的过滤器列表

GET /bank/_doc/1?filter_path=_version,_source.firstname,_source.age,_source.balance

加上后的响应:

{
  "_version" : 3,
  "_source" : {
    "balance" : 39225,
    "firstname" : "Amber",
    "age" : 32
  }
}

1.2.2 响应过滤支持通配符 "*"

请求:

# 响应过滤
GET /bank/_doc/1?filter_path=_version,_source.*name,_source.age,_source.*e

响应:

{
  "_version" : 3,
  "_source" : {
    "balance" : 39225,
    "firstname" : "Amber",
    "lastname" : "Duke",
    "age" : 32,
    "state" : "IL"
  }
}

原响应参考上边 1.2.1所列;

1.2.3 响应过滤:排除字段 "-"

请求:

GET /bank/_doc/1?filter_path=-_type,-_source

过滤后响应:

{
  "_index" : "bank",
  "_id" : "1",
  "_version" : 3,
  "_seq_no" : 1001,
  "_primary_term" : 1,
  "found" : true
}

可见, _type字段和_source节点,都被排除掉了;

1.2.4 响应过滤:*-混用时,先排除,再过滤

例1:

GET /bank/_doc/1?filter_path=_version,_source.*name,_source.age,_source.*e,-_source

结果:

{
  "_version" : 3
}

例2:

GET /bank/_doc/1?filter_path=_version,_source.*name,_source.age,_source.*e,-_source.lastname

结果:

{
  "_version" : 3,
  "_source" : {
    "balance" : 39225,
    "firstname" : "Amber",
    "age" : 32,
    "state" : "IL"
  }
}

1.3 时间单位

2d for 2 days

d Days
h Hours
m Minutes
s Seconds
ms Milliseconds
micros Microseconds
nanos Nanoseconds

 1.4 字节单位

b Bytes
kb Kilobytes
mb Megabytes
gb Gigabytes
tb Terabytes
pb Petabytes

1.5 打印错误堆栈踪迹:error_trace=true

看下面3个输出

GET /library/_search?size=1&filter_path=hits.hits._source
{
  "hits" : {
    "hits" : [
      {
        "_source" : {
          "title" : "Book #1",
          "rating" : 200.1
        }
      }
    ]
  }
}

这个是正确时的输出.

我们传个错误参数 size=Y

GET /library/_search?size=Y&filter_path=hits.hits._source

输出:

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "Failed to parse int parameter [size] with value [Y]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "Failed to parse int parameter [size] with value [Y]",
    "caused_by": {
      "type": "number_format_exception",
      "reason": "For input string: \"Y\""
    }
  },
  "status": 400
}

下面再看个完整堆栈信息的方式: error_trace

GET /library/_search?size=Y&filter_path=hits.hits._source&error_trace=true

outpu:

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "Failed to parse int parameter [size] with value [Y]",
        "stack_trace": "[Failed to parse int parameter [size] with value [Y]]; nested: IllegalArgumentException[Failed to parse int parameter [size] with value [Y]]; nested: NumberFormatException.........."
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "Failed to parse int parameter [size] with value [Y]",
    "caused_by": {
      "type": "number_format_exception",
      "reason": "For input string: \"Y\"",
      "stack_trace": "java.lang.NumberFormatException: For input string: \"Y\"\n\tat java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)................"
  },
  "status": 400
}

........省去了很多输出信息!

你可能感兴趣的