1. 程式人生 > >elasticsearch 第四篇(API約定)

elasticsearch 第四篇(API約定)

aml cas jsonp har 計算 pre 只需要 cal use

對多個indices進行操作

es中大多resetapi支持請求多個index, 例如”test1,test2,test3”,index也可以使用通配符, 例如”test*“, 還可以使用+,-來包含或移除某個或某類index, 例如”test*,-test1”
支持設置多個的api的請求字符串可設置以下參數:

  • ignore_unavailable: 是否忽略單個index是否可用(不存在或關閉), true表示忽略, false表示不忽略, 默認為false, 例如查詢已經關閉的index:

輸入: GET /test1/user,account/_search?ignore_unavailable=false


輸出:

1
2
3
4
{
   "error": "IndexClosedException[[test1] closed]",
   "status": 403
}

輸入: GET /test1/user,account/_search?ignore_unavailable=false
輸出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 0,
      "successful": 0,
      "failed": 0
   },
   "hits": {
      "total": 0,
      "max_score": 0,
      "hits": []
   }
}

  • allow_no_indices: 是否忽略通配符匹配不到index(不存在或關閉)的情況, true表示允許, false表示不允許,默認為true, 例如查詢已經關閉的index:

輸入: GET /test*/_search?allow_no_indices=false
輸出:

1
2
3
4
{
   "error": "IndexMissingException[[test*] missing]",
   "status": 404
}

輸入: GET /test*/_search?allow_no_indices=true

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 0,
      "successful": 0,
      "failed": 0
   },
   "hits": {
      "total": 0,
      "max_score": 0,
      "hits": []
   }
}
  • expand_wildcards: 設置是否擴展通配符到closed的index中,open表示只在匹配並為open的index中查詢,closed表示在匹配的所有的index中查詢, 默認為closed, 例如查詢已經關閉的index
    輸入: GEt /test*/_search?expand_wildcards=closed
    輸出:
    1
    2
    3
    4
    
    {
       "error": "IndexClosedException[[test1] closed]",
       "status": 403
    }

公共參數

  • format: 表示返回數據的格式, 可選值為yaml和json兩種, 例如:
    輸入: GET /test1/user/_search?format=yaml
    輸出:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    ---
    took: 23
    timed_out: false
    _shards:
      total: 5
      successful: 5
      failed: 0
    hits:
      total: 1
      max_score: 1.0
      hits:
      - _index: "test1"
        _type: "user"
        _id: "1"
        _score: 1.0
        _source:
          name: "silence"
    
  • pretty: 表示在已json格式返回數據時是否以可視化的格式返回, false或未在設置表示不格式化, 否則格式化

  • human: 表示是否對返回結果進行格式化處理,比如3600(s)顯示1h

  • 查詢結果過濾
    主要使用filter_path參數進行設置

1.在返回結果中我們只關註took, hits.total, hits.hits._id, hits._source, 則我們可以發起如此請求:
輸入:GET /test1/user/_search?filter_path=took,hits.total,hits.hits._id,hits.hits._source
輸出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
   "took": 1,
   "hits": {
      "total": 1,
      "hits": [
         {
            "_id": "1",
            "_source": {
               "name": "silence"
            }
         }
      ]
   }
}

2.也可以使用統配符進行設置
輸入: GET /_nodes/stats?filter_path=nodes.*.*ost*,nodes.*.os.*u
輸出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
   "nodes": {
      "9jfW4VeWRta-Uq7Cq7bK34": {
         "host": "silence",
         "os": {
            "cpu": {
               "sys": 1,
               "user": 1,
               "idle": 96,
               "usage": 2,
               "stolen": 0
            }
         }
      }
   }
}

3.若層級較多時可使用**進行簡化
輸入: GET /_nodes/stats?filter_path=nodes.**.*sys*
輸出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
   "nodes": {
      "9jfW4VeWRta-Uq7Cq7bK34": {
         "os": {
            "cpu": {
               "sys": 2
            }
         },
         "process": {
            "cpu": {
               "sys_in_millis": 139106
            }
         }
      }
   }
}

