영어로 작성된 게시글을 보면 검색어로 잘 사용하지 않는 a, an, the, or과 같은 불용어(= 의미없는 단어)가 많이 포함되어 있다. 역인덱스의 효율적인 저장과 활용을 위해 불용어를 제거하고 관리하는 방법을 알아보자.
// 기존 인덱스 삭제 DELETE /boards // 인덱스 생성 + 매핑 정의 + Custom Analyzer 적용 PUT /boards { "settings": { "analysis": { "analyzer": { "boards_content_analyzer": { "char_filter": [], "tokenizer": "standard", "filter": ["lowercase", "stop"] } } } }, "mappings": { "properties": { "content": { "type": "text", "analyzer": "boards_content_analyzer" } } } } // 잘 생성됐는 지 확인 GET /boards
standard tokenizer(공백 또는 ,, ., !, ?와 같은 문장 부호를 기준으로 문자열을 자름)와 lowercase token filter(소문자로 변환)에다가 stop token filter(불용어를 제거)를 추가했다. POST /boards/_doc { "content": "The cat and the dog are friends." }
GET /boards/_search { "query": { "match": { "content": "the" } } } GET /boards/_search { "query": { "match": { "content": "and" } } } GET /boards/_search { "query": { "match": { "content": "are" } } }
GET /boards/_analyze { "field": "content", "text": "The cat and the dog are friends." }
{ "tokens": [ { "token": "cat", "start_offset": 4, "end_offset": 7, "type": "<ALPHANUM>", "position": 1 }, { "token": "dog", "start_offset": 16, "end_offset": 19, "type": "<ALPHANUM>", "position": 4 }, { "token": "friends", "start_offset": 24, "end_offset": 31, "type": "<ALPHANUM>", "position": 6 } ] }
the, and, are)를 제거한 뒤에 역인덱스에 저장했다. 그래서 the, and, are로 검색했을 때 아무 데이터도 조회되지 않은 것이다. stop을 활용하도록 하자. stop을 적용시키지 않아야 한다.