콘텐츠로 이동

2025 05 02

2025-05-02

Elasticsearch Update

  • 인덱스 검색
    • GET /_cat/indices/prod-message-*
    • GET /_cat/indices/prod-message-*?v : ?v로 헤더(컬럼명) 포함하여 결과 보기 좋게
    • GET /_cat/indices/prod-message-*?v&s=index:asc : 오름차순으로 인덱스 이름 보도록 정렬
  • 인덱스 필드 조회
    • GET /prod-message-2025.05.02/_mapping
  • 인덱스 데이터 확인
    • GET /prod-message-2025.05.02/_search : 검색
    • GET /prod-message-2025.05.02/_doc/docID: 특정 ID로 도큐먼트 가져오기

Qs) logstash에서 파싱된 필드 > ES 중에 누락된 건을 보충하려면?

  • 방법1 - 기존 인덱스에 누락된 필드 추가하기

    • PUT Mapping으로 새 필드 추가하고 업데이트 하기
    • 장점 : 인덱스 이름 안바뀜
    • 단점 : 오류 발생하면 복구하기 어려움. 1. 누락된 매핑 추가
      PUT http://es.com/index-joel-*/_mapping
      {  
        "properties": {    
          "testId": {      
            "type": "long"    
          },    
          "phoneNumber": {      
            "type": "keyword"    
          }  
        }
      }
      
    1. 업데이트 쿼리 수행
      POST http://es.com/index-joel-*/_update_by_query
      
      {  
        "script": {    
          "lang": "painless",    
          "source": "if (ctx._source.message != null) { def matcher1 = /\\\"phoneNumber\\\"\\s*:\\s*\\\"([^\\\"]+)\\\"/.matcher(ctx._source.message); if (matcher1.find()) { ctx._source.phoneNumber = matcher1.group(1); } def matcher2 = /\\\"testId\\\"\\s*:\\s*(\\d+)/.matcher(ctx._source.message); if (matcher2.find()) { ctx._source.testId = Long.parseLong(matcher2.group(1)); } }"
        },  
        "query": {    
          "exists": {      
            "field": "message"    
          }  
        }
      }
      
  • 방법2 - Reindex

    • POST _reindex > script 써서 누락되었던 필드 인덱싱...
      • 누락된 필드 추출하는 pipeline 같은 걸 만들고 해당 pipeline 을 통해서 재색인할 수도 있음
      • 파이프라인을 끼워서 리인덱싱할때 이걸 태우고 리인덱싱하게 도움을 주는 장치
    • 장점
      • new로 만들어서 처리가능
    • 단점
      • 인덱스 이름이 바뀜(후처리 필요)
      • 저장공간 일시적 2배 1. 파이프 라인 생성
        http://es.com/_ingest/pipeline/joel_test
        
        {
          "description": "메시지 필드에서 phoneNumber, testId 추출",
          "processors": [
            {
              "grok": {
                "field": "message",
                "patterns": [
                  ".*\"phoneNumber\"\\s*:\\s*\"(?<extracted_phone_number>[^\"]+)\".*",
                  ".*\"testId\"\\s*:\\s*(?<extracted_test_id>\\d+).*"
                ],
                "ignore_missing": true,
                "ignore_failure": true
              }
            },
            {
              "set": {
                "field": "phoneNumber",
                "value": "{{extracted_phone_number}}",
                "if": "ctx.extracted_phone_number != null"
              }
            },
            {
              "set": {
                "field": "testId",
                "value": "{{extracted_test_id}}",
                "if": "ctx.extracted_test_id != null && ctx.extracted_test_id.length() > 0",
                "ignore_failure": true
              }
            },
            {
              "convert": {
                "field": "testId",
                "type": "long",
                "if": "ctx.testId != null && ctx.testId.length() > 0",
                "ignore_missing": true,
                "ignore_failure": true
              }
            },
            {
              "remove": {
                "field": ["extracted_phone_number", "extracted_test_id"],
                "ignore_missing": true
              }
            }
          ]
        }
        
    1. 신규 인덱스 생성

      PUT http://es.com/joel-pipeline
      
      { 기존 매핑... }
      

    2. 리인덱스

      POST http://bzm-moa-dev.es.onkakao.net:9200/_reindex
      
      {
        "source": {
          "index": "joel-original"
        },
        "dest": {
          "index": "joel-pipeline",
          "pipeline": "joel_test"
        }
      }