본문 바로가기
OpenSearch (ElasticSearch)

[API] Ingest APIs

by parkjp 2022. 3. 18.

 

인덱싱을 하다보면 필요에 따라 데이터를 가공해야될 때가 있다.

인덱스 별로 가공해야 될 필드가 다르고 필드명 또한 다른 경우 매번 인덱스에 해당하는 프로세스를 만들고, 배포하는 과정은 불편하기 마련이다.

이런  경우 Pipeline을 설정하여 데이터를 가공할 수 있다.

우선적으로 Pipeline을 설정하기 위해서는 Ingest Node가 활성화 되어있어야 한다.

 

 

// pipeline 설정 기본 구성 예시

PUT _ingest/pipeline/{id}
{
  "description" : "A description for your pipeline",
  "processors" : [
    {
      "set" : {
        "field": "field-name",
        "value": "value"
      }
    }
  ]
}

 

 위에 processors는 processor object의 배열 형태로 processor object는 어떻게 데이터를 가공할 것인지에 대한 설정 정보가 들어가 있는 구조이다.

예를 들어, 정의된 필드의 데이터를 구분자로 분해하여 배열형태로 만들어 주는 예시이다.

 

// 구분자로 데이터 가공 예시

PUT _ingest/pipeline/split_field
{
  "description": "Decompose the field by separator",
  "processors": [
    {
      "split": {
        "field": "productList",
        "separator": ","
      }
    }
  ]
}

 

HTML태그를 제거하는 HTML strip processor 예시

 

// HTML strip processor 예시

PUT _ingest/pipeline/html_strip
{
  "description": "Remove Html Tag",
  "processors": [
    {
      "html_strip": {
        "field": "contents"
      }
    }
  ]
}

 

pipeline을 월별로 저장하기 위해 설정하는 부분은 아래와 같다. (예시)

 

// 월별로 저장하기 위한 설정 예시

PUT _template/log  // 템플릿 생성 (템플릿 문서 참조) (파이프라인에 템플릿을 저장하는 방식을 사용해 데이터를 인덱스에 저장하기 위해)
{
   "template": "log_*",
   "mappings": {
       "properties": {
         "id": {
           "type": "long"
        },
        "log_date": {  // 'log_date'는 임의로 해놓은 값이다. 그때 그때 설정에따라 알맞는 key값을 설정해 주면 된다.
           "type": "date"
        }
    }
  }
}
 
 
PUT _ingest/pipeline/log-monthly-index  // log-monthly-index라는 파이프라인 생성
{
  "description": "monthly index naming for log",
  "processors" : [
    {
      "date_index_name" : {
        "field" : "log_date",
        "index_name_prefix" : "log_",
        "index_name_format" : "yyyy-MM",
        "date_rounding" : "M"
    }
  ]
}
 
 
PUT /log/_doc/1?pipeline=log-monthly-index
{
    "log_date" : "2022-01-20T17:16:55.000Z"  // 시간 예시
}
 
 
// 데이터 확인
GET /log_2022-01/_doc/1

 

 

 

1. Create and Update a Pipeline

 

Path and HTTP methods
PUT _ingest/pipeline/{id}

** https://opensearch.org/docs/latest/opensearch/rest-api/ingest-apis/create-update-ingest/

 

2. Get Ingest Pipeline

 

Path and HTTP methods
GET _ingest/pipeline
GET _ingest/pipeline/{id}

** https://opensearch.org/docs/latest/opensearch/rest-api/ingest-apis/get-ingest/

 

3. Simulate a Pipeline

 

Path and HTTP methods
// 마지막으로 만들어진 pipeline simulate
GET _ingest/pipeline/_simulate
POST _ingest/pipeline/_simulate
 
 
// id 기반 simulate
GET _ingest/pipeline/{id}/_simulate
POST _ingest/pipeline/{id}/_simulate

** https://opensearch.org/docs/latest/opensearch/rest-api/ingest-apis/simulate-ingest/

 

4. Delete Pipeline

 

Path and HTTP methods
DELETE _ingest/pipeline/{id}

** https://opensearch.org/docs/latest/opensearch/rest-api/ingest-apis/delete-ingest/

반응형