REST API 使用 HTTP 协议, 采用 JSON 格式

多索引查询

测试数据

  1. POST test1/_doc
  2. {
  3. "name": "tobias1",
  4. "company": 100912
  5. }
  6. POST test2/_doc
  7. {
  8. "name": "tobias2",
  9. "company": 100913
  10. }
  11. POST test3/_doc
  12. {
  13. "name": "tobias3",
  14. "company": 100914
  15. }

GET test*/_search/

  1. {
  2. "took" : 1,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 3,
  6. "successful" : 3,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 3,
  13. "relation" : "eq"
  14. },
  15. "max_score" : 1.0,
  16. "hits" : [
  17. {
  18. "_index" : "test1",
  19. "_type" : "_doc",
  20. "_id" : "MBGYKIABA0UtmzhiVsN9",
  21. "_score" : 1.0,
  22. "_source" : {
  23. "name" : "tobias1",
  24. "company" : 100912
  25. }
  26. },
  27. {
  28. "_index" : "test2",
  29. "_type" : "_doc",
  30. "_id" : "MRGYKIABA0UtmzhiVsPV",
  31. "_score" : 1.0,
  32. "_source" : {
  33. "name" : "tobias2",
  34. "company" : 100913
  35. }
  36. },
  37. {
  38. "_index" : "test3",
  39. "_type" : "_doc",
  40. "_id" : "MhGYKIABA0UtmzhiV8Ms",
  41. "_score" : 1.0,
  42. "_source" : {
  43. "name" : "tobias3",
  44. "company" : 100914
  45. }
  46. }
  47. ]
  48. }
  49. }
  • 支持通配符

    GET xxx*/_search

    1. {
    2. "took" : 0,
    3. "timed_out" : false,
    4. "_shards" : {
    5. "total" : 0,
    6. "successful" : 0,
    7. "skipped" : 0,
    8. "failed" : 0
    9. },
    10. "hits" : {
    11. "total" : {
    12. "value" : 0,
    13. "relation" : "eq"
    14. },
    15. "max_score" : 0.0,
    16. "hits" : [ ]
    17. }
    18. }
  • 无通配符开头的数据,默认 allow_no_indices=true

    GET xxx*/_search?allow_no_indices=false

    1. {
    2. "error" : {
    3. "root_cause" : [
    4. {
    5. "type" : "index_not_found_exception",
    6. "reason" : "no such index [xxx*]",
    7. "resource.type" : "index_or_alias",
    8. "resource.id" : "xxx*",
    9. "index_uuid" : "_na_",
    10. "index" : "xxx*"
    11. }
    12. ],
    13. "type" : "index_not_found_exception",
    14. "reason" : "no such index [xxx*]",
    15. "resource.type" : "index_or_alias",
    16. "resource.id" : "xxx*",
    17. "index_uuid" : "_na_",
    18. "index" : "xxx*"
    19. },
    20. "status" : 404
    21. }
  • 不允许索引不存在将会响应失败

日期数学格式

测试数据

  1. POST test-2022_04_12/_doc
  2. {
  3. "context":"hello,how are you"
  4. }
  5. POST test-2022_04_13/_doc
  6. {
  7. "context":"hello,how are you 04-13"
  8. }
  9. POST test-2022_04_14/_doc
  10. {
  11. "context":"hello,how are you 04-14"
  12. }

GET /test-2022_04_12,test-2022_04_13,test-2022_04_14/_search

  • 假如今天是 2022_04_15, 如上查询与下面查询等同 ```json //GET //_search{ “query”: { “match_all”: {} } }

//所有特殊字符都应进行URI编码,如下查询有效

GET /%3Ctest-%7Bnow%2Fd-3d%7Byyyy_MM_dd%7D%7D%3E%2C%3Ctest-%7Bnow%2Fd-2d%7Byyyy_MM_dd%7D%7D%3E%2C%3Ctest-%7Bnow%2Fd-d%7Byyyy_MM_dd%7D%7D%3E/_search { “query”: { “match_all”: {} } }

/////////////////////////////////////////////////////////////////////////////// { “took” : 1, “timed_out” : false, “_shards” : { “total” : 3, “successful” : 3, “skipped” : 0, “failed” : 0 }, “hits” : { “total” : { “value” : 3, “relation” : “eq” }, “max_score” : 1.0, “hits” : [ { “_index” : “test-2022_04_12”, “_type” : “_doc”, “_id” : “TRGwLYABA0UtmzhicsNN”, “_score” : 1.0, “_source” : { “context” : “hello,how are you” } }, { “_index” : “test-2022_04_13”, “_type” : “_doc”, “_id” : “ThG3LYABA0UtmzhixMOO”, “_score” : 1.0, “_source” : { “context” : “hello,how are you 04-13” } }, { “_index” : “test-2022_04_14”, “_type” : “_doc”, “_id” : “TxG3LYABA0Utmzhi8sM-“, “_score” : 1.0, “_source” : { “context” : “hello,how are you 04-14” } } ] } } ```

日期数学字符百分比编码

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

日期数学格式解析实例

  • 假如now 为 2022-04-15 下午 | 索引模式 | 解析结果 | | —- | —- | | | test-2022.04.15 | | | test-2022.04.01 | | | test-2022.04 | | | test-2022.03 | | | test-2022.04.16 |