실전에서 바로 써먹는 Elasticsearch 입문 (검색 최적화편)
✅ 최종 쿼리
[생성해야 할 인덱스 및 매핑]
DELETE /products
PUT /products
{
"settings": {
"analysis": {
// 필터 정의
"filter": {
"product_synonyms": {
"type": "synonym",
"synonyms": [
"samsung, 삼성",
"apple, 애플",
"노트북, 랩탑, 컴퓨터, computer, laptop, notebook",
"전화기, 휴대폰, 핸드폰, 스마트폰, 휴대전화, phone, smartphone, mobile phone, cell phone",
"아이폰, iphone",
"맥북, 맥, macbook, mac"
]
}
},
// 커스텀 애널라이저 정의
"analyzer": {
"products_name_analyzer": {
"char_filter": [],
"tokenizer": "nori_tokenizer",
"filter": [
"nori_part_of_speech",
"nori_readingform",
"lowercase",
"product_synonyms"
]
},
"products_description_analyzer": {
"char_filter": ["html_strip"],
"tokenizer": "nori_tokenizer",
"filter": [
"nori_part_of_speech",
"nori_readingform",
"lowercase"
]
},
"products_category_analyzer": {
"char_filter": [],
"tokenizer": "nori_tokenizer",
"filter": [
"nori_part_of_speech",
"nori_readingform",
"lowercase"
]
}
}
}
},
"mappings": {
"properties": {
"id": {
"type": "long"
},
"name": {
"type": "text", // 유연한 검색 필요
"analyzer": "products_name_analyzer",
// 멀티 필드로 search_as_you_type 타입을 추가
"fields": {
"auto_complete": {
"type": "search_as_you_type",
"analyzer": "nori"
}
}
},
"description": {
"type": "text", // 유연한 검색 필요
"analyzer": "products_description_analyzer"
},
"price": {
"type": "integer" // 10억 이하의 정수
},
"rating": {
"type": "double" // 실수(소수점을 가진 숫자 포함)
},
"category": {
"type": "text", // 유연한 검색 필요
"analyzer": "products_category_analyzer",
// 멀티 필드로 keyword 타입을 추가
"fields": {
"raw": {
"type": "keyword"
}
}
}
}
}
}
[검색 기능]
GET /products/_search
{
"query": {
"bool": {
"must": {
"multi_match": {
"query": "_____",
"fields": [
"name^3",
"description^1",
"category^2"
],
"fuzziness": "AUTO"
}
},
"filter": [
{
"term": {
"category.raw": "______"
}
},
{
"range": {
"price": {
"gte": _____,
"lte": _____
}
}
}
],
"should": [
{
"range": {
"rating": {
"gt": 4.0
}
}
}
]
}
},
"highlight": {
"fields": {
"name": {
"pre_tags": ["<b>"],
"post_tags": ["</b>"]
}
}
},
"from": 0,
"size": 5
}
[자동 완성 기능]
POST /products/_search
{
"query": {
"multi_match": {
"query": "________",
"type": "bool_prefix",
"fields": [
"name.auto_complete",
"name.auto_complete._2gram",
"name.auto_complete._3gram"
]
}
},
"size": 5
}
✅ 적용 전략 및 순서
Kibana에서 요구사항에 맞는 쿼리 작성하기 (완료!)
- Spring Boot에서
products 인덱스에 맞는 Document 정의하기
- Spring Data Elasticsearch 라이브러리에 맞게 쿼리 작성하기
- 더미 데이터 넣고 Spring Boot에서 잘 작동하는 지 테스트하기
👨🏻🏫
다음 강의에서는 Spring Boot에서 코드를 마저 작성해보자.