대부분의 게시글 서비스는 굵게, 기울임, 링크 등을 포함해서 작성할 수 있게 되어 있다. 그러려면 HTML 태그를 포함해서 그대로 DB에 저장해야 하는 경우가 많다. 이렇게 HTML 태그가 포함된 형태의 글은 어떻게 검색을 해야할 지 알아보도록 하자.
// 기존 인덱스 삭제 DELETE /boards // 인덱스 생성 + 매핑 정의 + Custom Analyzer 적용 PUT /boards { "settings": { "analysis": { "analyzer": { "boards_content_analyzer": { "char_filter": [], "tokenizer": "standard", "filter": ["lowercase"] } } } }, "mappings": { "properties": { "content": { "type": "text", "analyzer": "boards_content_analyzer" } } } } // 잘 생성됐는 지 확인 GET /boards
standard tokenizer(공백 또는 ,, ., !, ?와 같은 문장 부호를 기준으로 문자열을 자름)와 lowercase token filter(소문자로 변환)만 설정했다.// id를 자동 생성해서 도큐먼트 저장하기 POST /boards/_doc { "content": "<h1>Running cats, jumping quickly — over the lazy dogs!</h1>" }
GET /boards/_search { "query": { "match": { "content": "running" } } }
GET /boards/_search { "query": { "match": { "content": "h1" } } }
h1이라는 키워드에도 게시글이 조회됐다. 즉, HTML 태그에 대해서도 검색이 되다보니 검색 품질이 떨어지게 된다. 또 다른 단점을 알아보기 위해 Analyze API를 사용해서 분석해보자.GET /boards/_analyze { "field": "content", "text": "<h1>Running cats, jumping quickly — over the lazy dogs!</h1>" }
{ "tokens": [ { "token": "h1", "start_offset": 1, "end_offset": 3, "type": "<ALPHANUM>", "position": 0 }, ... ] }
html_strip character filter를 추가해서 인덱스를 생성해보자.// 기존 인덱스 삭제 DELETE /boards // 인덱스 생성 + 매핑 정의 + Custom Analyzer 적용 PUT /boards { "settings": { "analysis": { "analyzer": { "boards_content_analyzer": { "char_filter": ["html_strip"], "tokenizer": "standard", "filter": ["lowercase"] } } } }, "mappings": { "properties": { "content": { "type": "text", "analyzer": "boards_content_analyzer" } } } } // 잘 생성됐는 지 확인 GET /boards
POST /boards/_doc { "content": "<h1>Running cats, jumping quickly — over the lazy dogs!</h1>" }
GET /boards/_search { "query": { "match": { "content": "running" } } } GET /boards/_search { "query": { "match": { "content": "h1" } } }
running이라는 키워드로는 검색이 잘 되지만, h1이라는 키워드로는 검색이 잘 안되는 걸 확인할 수 있다. 검색 결과가 이렇게 나온 이유를 디버깅하기 위해 Analyze API를 사용해보자. GET /boards/_analyze { "field": "content", "text": "<h1>Running cats, jumping quickly — over the lazy dogs!</h1>" }
{ "tokens": [ { "token": "running", "start_offset": 4, "end_offset": 11, "type": "<ALPHANUM>", "position": 0 }, ... ] }
h1 이 토큰으로 저장되어 있지 않다. 이 때문에 h1이라고 검색하더라도 조회가 안 된 것이다. html_strip 적용을 고려해보자.