contains() function
출처: 문서
본문
contains() function
contains() 함수는 주어진 문자열이나 이벤트의 필드 값 안에 부분 문자열이 존재하는지 확인할 때 사용해요. 이 함수는 두 개의 인자를 받아요:
-
첫 번째 인자는 검색 대상인 필드나 값을 나타내는 리터럴 문자열 또는 JSON pointer예요.
-
두 번째 인자는 첫 번째 인자 안에서 검색할 부분 문자열이에요.
두 번째 인자로 지정한 부분 문자열이 첫 번째 인자가 나타내는 문자열이나 필드 값 안에서 발견되면 함수는 true를 반환하고, 발견되지 않으면 false를 반환해요.
예를 들어, message라는 필드 값 안에 "abcd" 문자열이 포함되어 있는지 확인하고 싶다면 다음과 같이 contains() 함수를 사용할 수 있어요:
'contains(/message, "abcd")'
{% include copy.html %}
이 호출은 message 필드가 abcd 부분 문자열을 포함하고 있으면 true를, 그렇지 않으면 false를 반환해요.
또는 첫 번째 인자로 리터럴 문자열을 사용할 수도 있어요:
'contains("This is a test message", "test")'
{% include copy.html %}
이 경우에는 test 부분 문자열이 This is a test message 문자열 안에 있기 때문에 함수가 true를 반환해요.
contains() 함수는 대소문자를 구분하는 검색을 수행해요.
{: .note}
예제
다음 파이프라인은 contains() 함수를 사용해서 /message 안의 부분 문자열을 기준으로 불리언 플래그 has_test를 추가하고, 일치하지 않는 이벤트는 걸러낸 뒤 ERROR 문자열이 포함된 메시지만 OpenSearch로 전달해요:
contains-demo-pipeline:
source:
http:
ssl: false
processor:
- add_entries:
entries:
- key: has_test
value_expression: contains(/message, "test")
- drop_events:
drop_when: not contains(/message, "ERROR")
sink:
- opensearch:
hosts: ["https://opensearch:9200"]
insecure: true
username: admin
password: admin_password
index_type: custom
index: demo-index-%{yyyy.MM.dd}
{% include copy.html %}
다음 명령어로 파이프라인을 테스트할 수 있어요:
curl -sS -X POST "http://localhost:2021/log/ingest" \
-H "Content-Type: application/json" \
-d '[
{"message":"ok hello"},
{"message":"this has test but ok"},
{"message":"ERROR: something bad"},
{"message":"ERROR: unit test failed"}
]'
{% include copy.html %}
OpenSearch에 저장된 문서에는 다음 정보가 들어 있어요:
{
...
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "demo-index-2025.10.21",
"_id": "5YACB5oBqZitdAAb4n3r",
"_score": 1,
"_source": {
"message": "ERROR: something bad",
"has_test": false
}
},
{
"_index": "demo-index-2025.10.21",
"_id": "5oACB5oBqZitdAAb4n3r",
"_score": 1,
"_source": {
"message": "ERROR: unit test failed",
"has_test": true
}
}
]
}
}