Skip to content

Commit

Permalink
Fetch chunk by batches. (#4177)
Browse files Browse the repository at this point in the history
### What problem does this PR solve?

#4173

### Type of change

- [x] Performance Improvement
  • Loading branch information
KevinHuSh authored Dec 23, 2024
1 parent 2cbe064 commit 31d67c8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
15 changes: 11 additions & 4 deletions rag/nlp/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def search(self, req, idx_names: str | list[str], kb_ids: list[str], emb_mdl=Non
pg = int(req.get("page", 1)) - 1
topk = int(req.get("topk", 1024))
ps = int(req.get("size", topk))
offset, limit = pg * ps, (pg + 1) * ps
offset, limit = pg * ps, ps

src = req.get("fields", ["docnm_kwd", "content_ltks", "kb_id", "img_id", "title_tks", "important_kwd", "position_int",
"doc_id", "page_num_int", "top_int", "create_timestamp_flt", "knowledge_graph_kwd", "question_kwd", "question_tks",
Expand Down Expand Up @@ -380,6 +380,13 @@ def sql_retrieval(self, sql, fetch_size=128, format="json"):

def chunk_list(self, doc_id: str, tenant_id: str, kb_ids: list[str], max_count=1024, fields=["docnm_kwd", "content_with_weight", "img_id"]):
condition = {"doc_id": doc_id}
res = self.dataStore.search(fields, [], condition, [], OrderByExpr(), 0, max_count, index_name(tenant_id), kb_ids)
dict_chunks = self.dataStore.getFields(res, fields)
return dict_chunks.values()
res = []
bs = 128
for p in range(0, max_count, bs):
res = self.dataStore.search(fields, [], condition, [], OrderByExpr(), p, bs, index_name(tenant_id), kb_ids)
dict_chunks = self.dataStore.getFields(res, fields)
if dict_chunks:
res.extend(dict_chunks.values())
if len(dict_chunks.values()) < bs:
break
return res
2 changes: 1 addition & 1 deletion rag/utils/es_conn.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def search(self, selectFields: list[str], highlightFields: list[str], condition:
s = s.sort(*orders)

if limit > 0:
s = s[offset:limit]
s = s[offset:offset+limit]
q = s.to_dict()
logger.debug(f"ESConnection.search {str(indexNames)} query: " + json.dumps(q))

Expand Down

0 comments on commit 31d67c8

Please sign in to comment.