4.若只需要_source中的某些值,則可以將filter_path和_source參數共同使用
輸入: GET /test1/account/_search?filter_path=hits.hits._source&_source=firstname,lastname,gender&size=2
輸出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
   "hits": {
      "hits": [
         {
            "_source": {
               "firstname": "Rodriquez",
               "gender": "F",
               "lastname": "Flores"
            }
         },
         {
            "_source": {
               "firstname": "Opal",
               "gender": "M",
               "lastname": "Meadows"
            }
         }
      ]
   }
}

5.flat_settings用於設置在查詢setting時,setting中的key格式, 默認為false:
輸入: GET /test1/_settings?flat_settings=true
輸出:

1
2
3
4
5
6
7
8
9
10
11
{
   "test1": {
      "settings": {
         "index.creation_date": "1442230557598",
         "index.uuid": "70bg061IRdKUdDNvgkUBoQ",
         "index.version.created": "1060099",
         "index.number_of_replicas": "1",
         "index.number_of_shards": "5"
      }
   }
}

輸入: GET /test1/_settings?flat_settings=false
輸出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
   "test1": {
      "settings": {
         "index": {
            "creation_date": "1442230557598",
            "number_of_shards": "5",
            "uuid": "70bg061IRdKUdDNvgkUBoQ",
            "version": {
               "created": "1060099"
            },
            "number_of_replicas": "1"
         }
      }
   }
}

  • 請求參數格式
    1.boolean: 在es中將”0”, 0, false, “false”, “off”識別為false,其他均按ture處理
    2.number
    3.time: 可以提交一個以毫秒時間的整數或者以日期標識結尾的字符串,例如”2d”表示2天,支持的格式有: y(year),M(month),w(week),d(day),h(hour),m(minute),s(second)
    4.距離: 可以提交一個以米為單位的證書或者以距離表示結尾的字符串,例如”2km”表示2千米,支持的格式有: mi/miles(mile英裏), yd/yards(yard碼), ft/feet(feet尺), in/inch(inch英寸), km/kilometers(kilometer千米), m/meters(meter米), cm/centimeters(centimeter厘米), mm/millimeters(millimeter毫米), NM/nmi/nauticalmiles(Nautical mile納米)
    5.模糊類型:
    a.數字,時間, IP:類似於range -fuzzines<=value<=+fuzzines
    b.字符串: 計算編輯距離

  • 返回結果中key的格式為駝峰還是下劃線分割, 通過case設置為camelCase則返回駝峰格式,否則為下劃線分割形式

  • jsonp: 可以用jsonp回調的方式調用es api, 需要通過callback設置回調函數名稱,並且需要在elasticsearch.yml中配置http.jsonp.enable: true來啟用jsonp格式

url訪問控制

可以通過代理方式進行es的url訪問控制,但是對於multi-search,multi-get和bulk等在請求參數中設置不同的index的情況很難解決.
為防止通過請求體設置index的情況,需要在elasticsearch.yml中設置rest.action.multi.allow_explicit_index:false, 此時es不允許在request body中設置index

如在修改前:
輸入:

1
2
3
4
5
POST /test1/user3/_bulk?pretty
{"index" : {"_index" : "test2", "_type" : "user1", "_id" : 1}}
{"name" : "silence1"}
{"index" : {"_index" : "test2", "_type" : "user1", "_id" : 2}}
{"name" : "silence2"}

輸出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
   "took": 225,
   "errors": false,
   "items": [
      {
         "index": {
            "_index": "test2",
            "_type": "user1",
            "_id": "1",
            "_version": 1,
            "status": 201
         }
      },
      {
         "index": {
            "_index": "test2",
            "_type": "user1",
            "_id": "2",
            "_version": 1,
            "status": 201
         }
      }
   ]
}

如在修改後(已重啟):
輸出:

1
2
3
4
{
   "error": "IllegalArgumentException[explicit index in bulk is not allowed]",
   "status": 500
}

輸入:

1
2
3
4
5
POST /test1/user3/_bulk?pretty
{"index" : {"_id" : 1}}
{"name" : "silence1"}
{"index" : {"_id" : 2}}
{"name" : "silence2"}

輸出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
   "took": 8,
   "errors": false,
   "items": [
      {
         "index": {
            "_index": "test1",
            "_type": "user3",
            "_id": "1",
            "_version": 1,
            "status": 201
         }
      },
      {
         "index": {
            "_index": "test1",
            "_type": "user3",
            "_id": "2",
            "_version": 1,
            "status": 201
         }
      }
   ]
}

elasticsearch 第四篇(API約定)