レスポンスデータフォーマット

テキスト形式は人間にとっては良いですが、コンピュータはより構造化されたものを好みます。

Elasticsearch SQLは、formatプロパティをURLで設定するか、Accept HTTPヘッダーを設定することで、以下のフォーマットでデータを返すことができます。

URLパラメータはAccept HTTPヘッダーよりも優先されます。どちらも指定されていない場合、レスポンスはリクエストと同じ形式で返されます。

フォーマット Accept HTTPヘッダー 説明
人間可読
csv text/csv カンマ区切り値
json application/json JSON (JavaScriptオブジェクト表記) 人間可読形式
tsv text/tab-separated-values タブ区切り値
txt text/plain CLIライクな表現
yaml application/yaml YAML (YAMLはマークアップ言語ではない) 人間可読形式
バイナリフォーマット
cbor application/cbor 簡潔なバイナリオブジェクト表現
smile application/smile Smile バイナリデータフォーマット、CBORに似ています

CSVフォーマットは、CSV値を区切るために使用する文字を示すフォーマットURLクエリ属性delimiterを受け入れます。デフォルトはカンマ(,)であり、次の値は使用できません: ダブルクォート(")、キャリッジリターン(\r)、および改行(\n)。タブ(\t)も使用できず、tsvフォーマットを代わりに使用する必要があります。

人間可読形式のいくつかの例を以下に示します:

CSV

Python

  1. resp = client.sql.query(
  2. format="csv",
  3. query="SELECT * FROM library ORDER BY page_count DESC",
  4. fetch_size=5,
  5. )
  6. print(resp)

Ruby

  1. response = client.sql.query(
  2. format: 'csv',
  3. body: {
  4. query: 'SELECT * FROM library ORDER BY page_count DESC',
  5. fetch_size: 5
  6. }
  7. )
  8. puts response

Js

  1. const response = await client.sql.query({
  2. format: "csv",
  3. query: "SELECT * FROM library ORDER BY page_count DESC",
  4. fetch_size: 5,
  5. });
  6. console.log(response);

コンソール

  1. POST /_sql?format=csv
  2. {
  3. "query": "SELECT * FROM library ORDER BY page_count DESC",
  4. "fetch_size": 5
  5. }

返される内容は:

テキスト

  1. author,name,page_count,release_date
  2. Peter F. Hamilton,Pandora's Star,768,2004-03-02T00:00:00.000Z
  3. Vernor Vinge,A Fire Upon the Deep,613,1992-06-01T00:00:00.000Z
  4. Frank Herbert,Dune,604,1965-06-01T00:00:00.000Z
  5. Alastair Reynolds,Revelation Space,585,2000-03-15T00:00:00.000Z
  6. James S.A. Corey,Leviathan Wakes,561,2011-06-02T00:00:00.000Z

または:

Python

  1. resp = client.sql.query(
  2. format="csv",
  3. delimiter=";",
  4. query="SELECT * FROM library ORDER BY page_count DESC",
  5. fetch_size=5,
  6. )
  7. print(resp)

Ruby

  1. response = client.sql.query(
  2. format: 'csv',
  3. delimiter: ';',
  4. body: {
  5. query: 'SELECT * FROM library ORDER BY page_count DESC',
  6. fetch_size: 5
  7. }
  8. )
  9. puts response

Js

  1. const response = await client.sql.query({
  2. format: "csv",
  3. delimiter: ";",
  4. query: "SELECT * FROM library ORDER BY page_count DESC",
  5. fetch_size: 5,
  6. });
  7. console.log(response);

コンソール

  1. POST /_sql?format=csv&delimiter=%3b
  2. {
  3. "query": "SELECT * FROM library ORDER BY page_count DESC",
  4. "fetch_size": 5
  5. }

返される内容は:

テキスト

  1. author;name;page_count;release_date
  2. Peter F. Hamilton;Pandora's Star;768;2004-03-02T00:00:00.000Z
  3. Vernor Vinge;A Fire Upon the Deep;613;1992-06-01T00:00:00.000Z
  4. Frank Herbert;Dune;604;1965-06-01T00:00:00.000Z
  5. Alastair Reynolds;Revelation Space;585;2000-03-15T00:00:00.000Z
  6. James S.A. Corey;Leviathan Wakes;561;2011-06-02T00:00:00.000Z

JSON

Python

  1. resp = client.sql.query(
  2. format="json",
  3. query="SELECT * FROM library ORDER BY page_count DESC",
  4. fetch_size=5,
  5. )
  6. print(resp)

Ruby

  1. response = client.sql.query(
  2. format: 'json',
  3. body: {
  4. query: 'SELECT * FROM library ORDER BY page_count DESC',
  5. fetch_size: 5
  6. }
  7. )
  8. puts response

Js

  1. const response = await client.sql.query({
  2. format: "json",
  3. query: "SELECT * FROM library ORDER BY page_count DESC",
  4. fetch_size: 5,
  5. });
  6. console.log(response);

コンソール

  1. POST /_sql?format=json
  2. {
  3. "query": "SELECT * FROM library ORDER BY page_count DESC",
  4. "fetch_size": 5
  5. }

返される内容は:

コンソール結果

  1. {
  2. "columns": [
  3. {"name": "author", "type": "text"},
  4. {"name": "name", "type": "text"},
  5. {"name": "page_count", "type": "short"},
  6. {"name": "release_date", "type": "datetime"}
  7. ],
  8. "rows": [
  9. ["Peter F. Hamilton", "Pandora's Star", 768, "2004-03-02T00:00:00.000Z"],
  10. ["Vernor Vinge", "A Fire Upon the Deep", 613, "1992-06-01T00:00:00.000Z"],
  11. ["Frank Herbert", "Dune", 604, "1965-06-01T00:00:00.000Z"],
  12. ["Alastair Reynolds", "Revelation Space", 585, "2000-03-15T00:00:00.000Z"],
  13. ["James S.A. Corey", "Leviathan Wakes", 561, "2011-06-02T00:00:00.000Z"]
  14. ],
  15. "cursor": "sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWWWdrRlVfSS1TbDYtcW9lc1FJNmlYdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl+v///w8="
  16. }

TSV

Python

  1. resp = client.sql.query(
  2. format="tsv",
  3. query="SELECT * FROM library ORDER BY page_count DESC",
  4. fetch_size=5,
  5. )
  6. print(resp)

Ruby

  1. response = client.sql.query(
  2. format: 'tsv',
  3. body: {
  4. query: 'SELECT * FROM library ORDER BY page_count DESC',
  5. fetch_size: 5
  6. }
  7. )
  8. puts response

Js

  1. const response = await client.sql.query({
  2. format: "tsv",
  3. query: "SELECT * FROM library ORDER BY page_count DESC",
  4. fetch_size: 5,
  5. });
  6. console.log(response);

コンソール

  1. POST /_sql?format=tsv
  2. {
  3. "query": "SELECT * FROM library ORDER BY page_count DESC",
  4. "fetch_size": 5
  5. }

返される内容は:

テキスト

  1. author name page_count release_date
  2. Peter F. Hamilton Pandora's Star 768 2004-03-02T00:00:00.000Z
  3. Vernor Vinge A Fire Upon the Deep 613 1992-06-01T00:00:00.000Z
  4. Frank Herbert Dune 604 1965-06-01T00:00:00.000Z
  5. Alastair Reynolds Revelation Space 585 2000-03-15T00:00:00.000Z
  6. James S.A. Corey Leviathan Wakes 561 2011-06-02T00:00:00.000Z

TXT

Python

  1. resp = client.sql.query(
  2. format="txt",
  3. query="SELECT * FROM library ORDER BY page_count DESC",
  4. fetch_size=5,
  5. )
  6. print(resp)

Ruby

  1. response = client.sql.query(
  2. format: 'txt',
  3. body: {
  4. query: 'SELECT * FROM library ORDER BY page_count DESC',
  5. fetch_size: 5
  6. }
  7. )
  8. puts response

Js

  1. const response = await client.sql.query({
  2. format: "txt",
  3. query: "SELECT * FROM library ORDER BY page_count DESC",
  4. fetch_size: 5,
  5. });
  6. console.log(response);

コンソール

  1. POST /_sql?format=txt
  2. {
  3. "query": "SELECT * FROM library ORDER BY page_count DESC",
  4. "fetch_size": 5
  5. }

返される内容は:

テキスト

  1. author | name | page_count | release_date
  2. -----------------+--------------------+---------------+------------------------
  3. Peter F. Hamilton|Pandora's Star |768 |2004-03-02T00:00:00.000Z
  4. Vernor Vinge |A Fire Upon the Deep|613 |1992-06-01T00:00:00.000Z
  5. Frank Herbert |Dune |604 |1965-06-01T00:00:00.000Z
  6. Alastair Reynolds|Revelation Space |585 |2000-03-15T00:00:00.000Z
  7. James S.A. Corey |Leviathan Wakes |561 |2011-06-02T00:00:00.000Z

YAML

Python

  1. resp = client.sql.query(
  2. format="yaml",
  3. query="SELECT * FROM library ORDER BY page_count DESC",
  4. fetch_size=5,
  5. )
  6. print(resp)

Ruby

  1. response = client.sql.query(
  2. format: 'yaml',
  3. body: {
  4. query: 'SELECT * FROM library ORDER BY page_count DESC',
  5. fetch_size: 5
  6. }
  7. )
  8. puts response

Js

  1. const response = await client.sql.query({
  2. format: "yaml",
  3. query: "SELECT * FROM library ORDER BY page_count DESC",
  4. fetch_size: 5,
  5. });
  6. console.log(response);

コンソール

  1. POST /_sql?format=yaml
  2. {
  3. "query": "SELECT * FROM library ORDER BY page_count DESC",
  4. "fetch_size": 5
  5. }

返される内容は:

Yaml

  1. columns:
  2. - name: "author"
  3. type: "text"
  4. - name: "name"
  5. type: "text"
  6. - name: "page_count"
  7. type: "short"
  8. - name: "release_date"
  9. type: "datetime"
  10. rows:
  11. - - "Peter F. Hamilton"
  12. - "Pandora's Star"
  13. - 768
  14. - "2004-03-02T00:00:00.000Z"
  15. - - "Vernor Vinge"
  16. - "A Fire Upon the Deep"
  17. - 613
  18. - "1992-06-01T00:00:00.000Z"
  19. - - "Frank Herbert"
  20. - "Dune"
  21. - 604
  22. - "1965-06-01T00:00:00.000Z"
  23. - - "Alastair Reynolds"
  24. - "Revelation Space"
  25. - 585
  26. - "2000-03-15T00:00:00.000Z"
  27. - - "James S.A. Corey"
  28. - "Leviathan Wakes"
  29. - 561
  30. - "2011-06-02T00:00:00.000Z"
  31. cursor: "sDXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAEWWWdrRlVfSS1TbDYtcW9lc1FJNmlYdw==:BAFmBmF1dGhvcgFmBG5hbWUBZgpwYWdlX2NvdW50AWYMcmVsZWFzZV9kYXRl+v///w8="