@@ -66,7 +66,7 @@ def __init__(self):
6666 raise ValueError ("ES_PORT 환경 변수는 반드시 숫자여야 합니다." )
6767
6868 self .es = Elasticsearch ([{'host' : host , 'port' : port , 'scheme' : 'http' }])
69- self .index_name = "place_data "
69+ self .index_name = "place_data_v2 "
7070 self .log_index_name = "chatbot_log"
7171 self .click_log_index_name = "click_log"
7272 self .search_log_index_name = "search_log"
@@ -79,14 +79,19 @@ def is_connected(self) -> bool:
7979 return False
8080
8181 def search_places (self , query : str , max_results : int = 23 , user_id : Optional [str ] = None ) -> List [Dict [str , Any ]]:
82- """장소 검색"""
82+ """장소 검색 (copy_to 필드와 multi_match로 고도화) """
8383 search_body = {
8484 "query" : {
85- "match" : {
86- "name" : {
87- "query" : query ,
88- "fuzziness" : "AUTO"
89- }
85+ "multi_match" : {
86+ "query" : query ,
87+ "fields" : [
88+ "name^4" ,
89+ "alias^3" ,
90+ "categories^2" ,
91+ "addresses^2" ,
92+ "content"
93+ ],
94+ "fuzziness" : "AUTO"
9095 }
9196 },
9297 "sort" : [{"_score" : {"order" : "desc" }}],
@@ -124,28 +129,24 @@ def search_places_for_llm_tool(self, region: str, categories: List[str], user_id
124129 LLM 도구를 위한 장소 검색.
125130 지역과 카테고리 정보를 바탕으로 장소 uuid 목록과 총 개수를 반환합니다.
126131 """
127- query_body = {
128- "query" : {
129- "bool" : {
130- "must" : [],
131- "filter" : []
132- }
133- },
134- "size" : 100 ,
135- "_source" : ["uuid" ],
136- "track_total_hits" : True
132+ # 1. bool 쿼리를 기본 쿼리로 정의
133+ base_query = {
134+ "bool" : {
135+ "must" : [],
136+ "filter" : []
137+ }
137138 }
138139
139140 if region :
140- query_body [ "query" ] ["bool" ]["must" ].append ({
141+ base_query ["bool" ]["must" ].append ({
141142 "multi_match" : {
142143 "query" : region ,
143144 "fields" : ["gu" , "dong" , "ro" , "station" , "address" ]
144145 }
145146 })
146147
147148 if categories :
148- query_body [ "query" ] ["bool" ]["filter" ].append ({
149+ base_query ["bool" ]["filter" ].append ({
149150 "bool" : {
150151 "should" : [
151152 {"terms" : {"category.keyword" : categories }},
@@ -155,6 +156,24 @@ def search_places_for_llm_tool(self, region: str, categories: List[str], user_id
155156 }
156157 })
157158
159+ # 2. function_score 쿼리로 기본 쿼리를 감싸고, random_score 함수를 추가
160+ query_body = {
161+ "query" : {
162+ "function_score" : {
163+ "query" : base_query ,
164+ "functions" : [
165+ {
166+ "random_score" : {}
167+ }
168+ ],
169+ "boost_mode" : "multiply" # 원래 점수와 랜덤 점수를 곱하여 자연스럽게 섞음
170+ }
171+ },
172+ "size" : 100 ,
173+ "_source" : ["uuid" ],
174+ "track_total_hits" : True
175+ }
176+
158177 response = self .es .search (index = self .index_name , body = query_body )
159178
160179 uuids = [hit ['_source' ]['uuid' ] for hit in response ['hits' ]['hits' ]]
0 commit comments