한글검색 오류 해결
이전 한글검색 방식의 문제점
한글 검색을 사용하기 위해 config 파일에서 defaultContentLanguage 를 “ko” 로 설정했었다.
이렇게 설정해주니 한글은 검색이 잘 됐지만, 영어 검색이 막혀버렸다.
hugobook에서는 flexsearch 라는 라이브러리로 검색기능이 구현되어 있는데
search.js 파일을 보면 FlexSearch 객체 생성중에 i18n의 bookSearchConfig로 설정을 초기화하는것을 볼 수 있다.
// hugo-book/assets/search.js
{{ $searchConfig := i18n "bookSearchConfig" | default "{}" }}
(function () {
const searchDataURL = '{{ $searchData.RelPermalink }}';
const indexConfig = Object.assign({{ $searchConfig }}, {
doc: {
id: 'id',
field: ['title', 'content'],
store: ['title', 'href', 'section']
}
});
defaultContentLanguage를 설정하면 i18n에서 설정한 나라의 언어파일에서 bookSearchConfig 값이 아래와 같이 되어있다.
// hugo-book/i18n/ko.yaml
- id: bookSearchConfig
translation: |
{
encode: false,
tokenize: function(str) {
return str.replace(/[\x00-\x7F]/g, '').split('');
}
}
해결방법 (아직 미해결)
커스텀 토크나이저 함수를 잘 보면 ascii 코드값을 전부 지워버리는것을 볼 수 있다.
아래 코드를 적용한 결과 한글검색어에서 첫번째 글자만 검색되는것을 확인할 수 있다. 정확히 파악 후 직접 구현해야될것같다.
{
encode: false,
tokenize: function(str) {
return str.split(/\W+/).concat(str.replace(/[\x00-\x7F]/g, '').split('')).filter(e => !!e)
}
}
관련 링크
- flexsearch github : https://github.com/nextapps-de/flexsearch
- hugobook 이슈 : https://github.com/alex-shpak/hugo-book/issues/80#issuecomment-621053508
Comments