startsWith() function
출처: 문서
본문
startsWith() 함수
startsWith() 함수는 문자열이 주어진 문자열로 시작하는지 확인해요. 이 함수는 두 개의 인자를 받아요:
-
첫 번째 인자는 리터럴 문자열이거나, 검사할 필드나 값을 나타내는 JSON pointer예요.
-
두 번째 인자는 첫 번째 인자에서 검사할 문자열이에요.
첫 번째 인자가 나타내는 문자열이나 필드 값이 두 번째 인자로 지정한 문자열로 시작하면 함수는 true를 반환하고, 그렇지 않으면 false를 반환해요.
예를 들어, 필드 이름 message의 값이 문자열 "abcd"로 시작하는지 확인하려면 startsWith() 함수를 다음과 같이 사용해요:
startsWith('/message', 'abcd')
{% include copy.html %}
이 호출은 message 필드가 문자열 abcd로 시작하면 true를, 그렇지 않으면 false를 반환해요.
또는 첫 번째 인자로 리터럴 문자열을 사용할 수도 있어요:
startsWith('abcdef', 'abcd')
{% include copy.html %}
이 경우 문자열 abcdef가 abcd로 시작하기 때문에 함수는 true를 반환해요.
startsWith() 함수는 대소문자를 구분하는 검사를 수행해요.
{: .note }
예제
다음 파이프라인은 startsWith() 함수를 사용해 각 이벤트에 starts_abcd와 starts_error라는 두 개의 불리언 플래그를 추가하고, 문자열 ERROR:로 시작하는 이벤트만 OpenSearch로 전달해요:
startswith-demo:
source:
http:
ssl: false
processor:
- add_entries:
entries:
- key: starts_abcd
value_expression: startsWith(/message, "abcd")
- key: starts_error
value_expression: startsWith(/message, "ERROR:")
# forward only messages that start with "ERROR:"
- drop_events:
drop_when: not startsWith(/message, "ERROR:")
sink:
- opensearch:
hosts: ["https://opensearch:9200"]
insecure: true
username: admin
password: admin_pass
index_type: custom
index: startswith-demo-%{yyyy.MM.dd}
{% include copy.html %}
다음 명령어로 파이프라인을 테스트할 수 있어요:
curl -X POST "http://localhost:2021/log/ingest" \
-H "Content-Type: application/json" \
-d '[
{"message":"ok hello"},
{"message":"abcd-hello"},
{"message":"ERROR: something bad"},
{"message":"ERROR: abcd unit test failed"}
]'
{% include copy.html %}
OpenSearch에 저장된 문서에는 다음 정보가 담겨 있어요:
{
...
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "startswith-demo-2025.11.10",
"_id" : "X97tbpoBnoSLj36HBGoL",
"_score" : 1.0,
"_source" : {
"starts_abcd" : false,
"starts_error" : true,
"message" : "ERROR: something bad"
}
},
{
"_index" : "startswith-demo-2025.11.10",
"_id" : "YN7tbpoBnoSLj36HBGoL",
"_score" : 1.0,
"_source" : {
"starts_abcd" : false,
"starts_error" : true,
"message" : "ERROR: abcd unit test failed"
}
}
]
}
}