Skip to content

Commit d5de299

Browse files
committed
feat: recmmend similar questions
1 parent 352eb6f commit d5de299

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

kubechat/chat/websocket/base_consumer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ async def receive(self, text_data=None, bytes_data=None):
126126
references = []
127127
related_question = []
128128
message_id = f"{now_unix_milliseconds()}"
129-
129+
130+
# index = generate_total_question_index_name(document.collection.id) # !!改
131+
# insert_question(index, message_id, message)
130132
try:
131133
# send start message
132134
await self.send(text_data=start_response(message_id))

kubechat/context/full_text.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)