@@ -144,3 +144,54 @@ async def search_document(index: str, keywords: List[str], topk=3):
144144 for hit in hits ["hits" ]:
145145 result .append (hit ["_source" ])
146146 return result
147+
148+ # insert document into elasticsearch
149+ def insert_question (index , que_id , content ):
150+ if es .indices .exists (index = index ).body :
151+ doc = {
152+ 'content' : content ,
153+ }
154+ es .index (index = index , id = f"{ que_id } " , document = doc )
155+ else :
156+ logger .warning ("index %s not exists" , index )
157+
158+
159+ def remove_question (index , que_id ):
160+ if es .indices .exists (index = index ).body :
161+ try :
162+ es .delete (index = index , id = f"{ que_id } " )
163+ except NotFoundError :
164+ logger .warning ("question %s not found in index %s" , que_id , index )
165+ else :
166+ logger .warning ("index %s not exists" , index )
167+
168+ async def search_question (index : str , keywords : List [str ], topk = 3 ):
169+ resp = await async_es .indices .exists (index = index )
170+ if not resp .body :
171+ return []
172+
173+ if not keywords :
174+ return []
175+
176+ query = {
177+ "bool" : {
178+ "should" : [
179+ {"match" : {"content" : keyword }} for keyword in keywords
180+ ],
181+ "minimum_should_match" : "80%" ,
182+ # "minimum_should_match": "-1",
183+ },
184+ }
185+ sort = [
186+ {
187+ "_score" : {
188+ "order" : "desc"
189+ }
190+ }
191+ ]
192+ resp = await async_es .search (index = index , query = query , sort = sort , size = topk )
193+ hits = resp .body ["hits" ]
194+ result = []
195+ for hit in hits ["hits" ]:
196+ result .append (hit ["_source" ])
197+ return result
0 commit comments