mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-24 17:36:47 +08:00
fix: not sorted when list chunks (#17329)
### Summary As title, sorted now
This commit is contained in:
@@ -840,8 +840,15 @@ func (s *ChunkService) List(req *service.ListChunksRequest, userID string) (*ser
|
||||
page := common.CoalesceInt(req.Page, 1)
|
||||
size := common.CoalesceInt(req.Size, 30)
|
||||
keywords := strings.TrimSpace(req.Keywords)
|
||||
var orderBy *types.OrderByExpr
|
||||
matchExprs := make([]interface{}, 0, 1)
|
||||
if keywords != "" {
|
||||
if keywords == "" {
|
||||
orderBy = (&types.OrderByExpr{}).
|
||||
Asc("chunk_order_int").
|
||||
Asc("page_num_int").
|
||||
Asc("top_int").
|
||||
Desc("create_timestamp_flt")
|
||||
} else {
|
||||
matchExprs = append(matchExprs, &types.MatchTextExpr{
|
||||
MatchingText: keywords,
|
||||
TopN: size,
|
||||
@@ -855,6 +862,7 @@ func (s *ChunkService) List(req *service.ListChunksRequest, userID string) (*ser
|
||||
KbIDs: kbIDs,
|
||||
Offset: (page - 1) * size,
|
||||
Limit: size,
|
||||
OrderBy: orderBy,
|
||||
SelectFields: []string{
|
||||
"id",
|
||||
"content_with_weight",
|
||||
|
||||
@@ -279,6 +279,54 @@ func TestParseRejectsRunningDocument(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestListSortsChunksByDocumentPosition(t *testing.T) {
|
||||
db := setupChunkTestDB(t)
|
||||
pushChunkTestDB(t, db)
|
||||
|
||||
userID := "user-1"
|
||||
tenantID := "tenant-1"
|
||||
datasetID := "kb-1"
|
||||
documentID := "doc-1"
|
||||
insertChunkTestUserTenant(t, userID, tenantID)
|
||||
insertChunkTestKB(t, datasetID, tenantID)
|
||||
insertChunkTestDoc(t, documentID, datasetID)
|
||||
|
||||
engine := &listChunksSearchEngine{}
|
||||
svc := &ChunkService{
|
||||
docEngine: engine,
|
||||
kbDAO: dao.NewKnowledgebaseDAO(),
|
||||
userTenantDAO: dao.NewUserTenantDAO(),
|
||||
documentDAO: dao.NewDocumentDAO(),
|
||||
}
|
||||
|
||||
page := 1
|
||||
size := 30
|
||||
if _, err := svc.List(&service.ListChunksRequest{
|
||||
DatasetID: datasetID,
|
||||
DocID: documentID,
|
||||
Page: &page,
|
||||
Size: &size,
|
||||
}, userID); err != nil {
|
||||
t.Fatalf("List() error = %v", err)
|
||||
}
|
||||
|
||||
if engine.searchReq == nil {
|
||||
t.Fatal("expected Search to be called")
|
||||
}
|
||||
if engine.searchReq.OrderBy == nil {
|
||||
t.Fatal("expected OrderBy to be set")
|
||||
}
|
||||
want := []types.OrderByField{
|
||||
{Field: "chunk_order_int", Type: types.SortAsc},
|
||||
{Field: "page_num_int", Type: types.SortAsc},
|
||||
{Field: "top_int", Type: types.SortAsc},
|
||||
{Field: "create_timestamp_flt", Type: types.SortDesc},
|
||||
}
|
||||
if !reflect.DeepEqual(engine.searchReq.OrderBy.Fields, want) {
|
||||
t.Fatalf("OrderBy fields = %#v, want %#v", engine.searchReq.OrderBy.Fields, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListBuildsMatchTextExprForKeywords(t *testing.T) {
|
||||
db := setupChunkTestDB(t)
|
||||
pushChunkTestDB(t, db)
|
||||
|
||||
Reference in New Issue
Block a user