검색 최적화(8)
검색 최적화(8)
검색을 한다면 검색어를 백엔드(fast api)로 보내고 여기서 토큰화를 진행 후 토큰화 데이터 db 저장(실시간 검색어을 위해) 그후 토큰화된 데이터를 이용해서 엘라스틱 서치에 검색한다 기존 엘라스틱 서치 기능을 쓴다면 한글에는 부족한 성능(검색시 제대로 된 결과가 나오지 않음)이 나와서 nori를 적용시켰고 거기에 좀더 커스텀을 적용시켰다
mapping 생성
먼저 우리가 저장하는 데이터의 형태로 mapping을 만들었다
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
curl -X PUT "localhost:9200/notice_index" -H 'Content-Type: application/json' -u elastic:<Pw> -d'
{
"settings": {
"analysis": {
"analyzer": {
"nori_analyzer": {
"type": "custom",
"tokenizer": "nori_tokenizer",
"filter": ["nori_part_of_speech"]
}
}
}
},
"mappings": {
"properties": {
"id": {
"type": "integer"
},
"url": {
"type": "keyword"
},
"title": {
"type": "text",
"analyzer": "nori_analyzer", # Nori 분석기 적용
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"content": {
"type": "text",
"analyzer": "nori_analyzer" # Nori 분석기 적용
},
"category": {
"type": "keyword"
},
"site": {
"type": "keyword"
},
"date": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
}
}
'
위 형식으로 mapping을 만들어서 사용한 결과 형태소 분석을 하고 단어를 분리시켰다
예시로 졸업식의 날짜 -> 졸업 + 식 + 날 + 짜 이런 식으로 분리되는 것을 확인했다 이정도로 분리되는 것은 검색할 때 오히려 데이터만 증가 시킬뿐 정확도는 더 낮아졌다 따라서 추가적으로 분리되는 것을 막기 위해 토큰화 시키는 것을 커스텀 했다
토크나이저 수정
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
"settings": {
"analysis": {
"tokenizer": { # 토크나이저 설정 추가
"nori_tokenizer_custom": {
"type": "nori_tokenizer",
"decompound_mode": "none" # 복합어를 분리하지 않도록 설정
}
},
"analyzer": {
"nori_analyzer": {
"type": "custom",
"tokenizer": "nori_tokenizer_custom", # 수정된 토크나이저 사용
"filter": ["nori_part_of_speech"]
}
}
엘라스틱 서치 문서를 참고해서 복합어를 분리하지 않도록 했다 이로써 검색어가 너무 분리되는것을 막을 수 있다
검색 형식
엘라스틱 서치에 검색을 할때 엔진에서 제공하는 api를 이용한다 title과 content 두개의 필드에서 검색하기 하기 위해 multi_match를 이용한다
이렇게 검색한다면 두개다 text기준으로 검색(위 mapping을 본다면 title은 keyword와 text두개의 형식중 하나만 적용)한다 그래서 title도 type도 text로 변경시켰다 최종 mapping
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
curl -X PUT "localhost:9200/notice_index" -H 'Content-Type: application/json' -u elastic:<pw> -d'
{
"settings": {
"analysis": {
"tokenizer": {
"nori_tokenizer_custom": {
"type": "nori_tokenizer",
"decompound_mode": "none"
}
},
"analyzer": {
"nori_analyzer": {
"type": "custom",
"tokenizer": "nori_tokenizer_custom",
"filter": ["nori_part_of_speech"]
}
}
}
},
"mappings": {
"properties": {
"id": {
"type": "integer"
},
"url": {
"type": "keyword"
},
"title": {
"type": "text",
"analyzer": "nori_analyzer"
},
"content": {
"type": "text",
"analyzer": "nori_analyzer"
},
"category": {
"type": "keyword"
},
"site": {
"type": "keyword"
},
"date": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
}
}
'
This post is licensed under CC BY 4.0 by the author.