전처리 필터를 거쳐 토크나이저 필터로 문서가 넘어오면 해당 텍스트는 Tokneizer의 특성에 맞게 적절히 분해된다.
분석기에서 어떠한 토크나이저를 사용하느냐에 따라 분석기의 전체적인 성격이 결정됨
GET _analyze
{
"tokenizer": "standard",
"text": "동해물과 백두산 이 마!르고"
}
{
"tokens": [
{
"token": "동해물과",
"start_offset": 0,
"end_offset": 4,
"type": "<HANGUL>",
"position": 0
},
{
"token": "백두산",
"start_offset": 5,
"end_offset": 8,
"type": "<HANGUL>",
"position": 1
},
{
"token": "이",
"start_offset": 9,
"end_offset": 10,
"type": "<HANGUL>",
"position": 2
},
{
"token": "마",
"start_offset": 11,
"end_offset": 12,
"type": "<HANGUL>",
"position": 3
},
{
"token": "르고",
"start_offset": 13,
"end_offset": 15,
"type": "<HANGUL>",
"position": 4
}
]
}
GET _analyze
{
"tokenizer": "whitespace",
"text": "동해물과 백두산 이 마 르 고"
}
{
"tokens": [
{
"token": "동해물과",
"start_offset": 0,
"end_offset": 4,
"type": "word",
"position": 0
},
{
"token": "백두산",
"start_offset": 5,
"end_offset": 8,
"type": "word",
"position": 1
},
{
"token": "이",
"start_offset": 9,
"end_offset": 10,
"type": "word",
"position": 2
},
{
"token": "마",
"start_offset": 11,
"end_offset": 12,
"type": "word",
"position": 3
},
{
"token": "르",
"start_offset": 13,
"end_offset": 14,
"type": "word",
"position": 4
},
{
"token": "고",
"start_offset": 15,
"end_offset": 16,
"type": "word",
"position": 5
}
]
}
Ngram은 기본적으로 한 글자씩 토큰화한다. Ngram에에 특정 문자를 지정할 수도 있으며, 이 경우 지정된 문자의 목록 중 하나를 만날 때마다 단어를 자른다. 그 밖에도 다양한 옵션을 조합해서 자동완성을 만들 때 유용하게 활용할 수 있다.
PUT product_test
{
"settings":{
"analysis":{
"analyzer": {
"ngram_analyzer":{
"tokenizer": "ngram_tokenizer"
}
},
"tokenizer": {
"ngram_tokenizer":{
"type": "ngram",
"min_gram": 3,
"max_gram": 3,
"token_chars": [
"letter"
]
}
}
}
}
}
GET product_test/_analyze
{
"tokenizer": "ngram_tokenizer",
"text": "동해물과 백두산 이 마르고"
}
{
"tokens": [
{
"token": "동해물",
"start_offset": 0,
"end_offset": 3,
"type": "word",
"position": 0
},
{
"token": "해물과",
"start_offset": 1,
"end_offset": 4,
"type": "word",
"position": 1
},
{
"token": "백두산",
"start_offset": 5,
"end_offset": 8,
"type": "word",
"position": 2
},
{
"token": "마르고",
"start_offset": 11,
"end_offset": 14,
"type": "word",
"position": 3
}
]
}
지정된 문자의 목록 중 하나를 만날 때마다 시작 부분을 고정시켜 단어를 자르는 방식으로 사용하는 토크나이저. 해당 Tokenizer 역시 자동 완성을 구현할 때 유용하게 활용 할 수 있다.
PUT product_test
{
"settings":{
"analysis":{
"analyzer": {
"ngram_analyzer":{
"tokenizer": "edge_ngram_tokenizer"
}
},
"tokenizer": {
"edge_ngram_tokenizer":{
"type": "edge_ngram",
"min_gram": 2,
"max_gram": 10,
"token_chars": [
"letter"
]
}
}
}
}
}
GET product_test/_analyze
{
"tokenizer": "edge_ngram_tokenizer",
"text": "동해물과 백두산 이 마르고"
}
{
"tokens": [
{
"token": "동해",
"start_offset": 0,
"end_offset": 2,
"type": "word",
"position": 0
},
{
"token": "동해물",
"start_offset": 0,
"end_offset": 3,
"type": "word",
"position": 1
},
{
"token": "동해물과",
"start_offset": 0,
"end_offset": 4,
"type": "word",
"position": 2
},
{
"token": "백두",
"start_offset": 5,
"end_offset": 7,
"type": "word",
"position": 3
},
{
"token": "백두산",
"start_offset": 5,
"end_offset": 8,
"type": "word",
"position": 4
},
{
"token": "마르",
"start_offset": 11,
"end_offset": 13,
"type": "word",
"position": 5
},
{
"token": "마르고",
"start_offset": 11,
"end_offset": 14,
"type": "word",
"position": 6
}
]
}
텍스트를 하나의 토큰으로 만든다
POST _analyze
{
"tokenizer": "keyword",
"text": "동해물과 백두산 이 마르고"
}
{
"tokens": [
{
"token": "동해물과 백두산 이 마르고",
"start_offset": 0,
"end_offset": 14,
"type": "word",
"position": 0
}
]
}