Execute Inline Script API
Execute Inline Script API
1.0에서 도입
Execute Inline Script API는 스크립트를 클러스터 상태에 저장하지 않고 바로 실행할 수 있게 해줘요. API가 호출될 때마다 스크립트가 컴파일되고 실행돼요. 자세한 내용은 Painless 스크립팅 언어 문서를 참고해요.
출처: 문서
본문
엔드포인트 (Endpoints)
GET /_scripts/painless/_execute
POST /_scripts/painless/_execute
요청 본문 필드 (Request body fields)
| 필드 | 설명 |
|---|---|
script |
실행할 스크립트예요. 필수예요. |
context |
스크립트의 컨텍스트예요. 선택 사항이며 기본값은 painless_test예요. |
context_setup |
컨텍스트의 추가 매개변수를 지정해요. 선택 사항이에요. |
예시 요청 (Example request)
POST /_scripts/painless/_execute
{
"script": {
"source": "doc['gpa_4_0'].value * params.max_gpa / 4.0",
"params": {
"max_gpa": 5.0
}
},
"context": "score",
"context_setup": {
"index": "testindex1",
"document": {
"gpa_4_0": 3.5
}
}
}
Python 클라이언트로는 이렇게 호출해요:
response = client.put_script(
id = "painless",
context = "_execute",
body = {
"script": {
"source": "doc['gpa_4_0'].value * params.max_gpa / 4.0",
"params": {
"max_gpa": 5.0
}
},
"context": "score",
"context_setup": {
"index": "testindex1",
"document": {
"gpa_4_0": 3.5
}
}
}
)
다음 요청은 스크립트에 기본 painless_context를 사용해요:
GET /_scripts/painless/_execute
{
"script": {
"source": "(params.x + params.y)/ 2",
"params": {
"x": 80,
"y": 100
}
}
}
Python 클라이언트로는 이렇게 호출해요:
response = client.scripts_painless_execute(
body = {
"script": {
"source": "(params.x + params.y)/ 2",
"params": {
"x": 80,
"y": 100
}
}
}
)
예시 응답 (Example response)
응답에는 두 스크립트 매개변수의 평균이 들어 있어요:
{
"result" : "90"
}
응답 본문 필드 (Response body fields)
| 필드 | 설명 |
|---|---|
result |
스크립트 결과예요. |
스크립트 컨텍스트 (Script contexts)
컨텍스트를 바꿔서 스크립트에 사용 가능한 변수와 결과의 반환 타입을 제어할 수 있어요. 기본 컨텍스트는 painless_test예요.
Painless test 컨텍스트
painless_test 컨텍스트는 기본 스크립트 컨텍스트로, 스크립트에 params 변수만 제공해요. 반환되는 결과는 항상 문자열로 변환돼요. 사용 예시는 앞선 예시 요청을 참고해요.
Filter 컨텍스트
filter 컨텍스트는 스크립트가 script 쿼리 안에 있는 것처럼 실행해요. 컨텍스트에 테스트 문서를 제공해야 해요. _source, 저장된 필드, _doc 변수를 스크립트에서 사용할 수 있어요.
filter 컨텍스트에 대해 context_setup에서 다음 매개변수를 지정할 수 있어요.
| 매개변수 | 설명 |
|---|---|
document |
메모리에 임시로 색인되어 스크립트에 사용 가능한 문서예요. |
index |
문서의 매핑이 들어 있는 인덱스 이름이에요. |
예를 들어 먼저 테스트 문서용 매핑이 있는 인덱스를 만들어요:
PUT /testindex1
{
"mappings": {
"properties": {
"grad": {
"type": "boolean"
},
"gpa": {
"type": "float"
}
}
}
}
학생이 최우등(우등)으로 졸업할 자격이 있는지 판단하는 스크립트를 실행해요:
POST /_scripts/painless/_execute
{
"script": {
"source": "doc['grad'].value == true && doc['gpa'].value >= params.min_honors_gpa",
"params": {
"min_honors_gpa": 3.5
}
},
"context": "filter",
"context_setup": {
"index": "testindex1",
"document": {
"grad": true,
"gpa": 3.79
}
}
}
Python 클라이언트로는 이렇게 호출해요:
response = client.put_script(
id = "painless",
context = "_execute",
body = {
"script": {
"source": "doc['grad'].value == true && doc['gpa'].value >= params.min_honors_gpa",
"params": {
"min_honors_gpa": 3.5
}
},
"context": "filter",
"context_setup": {
"index": "testindex1",
"document": {
"grad": true,
"gpa": 3.79
}
}
}
)
응답에는 결과가 들어 있어요:
{
"result" : true
}
Score 컨텍스트
score 컨텍스트는 스크립트가 function_score 쿼리의 script_score 함수 안에 있는 것처럼 실행해요.
score 컨텍스트에 대해 context_setup에서 다음 매개변수를 지정할 수 있어요.
| 매개변수 | 설명 |
|---|---|
document |
메모리에 임시로 색인되어 스크립트에 사용 가능한 문서예요. |
index |
문서의 매핑이 들어 있는 인덱스 이름이에요. |
query |
스크립트가 _score 매개변수를 사용한다면, 쿼리가 _score 필드로 점수를 계산하도록 지정할 수 있어요. |
예를 들어 먼저 테스트 문서용 매핑이 있는 인덱스를 만들어요:
PUT /testindex1
{
"mappings": {
"properties": {
"gpa_4_0": {
"type": "float"
}
}
}
}
4.0 척도의 GPA를 매개변수로 제공된 다른 척도로 변환하는 스크립트를 실행해요:
POST /_scripts/painless/_execute
{
"script": {
"source": "doc['gpa_4_0'].value * params.max_gpa / 4.0",
"params": {
"max_gpa": 5.0
}
},
"context": "score",
"context_setup": {
"index": "testindex1",
"document": {
"gpa_4_0": 3.5
}
}
}
Python 클라이언트로는 이렇게 호출해요:
response = client.put_script(
id = "painless",
context = "_execute",
body = {
"script": {
"source": "doc['gpa_4_0'].value * params.max_gpa / 4.0",
"params": {
"max_gpa": 5.0
}
},
"context": "score",
"context_setup": {
"index": "testindex1",
"document": {
"gpa_4_0": 3.5
}
}
}
)
응답에는 결과가 들어 있어요:
{
"result" : 4.375
}