2025년 3월 14일 금요일

ingest pipeline - 5th

다음은 url과 변수를 구분해서 검사하는 정규표현식.


grok processor 사용 시 에러 발생.

POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "processors": [
      {
        "grok": {
          "field": "msg",
          "patterns": ["(?<url>[^?]+)\?(?<param>.*)"]
        }
      }
    ]
  },
  "docs": [
    {
      "_source": {
        "msg": "index.php?a=b"
      }
    }
  ]
}

{
  "error": {
    "root_cause": [
      {
        "type": "parse_exception",
        "reason": "Failed to parse content to type"
      }
    ],
    "type": "parse_exception",
    "reason": "Failed to parse content to type",
    "caused_by": {
      "type": "json_parse_exception",
      "reason": """Unrecognized character escape '?' (code 63)
 at [Source: (byte[])"{
  "pipeline": {
    "processors": [
      {
        "grok": {
          "field": "msg",
          "patterns": ["(?<url>[^?]+)\?(?<param>.*)"]
        }
      }
    ]
  },
  "docs": [
    {
      "_source": {
        "msg": "index.php?a=b"
      }
    }
  ]
}
"; line: 7, column: 39]"""
    }
  },
  "status": 400
}

순수 문자 ?(\?)를 인식하지 못한다. 예외처리를 한 번 더 해줘야 함.

POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "processors": [
      {
        "grok": {
          "field": "msg",
          "patterns": ["(?<url>[^?]+)\\?(?<param>.*)"]
        }
      }
    ]
  },
  "docs": [
    {
      "_source": {
        "msg": "index.php?a=b"
      }
    }
  ]
}

{
  "docs": [
    {
      "doc": {
        "_index": "_index",
        "_version": "-3",
        "_id": "_id",
        "_source": {
          "msg": "index.php?a=b",
          "param": "a=b",
          "url": "index.php"
        },
        "_ingest": {
          "timestamp": "2025-03-14T09:25:47.8591859Z"
        }
      }
    }
  ]
}

grok과 script processor의 정규표현식 처리 방식이 다르다. 

POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "processors": [
      {
        "script": {
          "source": """
            def str = /([^?]+)\?(.*)/.matcher(ctx.msg);
            if (str.find()) {
              ctx.url = str.group(1);
              ctx.param = str.group(2);
            }  
          """
        }
      }
    ]
  },
  "docs": [
    {
      "_source": {
        "msg": "index.php?a=b"
      }
    }
  ]
}

{
  "docs": [
    {
      "doc": {
        "_index": "_index",
        "_version": "-3",
        "_id": "_id",
        "_source": {
          "msg": "index.php?a=b",
          "param": "a=b",
          "url": "index.php"
        },
        "_ingest": {
          "timestamp": "2025-03-14T09:29:27.6636501Z"
        }
      }
    }
  ]
}

    크리에이티브 커먼즈 라이선스