// 기존 인덱스 삭제 DELETE /products // 인덱스 생성 + 매핑 정의 + Custom Analyzer 적용 PUT /products { "settings": { "analysis": { "analyzer": { "products_name_analyzer": { "char_filter": [], "tokenizer": "standard", "filter": [] } } } }, "mappings": { "properties": { "name": { "type": "text", "analyzer": "products_name_analyzer" } } } } // 잘 생성됐는 지 확인 GET /products
products 인덱스에서 name 필드에 products_name_analyzer라는 Custom Analyzer를 적용시켰다. 이번 Analyzer에는 standard tokenizer(공백 또는 ,, ., !, ?와 같은 문장 부호를 기준으로 문자열을 자름)만 설정했고, lowercase token filter(소문자로 변환)은 설정하지 않았다.POST /products/_create/1 { "name": "Apple 2025 맥북 에어 13 M4 10코어" }
GET /products/_search { "query": { "match": { "name": "apple" } } }
GET /products/_search { "query": { "match": { "name": "Apple" } } }
apple이 아닌 Apple로 바꿔서 다시 검색해보면 아까 저장한 도큐먼트가 조회되는 걸 확인할 수 있다. 왜 그런 지 Analyze API를 활용해서 디버깅해보자. // 특정 인덱스의 필드에 적용된 Analyzer를 활용해 분석 GET /products/_analyze { "field": "name" "text": "Apple 2025 맥북 에어 13 M4 10코어" }
{ "tokens": [ { "token": "Apple", "start_offset": 0, "end_offset": 5, "type": "<ALPHANUM>", "position": 0 }, ... ] }
apple이라고 저장되어 있지 않고 Apple이라고 저장되어 있다. 이 때문에 apple이라고 검색했을 때 검색이 안 된 것이다. lowercase token filter를 추가해서 인덱스를 생성해보자.// 기존 인덱스 삭제 DELETE /products // 인덱스 생성 + 매핑 정의 + Custom Analyzer 적용 PUT /products { "settings": { "analysis": { "analyzer": { "products_name_analyzer": { "char_filter": [], "tokenizer": "standard", "filter": ["lowercase"] } } } }, "mappings": { "properties": { "name": { "type": "text", "analyzer": "products_name_analyzer" } } } } // 잘 생성됐는 지 확인 GET /products
POST /products/_create/1 { "name": "Apple 2025 맥북 에어 13 M4 10코어" }
GET /products/_search { "query": { "match": { "name": "apple" } } } GET /products/_search { "query": { "match": { "name": "Apple" } } }
// 특정 인덱스의 필드에 적용된 Analyzer를 활용해 분석 GET /products/_analyze { "field": "name" "text": "Apple 2025 맥북 에어 13 M4 10코어" }
{ "tokens": [ { "token": "apple", "start_offset": 0, "end_offset": 5, "type": "<ALPHANUM>", "position": 0 }, ... ] }
apple이라고 저장되어 있다. 이 때문에 apple이라고 검색했을 때 조회가 잘 된 것이다. 하지만 Apple이라고 검색했는데도 도큐먼트가 조회된 이유가 뭘까? 
Apple이라고 검색어를 입력하더라도 lowercase token filter에 의해 apple로 바뀐 채로 검색을 하게 된다. 그래서 Apple이라고 검색했는데도 불구하고 도큐먼트가 조회된 것이다. lowercase token filter를 활용함으로써 대소문자에 상관없이 데이터를 검색할 수 있게 된 것이다.