Elasticsearch (ELK)/Elasticsearch
7-3. 데이터 색인과 텍스트 분석(실전) - 캐릭터 필터
Velody
2020. 5. 28. 11:29
캐릭터 필터?
캐릭터 필터는 텍스트 분석 중 가장 먼저 처리되는 과정(전체 문장에 대해 적용되는 과정)을 처리하는 도구
1. HTML Strip
입력된 텍스트가 HTML인 경우에 일반 텍스트로 변경이 가능하다
POST _analyze
{
"tokenizer": "keyword",
"char_filter": [
"html_strip"
],
"text": "<p>I'm so <b>happy</b>!</p>"
}
"I'm so happy!" 라는 문장으로 변경 된다.
2. Mapping
특정 캐릭터를 특정 문자로 치환해주는 기능이다(예: C++를 C_plus__plus_로 바꿔줌).
coding 인덱스에 mapping 캐릭터 필터 설정
PUT coding
{
"settings": {
"analysis": {
"analyzer": {
"coding_analyzer": {
"char_filter": [
"cpp_char_filter"
],
"tokenizer": "whitespace",
"filter": [ "lowercase", "stop", "snowball" ]
}
},
"char_filter": {
"cpp_char_filter": {
"type": "mapping",
"mappings": [ "+ => _plus_", "- => _minus_" ]
}
}
}
},
"mappings": {
"properties": {
"language": {
"type": "text",
"analyzer": "coding_analyzer"
}
}
}
}
3. Pattern Replace
Pattern Replace 캐릭터 필터는 정규식(Regular Expression)을 이용해서 좀더 복잡한 패턴들을 치환할 수 있는 캐릭터 필터입니다. 다음은 카멜 표기법(camelCase)으로 된 단어를 대문자가 시작하는 단위 마다 공백을 삽입하여 세부 단어별로 토크나이징 될 수 있도록 camel 인덱스에 camel_analyzer 라는 애널라이저를 생성하는 예제입니다.
camel 인덱스에 pattern_replace 캐릭터 필터 설정
PUT camel
{
"settings": {
"analysis": {
"analyzer": {
"camel_analyzer": {
"char_filter": [
"camel_filter"
],
"tokenizer": "standard",
"filter": [
"lowercase"
]
}
},
"char_filter": {
"camel_filter": {
"type": "pattern_replace",
"pattern": "(?<=\\p{Lower})(?=\\p{Upper})",
"replacement": " "
}
}
}
}
}
이제 camel 인덱스에서 "FooBazBar" 라는 단어를 분석 해 보면 다음과 같이 foo, baz, bar 세개의 단어로 분리되는 것을 확인할 수 있습니다.
//camel_analyzer 애널라이저로 문장 분석
GET camel/_analyze
{
"analyzer": "camel_analyzer",
"text": [
"public void FooBazBar()"
]
}
// Response
{
"tokens" : [
{
"token" : "public",
"start_offset" : 0,
"end_offset" : 6,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "void",
"start_offset" : 7,
"end_offset" : 11,
"type" : "<ALPHANUM>",
"position" : 1
},
{
"token" : "foo",
"start_offset" : 12,
"end_offset" : 14,
"type" : "<ALPHANUM>",
"position" : 2
},
{
"token" : "baz",
"start_offset" : 15,
"end_offset" : 17,
"type" : "<ALPHANUM>",
"position" : 3
},
{
"token" : "bar",
"start_offset" : 18,
"end_offset" : 21,
"type" : "<ALPHANUM>",
"position" : 4
}
]
}
camel 인