Execute Stored Script API
Execute Stored Script API
1.0에서 도입
Create Stored Script API로 클러스터 상태에 저장해 둔 스크립트를 실행해요. 자세한 내용은 Painless 스크립팅 언어 문서를 참고해요.
OpenSearch는 스크립트를 실행할 여러 방법을 제공해요. 아래 섹션들은 GET <index>/_search 요청의 요청 본문에 스크립트 정보를 넘겨 실행하는 방법을 보여줘요.
출처: 문서
본문
엔드포인트 (Endpoints)
GET books/_search
{
"script_fields": {
"total_ratings": {
"script": {
"id": "my-first-script"
}
}
}
}
요청 본문 필드 (Request body fields)
| 필드 | 데이터 타입 | 설명 |
|---|---|---|
query |
Object | 처리할 문서를 지정하는 필터예요. |
script_fields |
Object | 출력에 포함할 필드예요. |
script |
Object | 필드의 값을 만들어내는 스크립트 ID예요. |
예시 요청 (Example request)
GET /books/_search
{
"query": {
"match_all": {}
},
"script_fields": {
"total_ratings": {
"script": {
"id": "multiplier-script",
"params": {
"multiplier": 2
}
}
}
}
}
Python 클라이언트로는 이렇게 호출해요:
response = client.search(
index = "books",
body = {
"query": {
"match_all": {}
},
"script_fields": {
"total_ratings": {
"script": {
"id": "multiplier-script",
"params": {
"multiplier": 2
}
}
}
}
}
)
다음 요청은 Create or update stored script에서 만든 저장된 스크립트를 실행해요. 스크립트는 각 책의 평점을 합산하고 그 합을 출력의 total_ratings 필드에 보여줘요.
스크립트의 대상은 books 인덱스예요.
"match_all": {} 속성 값은 빈 객체로, 인덱스의 각 문서를 처리하라는 뜻이에요.
total_ratings 필드 값은 my-first-script 실행 결과예요. Create or update stored script를 참고해요.
GET /books/_search
{
"query": {
"match_all": {}
},
"script_fields": {
"total_ratings": {
"script": {
"id": "my-first-script"
}
}
}
}
Python 클라이언트로는 이렇게 호출해요:
response = client.search(
index = "books",
body = {
"query": {
"match_all": {}
},
"script_fields": {
"total_ratings": {
"script": {
"id": "my-first-script"
}
}
}
}
)
예시 응답
GET books/_search 요청은 다음 필드를 반환해요:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "books",
"_id" : "1",
"_score" : 1.0,
"fields" : {
"total_ratings" : [
12
]
}
},
{
"_index" : "books",
"_id" : "2",
"_score" : 1.0,
"fields" : {
"total_ratings" : [
15
]
}
},
{
"_index" : "books",
"_id" : "3",
"_score" : 1.0,
"fields" : {
"total_ratings" : [
8
]
}
}
]
}
}
응답 본문 필드 (Response body fields)
| 필드 | 데이터 타입 | 설명 |
|---|---|---|
took |
Integer | 작업이 걸린 시간(밀리초)이에요. |
timed_out |
Boolean | 작업이 타임아웃됐는지 여부예요. |
_shards |
Object | 처리된 총 샤드 수와 성공·건너뜀·처리 안 됨 수예요. |
hits |
Object | 처리된 문서에 대한 상위 수준 정보와 hits 객체 배열을 담고 있어요. Hits 객체를 참고해요. |
Hits 객체
| 필드 | 데이터 타입 | 설명 |
|---|---|---|
total |
Object | 처리된 총 문서 수와 match 요청 필드와의 관계예요. |
max_score |
Double | 모든 히트에서 반환된 최고 관련성 점수예요. |
hits |
Array | 처리된 각 문서에 대한 정보예요. Document 객체를 참고해요. |
Document 객체
| 필드 | 데이터 타입 | 설명 |
|---|---|---|
_index |
String | 문서를 담고 있는 인덱스예요. |
_id |
String | 문서 ID예요. |
_score |
Float | 문서의 관련성 점수예요. |
fields |
Object | 스크립트에서 반환된 필드와 그 값이에요. |
매개변수가 있는 Painless 저장된 스크립트 실행하기 (Running a Painless stored script with parameters)
쿼리를 실행할 때마다 스크립트에 다른 매개변수를 전달하려면 script_fields에서 params를 정의해요.
다음 요청은 Create or update stored script에서 만든 저장된 스크립트를 실행해요. 스크립트는 각 책의 평점을 합산하고, 합산 값을 multiplier 매개변수로 곱한 결과를 출력에 보여줘요.
스크립트의 대상은 books 인덱스예요.
"match_all": {} 속성 값은 빈 객체로, 인덱스의 각 문서를 처리한다는 뜻이에요.
total_ratings 필드 값은 multiplier-script 실행 결과예요. 매개변수가 있는 저장된 스크립트 만들기·업데이트하기를 참고해요.
params 필드의 "multiplier": 2는 저장된 스크립트 multiplier-script에 전달되는 변수예요:
GET /books/_search
{
"query": {
"match_all": {}
},
"script_fields": {
"total_ratings": {
"script": {
"id": "multiplier-script",
"params": {
"multiplier": 2
}
}
}
}
}
Python 클라이언트로는 이렇게 호출해요:
response = client.search(
index = "books",
body = {
"query": {
"match_all": {}
},
"script_fields": {
"total_ratings": {
"script": {
"id": "multiplier-script",
"params": {
"multiplier": 2
}
}
}
}
}
)
응답에는 일치하는 문서가 들어 있어요:
{
"took" : 12,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "books",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"fields" : {
"total_ratings" : [
16
]
}
},
{
"_index" : "books",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"fields" : {
"total_ratings" : [
30
]
}
},
{
"_index" : "books",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"fields" : {
"total_ratings" : [
24
]
}
}
]
}
}
Painless 저장된 스크립트로 결과 정렬하기 (Sorting results using a Painless stored script)
다음 예시는 Painless 저장된 스크립트로 결과를 정렬해요:
GET books/_search
{
"query": {
"match_all": {}
},
"script_fields": {
"total_ratings": {
"script": {
"id": "multiplier-script",
"params": {
"multiplier": 2
}
}
}
},
"sort": {
"_script": {
"type": "number",
"script": {
"id": "multiplier-script",
"params": {
"multiplier": 2
}
},
"order": "desc"
}
}
}
예시 응답
{
"took" : 90,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "books",
"_type" : "_doc",
"_id" : "2",
"_score" : null,
"fields" : {
"total_ratings" : [
30
]
},
"sort" : [
30.0
]
},
{
"_index" : "books",
"_type" : "_doc",
"_id" : "1",
"_score" : null,
"fields" : {
"total_ratings" : [
24
]
},
"sort" : [
24.0
]
},
{
"_index" : "books",
"_type" : "_doc",
"_id" : "3",
"_score" : null,
"fields" : {
"total_ratings" : [
16
]
},
"sort" : [
16.0
]
}
]
}
}