ランタイムフィールドの使用
runtime_mappings
パラメータを使用して、検索中に既存のものから ランタイムフィールド または列を抽出および作成します。
次の検索は release_day_of_week
ランタイムフィールドを release_date
から作成し、レスポンスに返します。
Python
resp = client.sql.query(
format="txt",
runtime_mappings={
"release_day_of_week": {
"type": "keyword",
"script": "\n emit(doc['release_date'].value.dayOfWeekEnum.toString())\n "
}
},
query="\n SELECT * FROM library WHERE page_count > 300 AND author = 'Frank Herbert'\n ",
)
print(resp)
Ruby
response = client.sql.query(
format: 'txt',
body: {
runtime_mappings: {
release_day_of_week: {
type: 'keyword',
script: "\n emit(doc['release_date'].value.dayOfWeekEnum.toString())\n "
}
},
query: "\n SELECT * FROM library WHERE page_count > 300 AND author = 'Frank Herbert'\n "
}
)
puts response
Js
const response = await client.sql.query({
format: "txt",
runtime_mappings: {
release_day_of_week: {
type: "keyword",
script:
"\n emit(doc['release_date'].value.dayOfWeekEnum.toString())\n ",
},
},
query:
"\n SELECT * FROM library WHERE page_count > 300 AND author = 'Frank Herbert'\n ",
});
console.log(response);
コンソール
POST _sql?format=txt
{
"runtime_mappings": {
"release_day_of_week": {
"type": "keyword",
"script": """
emit(doc['release_date'].value.dayOfWeekEnum.toString())
"""
}
},
"query": """
SELECT * FROM library WHERE page_count > 300 AND author = 'Frank Herbert'
"""
}
APIは次のように返します:
Txt
author | name | page_count | release_date |release_day_of_week
---------------+---------------+---------------+------------------------+-------------------
Frank Herbert |Dune |604 |1965-06-01T00:00:00.000Z|TUESDAY