ランタイムフィールドの使用

runtime_mappings パラメータを使用して、検索中に既存のものから ランタイムフィールド または列を抽出および作成します。

次の検索は release_day_of_week ランタイムフィールドを release_date から作成し、レスポンスに返します。

Python

  1. resp = client.sql.query(
  2. format="txt",
  3. runtime_mappings={
  4. "release_day_of_week": {
  5. "type": "keyword",
  6. "script": "\n emit(doc['release_date'].value.dayOfWeekEnum.toString())\n "
  7. }
  8. },
  9. query="\n SELECT * FROM library WHERE page_count > 300 AND author = 'Frank Herbert'\n ",
  10. )
  11. print(resp)

Ruby

  1. response = client.sql.query(
  2. format: 'txt',
  3. body: {
  4. runtime_mappings: {
  5. release_day_of_week: {
  6. type: 'keyword',
  7. script: "\n emit(doc['release_date'].value.dayOfWeekEnum.toString())\n "
  8. }
  9. },
  10. query: "\n SELECT * FROM library WHERE page_count > 300 AND author = 'Frank Herbert'\n "
  11. }
  12. )
  13. puts response

Js

  1. const response = await client.sql.query({
  2. format: "txt",
  3. runtime_mappings: {
  4. release_day_of_week: {
  5. type: "keyword",
  6. script:
  7. "\n emit(doc['release_date'].value.dayOfWeekEnum.toString())\n ",
  8. },
  9. },
  10. query:
  11. "\n SELECT * FROM library WHERE page_count > 300 AND author = 'Frank Herbert'\n ",
  12. });
  13. console.log(response);

コンソール

  1. POST _sql?format=txt
  2. {
  3. "runtime_mappings": {
  4. "release_day_of_week": {
  5. "type": "keyword",
  6. "script": """
  7. emit(doc['release_date'].value.dayOfWeekEnum.toString())
  8. """
  9. }
  10. },
  11. "query": """
  12. SELECT * FROM library WHERE page_count > 300 AND author = 'Frank Herbert'
  13. """
  14. }

APIは次のように返します:

Txt

  1. author | name | page_count | release_date |release_day_of_week
  2. ---------------+---------------+---------------+------------------------+-------------------
  3. Frank Herbert |Dune |604 |1965-06-01T00:00:00.000Z|TUESDAY