본문 바로가기
WIKI 기술 지식 베이스

substringBefore() function

원문 보기 위키 갱신

출처: 문서

본문

substringBefore() 함수

substringBefore() 함수는 문자열에서 지정한 구분자가 처음 나타나는 위치 앞쪽에 해당하는 부분을 추출할 때 사용해요. 이 함수는 두 개의 인수를 받아요:

  1. 첫 번째 인수는 리터럴 문자열이거나 원본 문자열을 나타내는 JSON pointer예요.

  2. 두 번째 인수는 첫 번째 인수 안에서 찾을 구분자 문자열이에요.

구분자를 찾으면 함수는 구분자가 처음 나타나기 전까지의 문자열 부분을 반환해요. 구분자를 찾지 못하면 원본 문자열을 그대로 반환해요. 원본이 null로 평가되면 함수는 null을 반환해요. 구분자가 null이거나 비어 있으면 원본 문자열을 반환해요.

예를 들어 이메일 주소 필드에서 사용자 이름을 추출하려면 다음과 같이 substringBefore() 함수를 사용해요:

'substringBefore(/email, "@")'

{% include copy.html %}

/email 필드에 [email protected]이 들어 있다면 함수는 user를 반환해요.

또는 첫 번째 인수로 리터럴 문자열을 사용할 수도 있어요:

'substringBefore("hello-world-foo", "-")'

{% include copy.html %}

함수는 - 문자가 처음 나타나기 전까지의 부분을 추출하기 때문에 hello를 반환해요.

substringBefore() 함수는 대소문자를 구분하는 검색을 수행해요. {: .note}

예제

다음 파이프라인은 substringBefore() 함수를 사용해 URL 필드에서 URL 프로토콜을 추출해요. 그리고 추출한 프로토콜을 protocol이라는 새 필드로 추가해요:

substring-before-demo:
  source:
    http:
      ssl: false

  processor:
    - add_entries:
        entries:
          - key: protocol
            value_expression: 'substringBefore(/url, "://")'

  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 '[
        {"url":"https://opensearch.org/docs"},
        {"url":"http://example.com/page"}
      ]'

{% include copy.html %}

OpenSearch에 저장된 문서에는 다음과 같은 정보가 들어 있어요:

{
  ...
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "demo-index-2026.03.13",
        "_id": "abc123",
        "_score": 1,
        "_source": {
          "url": "https://opensearch.org/docs",
          "protocol": "https"
        }
      },
      {
        "_index": "demo-index-2026.03.13",
        "_id": "def456",
        "_score": 1,
        "_source": {
          "url": "http://example.com/page",
          "protocol": "http"
        }
      }
    ]
  }
}

더 알아보기 (Learn more)