Elasticsearch에서 텍스트 필드를 다룰 때 두 가지 분석기를 설정할 수 있습니다:
문서를 인덱싱할 때 텍스트를 토큰으로 분해하고 처리합니다.
{
"mappings": {
"properties": {
"content": {
"type": "text",
"analyzer": "standard"
}
}
}
}
검색 쿼리를 처리할 때 사용되는 분석기입니다.
{
"mappings": {
"properties": {
"content": {
"type": "text",
"analyzer": "standard",
"search_analyzer": "simple"
}
}
}
}
| 구분 | analyzer | search_analyzer |
|---|---|---|
| 적용 시점 | 인덱싱(문서 저장) 시 | 검색 쿼리 실행 시 |
| 처리 대상 | 문서의 텍스트 데이터 | 검색 쿼리 문자열 |
| 기본값 | standard analyzer | analyzer와 동일 |
| 설정 생략 시 | standard analyzer 사용 | analyzer 값 사용 |
대부분의 경우 동일한 분석기를 사용하지만, 다음과 같은 상황에서 다르게 설정합니다:
{
"settings": {
"analysis": {
"analyzer": {
"index_analyzer": {
"type": "standard"
},
"search_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": ["lowercase", "synonym"]
}
}
}
},
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "index_analyzer",
"search_analyzer": "search_analyzer"
}
}
}
}
이유: 인덱싱 시에는 원본 그대로 저장하고, 검색 시에만 동의어를 확장하여 검색 범위를 넓힙니다.
{
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "ngram_analyzer",
"search_analyzer": "standard"
}
}
}
}
이유:
인덱싱 시 edge n-gram을 사용하고 검색 시에는 keyword로 처리하는 경우
{
"settings": {
"analysis": {
"analyzer": {
"korean_index": {
"type": "custom",
"tokenizer": "nori_tokenizer",
"filter": ["lowercase"]
},
"korean_search": {
"type": "custom",
"tokenizer": "nori_tokenizer",
"filter": ["lowercase", "synonym"]
}
}
}
},
"mappings": {
"properties": {
"description": {
"type": "text",
"analyzer": "korean_index",
"search_analyzer": "korean_search"
}
}
}
}
POST /my_index/_analyze
{
"analyzer": "standard",
"text": "테스트 텍스트"
}
POST /my_index/_analyze
{
"field": "content",
"text": "검색어"
}
_analyze API로 결과 확인