mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-08-01 21:37:33 +08:00
Test: release Go-proxy RESTful contract tests verified passing in Go mode (#17468)
### Summary Aligns Go and Python error codes/messages so both backends honor the same RESTful API contract, removing implementation-specific error leaks (MySQL errors, `ValueError`, `AttributeError`, Gin validator format) in favor of clean business error codes. **Chat list** — invalid `orderby` now returns code 101 (was: raw Python `AttributeError` code 100); invalid `page`/`page_size` values fall back to defaults (was: raw `ValueError`/`ProgrammingError` code 100). **Dataset create/update/delete** — adds UUID validation (101), extra-field rejection (101), duplicate-id detection (101), content-type / JSON-syntax / object-shape checks (101), and "lacks permission" for nonexistent datasets (IDOR). Create auto-deduplicates dataset names. Pagerank updates tolerate a missing ES index. List response includes `parser_config` and `pagerank`. **Session list/update** — adds filtering, sorting, and pagination support. Empty payloads are valid no-ops. Authorization errors map to code 109. **Chunk list** — doc object uses Python key names (`chunk_count`, `dataset_id`, `chunk_method`, run text status). Add validates list element types. **Document update** — adds `chunk_method` alias, pydantic-style Field error messages, metadata index auto-create with refresh, and "These documents do not belong to dataset" messages. List validates `metadata_condition` and reports ownership errors for unmatched name/id filters. **Search completion** — `kb_ids` ownership failure returns code 102 instead of 109. Released 31 contract tests from `GO_ONLY_SKIPS` (all verified passing on both Go and Python backends with real LLM keys).
This commit is contained in:
@@ -287,7 +287,7 @@ func (e *elasticsearchEngine) UpdateChunks(ctx context.Context, condition map[st
|
||||
return fmt.Errorf("failed to check index existence: %w", err)
|
||||
}
|
||||
if !exists {
|
||||
return fmt.Errorf("index '%s' does not exist", fullIndexName)
|
||||
return fmt.Errorf("%w: '%s'", types.ErrIndexNotFound, fullIndexName)
|
||||
}
|
||||
|
||||
if strings.HasPrefix(fullIndexName, "memory_") {
|
||||
|
||||
@@ -36,7 +36,9 @@ func (e *elasticsearchEngine) dropIndex(ctx context.Context, indexName string) e
|
||||
return fmt.Errorf("failed to check index existence: %w", err)
|
||||
}
|
||||
if !exists {
|
||||
return fmt.Errorf("index '%s' does not exist", indexName)
|
||||
// Tolerate missing index (mirrors Python's docStoreConn which
|
||||
// silently returns False on a non-existent index).
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete index
|
||||
|
||||
@@ -89,19 +89,6 @@ func (e *elasticsearchEngine) InsertMetadata(ctx context.Context, metadata []map
|
||||
return nil, fmt.Errorf("index name cannot be empty")
|
||||
}
|
||||
|
||||
// Check if index exists, create if not
|
||||
exists, err := e.indexExists(ctx, indexName)
|
||||
if err != nil {
|
||||
common.Error("Failed to check index existence", err)
|
||||
return nil, fmt.Errorf("failed to check index existence: %w", err)
|
||||
}
|
||||
if !exists {
|
||||
// Create metadata index
|
||||
if createErr := e.CreateMetadataStore(ctx, tenantID); createErr != nil {
|
||||
return nil, fmt.Errorf("failed to create metadata index: %w", createErr)
|
||||
}
|
||||
}
|
||||
|
||||
// Build bulk request body
|
||||
var buf bytes.Buffer
|
||||
for _, doc := range metadata {
|
||||
@@ -114,12 +101,12 @@ func (e *elasticsearchEngine) InsertMetadata(ctx context.Context, metadata []map
|
||||
continue
|
||||
}
|
||||
|
||||
// Action line: use json.Marshal to properly escape string values
|
||||
compositeID := fmt.Sprintf("%d:%s|%d:%s", len(docID), docID, len(kbID), kbID)
|
||||
// Action line: use the plain document id as _id, matching Python's
|
||||
// es_conn.insert (readers shim hit._id back into the "id" field).
|
||||
action, err := json.Marshal(map[string]interface{}{
|
||||
"index": map[string]interface{}{
|
||||
"_index": indexName,
|
||||
"_id": compositeID,
|
||||
"_id": docID,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
@@ -170,30 +157,19 @@ func (e *elasticsearchEngine) InsertMetadata(ctx context.Context, metadata []map
|
||||
return []string{}, nil
|
||||
}
|
||||
|
||||
// UpdateMetadata updates or inserts document metadata in tenant's metadata index.
|
||||
// UpdateMetadata fully replaces the meta_fields for a document in tenant's metadata index.
|
||||
//
|
||||
// Examples (existing row → input → resulting meta_fields):
|
||||
// UpdateMetadata fully replaces the meta_fields for a document in the
|
||||
// document engine. Callers must send the complete desired meta_fields map —
|
||||
// unchanged keys are NOT preserved (unlike a merge). This mirrors Python's
|
||||
// replace_meta_fields semantics: stale keys must not survive an update.
|
||||
//
|
||||
// {character:["曹操","孙权"], year:2025}
|
||||
// + {author:["John","Tom"], category:"tech"}
|
||||
// = {character:["曹操","孙权"], year:2025, author:["John","Tom"], category:"tech"}
|
||||
//
|
||||
// {character:["曹操","孙权"], year:2025}
|
||||
// + {year:2026}
|
||||
// = {character:["曹操","孙权"], year:2026}
|
||||
// The metadata index must already exist; the service layer is responsible
|
||||
// for creating it before writing.
|
||||
func (e *elasticsearchEngine) UpdateMetadata(ctx context.Context, docID string, datasetID string, metaFields map[string]interface{}, tenantID string) error {
|
||||
indexName := buildMetadataIndexName(tenantID)
|
||||
common.Info("ElasticsearchConnection.UpdateMetadata called", zap.String("index_name", indexName), zap.String("docID", docID), zap.String("datasetID", datasetID))
|
||||
|
||||
// Check if index exists
|
||||
exists, err := e.indexExists(ctx, indexName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to check index existence: %w", err)
|
||||
}
|
||||
if !exists {
|
||||
return fmt.Errorf("index '%s' does not exist", indexName)
|
||||
}
|
||||
|
||||
// Build the document ID for update
|
||||
docIDStr := strings.ReplaceAll(docID, "'", "''")
|
||||
datasetIDStr := strings.ReplaceAll(datasetID, "'", "''")
|
||||
@@ -208,14 +184,13 @@ func (e *elasticsearchEngine) UpdateMetadata(ctx context.Context, docID string,
|
||||
},
|
||||
}
|
||||
|
||||
// Painless script: for every (key, value) in params.meta_fields,
|
||||
// set ctx._source.meta_fields[key] = value. Existing keys not
|
||||
// present in params.meta_fields are preserved. If the row has no
|
||||
// meta_fields at all yet, initialize it to an empty map first.
|
||||
// Painless script: fully replace meta_fields (mirrors Python's
|
||||
// replace_meta_fields — stale keys must not survive the update).
|
||||
updateReq := map[string]interface{}{
|
||||
"query": query,
|
||||
"query": query,
|
||||
"conflicts": "proceed",
|
||||
"script": map[string]interface{}{
|
||||
"source": "if (ctx._source.meta_fields == null) { ctx._source.meta_fields = new HashMap(); } for (entry in params.meta_fields.entrySet()) { ctx._source.meta_fields[entry.getKey()] = entry.getValue(); }",
|
||||
"source": "ctx._source.meta_fields = params.meta_fields;",
|
||||
"lang": "painless",
|
||||
"params": map[string]interface{}{
|
||||
"meta_fields": metaFields,
|
||||
@@ -229,8 +204,9 @@ func (e *elasticsearchEngine) UpdateMetadata(ctx context.Context, docID string,
|
||||
}
|
||||
|
||||
req := esapi.UpdateByQueryRequest{
|
||||
Index: []string{indexName},
|
||||
Body: bytes.NewReader(updateBytes),
|
||||
Index: []string{indexName},
|
||||
Body: bytes.NewReader(updateBytes),
|
||||
Refresh: func(b bool) *bool { return &b }(true),
|
||||
}
|
||||
|
||||
res, err := req.Do(ctx, e.client)
|
||||
@@ -300,10 +276,12 @@ func (e *elasticsearchEngine) DeleteMetadata(ctx context.Context, condition map[
|
||||
return 0, fmt.Errorf("failed to marshal delete body: %w", err)
|
||||
}
|
||||
|
||||
// Execute delete by query
|
||||
// Execute delete by query. Refresh so follow-up reads/writes do not see
|
||||
// the stale pre-delete state (mirrors Python's refresh semantics).
|
||||
req := esapi.DeleteByQueryRequest{
|
||||
Index: []string{indexName},
|
||||
Body: bytes.NewReader(bodyBytes),
|
||||
Index: []string{indexName},
|
||||
Body: bytes.NewReader(bodyBytes),
|
||||
Refresh: func(b bool) *bool { return &b }(true),
|
||||
}
|
||||
|
||||
res, err := req.Do(ctx, e.client)
|
||||
@@ -346,7 +324,7 @@ func (e *elasticsearchEngine) DeleteMetadataKeys(ctx context.Context, docID stri
|
||||
return fmt.Errorf("failed to check index existence: %w", err)
|
||||
}
|
||||
if !exists {
|
||||
return fmt.Errorf("index '%s' does not exist", indexName)
|
||||
return fmt.Errorf("%w: '%s'", types.ErrIndexNotFound, indexName)
|
||||
}
|
||||
|
||||
// Build the document ID for query (no escaping needed for ES term queries)
|
||||
@@ -508,8 +486,9 @@ func (e *elasticsearchEngine) DeleteMetadataKeys(ctx context.Context, docID stri
|
||||
}
|
||||
|
||||
req := esapi.UpdateByQueryRequest{
|
||||
Index: []string{indexName},
|
||||
Body: bytes.NewReader(updateBytes),
|
||||
Index: []string{indexName},
|
||||
Body: bytes.NewReader(updateBytes),
|
||||
Refresh: func(b bool) *bool { return &b }(true),
|
||||
}
|
||||
|
||||
res, err := req.Do(ctx, e.client)
|
||||
|
||||
@@ -400,6 +400,12 @@ func (e *infinityEngine) UpdateChunks(ctx context.Context, condition map[string]
|
||||
|
||||
table, err := db.GetTable(tableName)
|
||||
if err != nil {
|
||||
// Tolerate missing table (mirrors Python's docStoreConn which
|
||||
// silently returns False on a non-existent table).
|
||||
errMsg := strings.ToLower(err.Error())
|
||||
if strings.Contains(errMsg, "not found") || strings.Contains(errMsg, "doesn't exist") {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("Failed to get table %s: %w", tableName, err)
|
||||
}
|
||||
|
||||
|
||||
@@ -135,7 +135,8 @@ func (e *infinityEngine) createMetadataStoreWithDB(db *infinity.Database, tenant
|
||||
}
|
||||
|
||||
// InsertMetadata inserts document metadata into tenant's metadata table
|
||||
// Auto-create the table if it doesn't exist
|
||||
// The metadata table must already exist; the service layer is responsible
|
||||
// for creating it before writing.
|
||||
// Replace existing metadata with same id and kb_id
|
||||
func (e *infinityEngine) InsertMetadata(ctx context.Context, metadata []map[string]interface{}, tenantID string) ([]string, error) {
|
||||
tableName := buildMetadataTableName(tenantID)
|
||||
@@ -149,21 +150,7 @@ func (e *infinityEngine) InsertMetadata(ctx context.Context, metadata []map[stri
|
||||
|
||||
table, err := db.GetTable(tableName)
|
||||
if err != nil {
|
||||
// Table doesn't exist, try to create it
|
||||
errMsg := strings.ToLower(err.Error())
|
||||
if !strings.Contains(errMsg, "not found") && !strings.Contains(errMsg, "doesn't exist") {
|
||||
return nil, fmt.Errorf("failed to get table %s: %w", tableName, err)
|
||||
}
|
||||
|
||||
// Create metadata table
|
||||
if createErr := e.createMetadataStoreWithDB(db, tenantID); createErr != nil {
|
||||
return nil, fmt.Errorf("failed to create metadata table: %w", createErr)
|
||||
}
|
||||
|
||||
table, err = db.GetTable(tableName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get table after creation: %w", err)
|
||||
}
|
||||
return nil, fmt.Errorf("failed to get table %s: %w", tableName, err)
|
||||
}
|
||||
|
||||
// Transform metadata - convert meta_fields map to JSON string
|
||||
@@ -421,6 +408,12 @@ func (e *infinityEngine) DeleteMetadataKeys(ctx context.Context, docID string, d
|
||||
|
||||
table, err := db.GetTable(tableName)
|
||||
if err != nil {
|
||||
// Tolerate missing metadata table (mirrors Python's docStoreConn
|
||||
// which silently returns False on a non-existent table).
|
||||
errMsg := strings.ToLower(err.Error())
|
||||
if strings.Contains(errMsg, "not found") || strings.Contains(errMsg, "doesn't exist") {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("failed to get metadata table %s: %w", tableName, err)
|
||||
}
|
||||
|
||||
|
||||
@@ -27,6 +27,11 @@ import (
|
||||
|
||||
var ErrDocumentNotFound = errors.New("document not found")
|
||||
|
||||
// ErrIndexNotFound marks operations against a document-engine index that does
|
||||
// not exist yet (e.g. no chunks were ever indexed). Callers may treat it as a
|
||||
// tolerable no-op, mirroring Python's docStoreConn behavior.
|
||||
var ErrIndexNotFound = errors.New("index does not exist")
|
||||
|
||||
// SearchRequest unified search request for all engines
|
||||
type SearchRequest struct {
|
||||
// Search target
|
||||
|
||||
Reference in New Issue
Block a user