Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

Refine handling of POST /api/v1/datasets/search in GO (#15583)

qinling0210c960dc2a4c54
diff --git a/api/apps/services/dataset_api_service.py b/api/apps/services/dataset_api_service.py
index 3994316123..6cbc9770d7 100644
--- a/api/apps/services/dataset_api_service.py
+++ b/api/apps/services/dataset_api_service.py
@@ -1058,9 +1058,8 @@ async def search(dataset_id: str, tenant_id: str, req: dict):
ranks["chunks"].insert(0, ck)
except Exception:
logging.warning("search KG retrieval failed: dataset=%s tenant=%s", dataset_id, tenant_id, exc_info=True)
- total = ranks.get("total", 0)
ranks["chunks"] = settings.retriever.retrieval_by_children(ranks["chunks"], tenant_ids)
- ranks["total"] = total
+ ranks["total"] = len(ranks["chunks"])
for c in ranks["chunks"]:
c.pop("vector", None)
@@ -1430,9 +1429,8 @@ async def search_datasets(tenant_id: str, req: dict):
ranks["chunks"].insert(0, ck)
except Exception:
logging.warning("search_datasets KG retrieval failed: datasets=%s tenant=%s", kb_ids, tenant_id, exc_info=True)
- total = ranks.get("total", 0)
ranks["chunks"] = settings.retriever.retrieval_by_children(ranks["chunks"], tenant_ids)
- ranks["total"] = total
+ ranks["total"] = len(ranks["chunks"])
for c in ranks["chunks"]:
c.pop("vector", None)
diff --git a/api/db/services/doc_metadata_service.py b/api/db/services/doc_metadata_service.py
index fbe32f9e5b..ccc66925bd 100644
--- a/api/db/services/doc_metadata_service.py
+++ b/api/db/services/doc_metadata_service.py
@@ -24,7 +24,7 @@ import json
import logging
import re
from copy import deepcopy
-from typing import Dict, List, Optional
+from typing import Any, Dict, List, Optional
from api.db.db_models import DB, Document
from common import settings
@@ -33,6 +33,36 @@ from api.db.db_models import Knowledgebase
from common.doc_store.doc_store_base import OrderByExpr
+def _es_response_total(response: Any) -> Optional[int]:
+ """Extract the exact total hit count from an ES search response.
+
+ Returns ``None`` when the field is missing or in an unexpected shape
+ — callers should treat that as "cannot verify" rather than "no
+ overflow".
+ """
+ if not isinstance(response, dict):
+ try:
+ response = dict(response)
+ except Exception:
+ return None
+ hits = response.get("hits")
+ if not isinstance(hits, dict):
+ return None
+ total = hits.get("total")
+ if isinstance(total, dict):
+ value = total.get("value")
+ if isinstance(value, bool):
+ return None
+ if isinstance(value, int):
+ return value
+ if isinstance(value, float):
+ return int(value)
+ elif isinstance(total, int):
+ # Legacy shape: some clients return the count directly.
+ return total
+ return None
+
+
class DocMetadataService:
"""Service for managing document metadata in ES/Infinity"""
@@ -876,6 +906,10 @@ class DocMetadataService:
**query_body,
"size": limit,
"_source": ["id"],
+ # Make hits.total.value exact. ES otherwise caps the tracked
+ # total at 10,000 with relation="gte", which would let
+ # overflow slip through undetected.
+ "track_total_hits": True,
}
try:
@@ -898,6 +932,22 @@ class DocMetadataService:
f"ES metadata filter hit limit {limit} for KBs {kb_ids}"
)
+ # Detect silent truncation: the push-down is a fast path, not
+ # the system of record. When the query matched more than
+ # ``limit`` docs, the slice we built here is necessarily a
+ # strict subset of the truth, and the caller treats any
+ # non-None result as definitive. Bail out and let the caller
+ # fall back to the in-memory ``meta_filter`` (correct, just
+ # slower for very large result sets) instead of silently
+ # dropping docs.
+ total = _es_response_total(response)
+ if total is not None and total > limit:
+ logging.warning(
+ f"ES metadata filter result exceeds push-down cap, falling back to in-memory: "
+ f"total={total}, cap={limit}, kb_ids={kb_ids}"
+ )
+ return None
+
logging.debug(f"ES metadata filter returned {len(unique)} matches for KBs {kb_ids}")
return unique
diff --git a/cmd/admin_server.go b/cmd/admin_server.go
index 46c695fa60..940606ab93 100644
--- a/cmd/admin_server.go
+++ b/cmd/admin_server.go
@@ -45,7 +45,7 @@ func main() {
flag.Parse()
// Initialize logger
- if err := common.Init("info"); err != nil {
+ if err := common.Init("info", "admin_server.log"); err != nil {
panic("failed to initialize logger: " + err.Error())
}
@@ -59,7 +59,7 @@ func main() {
// Reinitialize logger with configured level if different
if cfg.Log.Level != "" && cfg.Log.Level != "info" {
- if err := common.Init(cfg.Log.Level); err != nil {
+ if err := common.Init(cfg.Log.Level, "admin_server.log"); err != nil {
common.Error("Failed to reinitialize logger with configured level", err)
}
}
diff --git a/cmd/ingestion_server.go b/cmd/ingestion_server.go
index 94a45f129f..a46a70e40f 100644
--- a/cmd/ingestion_server.go
+++ b/cmd/ingestion_server.go
@@ -72,7 +72,7 @@ func main() {
flag.Parse()
// Initialize logger with default level
- if err := common.Init("info"); err != nil {
+ if err := common.Init("info", "ingestion_server.log"); err != nil {
panic(fmt.Sprintf("Failed to initialize logger: %v", err))
}
@@ -96,10 +96,12 @@ func main() {
}
// Reinitialize logger with configured level if different
- if config.Log.Level != "" && config.Log.Level != "info" {
- if err := common.Init(config.Log.Level); err != nil {
- common.Error("Failed to reinitialize logger with configured level", err)
- }
+ level := config.Log.Level
+ if level == "" {
+ level = "info"
+ }
+ if err := common.Init(level, "ingestion_server.log"); err != nil {
+ common.Error("Failed to reinitialize logger", err)
}
server.SetLogger(common.Logger)
diff --git a/cmd/ragflow_cli.go b/cmd/ragflow_cli.go
index d8303ca9b0..f4e33c1e7c 100644
--- a/cmd/ragflow_cli.go
+++ b/cmd/ragflow_cli.go
@@ -39,7 +39,7 @@ func main() {
if args.Verbose {
logLevel = "info"
}
- if err = common.Init(logLevel); err != nil {
+ if err = common.Init(logLevel, ""); err != nil {
fmt.Printf("Warning: Failed to initialize logger: %v\n", err)
}
diff --git a/cmd/server_main.go b/cmd/server_main.go
index c7a49b507a..f1b656d4d3 100644
--- a/cmd/server_main.go
+++ b/cmd/server_main.go
@@ -71,7 +71,7 @@ func main() {
// Initialize logger with default level
// logger.Init("info"); // set debug log level
- if err := common.Init("info"); err != nil {
+ if err := common.Init("info", "server_main.log"); err != nil {
panic(fmt.Sprintf("Failed to initialize logger: %v", err))
}
@@ -92,10 +92,12 @@ func main() {
}
// Reinitialize logger with configured level if different
- if config.Log.Level != "" && config.Log.Level != "info" {
- if err := common.Init(config.Log.Level); err != nil {
- common.Error("Failed to reinitialize logger with configured level", err)
- }
+ level := config.Log.Level
+ if level == "" {
+ level = "info"
+ }
+ if err := common.Init(level, "server_main.log"); err != nil {
+ common.Error("Failed to reinitialize logger", err)
}
server.SetLogger(common.Logger)
if config.Log.Level == "" {
diff --git a/common/doc_store/infinity_conn_base.py b/common/doc_store/infinity_conn_base.py
index af8493b82b..169bd85e87 100644
--- a/common/doc_store/infinity_conn_base.py
+++ b/common/doc_store/infinity_conn_base.py
@@ -546,6 +546,17 @@ class InfinityConnectionBase(DocStoreConnection):
except Exception as e:
self.logger.warning(f"Failed to create index on kb_id for {table_name}: {e}")
+ # Create secondary index on meta_fields for metadata filter queries
+ try:
+ inf_table.create_index(
+ f"idx_{table_name}_meta_fields",
+ IndexInfo("meta_fields", IndexType.Secondary),
+ ConflictType.Ignore,
+ )
+ self.logger.debug(f"INFINITY created secondary index on meta_fields for table {table_name}")
+ except Exception as e:
+ self.logger.warning(f"Failed to create index on meta_fields for {table_name}: {e}")
+
self.logger.debug(f"INFINITY created document metadata table {table_name} with secondary indexes")
return True
diff --git a/go.mod b/go.mod
index 5d748842a6..54ce2bee7a 100644
--- a/go.mod
+++ b/go.mod
@@ -14,6 +14,7 @@ require (
github.com/google/uuid v1.6.0
github.com/infiniflow/infinity-go-sdk v0.0.0-00010101000000-000000000000
github.com/iromli/go-itsdangerous v0.0.0-20220223194502-9c8bef8dac6a
+ github.com/json-iterator/go v1.1.12
github.com/minio/minio-go/v7 v7.0.99
github.com/peterh/liner v1.2.2
github.com/redis/go-redis/v9 v9.18.0
@@ -78,7 +79,6 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
- github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.18.2 // indirect
github.com/klauspost/cpuid/v2 v2.2.11 // indirect
github.com/klauspost/crc32 v1.3.0 // indirect
diff --git a/internal/cli/README.md b/internal/cli/README.md
index f55dc21e14..0dcb8ccf30 100644
--- a/internal/cli/README.md
+++ b/internal/cli/README.md
@@ -103,6 +103,9 @@ search "machine learning" # Search all datasets (JSON output)
search "neural networks" datasets/kb1 # Search in kb1
search "AI" datasets/kb1 --output plain # Plain text output
search "RAG" -n 20 # Return 20 results
+SEARCH 'machine learning' ON DATASETS 'kb1' 'kb2'
+SEARCH 'AI' ON DATASETS 'kb1' WITH top_k 1024 similarity_threshold 0.0 vector_similarity_weight 0.3 keyword true
+SEARCH 'AI' ON DATASETS 'kb1' WITH cross_languages ['Chinese']
```
#### `cat <path>` - Display content
diff --git a/internal/cli/cli.go b/internal/cli/cli.go
index 770ca98341..42a9b37910 100644
--- a/internal/cli/cli.go
+++ b/internal/cli/cli.go
@@ -421,13 +421,13 @@ const historyFileName = ".ragflow_cli_history"
// CLI represents the command line interface
type CLI struct {
- client *RAGFlowClient
- contextEngine *filesystem.Engine
- prompt string
- running bool
- line *liner.State
- args *ConnectionArgs
- outputFormat OutputFormat // Output format
+ client *RAGFlowClient
+ contextEngine *filesystem.Engine
+ prompt string
+ running bool
+ line *liner.State
+ args *ConnectionArgs
+ outputFormat OutputFormat // Output format
}
// NewCLI creates a new CLI instance
@@ -1072,13 +1072,13 @@ func (c *CLI) printSkillSearchResults(result *filesystem.Result, format OutputFo
// Skill search result structure
type skillSearchResult struct {
- SkillID string `json:"skill_id"`
- Name string `json:"name"`
- Description string `json:"description"`
- Tags string `json:"tags"`
- Score float64 `json:"score"`
- BM25Score float64 `json:"bm25_score"`
- VectorScore float64 `json:"vector_score"`
+ SkillID string `json:"skill_id"`
+ Name string `json:"name"`
+ Description string `json:"description"`
+ Tags string `json:"tags"`
+ Score float64 `json:"score"`
+ BM25Score float64 `json:"bm25_score"`
+ VectorScore float64 `json:"vector_score"`
}
results := make([]skillSearchResult, 0, len(result.Nodes))
@@ -1457,6 +1457,41 @@ Examples:
search "AI" datasets/kb1 # Search in kb1
search "RAG" skills/space1 -n 20 # Search skills in hub1, return 20 results
search "data processing" skills # Search skills (default space)
+
+Datasets syntax (full filter set):
+ search 'query' on datasets 'kb_names' [with <option> <value> ...] [;]
+
+ 'kb_names' is a single quoted string. Pass one name for a single
+ dataset, or a comma-separated list (no spaces) to search across
+ multiple datasets in one call:
+ 'kb1' # one dataset
+ 'kb1,kb2' # two datasets
+ 'kb1,kb2,kb3' # three datasets
+
+ When 'on datasets' is given, the search runs against the named
+ dataset(s) and accepts the full WITH-option set below.
+
+ WITH options (space-separated, not comma-separated):
+ top_k <int> Number of results (default 5)
+ page_size <int> Page size for pagination
+ page <int> Page number (1-based)
+ similarity_threshold <float> Minimum similarity score (0.0-1.0)
+ vector_similarity_weight <float> Weight given to vector vs text score
+ keyword true|false Enable keyword extraction via LLM
+ use_kg true|false Enable knowledge-graph augmentation
+ rerank_id 'id' Rerank model to apply
+ tenant_rerank_id 'id' Tenant-scoped rerank model
+ search_id 'id' Idempotency / search-session id
+ meta_data_filter '<json>' Metadata filter (must be valid JSON)
+ cross_languages ['a','b'] Source languages to translate from
+ doc_ids ['d1',...] Restrict to specific document ids
+
+ Examples:
+ search 'AI' on datasets 'kb_chinese' with top_k 10;
+ search 'AI' on datasets 'kb1' 'kb2' with top_k 20 similarity_threshold 0.3 cross_languages ['Chinese']
+ doc_ids ['d1', 'd2'];
+ search 'manual' on datasets 'kb1' with
+ meta_data_filter '{"method":"manual","conditions":[{"key":"author","op":"eq","value":"Luo"}]}';
`
fmt.Println(help)
}
diff --git a/internal/cli/client.go b/internal/cli/client.go
index 961265328e..a7c2613de1 100644
--- a/internal/cli/client.go
+++ b/internal/cli/client.go
@@ -223,6 +223,9 @@ func (c *RAGFlowClient) ExecuteUserCommand(cmd *Command) (ResponseIf, error) {
return c.ListDatasetDocumentUserCommand(cmd)
case "search_on_datasets":
return c.SearchOnDatasets(cmd)
+ case "search_help":
+ printSearchHelp()
+ return nil, nil
case "create_token":
return c.CreateToken(cmd)
case "list_tokens":
@@ -335,8 +338,8 @@ func (c *RAGFlowClient) ExecuteUserCommand(cmd *Command) (ResponseIf, error) {
return c.RmTags(cmd)
case "remove_chunks":
return c.RemoveChunks(cmd)
- case "list_metadata":
- return c.ListMetadata(cmd)
+ case "get_metadata":
+ return c.GetMetadata(cmd)
case "parse_documents_user_command":
return c.ParseDocumentsUserCommand(cmd)
// ContextEngine commands
diff --git a/internal/cli/lexer.go b/internal/cli/lexer.go
index ea9075908b..cfd634d005 100644
--- a/internal/cli/lexer.go
+++ b/internal/cli/lexer.go
@@ -94,6 +94,12 @@ func (l *Lexer) NextToken() Token {
case '-':
tok = newToken(TokenDash, l.ch)
l.readChar()
+ case '[':
+ tok = newToken(TokenLBracket, l.ch)
+ l.readChar()
+ case ']':
+ tok = newToken(TokenRBracket, l.ch)
+ l.readChar()
case '\'':
tok.Type = TokenQuotedString
tok.Value = l.readQuotedString('\'')
diff --git a/internal/cli/parser.go b/internal/cli/parser.go
index 214a1fa963..48493c2b98 100644
--- a/internal/cli/parser.go
+++ b/internal/cli/parser.go
@@ -308,7 +308,10 @@ func (p *Parser) parseNumber() (int, error) {
}
func (p *Parser) parseFloat() (float64, error) {
- if p.curToken.Type != TokenInteger {
+ // Accept either TokenInteger or TokenFloat so that literals like
+ // `0.3` (which the lexer tags as TokenFloat) and `10` (TokenInteger)
+ // both parse cleanly.
+ if p.curToken.Type != TokenInteger && p.curToken.Type != TokenFloat {
return math.NaN(), fmt.Errorf("expected number, got %s", p.curToken.Value)
}
result, err := strconv.ParseFloat(p.curToken.Value, 64)
@@ -319,8 +322,66 @@ func (p *Parser) parseFloat() (float64, error) {
return result, nil
}
+// parseQuotedStringList consumes a bracket-delimited list of quoted strings:
+// [ 'a', 'b', 'c' ]
+// Empty list [] is allowed. The cursor must be positioned on '[' when called;
+// on return, the cursor is positioned just past the closing ']'.
+func (p *Parser) parseQuotedStringList() ([]string, error) {
+ if p.curToken.Type != TokenLBracket {
+ return nil, fmt.Errorf("expected '[', got %s", p.curToken.Value)
+ }
+ p.nextToken() // skip '['
+
+ // Always return a non-nil slice so callers (and json.Marshal) see []
+ // instead of null for the empty-list case.
+ list := make([]string, 0)
+ // Allow empty list []
+ if p.curToken.Type == TokenRBracket {
+ p.nextToken() // skip ']'
+ return list, nil
+ }
+
+ for {
+ s, err := p.parseQuotedString()
+ if err != nil {
+ return nil, fmt.Errorf("expected quoted string in list: %w", err)
+ }
+ list = append(list, s)
+ p.nextToken() // step past the closing quote
+
+ if p.curToken.Type == TokenComma {
+ p.nextToken() // step past ','
+ continue
+ }
+ if p.curToken.Type == TokenRBracket {
+ p.nextToken() // step past ']'
+ return list, nil
+ }
+ return nil, fmt.Errorf("expected ',' or ']' in list, got %s", p.curToken.Value)
+ }
+}
+
func tokenTypeToString(t int) string {
- // Simplified for error messages
+ switch t {
+ case TokenEOF:
+ return "end of input"
+ case TokenIdentifier:
+ return "identifier"
+ case TokenInteger:
+ return "integer"
+ case TokenFloat:
+ return "float"
+ case TokenQuotedString:
+ return "quoted string"
+ case TokenLBracket:
+ return "'['"
+ case TokenRBracket:
+ return "']'"
+ case TokenComma:
+ return "','"
+ case TokenSemicolon:
+ return "';'"
+ }
return fmt.Sprintf("token(%d)", t)
}
diff --git a/internal/cli/types.go b/internal/cli/types.go
index ed662843ea..c8634b4fd9 100644
--- a/internal/cli/types.go
+++ b/internal/cli/types.go
@@ -187,6 +187,8 @@ const (
TokenEOF
TokenDash
TokenIllegal
+ TokenLBracket // '['
+ TokenRBracket // ']'
)
// Token represents a lexical token
diff --git a/internal/cli/user_command.go b/internal/cli/user_command.go
index 9a643c69b8..43c8d94614 100644
--- a/internal/cli/user_command.go
+++ b/internal/cli/user_command.go
@@ -498,8 +498,8 @@ func (c *RAGFlowClient) getDatasetID(datasetName string) (string, error) {
return "", fmt.Errorf("dataset '%s' not found", datasetName)
}
-// ListMetadata lists metadata for datasets
-func (c *RAGFlowClient) ListMetadata(cmd *Command) (ResponseIf, error) {
+// GetMetadata gets metadata for one or more datasets
+func (c *RAGFlowClient) GetMetadata(cmd *Command) (ResponseIf, error) {
if c.ServerType != "user" {
return nil, fmt.Errorf("this command is only allowed in USER mode")
}
@@ -608,6 +608,68 @@ func (c *RAGFlowClient) SearchOnDatasets(cmd *Command) (ResponseIf, error) {
"vector_similarity_weight": 0.3,
}
+ // Add optional parameters from command
+ if val, ok := cmd.Params["top_k"]; ok {
+ payload["top_k"] = val
+ }
+ if val, ok := cmd.Params["similarity_threshold"]; ok {
+ payload["similarity_threshold"] = val
+ }
+ if val, ok := cmd.Params["vector_similarity_weight"]; ok {
+ payload["vector_similarity_weight"] = val
+ }
+ if val, ok := cmd.Params["keyword"]; ok {
+ payload["keyword"] = val
+ }
+ if val, ok := cmd.Params["use_kg"]; ok {
+ payload["use_kg"] = val
+ }
+ if val, ok := cmd.Params["rerank_id"]; ok {
+ payload["rerank_id"] = val
+ }
+ if val, ok := cmd.Params["tenant_rerank_id"]; ok {
+ payload["tenant_rerank_id"] = val
+ }
+ if val, ok := cmd.Params["page_size"]; ok {
+ payload["page_size"] = val
+ }
+ if val, ok := cmd.Params["page"]; ok {
+ payload["page"] = val
+ }
+ if val, ok := cmd.Params["search_id"]; ok {
+ if s, ok := val.(string); ok {
+ payload["search_id"] = s
+ }
+ }
+ if val, ok := cmd.Params["cross_languages"]; ok {
+ if list, ok := val.([]string); ok {
+ payload["cross_languages"] = list
+ }
+ }
+ if val, ok := cmd.Params["doc_ids"]; ok {
+ if list, ok := val.([]string); ok {
+ payload["doc_ids"] = list
+ }
+ }
+ if val, ok := cmd.Params["meta_data_filter"]; ok {
+ // Accept either a raw JSON string from the CLI or a pre-decoded
+ // map[string]interface{} (future-proofing for callers that
+ // construct the command programmatically). The string form is
+ // the public CLI surface; the map form is for unit tests.
+ switch v := val.(type) {
+ case string:
+ var decoded map[string]interface{}
+ if err := json.Unmarshal([]byte(v), &decoded); err != nil {
+ return nil, fmt.Errorf("invalid meta_data_filter JSON: %w", err)
+ }
+ payload["meta_data_filter"] = decoded
+ case map[string]interface{}:
+ payload["meta_data_filter"] = v
+ default:
+ return nil, fmt.Errorf("meta_data_filter must be JSON string or object")
+ }
+ }
+
if iterations > 1 {
// Benchmark mode - return raw result for benchmark stats
return c.HTTPClient.RequestWithIterations("POST", "/datasets/search", "web", nil, payload, iterations)
diff --git a/internal/cli/user_parser.go b/internal/cli/user_parser.go
index 1dbc848b2b..01613ee9ff 100644
--- a/internal/cli/user_parser.go
+++ b/internal/cli/user_parser.go
@@ -6,6 +6,13 @@ import (
"strings"
)
+func tokenTypeDescription(t int, tok Token) string {
+ if tok.Type == t && tok.Value != "" {
+ return fmt.Sprintf("%s %q", tokenTypeToString(t), tok.Value)
+ }
+ return tokenTypeToString(t)
+}
+
// Command parsers
func (p *Parser) parseLogout() (*Command, error) {
cmd := NewCommand("logout")
@@ -138,8 +145,6 @@ func (p *Parser) parseListCommand() (*Command, error) {
return p.parseListDatasets()
case TokenDocuments:
return p.parseListDatasetDocuments()
- case TokenMetadata:
- return p.parseListMetadata()
case TokenAgents:
return p.parseListAgents()
case TokenTokens:
@@ -210,7 +215,7 @@ func (p *Parser) parseListDatasetDocuments() (*Command, error) {
return cmd, nil
}
-func (p *Parser) parseListMetadata() (*Command, error) {
+func (p *Parser) parseGetMetadata() (*Command, error) {
p.nextToken() // consume METADATA
if p.curToken.Type != TokenOf {
@@ -233,6 +238,10 @@ func (p *Parser) parseListMetadata() (*Command, error) {
datasetNames = append(datasetNames, name)
p.nextToken()
+
+ if p.curToken.Type == TokenComma {
+ return nil, fmt.Errorf("syntax error: dataset names must be space-separated, not comma-separated (got %q after %q)", "'", name)
+ }
// Stop at semicolon or non-quoted (dataset name must be quoted)
if p.curToken.Type == TokenSemicolon {
break
@@ -243,7 +252,7 @@ func (p *Parser) parseListMetadata() (*Command, error) {
}
}
- cmd := NewCommand("list_metadata")
+ cmd := NewCommand("get_metadata")
cmd.Params["dataset_names"] = datasetNames
// Semicolon is optional
@@ -2379,6 +2388,27 @@ func (p *Parser) parseInsertMetadataFromFile() (*Command, error) {
func (p *Parser) parseSearchCommand() (*Command, error) {
p.nextToken() // consume SEARCH
+ // Handle help flag: -h / --help. The lexer tokenizes each leading
+ // `-` as a separate `TokenDash` and then the rest of the flag name
+ // (e.g. "help") as a `TokenIdentifier`. We collect any leading
+ // dashes before checking the identifier value. Short-circuit with
+ // a dedicated command type so the dispatcher can print the search
+ // usage instead of erroring out on the missing question.
+ if p.curToken.Type == TokenDash {
+ dashCount := 0
+ for p.curToken.Type == TokenDash {
+ dashCount++
+ p.nextToken()
+ }
+ if dashCount > 0 && p.curToken.Type == TokenIdentifier {
+ switch strings.ToLower(p.curToken.Value) {
+ case "h", "help":
+ return NewCommand("search_help"), nil
+ }
+ }
+ return nil, fmt.Errorf("expected quoted string or identifier")
+ }
+
var err error
var question string
if p.curToken.Type == TokenQuotedString {
@@ -2415,7 +2445,119 @@ func (p *Parser) parseSearchCommand() (*Command, error) {
cmd.Params["datasets"] = datasets
p.nextToken()
- // Semicolon is optional for UNSET TOKEN
+
+ // Parse optional WITH clause for additional parameters
+ if p.curToken.Type == TokenWith || (p.curToken.Type == TokenIdentifier && strings.ToLower(p.curToken.Value) == "with") {
+ if p.curToken.Type == TokenWith {
+ p.nextToken()
+ } else {
+ p.nextToken() // skip "with" identifier
+ }
+
+ for p.curToken.Type != TokenEOF && p.curToken.Type != TokenSemicolon {
+ if p.curToken.Type == TokenComma {
+ return nil, fmt.Errorf("syntax error: WITH options must be space-separated, not comma-separated")
+ }
+ // Parse parameter name
+ if p.curToken.Type != TokenIdentifier {
+ break
+ }
+ paramName := strings.ToLower(p.curToken.Value)
+ p.nextToken()
+
+ // Parse parameter value
+ var paramValue interface{}
+ valueToken := p.curToken.Type
+ var valueErr error
+ switch p.curToken.Type {
+ case TokenInteger:
+ paramValue, valueErr = p.parseNumber()
+ if valueErr != nil {
+ return nil, valueErr
+ }
+ p.nextToken() // step past the integer
+ case TokenFloat:
+ paramValue, valueErr = p.parseFloat()
+ if valueErr != nil {
+ return nil, valueErr
+ }
+ p.nextToken() // step past the float
+ case TokenQuotedString:
+ paramValue, valueErr = p.parseQuotedString()
+ if valueErr != nil {
+ return nil, valueErr
+ }
+ p.nextToken() // step past the closing quote
+ case TokenIdentifier:
+ // Bare identifiers are only meaningful for the
+ // boolean keys (keyword / use_kg = true|false);
+ // everything else rejects them.
+ paramValue = p.curToken.Value
+ p.nextToken()
+ case TokenLBracket:
+ // List value: parsed inside the switch below by
+ // cross_languages / doc_ids. No value is captured here.
+ paramValue = nil
+ default:
+ // EOF, ';', or any other non-value token: the option
+ // is missing a value, which is a hard error rather
+ // than a silent drop.
+ return nil, fmt.Errorf("WITH option %q is missing a value", paramName)
+ }
+
+ switch paramName {
+ case "top_k", "page_size", "page":
+ if valueToken != TokenInteger {
+ return nil, fmt.Errorf("WITH option %q must be an integer, got %s", paramName, tokenTypeDescription(valueToken, p.curToken))
+ }
+ cmd.Params[paramName] = paramValue
+ case "similarity_threshold", "vector_similarity_weight":
+ switch n := paramValue.(type) {
+ case int:
+ cmd.Params[paramName] = float64(n)
+ case float64:
+ cmd.Params[paramName] = n
+ default:
+ return nil, fmt.Errorf("WITH option %q must be a number, got %s", paramName, tokenTypeDescription(valueToken, p.curToken))
+ }
+ case "keyword", "use_kg":
+ s, ok := paramValue.(string)
+ if !ok {
+ return nil, fmt.Errorf("WITH option %q must be true or false, got %s", paramName, tokenTypeDescription(valueToken, p.curToken))
+ }
+ switch strings.ToLower(s) {
+ case "true":
+ cmd.Params[paramName] = true
+ case "false":
+ cmd.Params[paramName] = false
+ default:
+ return nil, fmt.Errorf("WITH option %q must be true or false, got %q", paramName, s)
+ }
+ case "rerank_id", "tenant_rerank_id", "search_id", "meta_data_filter":
+ if valueToken != TokenQuotedString {
+ return nil, fmt.Errorf("WITH option %q must be a quoted string, got %s", paramName, tokenTypeDescription(valueToken, p.curToken))
+ }
+ // meta_data_filter JSON string is decoded into a map in
+ // the SearchOnDatasets handler; parser stores the raw
+ // string so the handler can surface a clean error on
+ // invalid JSON.
+ cmd.Params[paramName] = paramValue
+ case "cross_languages", "doc_ids":
+ if p.curToken.Type != TokenLBracket {
+ return nil, fmt.Errorf("WITH option %q must be a list, e.g. %q ['a', 'b']", paramName, paramName)
+ }
+ list, err := p.parseQuotedStringList()
+ if err != nil {
+ return nil, err
+ }
+ cmd.Params[paramName] = list
+ default:
+ return nil, fmt.Errorf("unknown WITH option %q", paramName)
+ }
+ }
+ }
+
+ // Semicolon is optional
if p.curToken.Type == TokenSemicolon {
p.nextToken()
}
@@ -3599,13 +3741,16 @@ func (p *Parser) parseUnsetCommand() (*Command, error) {
return NewCommand("unset_token"), nil
}
-// parseGetCommand parses: GET CHUNK 'chunk_id'
+// parseGetCommand parses: GET CHUNK or GET METADATA
func (p *Parser) parseGetCommand() (*Command, error) {
p.nextToken() // consume GET
if p.curToken.Type == TokenChunk {
return p.parseGetChunk()
}
+ if p.curToken.Type == TokenMetadata {
+ return p.parseGetMetadata()
+ }
return nil, fmt.Errorf("unknown GET target: %s", p.curToken.Value)
}
diff --git a/internal/common/constants.go b/internal/common/constants.go
index c9d730727a..4c3283b3a7 100644
--- a/internal/common/constants.go
+++ b/internal/common/constants.go
@@ -5,4 +5,9 @@ const (
PAGERANK_FLD = "pagerank_fea"
// TAG_FLD is the field name for tag features
TAG_FLD = "tag_feas"
+ // MAX_RESULT_WINDOW is the maximum result window for ES
+ MAX_RESULT_WINDOW = 10000
+ // SearchAfterBatchSize caps how many hits one Elasticsearch
+ // request can return per search_after iteration.
+ SearchAfterBatchSize = 1000
)
diff --git a/internal/common/float.go b/internal/common/float.go
index b3dca37784..fd989f1000 100644
--- a/internal/common/float.go
+++ b/internal/common/float.go
@@ -16,25 +16,126 @@
package common
-const epsilon32 = 1e-6
-const epsilon64 = 1e-9
+import "strconv"
-func Float64IsZero(f float64) bool {
- if f < 0 && f >= -epsilon64 {
- return true
- }
- if f > 0 && f <= epsilon64 {
- return true
+// PyFloat64 is a float64 that serializes to JSON using the same format as Python's json.dumps.
+// Python uses the "shortest unique representation" algorithm (dtoa) for float64,
+// which is equivalent to Go's strconv.FormatFloat with 'g' and precision -1.
+// This ensures deterministic and Python-compatible float serialization.
+type PyFloat64 float64
+
+// MarshalJSON implements the json.Marshaler interface for PyFloat64.
+// Uses strconv.FormatFloat with 'g' format and -1 precision to produce
+// the shortest decimal representation that uniquely identifies the float64,
+// matching Python's json.dumps behavior.
+func (f PyFloat64) MarshalJSON() ([]byte, error) {
+ return []byte(strconv.FormatFloat(float64(f), 'g', -1, 64)), nil
+}
+
+// ConvertFloatsToPyFormat recursively converts all float64 values in nested
+// map[string]interface{} and []interface{} structures to PyFloat64, ensuring
+// Python-compatible JSON serialization. Typed float slices ([]float64,
+// []float32, and their nested variants) are also handled so common vector
+// payload shapes don't fall through to Go's default float formatting.
+func ConvertFloatsToPyFormat(v interface{}) interface{} {
+ switch val := v.(type) {
+ case float64:
+ return PyFloat64(val)
+ case float32:
+ return PyFloat64(val)
+ case map[string]interface{}:
+ result := make(map[string]interface{}, len(val))
+ for k, v2 := range val {
+ result[k] = ConvertFloatsToPyFormat(v2)
+ }
+ return result
+ case []interface{}:
+ result := make([]interface{}, len(val))
+ for i, item := range val {
+ result[i] = ConvertFloatsToPyFormat(item)
+ }
+ return result
+ case []map[string]interface{}:
+ result := make([]map[string]interface{}, len(val))
+ for i, item := range val {
+ result[i] = ConvertFloatsToPyFormat(item).(map[string]interface{})
+ }
+ return result
+ case []float64:
+ result := make([]PyFloat64, len(val))
+ for i, f := range val {
+ result[i] = PyFloat64(f)
+ }
+ return result
+ case []float32:
+ result := make([]PyFloat64, len(val))
+ for i, f := range val {
+ result[i] = PyFloat64(f)
+ }
+ return result
+ case [][]float64:
+ result := make([][]PyFloat64, len(val))
+ for i, inner := range val {
+ converted := ConvertFloatsToPyFormat(inner).([]PyFloat64)
+ result[i] = converted
+ }
+ return result
+ case [][]float32:
+ result := make([][]PyFloat64, len(val))
+ for i, inner := range val {
+ converted := ConvertFloatsToPyFormat(inner).([]PyFloat64)
+ result[i] = converted
+ }
+ return result
+ default:
+ return v
}
- return false
}
-func Float32IsNotZero(f float32) bool {
- if f < 0 && f >= -epsilon32 {
- return true
+// PairwiseSum returns the sum of xs computed via pairwise (cascade) summation,
+// matching the error behavior of numpy.sum(): O(log n * eps) instead of
+// the O(n * eps) of a naive left-to-right loop.
+//
+// This implementation matches numpy's exact pairwise summation algorithm:
+// - For n < 16: uses naive left-to-right sum (matching numpy's small-array optimization)
+// - For n >= 16: processes pairs left-to-right, carrying any odd element to the end
+// of the next level. This matches numpy's pairwise reduction in
+// numpy/core/src/umath/reduction.c.
+//
+// xs is modified in place. Pass a copy if the caller still needs the input.
+//
+// Empty input returns 0; single-element input returns xs[0].
+func PairwiseSum(xs []float64) float64 {
+ n := len(xs)
+ if n == 0 {
+ return 0
+ }
+
+ // For small arrays (n < 16), numpy uses naive left-to-right sum.
+ // This is critical for matching Python's exact float64 results.
+ // Empirically verified: numpy's np.sum() uses naive sum for n < 16.
+ if n < 16 {
+ sum := 0.0
+ for _, x := range xs {
+ sum += x
+ }
+ return sum
}
- if f > 0 && f <= epsilon32 {
- return true
+
+ // Pairwise summation matching numpy's algorithm:
+ // Process pairs left-to-right, carry odd element to the end.
+ for n > 1 {
+ m := n / 2
+ for i := 0; i < m; i++ {
+ xs[i] = xs[2*i] + xs[2*i+1]
+ }
+ // If odd length, carry the last element to position m
+ if n%2 != 0 {
+ xs[m] = xs[n-1]
+ n = m + 1
+ } else {
+ n = m
+ }
}
- return false
+ return xs[0]
}
diff --git a/internal/common/libm_cgo.go b/internal/common/libm_cgo.go
new file mode 100644
index 0000000000..f2d48845af
--- /dev/null
+++ b/internal/common/libm_cgo.go
@@ -0,0 +1,47 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+//go:build cgo
+
+package common
+
+/*
+#cgo LDFLAGS: -lm
+#include <math.h>
+*/
+import "C"
+
+// PyLog10 calls the C library's log10 (matching Python's math.log10).
+//
+// Go's pure-Go math.Log10 can differ from glibc's log10 by 1 ULP on
+// some inputs (e.g. log10(0.1) returns 0xbfefffffffffffff in Go vs
+// 0xbff0000000000000 in glibc), which breaks bit-exact parity tests
+// against Python scoring code. PyLog10 routes through cgo so the
+// result matches Python's math.log10 exactly.
+func PyLog10(x float64) float64 {
+ return float64(C.log10(C.double(x)))
+}
+
+// PySqrt calls the C library's sqrt (matching Python's math.sqrt).
+//
+// Go's math.Sqrt is a correctly-rounded pure-Go implementation, but
+// PySqrt exists for symmetry with PyLog10 and as a defensive guarantee
+// against the rare inputs where Go's implementation diverges from
+// glibc's. The cgo overhead is negligible on the scoring paths that
+// use it.
+func PySqrt(x float64) float64 {
+ return float64(C.sqrt(C.double(x)))
+}
diff --git a/internal/common/libm_purego.go b/internal/common/libm_purego.go
new file mode 100644
index 0000000000..d7e7c70d07
--- /dev/null
+++ b/internal/common/libm_purego.go
@@ -0,0 +1,45 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+//go:build !cgo
+
+package common
+
+import "math"
+
+// PyLog10 returns log10(x) using Go's pure-Go math implementation.
+//
+// The cgo build of this package (see libm_cgo.go) routes through glibc's
+// log10 to match Python's math.log10 bit-exactly. That build is
+// preferred for the scoring paths that compare results against Python
+// reference output. This fallback is provided so that internal/common
+// — and the entrypoints that import it — remain buildable with
+// CGO_ENABLED=0 (and other constrained cross-compilation setups that
+// can't link -lm). Results may differ from Python's math.log10 by up
+// to 1 ULP on some inputs; this is acceptable for non-strict-parity
+// builds.
+func PyLog10(x float64) float64 {
+ return math.Log10(x)
+}
+
+// PySqrt returns sqrt(x) using Go's pure-Go math implementation.
+//
+// Provided as a counterpart to PyLog10 for the !cgo build. Go's
+// math.Sqrt is a correctly-rounded implementation; PySqrt exists for
+// API symmetry with the cgo build.
+func PySqrt(x float64) float64 {
+ return math.Sqrt(x)
+}
diff --git a/internal/common/logger.go b/internal/common/logger.go
index f856761c04..24aa274a6f 100644
--- a/internal/common/logger.go
+++ b/internal/common/logger.go
@@ -19,6 +19,7 @@ package common
import (
"fmt"
"os"
+ "path/filepath"
"runtime"
"strings"
"sync"
@@ -93,7 +94,7 @@ func initPackageLogLevels(rootLevel zapcore.Level) {
// Init initializes the global logger
// Note: This requires zap to be installed: go get go.uber.org/zap
-func Init(level string) error {
+func Init(level string, logFile string) error {
zapLevel, err := parseZapLevel(level)
if err != nil {
zapLevel = zapcore.InfoLevel
@@ -118,14 +119,23 @@ func Init(level string) error {
EncodeCaller: zapcore.ShortCallerEncoder, // Not used since CallerKey is empty
}
+ // Determine output paths
+ outputPaths := []string{"stdout"}
+ errorPaths := []string{"stderr"}
+ if logFile != "" {
+ logFile = filepath.Join("logs", logFile)
+ outputPaths = append(outputPaths, logFile)
+ errorPaths = append(errorPaths, logFile)
+ }
+
// Configure zap
config := zap.Config{
Level: atomicLevel,
Development: false,
Encoding: "console",
EncoderConfig: encoderConfig,
- OutputPaths: []string{"stdout"},
- ErrorOutputPaths: []string{"stderr"},
+ OutputPaths: outputPaths,
+ ErrorOutputPaths: errorPaths,
}
// Build logger
diff --git a/internal/cpp/darts_trie.cpp b/internal/cpp/darts_trie.cpp
index 15b103b33e..5fc44779e0 100644
--- a/internal/cpp/darts_trie.cpp
+++ b/internal/cpp/darts_trie.cpp
@@ -43,6 +43,7 @@ int32_t POSTable::Load() {
x.second = index++;
pos_vec_.push_back(x.first);
}
+ table_size_ = static_cast<int32_t>(pos_vec_.size());
return 0;
// return Status::OK();
}
diff --git a/internal/dao/kb.go b/internal/dao/kb.go
index d74c98b7b0..2bae3e0417 100644
--- a/internal/dao/kb.go
+++ b/internal/dao/kb.go
@@ -18,6 +18,7 @@ package dao
import (
"errors"
+ "fmt"
"path"
"ragflow/internal/entity"
@@ -27,6 +28,18 @@ import (
"gorm.io/gorm"
)
+// GetTenantIDByKBID is a convenience function that retrieves the tenant ID
+// for a given knowledge base ID. It is a package-level helper so both the
+// service and engine layers can use it without circular imports.
+func GetTenantIDByKBID(kbID string) (string, error) {
+ kbDAO := NewKnowledgebaseDAO()
+ kb, err := kbDAO.GetByID(kbID)
+ if err != nil {
+ return "", fmt.Errorf("knowledgebase not found: %w", err)
+ }
+ return kb.TenantID, nil
+}
+
// KnowledgebaseDAO knowledge base data access object
type KnowledgebaseDAO struct{}
@@ -240,14 +253,32 @@ func (dao *KnowledgebaseDAO) GetDetail(kbID string) (*entity.KnowledgebaseDetail
return &detail, nil
}
-// Accessible checks if a knowledge base is accessible by a user
-// This matches the Python accessible method
+// Accessible checks if a knowledge base is accessible by a user.
+// This matches the Python accessible method:
+// 1. KB must exist and be VALID
+// 2. If user is the owner tenant, return true
+// 3. If permission is "me", only owner tenant can access
+// 4. If permission is "team", user must be a member of the tenant
func (dao *KnowledgebaseDAO) Accessible(kbID, userID string) bool {
+ var kb entity.Knowledgebase
+ err := DB.Where("id = ? AND status = ?", kbID, string(entity.StatusValid)).First(&kb).Error
+ if err != nil {
+ return false
+ }
+
+ // User is the owner tenant itself
+ if kb.TenantID == userID {
+ return true
+ }
+
+ // If permission is "me", only the owner can access
+ if kb.Permission == string(entity.TenantPermissionMe) {
+ return false
+ }
+
var count int64
- err := DB.Table("knowledgebase").
- Joins("JOIN user_tenant ON user_tenant.tenant_id = knowledgebase.tenant_id").
- Where("knowledgebase.id = ? AND user_tenant.user_id = ? AND knowledgebase.status = ?",
- kbID, userID, string(entity.StatusValid)).
+ err = DB.Table("user_tenant").
+ Where("tenant_id = ? AND user_id = ?", kb.TenantID, userID).
Count(&count).Error
if err != nil {
@@ -324,7 +355,7 @@ func (dao *KnowledgebaseDAO) AtomicIncreaseDocNumByID(kbID string) error {
return DB.Model(&entity.Knowledgebase{}).
Where("id = ?", kbID).
Updates(map[string]interface{}{
- "doc_num": DB.Raw("doc_num + 1"),
+ "doc_num": DB.Raw("doc_num + 1"),
}).Error
}
@@ -334,9 +365,9 @@ func (dao *KnowledgebaseDAO) DecreaseDocumentNum(kbID string, docNum, chunkNum,
return DB.Model(&entity.Knowledgebase{}).
Where("id = ?", kbID).
Updates(map[string]interface{}{
- "doc_num": DB.Raw("doc_num - ?", docNum),
- "chunk_num": DB.Raw("chunk_num - ?", chunkNum),
- "token_num": DB.Raw("token_num - ?", tokenNum),
+ "doc_num": DB.Raw("doc_num - ?", docNum),
+ "chunk_num": DB.Raw("chunk_num - ?", chunkNum),
+ "token_num": DB.Raw("token_num - ?", tokenNum),
}).Error
}
diff --git a/internal/dao/tenant.go b/internal/dao/tenant.go
index 044adfbcb3..f857944f4f 100644
--- a/internal/dao/tenant.go
+++ b/internal/dao/tenant.go
@@ -62,7 +62,7 @@ type TenantInfo struct {
ASRID string `gorm:"column:asr_id" json:"asr_id"`
Img2TxtID string `gorm:"column:img2txt_id" json:"img2txt_id"`
TTSID *string `gorm:"column:tts_id" json:"tts_id,omitempty"`
- OCRID string `gorm:"column:ocr_id" json:"ocr_id"`
+ OCRID *string `gorm:"column:ocr_id" json:"ocr_id,omitempty"`
ParserIDs string `gorm:"column:parser_ids" json:"parser_ids"`
Role string `gorm:"column:role" json:"role"`
}
diff --git a/internal/dao/tenant_model.go b/internal/dao/tenant_model.go
index 7df8f809f9..88dd719e95 100644
--- a/internal/dao/tenant_model.go
+++ b/internal/dao/tenant_model.go
@@ -83,6 +83,24 @@ func (dao *TenantModelDAO) GetModelByProviderIDAndInstanceIDAndModelName(provide
return &model, nil
}
+func (dao *TenantModelDAO) GetModelsByProviderIDAndInstanceIDAndModelName(providerID, instanceID, modelName string) ([]*entity.TenantModel, error) {
+ var models []*entity.TenantModel
+ err := DB.Where("provider_id = ? AND instance_id = ? AND model_name = ?", providerID, instanceID, modelName).Find(&models).Error
+ if err != nil {
+ return nil, err
+ }
+ return models, nil
+}
+
+func (dao *TenantModelDAO) GetByProviderIDAndInstanceIDAndModelTypeAndModelName(providerID, instanceID, modelType, modelName string) (*entity.TenantModel, error) {
+ var model entity.TenantModel
+ err := DB.Where("provider_id = ? AND instance_id = ? AND model_type = ? AND model_name = ?", providerID, instanceID, modelType, modelName).First(&model).Error
+ if err != nil {
+ return nil, err
+ }
+ return &model, nil
+}
+
// GetModelsByInstanceID get all models by instance ID
func (dao *TenantModelDAO) GetModelsByInstanceID(instanceID string) ([]*entity.TenantModel, error) {
var models []*entity.TenantModel
diff --git a/internal/development.md b/internal/development.md
index 9834ef4f7e..8e0f275e17 100644
--- a/internal/development.md
+++ b/internal/development.md
@@ -423,7 +423,7 @@ RAGFlow(user)> DROP CHUNK STORE FOR DATASET 'test'
- Search chunks
```
-RAGFlow(user)> SEARCH '曹操' ON DATASETS 'test'
+RAGFlow(user)> SEARCH 'AI' ON DATASETS 'test'
```
- Get chunks
@@ -462,7 +462,42 @@ DELETE METADATA OF DOCUMENT 'bbe55942535e11f1bc5184ba59049aa3' KEYS '["key1", "k
RAGFlow(user)> DROP METADATA STORE
```
-- List metadata
+- Get metadata
```
-RAGFlow(user)> LIST METADATA OF DATASET 'test' 'test2'
+RAGFlow(user)> GET METADATA OF DATASET 'test' 'test2'
+```
+
+### 6.28 Search datasets
+
+- Search datasets
+```
+RAGFlow(user)> SEARCH 'AI' ON DATASETS 'test';
+
+RAGFlow(user)> SEARCH 'AI' ON DATASETS 'test1' 'test2';
+
+RAGFlow(user)> SEARCH 'AI' ON DATASETS 'test' WITH top_k 1;
+
+RAGFlow(user)> SEARCH 'AI' ON DATASETS 'test' WITH page 2 page_size 20;
+
+RAGFlow(user)> SEARCH 'AI' ON DATASETS 'test' WITH similarity_threshold 0.5;
+
+RAGFlow(user)> SEARCH 'AI' ON DATASETS 'test' WITH vector_similarity_weight 0.0;
+
+RAGFlow(user)> SEARCH 'AI' ON DATASETS 'test' WITH keyword true;
+
+RAGFlow(user)> SEARCH 'AI' ON DATASETS 'test' WITH use_kg true;
+
+RAGFlow(user)> SEARCH 'AI' ON DATASETS 'test' WITH rerank_id 'BAAI/bge-reranker-v2-m3@CI@SILICONFLOW';
+
+RAGFlow(user)> SEARCH 'AI' ON DATASETS 'test' WITH search_id 'abc123';
+
+RAGFlow(user)> SEARCH 'AI' ON DATASETS 'test' WITH cross_languages ['Chinese'];
+
+RAGFlow(user)> SEARCH 'AI' ON DATASETS 'test' WITH doc_ids ['doc_a', 'doc_b'];
+
+RAGFlow(user)> SEARCH 'AI' ON DATASETS 'test' WITH meta_data_filter '{"method":"auto"}';
+
+RAGFlow(user)> SEARCH 'AI' ON DATASETS 'test' WITH meta_data_filter '{"method":"manual","conditions":[{"key":"author","op":"eq","value":"Luo"}]}';
+
+RAGFlow(user)> SEARCH 'AI' ON DATASETS 'test' WITH top_k 50 similarity_threshold 0.5 vector_similarity_weight 0.5 use_kg true;
```
\ No newline at end of file
diff --git a/internal/engine/elasticsearch/chunk.go b/internal/engine/elasticsearch/chunk.go
index 39e8707443..71c928b60b 100644
--- a/internal/engine/elasticsearch/chunk.go
+++ b/internal/engine/elasticsearch/chunk.go
@@ -23,24 +23,31 @@ import (
"fmt"
"io"
"os"
+ "reflect"
"slices"
"sort"
+ "strconv"
"strings"
"github.com/elastic/go-elasticsearch/v8/esapi"
+ "github.com/json-iterator/go"
"ragflow/internal/common"
"ragflow/internal/engine/types"
"go.uber.org/zap"
)
+var jsonIterator = jsoniter.Config{
+ SortMapKeys: false,
+}.Froze()
+
// CreateChunkStore creates an index
func (e *elasticsearchEngine) CreateChunkStore(ctx context.Context, baseName, datasetID string, vectorSize int, parserID string) error {
if baseName == "" {
return fmt.Errorf("index name cannot be empty")
}
- // Check if index already exists (matches Python create_idx behavior)
+ // Check if index already exists
exists, err := e.indexExists(ctx, baseName)
if err != nil {
return fmt.Errorf("failed to check index existence: %w", err)
@@ -138,13 +145,11 @@ func (e *elasticsearchEngine) InsertChunks(ctx context.Context, chunks []map[str
continue
}
- compositeID := fmt.Sprintf("%s_%s_%s", docID, datasetID, chunkID)
-
// Action line: use json.Marshal to properly escape string values
action, err := json.Marshal(map[string]interface{}{
"index": map[string]interface{}{
"_index": baseName,
- "_id": compositeID,
+ "_id": chunkID,
},
})
if err != nil {
@@ -157,12 +162,12 @@ func (e *elasticsearchEngine) InsertChunks(ctx context.Context, chunks []map[str
// Document line: work with a copy to avoid mutating the original
docCopy := copyFields(doc)
docCopy["kb_id"] = datasetID
- if err := json.NewEncoder(&buf).Encode(docCopy); err != nil {
+ if err := jsonIterator.NewEncoder(&buf).Encode(docCopy); err != nil {
return nil, fmt.Errorf("failed to encode document: %w", err)
}
}
- // Execute bulk request with refresh="wait_for" (matches Python behavior)
+ // Execute bulk request with refresh="wait_for"
req := esapi.BulkRequest{
Body: bytes.NewReader(buf.Bytes()),
Refresh: "wait_for",
@@ -229,7 +234,7 @@ func (e *elasticsearchEngine) UpdateChunks(ctx context.Context, condition map[st
return e.updateChunksByQuery(ctx, fullIndexName, condition, newValue)
}
-// updateSingleChunk handles single document update (matches Python lines 350-398)
+// updateSingleChunk handles single document update
func (e *elasticsearchEngine) updateSingleChunk(ctx context.Context, indexName, chunkID string, newValue map[string]interface{}) error {
common.Debug("ElasticsearchConnection.updateSingleChunk called", zap.String("indexName", indexName), zap.String("chunkID", chunkID))
@@ -502,10 +507,10 @@ func (e *elasticsearchEngine) updateChunksByQuery(ctx context.Context, indexName
// Execute update by query with refresh=true, slices=5, conflicts="proceed"
refreshTrue := true
req := esapi.UpdateByQueryRequest{
- Index: []string{indexName},
- Body: bytes.NewReader(bodyBytes),
- Refresh: &refreshTrue,
- Slices: 5,
+ Index: []string{indexName},
+ Body: bytes.NewReader(bodyBytes),
+ Refresh: &refreshTrue,
+ Slices: 5,
Conflicts: "proceed",
}
@@ -708,8 +713,15 @@ type SearchResponse struct {
} `json:"total"`
Hits []struct {
ID string `json:"_id"`
+ Index string `json:"_index"`
Score float64 `json:"_score"`
Source map[string]interface{} `json:"_source"`
+ Fields map[string]interface{} `json:"fields"` // ES 9.x stores dense_vector here
+ // Sort is populated when the request body specifies a `sort`
+ // clause. The last hit's Sort is the cursor for the next
+ // search_after request — without it, deep pagination can't
+ // advance.
+ Sort []interface{} `json:"sort,omitempty"`
} `json:"hits"`
} `json:"hits"`
Aggregations map[string]interface{} `json:"aggregations"`
@@ -717,364 +729,287 @@ type SearchResponse struct {
// Search executes search with unified types.SearchRequest
func (e *elasticsearchEngine) Search(ctx context.Context, req *types.SearchRequest) (*types.SearchResult, error) {
- return e.searchUnified(ctx, req)
-}
-
-// searchUnified handles the unified types.SearchRequest
-// Matches the behavior of Infinity's Search() method
-func (e *elasticsearchEngine) searchUnified(ctx context.Context, req *types.SearchRequest) (*types.SearchResult, error) {
- common.Debug("Search in Elasticsearch started", zap.Any("indexNames", req.IndexNames))
+ types.LogSearchRequest("Elasticsearch", req)
+ // Validate inputs and set defaults
if len(req.IndexNames) == 0 {
return nil, fmt.Errorf("index names cannot be empty")
}
- // Get retrieval parameters with defaults
- pageSize := req.Limit
- if pageSize <= 0 {
- pageSize = 30
- }
-
offset := req.Offset
if offset < 0 {
offset = 0
}
+ limit := req.Limit
+ if limit <= 0 {
+ limit = 30
+ }
- isMetadataTable := false
+ // Detect index types
isSkillIndex := false
for _, idx := range req.IndexNames {
- if strings.HasPrefix(idx, "ragflow_doc_meta_") {
- isMetadataTable = true
- break
- }
if strings.HasPrefix(idx, "skill_") {
isSkillIndex = true
break
}
}
- var outputColumns []string
- if isMetadataTable {
- outputColumns = []string{"id", "kb_id", "meta_fields"}
- } else if isSkillIndex {
- outputColumns = []string{
- "skill_id", "space_id", "folder_id", "name", "tags", "description", "content",
- "version", "status", "create_time", "update_time",
- }
- } else {
- outputColumns = []string{
- "id", "doc_id", "kb_id", "content_ltks", "content_with_weight",
- "title_tks", "docnm_kwd", "img_id", "available_int", "important_kwd",
- "position_int", "page_num_int", "top_int", "chunk_order_int",
- "create_timestamp_flt", "knowledge_graph_kwd", "question_kwd", "question_tks",
- "doc_type_kwd", "mom_id", "tag_kwd", "pagerank_fea", "tag_feas",
- }
- }
-
- // Allow caller to override output columns (used by KG search, etc.)
- if len(req.SelectFields) > 0 {
- outputColumns = req.SelectFields
- }
+ // Build bool query from condition
+ boolQuery := buildBoolQueryFromCondition(req.Filter, req.KbIDs, isSkillIndex)
- hasTextMatch := false
- hasVectorMatch := false
+ // Extract vector_similarity_weight from FusionExpr
var matchText *types.MatchTextExpr
var matchDense *types.MatchDenseExpr
- if req.MatchExprs != nil && len(req.MatchExprs) > 0 {
- for _, expr := range req.MatchExprs {
- if expr == nil {
- continue
- }
- switch e := expr.(type) {
- case string:
- if e != "" {
- hasTextMatch = true
- matchText = &types.MatchTextExpr{
- MatchingText: e,
- TopN: pageSize,
+ vectorSimilarityWeight := 0.5
+ for _, expr := range req.MatchExprs {
+ if expr == nil {
+ continue
+ }
+ switch m := expr.(type) {
+ case *types.FusionExpr:
+ if m.Method == "weighted_sum" {
+ if weights, ok := m.FusionParams["weights"].(string); ok {
+ // Assert structure only when FusionExpr has weighted_sum with weights
+ if len(req.MatchExprs) != 3 {
+ return nil, fmt.Errorf("match_expressions must have exactly 3 elements with FusionExpr, got %d", len(req.MatchExprs))
+ }
+ if _, ok := req.MatchExprs[0].(*types.MatchTextExpr); !ok {
+ return nil, fmt.Errorf("match_expressions[0] must be MatchTextExpr")
+ }
+ if _, ok := req.MatchExprs[1].(*types.MatchDenseExpr); !ok {
+ return nil, fmt.Errorf("match_expressions[1] must be MatchDenseExpr")
+ }
+ if _, ok := req.MatchExprs[2].(*types.FusionExpr); !ok {
+ return nil, fmt.Errorf("match_expressions[2] must be FusionExpr")
+ }
+ parts := strings.Split(weights, ",")
+ if len(parts) == 2 {
+ if w, err := strconv.ParseFloat(parts[1], 64); err == nil {
+ vectorSimilarityWeight = w
+ }
}
}
- case *types.MatchTextExpr:
- if e.MatchingText != "" {
- hasTextMatch = true
- matchText = e
- }
- case *types.MatchDenseExpr:
- if len(e.EmbeddingData) > 0 {
- hasVectorMatch = true
- matchDense = e
- }
- }
- }
- }
-
- // Extract FusionExpr if present (used for hybrid search fusion)
- var fusionExpr *types.FusionExpr
- if len(req.MatchExprs) > 2 {
- if fe, ok := req.MatchExprs[2].(*types.FusionExpr); ok {
- fusionExpr = fe
- }
- }
- _ = fusionExpr // TODO: implement fusion for ES hybrid search
-
- if hasTextMatch || hasVectorMatch {
- if !isSkillIndex {
- if !slices.Contains(outputColumns, common.PAGERANK_FLD) {
- outputColumns = append(outputColumns, common.PAGERANK_FLD)
- }
- if !slices.Contains(outputColumns, common.TAG_FLD) {
- outputColumns = append(outputColumns, common.TAG_FLD)
}
+ case *types.MatchTextExpr:
+ matchText = m
+ case *types.MatchDenseExpr:
+ matchDense = m
}
}
- if hasVectorMatch && matchDense != nil && matchDense.VectorColumnName != "" {
- outputColumns = append(outputColumns, matchDense.VectorColumnName)
- }
-
- // Build filter string
- var filterParts []string
+ // Build query body with text match and/or knn match
+ queryBody := make(map[string]interface{})
- if !isMetadataTable && (hasTextMatch || hasVectorMatch) {
- if req.Filter != nil {
- if availInt, ok := req.Filter["available_int"]; ok {
- filterParts = append(filterParts, fmt.Sprintf("available_int=%v", availInt))
- } else if status, ok := req.Filter["status"]; ok {
- filterParts = append(filterParts, fmt.Sprintf("status='%s'", status))
- } else {
- if isSkillIndex {
- filterParts = append(filterParts, "status='1'")
+ if matchText != nil {
+ textQuery := buildQueryStringQuery(matchText, vectorSimilarityWeight, isSkillIndex)
+ if boolQuery != nil {
+ if boolMap, ok := boolQuery["bool"].(map[string]interface{}); ok {
+ if must, ok := boolMap["must"].([]interface{}); ok {
+ must = append(must, textQuery)
+ boolMap["must"] = must
} else {
- filterParts = append(filterParts, "available_int=1")
+ boolMap["must"] = []interface{}{textQuery}
}
+ boolMap["boost"] = 1.0 - vectorSimilarityWeight
}
} else {
- if isSkillIndex {
- filterParts = append(filterParts, "status='1'")
- } else {
- filterParts = append(filterParts, "available_int=1")
- }
+ boolQuery = textQuery
}
}
- // Build filter string from req.Filter
- if req.Filter != nil {
- filterCopy := req.Filter
- if !isMetadataTable {
- filterCopy = make(map[string]interface{})
- for k, v := range req.Filter {
- if k != "kb_id" {
- filterCopy[k] = v
- }
- }
+ hasVectorMatch := matchDense != nil && len(matchDense.EmbeddingData) > 0
+ if hasVectorMatch {
+ k := matchDense.TopN
+ if k <= 0 {
+ k = limit
}
-
- condStr := equivalentConditionToStr(filterCopy)
- if condStr != "" {
- filterParts = append(filterParts, condStr)
+ if k <= 0 {
+ k = 1024
}
- }
- filterStr := strings.Join(filterParts, " AND ")
+ numCandidates := k * 2
- orderBy := req.OrderBy
- _ = orderBy // TODO: implement rank feature for ES
+ similarity := 0.0
+ if matchDense.ExtraOptions != nil {
+ if sim, ok := matchDense.ExtraOptions["similarity"].(float64); ok {
+ similarity = sim
+ }
+ }
- var allResults []map[string]interface{}
- totalHits := int64(0)
+ vectorFieldName := matchDense.VectorColumnName
- for _, indexName := range req.IndexNames {
- var indexNames []string
- if strings.HasPrefix(indexName, "ragflow_doc_meta_") {
- indexNames = []string{indexName}
- } else {
- indexNames = []string{indexName}
+ knnQuery := map[string]interface{}{
+ "field": vectorFieldName,
+ "query_vector": matchDense.EmbeddingData,
+ "k": k,
+ "num_candidates": numCandidates,
+ "similarity": similarity,
+ "filter": boolQuery,
}
- for _, fullIndexName := range indexNames {
- // Build search query body
- queryBody := make(map[string]interface{})
-
- // Determine text fields for the query (used indirectly via buildESKeywordQuery)
- if matchText != nil && len(matchText.Fields) > 0 {
- // Use matchText.Fields for text matching
- } else if isSkillIndex {
- // Use skill-specific fields in buildSkillKeywordQuery
- } else {
- // Use default fields in buildESKeywordQuery
- }
+ queryBody["knn"] = knnQuery
+ if boolQuery != nil {
+ queryBody["query"] = boolQuery
+ }
+ } else if boolQuery != nil {
+ queryBody["query"] = boolQuery
+ } else {
+ queryBody["query"] = map[string]interface{}{
+ "match_all": map[string]interface{}{},
+ }
+ }
- var vectorFieldName string
- if !hasVectorMatch || matchDense == nil {
- // Keyword-only search (no vector match)
- queryBody["query"] = map[string]interface{}{
- "match_all": map[string]interface{}{},
- }
- if hasTextMatch && matchText != nil {
- if isSkillIndex {
- queryBody["query"] = buildSkillKeywordQuery(matchText.MatchingText, nil, 1.0)
+ // Add rank_feature queries
+ if req.RankFeature != nil && len(req.RankFeature) > 0 && !isSkillIndex {
+ rankFeatureQuery := buildRankFeatureQuery(req.RankFeature)
+ if rankFeatureQuery != nil {
+ if boolQuery, ok := queryBody["query"].(map[string]interface{}); ok {
+ if boolMap, ok := boolQuery["bool"].(map[string]interface{}); ok {
+ if should, ok := boolMap["should"].([]interface{}); ok {
+ for _, q := range rankFeatureQuery {
+ boolMap["should"] = append(should, q)
+ }
} else {
- queryBody["query"] = buildESKeywordQuery(matchText.MatchingText, nil, 1.0)
- }
- // Add filter if present
- if filterStr != "" {
- if boolQuery, ok := queryBody["query"].(map[string]interface{}); ok {
- if boolMap, ok := boolQuery["bool"].(map[string]interface{}); ok {
- filterClauses := buildFilterClausesFromStr(filterStr)
- if existingFilter, ok := boolMap["filter"].([]map[string]interface{}); ok {
- boolMap["filter"] = append(existingFilter, filterClauses...)
- } else {
- boolMap["filter"] = filterClauses
- }
- }
+ interfaceSlice := make([]interface{}, len(rankFeatureQuery))
+ for i, q := range rankFeatureQuery {
+ interfaceSlice[i] = q
}
+ boolMap["should"] = interfaceSlice
}
}
- } else {
- // Hybrid search: keyword + vector
- textWeight := 0.7 // default: vector weight = 0.3
- vectorWeight := 0.3
- if matchDense.ExtraOptions != nil {
- if vw, ok := matchDense.ExtraOptions["text_weight"].(float64); ok {
- textWeight = vw
- }
- if vw, ok := matchDense.ExtraOptions["vector_weight"].(float64); ok {
- vectorWeight = vw
- }
- }
-
- // Build boolean query for text match and filters
- var boolQuery map[string]interface{}
- matchingText := ""
- if matchText != nil {
- matchingText = matchText.MatchingText
- }
- if isSkillIndex {
- boolQuery = buildSkillKeywordQuery(matchingText, nil, textWeight)
- } else {
- boolQuery = buildESKeywordQuery(matchingText, nil, textWeight)
- }
+ }
+ }
+ }
- // Add filter to bool query
- if filterStr != "" {
- if boolMap, ok := boolQuery["bool"].(map[string]interface{}); ok {
- filterClauses := buildFilterClausesFromStr(filterStr)
- if existingFilter, ok := boolMap["filter"].([]map[string]interface{}); ok {
- boolMap["filter"] = append(existingFilter, filterClauses...)
- } else {
- boolMap["filter"] = filterClauses
- }
- }
- }
+ // Add sorting if order_by specified
+ if req.OrderBy != nil && len(req.OrderBy.Fields) > 0 {
+ sort := parseOrderByExpr(req.OrderBy)
+ if len(sort) > 0 {
+ queryBody["sort"] = sort
+ }
+ }
- // Build kNN query
- vectorData := matchDense.EmbeddingData
- vectorFieldName = matchDense.VectorColumnName
- k := matchDense.TopN
- if k <= 0 {
- k = req.Limit
- }
- if k <= 0 {
- k = 1024
- }
- numCandidates := k * 2
+ // Determine use_search_after for deep pagination
+ //
+ // ES rejects from + size combinations where from + size > index.max_result_window
+ // (default 10,000) — see https://www.elastic.co/guide/en/elasticsearch/reference/current/paginate-search-results.html.
+ // For those requests we must drop `from` and walk the result set with
+ // `search_after` instead. The preconditions mirror the Python reference
+ // (rag/utils/es_conn.py):
+ // - explicit OrderBy is required (search_after needs a stable cursor,
+ // and _score / KNN-similarity sorts are not unique enough to be safe)
+ // - no dense/KNN match (knn queries do not honour `search_after` in the
+ // same way and the Python path explicitly disallows them here)
+ hasDense := hasVectorMatch
+ hasExplicitSort := req.OrderBy != nil && len(req.OrderBy.Fields) > 0
+ useSearchAfter := limit > 0 && (offset+limit > common.MAX_RESULT_WINDOW) && hasExplicitSort && !hasDense
- similarity := 0.0
- if matchDense.ExtraOptions != nil {
- if sim, ok := matchDense.ExtraOptions["similarity"].(float64); ok {
- similarity = sim
- }
- }
+ // Apply offset/limit pagination. When useSearchAfter is true, the
+ // caller is going to drive pagination via searchAfterCursor()
+ // instead, so we must NOT emit from/size here — leaving them out
+ // is the whole point of routing to the search_after path.
+ if !useSearchAfter && limit > 0 {
+ queryBody["size"] = limit
+ queryBody["from"] = offset
+ }
- knnQuery := map[string]interface{}{
- "field": vectorFieldName,
- "query_vector": vectorData,
- "k": k,
- "num_candidates": numCandidates,
- "similarity": similarity,
- "boost": vectorWeight,
+ // Set _source and fields for vector fields
+ hasTextMatch := matchText != nil
+ if len(req.SelectFields) > 0 {
+ // Use caller-specified fields, add pagerank_fld/tag_fld if needed
+ queryBody["_source"] = req.SelectFields
+ if hasTextMatch || hasVectorMatch {
+ if !isSkillIndex {
+ if !slices.Contains(req.SelectFields, common.PAGERANK_FLD) {
+ queryBody["_source"] = append(queryBody["_source"].([]string), common.PAGERANK_FLD)
}
-
- queryBody["knn"] = knnQuery
- queryBody["query"] = boolQuery
-
- // Add vector column to output columns
- if vectorFieldName != "" {
- outputColumns = append(outputColumns, vectorFieldName)
+ if !slices.Contains(req.SelectFields, common.TAG_FLD) {
+ queryBody["_source"] = append(queryBody["_source"].([]string), common.TAG_FLD)
}
}
-
- queryBody["size"] = pageSize
- queryBody["from"] = offset
-
- // Add sorting if specified
- if orderBy != nil && len(orderBy.Fields) > 0 {
- sort := parseOrderByExpr(orderBy)
- if len(sort) > 0 {
- queryBody["sort"] = sort
- }
+ }
+ var vectorFields []string
+ for _, f := range req.SelectFields {
+ if strings.HasSuffix(f, "_vec") {
+ vectorFields = append(vectorFields, f)
}
-
- // Serialize query
- var buf bytes.Buffer
- if err := json.NewEncoder(&buf).Encode(queryBody); err != nil {
- return nil, fmt.Errorf("error encoding query: %w", err)
+ }
+ if len(vectorFields) > 0 {
+ queryBody["fields"] = vectorFields
+ }
+ } else {
+ // No explicit SelectFields - use match_all, but add pagerank_fld/tag_fld for scoring if needed
+ if hasTextMatch || hasVectorMatch {
+ if !isSkillIndex {
+ queryBody["_source"] = []string{common.PAGERANK_FLD, common.TAG_FLD}
}
+ }
+ }
- // Log search details
- common.Debug("Elasticsearch searching index", zap.String("index", fullIndexName))
- common.Debug("Elasticsearch DSL", zap.Any("dsl", queryBody))
-
- // Build search request
- reqES := esapi.SearchRequest{
- Index: []string{fullIndexName},
- Body: &buf,
- }
+ // Serialize query
+ var buf bytes.Buffer
+ if err := json.NewEncoder(&buf).Encode(queryBody); err != nil {
+ return nil, fmt.Errorf("error encoding query: %w", err)
+ }
+
+ // Execute search. When useSearchAfter is true we must NOT send
+ // from/size (we dropped them above) and instead walk the result set
+ // page-by-page with the search_after cursor — ES otherwise returns
+ // the first page and the caller gets the wrong page.
+ var (
+ totalHits int64
+ allResults []map[string]interface{}
+ err error
+ )
- // Execute search
- res, err := reqES.Do(ctx, e.client)
+ if useSearchAfter {
+ allResults, totalHits, err = e.searchAfterCursor(ctx, req, queryBody, offset, limit)
+ if err != nil {
+ return nil, err
+ }
+ } else {
+ // WithBody takes an io.Reader that the Go client streams
+ // directly into the request. Reusing &buf across iterations
+ // would drain it on the first request and leave the rest
+ // with an empty body — so we copy the bytes once and hand
+ // each iteration a fresh bytes.NewReader.
+ payload := append([]byte(nil), buf.Bytes()...)
+ for _, indexName := range req.IndexNames {
+ res, err := e.client.Search(
+ e.client.Search.WithContext(ctx),
+ e.client.Search.WithIndex(indexName),
+ e.client.Search.WithBody(bytes.NewReader(payload)),
+ e.client.Search.WithTrackTotalHits(true),
+ )
if err != nil {
- common.Warn("Elasticsearch query failed", zap.String("index", fullIndexName), zap.Error(err))
+ common.Warn("Elasticsearch query failed", zap.String("index", indexName), zap.Error(err))
continue
}
+ defer res.Body.Close()
if res.IsError() {
- bodyBytes, err := io.ReadAll(res.Body)
- res.Body.Close()
- if err != nil {
- common.Error("Elasticsearch failed to read error response body", err)
- } else {
- common.Warn("Elasticsearch error response", zap.String("index", fullIndexName), zap.String("body", string(bodyBytes)))
- }
+ bodyBytes, _ := io.ReadAll(res.Body)
+ common.Warn("Elasticsearch error response", zap.String("index", indexName), zap.String("body", string(bodyBytes)))
continue
}
- // Parse response
+ // Parse response and return results
var esResp SearchResponse
if err := json.NewDecoder(res.Body).Decode(&esResp); err != nil {
- res.Body.Close()
- common.Warn("Elasticsearch failed to parse response", zap.String("index", fullIndexName), zap.Error(err))
+ common.Warn("Elasticsearch failed to parse response", zap.String("index", indexName), zap.Error(err))
continue
}
- res.Body.Close()
-
- // Convert to unified response
- searchChunks := convertESResponse(&esResp, vectorFieldName)
+ searchChunks := convertESResponse(&esResp, "")
totalHits += esResp.Hits.Total.Value
- // Apply field name mapping and row_id handling
- if !isSkillIndex {
- GetFields(searchChunks, nil)
- }
-
allResults = append(allResults, searchChunks...)
}
}
- // Calculate scores and sort
- if hasTextMatch || hasVectorMatch {
+ // Post-processing: Sort results by score
+ if len(allResults) > 0 && (matchText != nil || hasVectorMatch) {
scoreColumn := "_score"
- if hasTextMatch && hasVectorMatch {
+ if matchText != nil && hasVectorMatch {
scoreColumn = "SCORE"
}
@@ -1084,15 +1019,10 @@ func (e *elasticsearchEngine) searchUnified(ctx context.Context, req *types.Sear
}
allResults = calculateScores(allResults, scoreColumn, pagerankField)
- allResults = sortByScore(allResults, len(allResults))
- }
-
- // Limit results
- if len(allResults) > pageSize {
- allResults = allResults[:pageSize]
+ allResults = sortByScore(allResults, limit)
}
- common.Debug("Search in Elasticsearch completed", zap.Int("returnedRows", len(allResults)), zap.Int64("totalHits", totalHits))
+ common.Info("ES Search completed", zap.Int("returnedRows", len(allResults)), zap.Int64("totalHits", totalHits))
return &types.SearchResult{
Chunks: allResults,
@@ -1100,259 +1030,854 @@ func (e *elasticsearchEngine) searchUnified(ctx context.Context, req *types.Sear
}, nil
}
-// buildFilterClausesFromStr converts a filter string to ES filter clauses
-func buildFilterClausesFromStr(filterStr string) []map[string]interface{} {
- if filterStr == "" {
- return nil
- }
- return []map[string]interface{}{
- {"query_string": map[string]interface{}{
- "query": filterStr,
- }},
+// searchAfterFetcher issues one ES search request with the given batch
+// size and search_after cursor, returning the decoded response. Defined
+// as a function type so the pagination logic below can be unit-tested
+// with a mock fetcher instead of a real Elasticsearch client.
+type searchAfterFetcher func(
+ ctx context.Context,
+ baseQuery map[string]interface{},
+ batch int,
+ cursor []interface{},
+ trackTotalHits bool,
+) (SearchResponse, error)
+
+// searchAfterCursor walks ES with the search_after pagination protocol,
+// returning the page [offset, offset+limit) of an explicitly-sorted
+// result set. Used when offset+limit exceeds common.MAX_RESULT_WINDOW
+// and ES would otherwise reject the from/size combination.
+//
+// Mirrors rag/utils/es_conn.py:ESConnection._search_with_search_after:
+//
+// 1. Drop from/size from the base query (the caller has already omitted
+// them on this path; this is a defensive no-op).
+// 2. Skip phase: discard hits until we have skipped `offset` of them.
+// 3. Take phase: collect hits until we have `limit` of them, or the
+// index is exhausted.
+// 4. After each batch, advance the cursor with the last hit's `sort`
+// field. If `sort` is missing or unchanged, the index is exhausted.
+//
+// The first request carries trackTotalHits=true so the caller still
+// gets an accurate total; subsequent requests skip it for efficiency.
+// Returns the (possibly empty) collected hits and the total hit count
+// from the first response.
+func (e *elasticsearchEngine) searchAfterCursor(
+ ctx context.Context,
+ req *types.SearchRequest,
+ baseQuery map[string]interface{},
+ offset, limit int,
+) ([]map[string]interface{}, int64, error) {
+ // Defensive: strip from/size if the caller left them in. In the
+ // current code path they are never set when useSearchAfter is true,
+ // but the base query is a shared map and future callers may forget.
+ delete(baseQuery, "from")
+ delete(baseQuery, "size")
+
+ return searchAfterPaginate(ctx, baseQuery, offset, limit, e.buildSearchAfterFetcher(req))
+}
+
+// buildSearchAfterFetcher returns a fetcher that delegates each
+// iteration to executeSearchRequest, which talks to the real ES client.
+func (e *elasticsearchEngine) buildSearchAfterFetcher(req *types.SearchRequest) searchAfterFetcher {
+ return func(
+ ctx context.Context,
+ baseQuery map[string]interface{},
+ batch int,
+ cursor []interface{},
+ trackTotalHits bool,
+ ) (SearchResponse, error) {
+ return e.executeSearchRequest(ctx, req, baseQuery, batch, cursor, trackTotalHits)
}
}
-// GetChunk gets a chunk by ID using ES search API
-// _id in ES is composite: {doc_id}_{kb_id}_{chunk_id}
-func (e *elasticsearchEngine) GetChunk(ctx context.Context, baseName, chunkID string, datasetIDs []string) (interface{}, error) {
- // Try search by doc_id field (which is stored in the document)
- for _, datasetID := range datasetIDs {
- searchReq := map[string]interface{}{
- "query": map[string]interface{}{
- "bool": map[string]interface{}{
- "must": []map[string]interface{}{
- {"term": map[string]interface{}{"id": chunkID}},
- {"term": map[string]interface{}{"kb_id": datasetID}},
- },
- },
- },
- }
+// searchAfterPaginate is the pure, callback-driven pagination loop
+// shared by the engine and the unit tests. See searchAfterCursor for
+// the semantics.
+func searchAfterPaginate(
+ ctx context.Context,
+ baseQuery map[string]interface{},
+ offset, limit int,
+ fetch searchAfterFetcher,
+) ([]map[string]interface{}, int64, error) {
+ var (
+ cursor []interface{}
+ totalHits int64
+ collected []map[string]interface{}
+ collectedTake int
+ firstCall = true
+ )
- body, err := json.Marshal(searchReq)
- if err != nil {
- return nil, fmt.Errorf("failed to marshal search request: %w", err)
+ // Skip phase: walk past `offset` hits without retaining them.
+ remainingSkip := offset
+ for remainingSkip > 0 {
+ batch := remainingSkip
+ if batch > common.SearchAfterBatchSize {
+ batch = common.SearchAfterBatchSize
}
- res, err := e.client.Search(
- e.client.Search.WithContext(ctx),
- e.client.Search.WithIndex(baseName),
- e.client.Search.WithBody(bytes.NewReader(body)),
- )
+ resp, err := fetch(ctx, baseQuery, batch, cursor, firstCall)
+ firstCall = false
if err != nil {
- return nil, fmt.Errorf("failed to search for chunk: %w", err)
+ return nil, 0, err
}
-
- if res.IsError() {
- res.Body.Close()
- return nil, fmt.Errorf("failed to search for chunk: %s", res.Status())
+ if totalHits == 0 {
+ totalHits = resp.Hits.Total.Value
+ }
+ if len(resp.Hits.Hits) == 0 {
+ break
+ }
+ nextCursor := resp.Hits.Hits[len(resp.Hits.Hits)-1].Sort
+ if len(nextCursor) == 0 || sortValuesEqual(nextCursor, cursor) {
+ // ES returned hits but no usable cursor (e.g. sort field
+ // missing or unchanged). The index is exhausted from our
+ // point of view.
+ break
+ }
+ cursor = nextCursor
+ remainingSkip -= len(resp.Hits.Hits)
+ if len(resp.Hits.Hits) < batch {
+ // Short batch — we asked for more than was available, so
+ // the cursor is at the end of the index.
+ break
}
+ }
- var searchResult map[string]interface{}
- if err := json.NewDecoder(res.Body).Decode(&searchResult); err != nil {
- res.Body.Close()
- return nil, fmt.Errorf("failed to parse search response: %w", err)
+ // Take phase: collect up to `limit` hits. ES may return up to
+ // `batch` hits per request, but we stop at `limit` (the absolute
+ // target) regardless of how many we asked for in this iteration.
+ for collectedTake < limit {
+ want := limit - collectedTake
+ batch := want
+ if batch > common.SearchAfterBatchSize {
+ batch = common.SearchAfterBatchSize
}
- res.Body.Close()
- hits, ok := searchResult["hits"].(map[string]interface{})
- if !ok {
- continue
+ resp, err := fetch(ctx, baseQuery, batch, cursor, firstCall)
+ firstCall = false
+ if err != nil {
+ return nil, 0, err
+ }
+ if totalHits == 0 {
+ totalHits = resp.Hits.Total.Value
+ }
+ if len(resp.Hits.Hits) == 0 {
+ break
}
- hitList, ok := hits["hits"].([]interface{})
- if !ok || len(hitList) == 0 {
- continue
+ // Convert and append. We could parallelize the conversion with
+ // the next request, but conversion is cheap relative to the
+ // ES round-trip, so keep the loop straightforward.
+ for _, hit := range resp.Hits.Hits {
+ if collectedTake >= limit {
+ break
+ }
+ chunk := hit.Source
+ if chunk == nil {
+ chunk = map[string]interface{}{}
+ }
+ chunk["_score"] = hit.Score
+ chunk["_id"] = hit.ID
+ chunk["_index"] = hit.Index
+ collected = append(collected, chunk)
+ collectedTake++
}
- firstHit, ok := hitList[0].(map[string]interface{})
- if !ok {
- continue
+ // Reached the absolute limit — stop without advancing the
+ // cursor (we already have what was asked for).
+ if collectedTake >= limit {
+ break
}
- source, ok := firstHit["_source"].(map[string]interface{})
- if !ok {
- continue
+ nextCursor := resp.Hits.Hits[len(resp.Hits.Hits)-1].Sort
+ if len(nextCursor) == 0 || sortValuesEqual(nextCursor, cursor) {
+ break
+ }
+ cursor = nextCursor
+ if len(resp.Hits.Hits) < batch {
+ break
}
+ }
- common.Info("GetChunk found hit", zap.String("baseName", baseName), zap.String("chunkID", chunkID))
- source["id"] = chunkID
- return source, nil
+ // If we never sent a request (e.g. offset == 0 and limit == 0) we
+ // still need a total. Issue one count-only request.
+ if totalHits == 0 {
+ resp, err := fetch(ctx, baseQuery, 0, nil, true)
+ if err != nil {
+ return nil, 0, err
+ }
+ totalHits = resp.Hits.Total.Value
}
- common.Info("GetChunk no hits found", zap.String("baseName", baseName), zap.String("chunkID", chunkID))
- return nil, nil
+ return collected, totalHits, nil
}
-// GetFields is not implemented for Elasticsearch
-func (e *elasticsearchEngine) GetFields(chunks []map[string]interface{}, fields []string) map[string]map[string]interface{} {
- common.Warn("GetFields not implemented for Elasticsearch")
- return nil
-}
+// executeSearchRequest sends one ES search request with the given
+// batch size and search_after cursor. If trackTotalHits is true the
+// request asks ES to compute an exact total (cheap to omit on
+// pagination iterations after the first).
+func (e *elasticsearchEngine) executeSearchRequest(
+ ctx context.Context,
+ req *types.SearchRequest,
+ baseQuery map[string]interface{},
+ batch int,
+ cursor []interface{},
+ trackTotalHits bool,
+) (SearchResponse, error) {
+ queryBody := make(map[string]interface{}, len(baseQuery)+2)
+ for k, v := range baseQuery {
+ queryBody[k] = v
+ }
+ if batch > 0 {
+ queryBody["size"] = batch
+ }
+ if len(cursor) > 0 {
+ queryBody["search_after"] = cursor
+ }
-// GetAggregation is not implemented for Elasticsearch
-func (e *elasticsearchEngine) GetAggregation(chunks []map[string]interface{}, fieldName string) []map[string]interface{} {
- common.Warn("GetAggregation not implemented for Elasticsearch")
- return nil
-}
+ var buf bytes.Buffer
+ if err := json.NewEncoder(&buf).Encode(queryBody); err != nil {
+ return SearchResponse{}, fmt.Errorf("error encoding query: %w", err)
+ }
-// GetHighlight is not implemented for Elasticsearch
-func (e *elasticsearchEngine) GetHighlight(chunks []map[string]interface{}, keywords []string, fieldName string) map[string]string {
- common.Warn("GetHighlight not implemented for Elasticsearch")
- return nil
-}
+ res, err := e.client.Search(
+ e.client.Search.WithContext(ctx),
+ e.client.Search.WithIndex(req.IndexNames...),
+ e.client.Search.WithBody(&buf),
+ e.client.Search.WithTrackTotalHits(trackTotalHits),
+ )
+ if err != nil {
+ return SearchResponse{}, fmt.Errorf("elasticsearch search failed: %w", err)
+ }
+ defer res.Body.Close()
-// DropChunkStore deletes a chunk index
-func (e *elasticsearchEngine) DropChunkStore(ctx context.Context, baseName, datasetID string) error {
- return e.dropIndex(ctx, baseName)
+ if res.IsError() {
+ bodyBytes, _ := io.ReadAll(res.Body)
+ return SearchResponse{}, fmt.Errorf("elasticsearch error response: %s", string(bodyBytes))
+ }
+
+ var esResp SearchResponse
+ if err := json.NewDecoder(res.Body).Decode(&esResp); err != nil {
+ return SearchResponse{}, fmt.Errorf("elasticsearch failed to parse response: %w", err)
+ }
+ return esResp, nil
}
-// ChunkStoreExists checks if a chunk index exists
-func (e *elasticsearchEngine) ChunkStoreExists(ctx context.Context, baseName, datasetID string) (bool, error) {
- return e.indexExists(ctx, baseName)
+// sortValuesEqual reports whether two sort cursors are identical.
+// ES guarantees that successive requests with `search_after: <cursor>`
+// advance strictly past the cursor, so an unchanged cursor between
+// iterations means the index is exhausted.
+func sortValuesEqual(a, b []interface{}) bool {
+ if len(a) != len(b) {
+ return false
+ }
+ for i := range a {
+ if a[i] != b[i] {
+ return false
+ }
+ }
+ return true
}
-// buildQueryFromCondition builds an ES query from condition map
-func (e *elasticsearchEngine) buildQueryFromCondition(condition map[string]interface{}) map[string]interface{} {
- if len(condition) == 0 {
- return nil
+// buildBoolQueryFromCondition builds an ES bool query from condition map
+// For skill index, uses 'status' field instead of 'available_int'
+func buildBoolQueryFromCondition(filter map[string]interface{}, kbIDs []string, isSkillIndex bool) map[string]interface{} {
+ var mustClauses []interface{}
+ var filterClauses []interface{}
+ var shouldClauses []interface{}
+
+ // Add kb_id to condition
+ if kbIDs != nil && len(kbIDs) > 0 {
+ filterClauses = append(filterClauses, map[string]interface{}{
+ "terms": map[string]interface{}{"kb_id": kbIDs},
+ })
}
- var clauses []map[string]interface{}
+ // For skill index, add status = "1" filter by default (active skills)
+ if isSkillIndex {
+ filterClauses = append(filterClauses, map[string]interface{}{
+ "term": map[string]interface{}{
+ "status": "1",
+ },
+ })
+ }
- for k, v := range condition {
- if v == nil {
- continue
- }
+ if filter == nil {
+ filter = make(map[string]interface{})
+ }
- switch k {
- case "kb_id":
- // Handle kb_id as terms query
- if listVal, ok := v.([]interface{}); ok {
- clauses = append(clauses, map[string]interface{}{
- "terms": map[string]interface{}{k: listVal},
+ for k, v := range filter {
+ // For skill index, handle 'status' field instead of 'available_int'
+ if isSkillIndex && k == "status" {
+ if v == nil || v == "" {
+ continue
+ }
+ if listVal, ok := v.([]interface{}); ok && len(listVal) > 0 {
+ filterClauses = append(filterClauses, map[string]interface{}{
+ "terms": map[string]interface{}{"status": listVal},
})
- } else {
- clauses = append(clauses, map[string]interface{}{
- "term": map[string]interface{}{k: v},
+ } else if strVal, ok := v.(string); ok && strVal != "" {
+ filterClauses = append(filterClauses, map[string]interface{}{
+ "term": map[string]interface{}{"status": strVal},
})
}
- case "id":
- // Handle id as terms or term query
- if listVal, ok := v.([]interface{}); ok {
- clauses = append(clauses, map[string]interface{}{
- "terms": map[string]interface{}{k: listVal},
+ continue
+ }
+ if k == "available_int" {
+ var numVal float64
+ switch val := v.(type) {
+ case float64:
+ numVal = val
+ case int:
+ numVal = float64(val)
+ case int64:
+ numVal = float64(val)
+ default:
+ continue
+ }
+ if numVal == 0 {
+ filterClauses = append(filterClauses, map[string]interface{}{
+ "range": map[string]interface{}{"available_int": map[string]interface{}{"lt": 1}},
})
} else {
- clauses = append(clauses, map[string]interface{}{
- "term": map[string]interface{}{k: v},
+ filterClauses = append(filterClauses, map[string]interface{}{
+ "bool": map[string]interface{}{
+ "must_not": []map[string]interface{}{
+ {"range": map[string]interface{}{"available_int": map[string]interface{}{"lt": 1}}},
+ },
+ },
})
}
- case "available_int":
- // Handle available_int as term query
- clauses = append(clauses, map[string]interface{}{
- "term": map[string]interface{}{k: v},
- })
- default:
- // Default: treat as term query
- clauses = append(clauses, map[string]interface{}{
- "term": map[string]interface{}{k: v},
+ continue
+ }
+ if k == "id" {
+ if v == nil || v == "" {
+ continue
+ }
+ if listVal, ok := v.([]interface{}); ok && len(listVal) > 0 {
+ shouldClauses = append(shouldClauses,
+ map[string]interface{}{"terms": map[string]interface{}{"id": listVal}},
+ map[string]interface{}{"terms": map[string]interface{}{"_id": listVal}},
+ )
+ } else if strVal, ok := v.(string); ok && strVal != "" {
+ shouldClauses = append(shouldClauses,
+ map[string]interface{}{"term": map[string]interface{}{"id": strVal}},
+ map[string]interface{}{"term": map[string]interface{}{"_id": strVal}},
+ )
+ } else if intVal, ok := v.(int); ok && intVal != 0 {
+ shouldClauses = append(shouldClauses,
+ map[string]interface{}{"term": map[string]interface{}{"id": intVal}},
+ map[string]interface{}{"term": map[string]interface{}{"_id": intVal}},
+ )
+ }
+ continue
+ }
+ if v == nil || v == "" {
+ continue
+ }
+ if listVal, ok := v.([]interface{}); ok {
+ filterClauses = append(filterClauses, map[string]interface{}{
+ "terms": map[string]interface{}{k: listVal},
+ })
+ } else if strListVal, ok := v.([]string); ok {
+ filterClauses = append(filterClauses, map[string]interface{}{
+ "terms": map[string]interface{}{k: strListVal},
+ })
+ } else if strVal, ok := v.(string); ok && strVal != "" {
+ filterClauses = append(filterClauses, map[string]interface{}{
+ "term": map[string]interface{}{k: strVal},
+ })
+ } else if intVal, ok := v.(int); ok {
+ filterClauses = append(filterClauses, map[string]interface{}{
+ "term": map[string]interface{}{k: intVal},
+ })
+ } else if floatVal, ok := v.(float64); ok {
+ filterClauses = append(filterClauses, map[string]interface{}{
+ "term": map[string]interface{}{k: floatVal},
})
}
}
- if len(clauses) == 0 {
+ // Build the bool query
+ boolQuery := make(map[string]interface{})
+ if len(mustClauses) > 0 {
+ boolQuery["must"] = mustClauses
+ }
+ if len(filterClauses) > 0 {
+ boolQuery["filter"] = filterClauses
+ }
+ if len(shouldClauses) > 0 {
+ boolQuery["should"] = shouldClauses
+ boolQuery["minimum_should_match"] = 1
+ }
+
+ if len(boolQuery) == 0 {
return nil
}
- if len(clauses) == 1 {
- return clauses[0]
+ return map[string]interface{}{"bool": boolQuery}
+}
+
+// buildQueryStringQuery builds a query_string query from MatchTextExpr
+// When isSkillIndex is true, uses skill-specific fields (name_tks, tags_tks, etc.)
+// Otherwise uses document fields (title_tks, content_ltks, etc.)
+func buildQueryStringQuery(matchText *types.MatchTextExpr, vectorSimilarityWeight float64, isSkillIndex bool) map[string]interface{} {
+ if matchText == nil {
+ return nil
+ }
+
+ minimumShouldMatch := "0%"
+ if matchText.ExtraOptions != nil {
+ if msm, ok := matchText.ExtraOptions["minimum_should_match"].(float64); ok {
+ minimumShouldMatch = fmt.Sprintf("%d%%", int(msm*100))
+ }
+ }
+
+ fields := matchText.Fields
+ if fields == nil || len(fields) == 0 {
+ if isSkillIndex {
+ fields = []string{"name_tks^10", "tags_tks^5", "description_tks^3", "content_tks^1"}
+ } else {
+ fields = []string{"title_tks^10", "title_sm_tks^5", "important_kwd^30", "important_tks^20", "question_tks^20", "content_ltks^2", "content_sm_ltks"}
+ }
+ }
+
+ boost := 1.0
+ if matchText.ExtraOptions != nil {
+ if b, ok := matchText.ExtraOptions["boost"].(float64); ok {
+ boost = b
+ }
}
return map[string]interface{}{
- "bool": map[string]interface{}{
- "must": clauses,
+ "query_string": map[string]interface{}{
+ "fields": fields,
+ "type": "best_fields",
+ "query": matchText.MatchingText,
+ "minimum_should_match": minimumShouldMatch,
+ "boost": boost,
},
}
}
-// buildRemoveOperations builds ES script operations for remove
-func (e *elasticsearchEngine) buildRemoveOperations(removeData map[string]interface{}, query map[string]interface{}, indexName string) []map[string]interface{} {
- // For ES, we handle removals differently - they are typically done via separate update operations
- // This is a simplified implementation
- return nil
-}
+// buildRankFeatureQuery builds rank_feature queries for learning to rank
+func buildRankFeatureQuery(rankFeature map[string]float64) []map[string]interface{} {
+ if rankFeature == nil || len(rankFeature) == 0 {
+ return nil
+ }
+
+ // Sort keys for deterministic query order (Go map iteration is randomized)
+ keys := make([]string, 0, len(rankFeature))
+ for k := range rankFeature {
+ keys = append(keys, k)
+ }
+ sort.Strings(keys)
-// needsScriptUpdate checks if the update requires a script (more complex operations)
-func (e *elasticsearchEngine) needsScriptUpdate(newValue map[string]interface{}) bool {
- // Check if any values contain operations that need scripts
- return false
+ var queries []map[string]interface{}
+ for _, fld := range keys {
+ if fld == common.PAGERANK_FLD {
+ continue
+ }
+ sc := rankFeature[fld]
+ tagField := fmt.Sprintf("%s.%s", common.TAG_FLD, fld)
+ queries = append(queries, map[string]interface{}{
+ "rank_feature": map[string]interface{}{
+ "field": tagField,
+ "linear": map[string]interface{}{},
+ "boost": sc,
+ },
+ })
+ }
+ return queries
}
-// buildUpdateScript builds an ES script for updates
-func (e *elasticsearchEngine) buildUpdateScript(newValue map[string]interface{}, removeOperations []map[string]interface{}) map[string]interface{} {
- script := map[string]interface{}{
- "source": "ctx._source.putAll(params.doc)",
- "params": map[string]interface{}{
- "doc": newValue,
- },
+// GetChunk gets a chunk by ID using ES search API
+func (e *elasticsearchEngine) GetChunk(ctx context.Context, baseName, chunkID string, datasetIDs []string) (interface{}, error) {
+ // Try search by doc_id field (which is stored in the document)
+ for _, datasetID := range datasetIDs {
+ searchReq := map[string]interface{}{
+ "query": map[string]interface{}{
+ "bool": map[string]interface{}{
+ "must": []map[string]interface{}{
+ {"term": map[string]interface{}{"id": chunkID}},
+ {"term": map[string]interface{}{"kb_id": datasetID}},
+ },
+ },
+ },
+ }
+
+ body, err := json.Marshal(searchReq)
+ if err != nil {
+ return nil, fmt.Errorf("failed to marshal search request: %w", err)
+ }
+
+ res, err := e.client.Search(
+ e.client.Search.WithContext(ctx),
+ e.client.Search.WithIndex(baseName),
+ e.client.Search.WithBody(bytes.NewReader(body)),
+ )
+ if err != nil {
+ return nil, fmt.Errorf("failed to search for chunk: %w", err)
+ }
+
+ if res.IsError() {
+ res.Body.Close()
+ return nil, fmt.Errorf("failed to search for chunk: %s", res.Status())
+ }
+
+ var searchResult map[string]interface{}
+ if err := json.NewDecoder(res.Body).Decode(&searchResult); err != nil {
+ res.Body.Close()
+ return nil, fmt.Errorf("failed to parse search response: %w", err)
+ }
+ res.Body.Close()
+
+ hits, ok := searchResult["hits"].(map[string]interface{})
+ if !ok {
+ continue
+ }
+
+ hitList, ok := hits["hits"].([]interface{})
+ if !ok || len(hitList) == 0 {
+ continue
+ }
+
+ firstHit, ok := hitList[0].(map[string]interface{})
+ if !ok {
+ continue
+ }
+
+ source, ok := firstHit["_source"].(map[string]interface{})
+ if !ok {
+ continue
+ }
+
+ common.Info("GetChunk found hit", zap.String("baseName", baseName), zap.String("chunkID", chunkID))
+ source["id"] = chunkID
+ return source, nil
}
- return script
+
+ common.Info("GetChunk no hits found", zap.String("baseName", baseName), zap.String("chunkID", chunkID))
+ return nil, nil
}
-// buildMetadataQueryFromCondition builds an ES query for metadata index
-func (e *elasticsearchEngine) buildMetadataQueryFromCondition(condition map[string]interface{}) map[string]interface{} {
- if len(condition) == 0 {
- return nil
+// GetFields extracts the requested fields from ES search response chunks
+//
+// Unlike Infinity, Elasticsearch does NOT use convertSelectFields before querying.
+// The original requested field names ARE the database column names:
+// - "content_with_weight" is stored and returned as "content_with_weight"
+// - No field name mapping is needed in GetFields
+func (e *elasticsearchEngine) GetFields(chunks []map[string]interface{}, fields []string) map[string]map[string]interface{} {
+ common.Info("GetFields called", zap.Int("chunkCount", len(chunks)), zap.Strings("fields", fields))
+ result := make(map[string]map[string]interface{})
+
+ if len(fields) == 0 || len(chunks) == 0 {
+ return result
}
- var clauses []map[string]interface{}
+ // Build field set for lookup
+ fieldSet := make(map[string]bool)
+ for _, f := range fields {
+ fieldSet[f] = true
+ }
- for k, v := range condition {
- if v == nil {
+ for _, chunk := range chunks {
+ docID, ok := chunk["_id"].(string)
+ if !ok {
continue
}
- switch k {
- case "kb_id":
- if listVal, ok := v.([]interface{}); ok {
- clauses = append(clauses, map[string]interface{}{
- "terms": map[string]interface{}{k: listVal},
- })
- } else {
- clauses = append(clauses, map[string]interface{}{
- "term": map[string]interface{}{k: v},
- })
+ m := make(map[string]interface{})
+ for field := range fieldSet {
+ val := chunk[field]
+
+ if val == nil {
+ continue
}
- case "id":
- if listVal, ok := v.([]interface{}); ok {
- clauses = append(clauses, map[string]interface{}{
- "terms": map[string]interface{}{k: listVal},
- })
- } else {
- clauses = append(clauses, map[string]interface{}{
- "term": map[string]interface{}{k: v},
- })
+
+ if listVal, ok := val.([]interface{}); ok {
+ if len(listVal) == 1 {
+ if _, isArray := listVal[0].([]interface{}); !isArray {
+ val = listVal[0]
+ }
+ }
}
- default:
- clauses = append(clauses, map[string]interface{}{
- "term": map[string]interface{}{k: v},
- })
+
+ if _, ok := val.([]interface{}); ok {
+ m[field] = val
+ continue
+ }
+
+ if field == "available_int" {
+ if _, ok := val.(int); ok {
+ m[field] = val
+ continue
+ }
+ if _, ok := val.(float64); ok {
+ m[field] = val
+ continue
+ }
+ }
+
+ if _, ok := val.(string); !ok {
+ val = fmt.Sprintf("%v", val)
+ }
+ m[field] = val
+ }
+
+ if len(m) > 0 {
+ result[docID] = m
}
}
- if len(clauses) == 0 {
- return nil
+ common.Info("GetFields result", zap.Int("resultCount", len(result)), zap.Strings("keys", func() []string {
+ keys := make([]string, 0, len(result))
+ for k := range result {
+ keys = append(keys, k)
+ }
+ return keys
+ }()))
+ return result
+}
+
+// GetAggregation aggregates chunk values by field name
+// Input: [{"docnm_kwd": "docA"}, {"docnm_kwd": "docA"}, {"docnm_kwd": "docB"}]
+// Returns: [{"key": "docA", "count": 2}, {"key": "docB", "count": 1}]
+func (e *elasticsearchEngine) GetAggregation(chunks []map[string]interface{}, fieldName string) []map[string]interface{} {
+ if len(chunks) == 0 || fieldName == "" {
+ return []map[string]interface{}{}
}
- if len(clauses) == 1 {
- return clauses[0]
+ counts := make(map[string]int)
+ for _, chunk := range chunks {
+ if val, ok := chunk[fieldName]; ok && val != nil {
+ key := fmt.Sprintf("%v", val)
+ if key != "" {
+ counts[key]++
+ }
+ }
}
- return map[string]interface{}{
- "bool": map[string]interface{}{
- "must": clauses,
+ result := make([]map[string]interface{}, 0, len(counts))
+ for key, count := range counts {
+ result = append(result, map[string]interface{}{
+ "key": key,
+ "count": count,
+ })
+ }
+ return result
+}
+
+// GetChunkIDs extracts chunk IDs from ES search response chunks.
+// Uses _id field (composite: {doc_id}_{kb_id}_{chunk_id}).
+func (e *elasticsearchEngine) GetChunkIDs(chunks []map[string]interface{}) []string {
+ ids := make([]string, 0, len(chunks))
+ for _, chunk := range chunks {
+ if id, ok := chunk["_id"].(string); ok {
+ ids = append(ids, id)
+ }
+ }
+ return ids
+}
+
+// GetHighlight returns highlighted text for matching keywords
+func (e *elasticsearchEngine) GetHighlight(chunks []map[string]interface{}, keywords []string, fieldName string) map[string]string {
+ result := make(map[string]string)
+ if len(chunks) == 0 || len(keywords) == 0 {
+ return result
+ }
+
+ for _, chunk := range chunks {
+ docID, ok := chunk["_id"].(string)
+ if !ok {
+ continue
+ }
+
+ highlight, ok := chunk["highlight"].(map[string]interface{})
+ if !ok || len(highlight) == 0 {
+ continue
+ }
+
+ // Get first highlight entry
+ var highlightText string
+ for _, vals := range highlight {
+ if arr, ok := vals.([]interface{}); ok && len(arr) > 0 {
+ if str, ok := arr[0].(string); ok {
+ highlightText = str
+ }
+ break
+ }
+ }
+
+ if highlightText != "" {
+ result[docID] = highlightText
+ }
+ }
+ return result
+}
+
+// DropChunkStore deletes a chunk index
+func (e *elasticsearchEngine) DropChunkStore(ctx context.Context, baseName, datasetID string) error {
+ return e.dropIndex(ctx, baseName)
+}
+
+// ChunkStoreExists checks if a chunk index exists
+func (e *elasticsearchEngine) ChunkStoreExists(ctx context.Context, baseName, datasetID string) (bool, error) {
+ return e.indexExists(ctx, baseName)
+}
+
+// KNNScores performs a second-pass KNN search to get clean cosine similarities for ES.
+// This keeps chunk vectors in the index and asks ES to compute the cosine similarity.
+func (e *elasticsearchEngine) KNNScores(ctx context.Context, chunks []map[string]interface{}, queryVector []float64, topK int) (map[string]interface{}, error) {
+ if len(chunks) == 0 || len(queryVector) == 0 {
+ return nil, nil
+ }
+
+ // Extract chunk IDs from first search results
+ chunkIDs := make([]string, 0, len(chunks))
+ for _, chunk := range chunks {
+ if id, ok := chunk["_id"].(string); ok {
+ chunkIDs = append(chunkIDs, id)
+ }
+ }
+ if len(chunkIDs) == 0 {
+ return nil, nil
+ }
+
+ common.Info("KNNScores starting", zap.Int("chunkCount", len(chunkIDs)), zap.Strings("chunkIDs", chunkIDs), zap.Int("vectorSize", len(queryVector)))
+
+ // Build KNN-only query filtered by chunk IDs
+ vectorSize := len(queryVector)
+ k := len(chunkIDs)
+ knnQuery := map[string]interface{}{
+ "field": fmt.Sprintf("q_%d_vec", vectorSize),
+ "query_vector": queryVector,
+ "k": k,
+ "num_candidates": k * 2,
+ "similarity": 0.0, // No threshold - get all
+ "filter": map[string]interface{}{
+ "terms": map[string]interface{}{"id": chunkIDs},
},
}
+
+ queryBody := map[string]interface{}{
+ "knn": knnQuery,
+ "size": k,
+ "_source": false, // Don't need source fields, only need _id and _score
+ }
+
+ body, err := json.Marshal(queryBody)
+ if err != nil {
+ return nil, fmt.Errorf("failed to marshal KNN query: %w", err)
+ }
+
+ //common.Info("KNNScores query body", zap.String("body", string(body)))
+
+ // Execute search - use first index name from chunks if available
+ indexName := ""
+ if len(chunks) > 0 {
+ if idx, ok := chunks[0]["_index"].(string); ok {
+ indexName = idx
+ }
+ }
+
+ res, err := e.client.Search(
+ e.client.Search.WithContext(ctx),
+ e.client.Search.WithIndex(indexName),
+ e.client.Search.WithBody(bytes.NewReader(body)),
+ )
+ if err != nil {
+ return nil, fmt.Errorf("KNN scores search failed: %w", err)
+ }
+ defer res.Body.Close()
+
+ if res.IsError() {
+ bodyBytes, _ := io.ReadAll(res.Body)
+ return nil, fmt.Errorf("KNN scores search returned error: %s, body: %s", res.Status(), string(bodyBytes))
+ }
+
+ var esResp SearchResponse
+ if err := json.NewDecoder(res.Body).Decode(&esResp); err != nil {
+ return nil, fmt.Errorf("failed to parse KNN scores response: %w", err)
+ }
+
+ common.Info("KNNScores ES response", zap.Int("hitCount", len(esResp.Hits.Hits)), zap.Any("firstHit", func() interface{} {
+ if len(esResp.Hits.Hits) > 0 {
+ return esResp.Hits.Hits[0]
+ }
+ return nil
+ }()))
+
+ // Return raw ES response
+ // Caller will pass to GetScores to extract scores
+ knnResult := make(map[string]interface{})
+ knnResult["hits"] = map[string]interface{}{
+ "hits": esResp.Hits.Hits,
+ }
+ return knnResult, nil
+}
+
+// GetScores extracts similarity scores from KNN search result
+func (e *elasticsearchEngine) GetScores(knnResult map[string]interface{}) map[string]float64 {
+ scores := make(map[string]float64)
+ hits, ok := knnResult["hits"].(map[string]interface{})
+ if !ok {
+ return scores
+ }
+ hitsList, ok := hits["hits"]
+ if !ok {
+ return scores
+ }
+
+ switch v := hitsList.(type) {
+ case []interface{}:
+ for _, h := range v {
+ if hit, ok := h.(map[string]interface{}); ok {
+ if docID, ok := hit["_id"].(string); ok && docID != "" {
+ if scoreVal := hit["_score"]; scoreVal != nil {
+ if score, ok := scoreVal.(float64); ok {
+ scores[docID] = score
+ }
+ }
+ }
+ }
+ }
+ case []map[string]interface{}:
+ for _, hit := range v {
+ if docID, ok := hit["_id"].(string); ok && docID != "" {
+ if scoreVal := hit["_score"]; scoreVal != nil {
+ if score, ok := scoreVal.(float64); ok {
+ scores[docID] = score
+ }
+ }
+ }
+ }
+ default:
+ // Handle slice of structs via reflection
+ rv := reflect.ValueOf(v)
+ if rv.Kind() == reflect.Slice {
+ for i := 0; i < rv.Len(); i++ {
+ elem := rv.Index(i)
+ idField := elem.FieldByName("ID")
+ if !idField.IsValid() {
+ idField = elem.FieldByName("Id")
+ }
+ if !idField.IsValid() || idField.Kind() != reflect.String {
+ continue
+ }
+ docID := idField.String()
+ if docID == "" {
+ continue
+ }
+ scoreField := elem.FieldByName("Score")
+ if !scoreField.IsValid() || scoreField.Kind() != reflect.Float64 {
+ continue
+ }
+ scores[docID] = scoreField.Float()
+ }
+ }
+ }
+
+ return scores
}
// loadSkillMapping loads the skill index mapping from config file
@@ -1613,71 +2138,6 @@ func buildFilterFromMap(filter map[string]interface{}) []map[string]interface{}
return filters
}
-// buildESKeywordQuery builds keyword-only search query for ES
-// Uses query_string if matchText is in query_string format, otherwise uses multi_match
-// boost is applied to the text match clause (query_string or multi_match)
-func buildESKeywordQuery(matchText string, filterClauses []map[string]interface{}, boost float64) map[string]interface{} {
- var mustClause map[string]interface{}
-
- // Handle wildcard query (match all)
- if matchText == "*" || matchText == "" {
- mustClause = map[string]interface{}{
- "match_all": map[string]interface{}{},
- }
- } else {
- // Use query_string for complex queries
- queryString := map[string]interface{}{
- "query": matchText,
- "fields": []string{"title_tks^10", "title_sm_tks^5", "important_kwd^30", "important_tks^20", "question_tks^20", "content_ltks^2", "content_sm_ltks"},
- "type": "best_fields",
- "minimum_should_match": "30%",
- "boost": boost,
- }
- mustClause = map[string]interface{}{
- "query_string": queryString,
- }
- }
-
- return map[string]interface{}{
- "bool": map[string]interface{}{
- "must": mustClause,
- "filter": filterClauses,
- },
- }
-}
-
-// buildSkillKeywordQuery builds keyword-only search query for skill index
-// Skill index uses different field names: name_tks, tags_tks, description_tks, content_tks
-func buildSkillKeywordQuery(matchText string, filterClauses []map[string]interface{}, boost float64) map[string]interface{} {
- var mustClause map[string]interface{}
-
- // Handle wildcard query (match all)
- if matchText == "*" || matchText == "" {
- mustClause = map[string]interface{}{
- "match_all": map[string]interface{}{},
- }
- } else {
- // Use query_string for complex queries with skill-specific fields
- queryString := map[string]interface{}{
- "query": matchText,
- "fields": []string{"name_tks^10", "tags_tks^5", "description_tks^3", "content_tks^1"},
- "type": "best_fields",
- "minimum_should_match": "30%",
- "boost": boost,
- }
- mustClause = map[string]interface{}{
- "query_string": queryString,
- }
- }
-
- return map[string]interface{}{
- "bool": map[string]interface{}{
- "must": mustClause,
- "filter": filterClauses,
- },
- }
-}
-
// convertESResponse converts ES SearchResponse to unified chunks format
func convertESResponse(esResp *SearchResponse, vectorFieldName string) []map[string]interface{} {
if esResp == nil || esResp.Hits.Hits == nil {
@@ -1689,6 +2149,7 @@ func convertESResponse(esResp *SearchResponse, vectorFieldName string) []map[str
chunks[i] = hit.Source
chunks[i]["_score"] = hit.Score
chunks[i]["_id"] = hit.ID
+ chunks[i]["_index"] = hit.Index
}
return chunks
}
@@ -1706,13 +2167,40 @@ func parseOrderByExpr(orderBy *types.OrderByExpr) []map[string]interface{} {
direction = "desc"
}
- if field.Field == "_score" || field.Field == "score" {
+ // Skip id field (cannot order by text field)
+ if field.Field == "id" {
+ continue
+ }
+
+ // Special handling for page_num_int and top_int
+ if field.Field == "page_num_int" || field.Field == "top_int" {
+ result = append(result, map[string]interface{}{
+ field.Field: map[string]interface{}{
+ "order": direction,
+ "unmapped_type": "float",
+ "mode": "avg",
+ "numeric_type": "double",
+ },
+ })
+ } else if strings.HasSuffix(field.Field, "_int") || strings.HasSuffix(field.Field, "_flt") {
+ // Fields ending with _int or _flt
+ result = append(result, map[string]interface{}{
+ field.Field: map[string]interface{}{
+ "order": direction,
+ "unmapped_type": "float",
+ },
+ })
+ } else if field.Field == "_score" || field.Field == "score" {
result = append(result, map[string]interface{}{
"_score": direction,
})
} else {
+ // Default: unmapped_type = keyword
result = append(result, map[string]interface{}{
- field.Field: direction,
+ field.Field: map[string]interface{}{
+ "order": direction,
+ "unmapped_type": "keyword",
+ },
})
}
}
@@ -1720,191 +2208,6 @@ func parseOrderByExpr(orderBy *types.OrderByExpr) []map[string]interface{} {
return result
}
-// Helper query builder functions (legacy)
-
-// BuildMatchTextQuery builds a text match query
-func BuildMatchTextQuery(fields []string, text string, fuzziness string) map[string]interface{} {
- query := map[string]interface{}{
- "multi_match": map[string]interface{}{
- "query": text,
- "fields": fields,
- },
- }
-
- if fuzziness != "" {
- if multiMatch, ok := query["multi_match"].(map[string]interface{}); ok {
- multiMatch["fuzziness"] = fuzziness
- }
- }
-
- return query
-}
-
-// BuildTermQuery builds a term query
-func BuildTermQuery(field string, value interface{}) map[string]interface{} {
- return map[string]interface{}{
- "term": map[string]interface{}{
- field: value,
- },
- }
-}
-
-// BuildRangeQuery builds a range query
-func BuildRangeQuery(field string, from, to interface{}) map[string]interface{} {
- rangeQuery := make(map[string]interface{})
- if from != nil {
- rangeQuery["gte"] = from
- }
- if to != nil {
- rangeQuery["lte"] = to
- }
-
- return map[string]interface{}{
- "range": map[string]interface{}{
- field: rangeQuery,
- },
- }
-}
-
-// BuildBoolQuery builds a bool query
-func BuildBoolQuery() map[string]interface{} {
- return map[string]interface{}{
- "bool": make(map[string]interface{}),
- }
-}
-
-// AddMust adds must clause to bool query
-func AddMust(query map[string]interface{}, clauses ...map[string]interface{}) {
- if boolQuery, ok := query["bool"].(map[string]interface{}); ok {
- if _, exists := boolQuery["must"]; !exists {
- boolQuery["must"] = []map[string]interface{}{}
- }
- if must, ok := boolQuery["must"].([]map[string]interface{}); ok {
- boolQuery["must"] = append(must, clauses...)
- }
- }
-}
-
-// AddShould adds should clause to bool query
-func AddShould(query map[string]interface{}, clauses ...map[string]interface{}) {
- if boolQuery, ok := query["bool"].(map[string]interface{}); ok {
- if _, exists := boolQuery["should"]; !exists {
- boolQuery["should"] = []map[string]interface{}{}
- }
- if should, ok := boolQuery["should"].([]map[string]interface{}); ok {
- boolQuery["should"] = append(should, clauses...)
- }
- }
-}
-
-// AddFilter adds filter clause to bool query
-func AddFilter(query map[string]interface{}, clauses ...map[string]interface{}) {
- if boolQuery, ok := query["bool"].(map[string]interface{}); ok {
- if _, exists := boolQuery["filter"]; !exists {
- boolQuery["filter"] = []map[string]interface{}{}
- }
- if filter, ok := boolQuery["filter"].([]map[string]interface{}); ok {
- boolQuery["filter"] = append(filter, clauses...)
- }
- }
-}
-
-// AddMustNot adds must_not clause to bool query
-func AddMustNot(query map[string]interface{}, clauses ...map[string]interface{}) {
- if boolQuery, ok := query["bool"].(map[string]interface{}); ok {
- if _, exists := boolQuery["must_not"]; !exists {
- boolQuery["must_not"] = []map[string]interface{}{}
- }
- if mustNot, ok := boolQuery["must_not"].([]map[string]interface{}); ok {
- boolQuery["must_not"] = append(mustNot, clauses...)
- }
- }
-}
-
-// GetDocIDs is not implemented for Elasticsearch
-func (e *elasticsearchEngine) GetDocIDs(chunks []map[string]interface{}) []string {
- common.Warn("GetDocIDs not implemented for Elasticsearch")
- return nil
-}
-
-// equivalentConditionToStr converts a condition map to a filter string (for ES query_string)
-func equivalentConditionToStr(condition map[string]interface{}) string {
- if len(condition) == 0 {
- return ""
- }
-
- var cond []string
-
- for k, v := range condition {
- if k == "_id" {
- continue
- }
- if v == nil || v == "" {
- continue
- }
-
- // Handle list values
- if list, ok := v.([]interface{}); ok && len(list) > 0 {
- var items []string
- for _, item := range list {
- if s, ok := item.(string); ok {
- items = append(items, fmt.Sprintf("%s:'%s'", k, strings.ReplaceAll(s, "'", "\\'")))
- } else {
- items = append(items, fmt.Sprintf("%s:%v", k, item))
- }
- }
- if len(items) > 0 {
- cond = append(cond, "("+strings.Join(items, " OR ")+")")
- }
- continue
- }
-
- if list, ok := v.([]string); ok && len(list) > 0 {
- var items []string
- for _, item := range list {
- items = append(items, fmt.Sprintf("%s:'%s'", k, strings.ReplaceAll(item, "'", "\\'")))
- }
- if len(items) > 0 {
- cond = append(cond, "("+strings.Join(items, " OR ")+")")
- }
- continue
- }
-
- // Handle numeric values (no quotes)
- if isNumericValue(v) {
- cond = append(cond, fmt.Sprintf("%s:%v", k, v))
- continue
- }
-
- // Handle string values (with quotes and escaping)
- if str, ok := v.(string); ok {
- cond = append(cond, fmt.Sprintf("%s:'%s'", k, strings.ReplaceAll(str, "'", "\\'")))
- continue
- }
-
- // Fallback: treat as string
- cond = append(cond, fmt.Sprintf("%s:'%v'", k, v))
- }
-
- if len(cond) == 0 {
- return ""
- }
- return strings.Join(cond, " AND ")
-}
-
-// isNumericValue checks if a value is numeric
-func isNumericValue(v interface{}) bool {
- switch v.(type) {
- case int, int8, int16, int32, int64:
- return true
- case uint, uint8, uint16, uint32, uint64:
- return true
- case float32, float64:
- return true
- }
- return false
-}
-
// calculateScores calculates _score for chunks
func calculateScores(chunks []map[string]interface{}, scoreColumn, pagerankField string) []map[string]interface{} {
for i := range chunks {
@@ -1975,82 +2278,3 @@ func getChunkScore(chunk map[string]interface{}) float64 {
}
return 0.0
}
-
-// GetFields applies field mappings to chunks and returns a dict keyed by chunk ID.
-// This mirrors the Infinity GetFields function behavior.
-func GetFields(chunks []map[string]interface{}, fields []string) map[string]map[string]interface{} {
- result := make(map[string]map[string]interface{})
- if len(chunks) == 0 {
- return result
- }
-
- // If fields is provided, create a set for lookup
- fieldSet := make(map[string]bool)
- for _, f := range fields {
- fieldSet[f] = true
- }
-
- for _, chunk := range chunks {
- // Apply field mappings
- // docnm -> docnm_kwd, title_tks, title_sm_tks
- if val, ok := chunk["docnm"].(string); ok {
- chunk["docnm_kwd"] = val
- chunk["title_tks"] = val
- chunk["title_sm_tks"] = val
- }
-
- // important_keywords -> important_kwd (split by comma), important_tks
- if val, ok := chunk["important_keywords"].(string); ok {
- if val == "" {
- chunk["important_kwd"] = []interface{}{}
- } else {
- parts := strings.Split(val, ",")
- chunk["important_kwd"] = parts
- }
- chunk["important_tks"] = val
- } else {
- chunk["important_kwd"] = []interface{}{}
- chunk["important_tks"] = []interface{}{}
- }
-
- // questions -> question_kwd (split by newline), question_tks
- if val, ok := chunk["questions"].(string); ok {
- if val == "" {
- chunk["question_kwd"] = []interface{}{}
- } else {
- parts := strings.Split(val, "\n")
- chunk["question_kwd"] = parts
- }
- chunk["question_tks"] = val
- } else {
- chunk["question_kwd"] = []interface{}{}
- chunk["question_tks"] = []interface{}{}
- }
-
- // content -> content_with_weight, content_ltks, content_sm_ltks
- if val, ok := chunk["content"].(string); ok {
- chunk["content_with_weight"] = val
- chunk["content_ltks"] = val
- chunk["content_sm_ltks"] = val
- }
-
- // authors -> authors_tks, authors_sm_tks
- if val, ok := chunk["authors"].(string); ok {
- chunk["authors_tks"] = val
- chunk["authors_sm_tks"] = val
- }
-
- // Build result map keyed by id
- if id, ok := chunk["id"].(string); ok {
- fieldMap := make(map[string]interface{})
- for field, value := range chunk {
- if len(fieldSet) == 0 || fieldSet[field] {
- fieldMap[field] = value
- }
- }
- result[id] = fieldMap
- }
- }
-
- return result
-}
\ No newline at end of file
diff --git a/internal/engine/elasticsearch/chunk_test.go b/internal/engine/elasticsearch/chunk_test.go
new file mode 100644
index 0000000000..e7fb7193b5
--- /dev/null
+++ b/internal/engine/elasticsearch/chunk_test.go
@@ -0,0 +1,433 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package elasticsearch
+
+import (
+ "context"
+ "reflect"
+ "sort"
+ "testing"
+
+ "ragflow/internal/common"
+)
+
+// makeResponse builds a SearchResponse with `n` synthetic hits whose id
+// is the index in the batch ("h-0", "h-1", ...) and whose sort cursor is
+// ["h-0", "h-1", ...] so each iteration's cursor advances deterministically.
+func makeResponse(n int, startID int, total int64) SearchResponse {
+ resp := SearchResponse{}
+ resp.Hits.Total.Value = total
+ if n == 0 {
+ return resp
+ }
+ hits := make([]struct {
+ ID string `json:"_id"`
+ Index string `json:"_index"`
+ Score float64 `json:"_score"`
+ Source map[string]interface{} `json:"_source"`
+ Fields map[string]interface{} `json:"fields"`
+ Sort []interface{} `json:"sort,omitempty"`
+ }, n)
+ for i := 0; i < n; i++ {
+ id := startID + i
+ hits[i].ID = "h-" + itoa(id)
+ hits[i].Source = map[string]interface{}{"id": id}
+ hits[i].Sort = []interface{}{"h-" + itoa(id)}
+ }
+ resp.Hits.Hits = hits
+ return resp
+}
+
+func itoa(i int) string {
+ if i == 0 {
+ return "0"
+ }
+ neg := false
+ if i < 0 {
+ neg = true
+ i = -i
+ }
+ var buf [20]byte
+ pos := len(buf)
+ for i > 0 {
+ pos--
+ buf[pos] = byte('0' + i%10)
+ i /= 10
+ }
+ if neg {
+ pos--
+ buf[pos] = '-'
+ }
+ return string(buf[pos:])
+}
+
+// mockFetcher returns searchAfterFetcher implementations that draw
+// from a pre-loaded sequence of (batch, hits) responses and record
+// every call so tests can assert against them.
+//
+// The mock honours the `batch` argument the way a real ES client
+// would: it returns at most `min(batch, scriptedHitsRemaining)` hits
+// per call. This makes the loop's "did we ask for more than ES gave
+// us?" branch observable.
+type mockFetcher struct {
+ // scripted: each entry is the FULL response the fetcher would
+ // return for one request. The hits inside it are pre-truncated to
+ // the size the test wants the fetcher to deliver.
+ scripted []SearchResponse
+ scriptedTotal int64
+ idx int
+ // calls records every (batch, cursor, trackTotalHits) tuple the
+ // pagination loop sent.
+ calls []mockCall
+}
+
+type mockCall struct {
+ batch int
+ cursor []interface{}
+ trackTotalHits bool
+}
+
+func (m *mockFetcher) fetch(_ context.Context, _ map[string]interface{}, batch int, cursor []interface{}, trackTotalHits bool) (SearchResponse, error) {
+ m.calls = append(m.calls, mockCall{batch: batch, cursor: cursor, trackTotalHits: trackTotalHits})
+ if m.idx >= len(m.scripted) {
+ return SearchResponse{}, nil
+ }
+ resp := m.scripted[m.idx]
+ m.idx++
+ // Honour `batch` like a real ES: trim the hit list to the
+ // requested size so the loop's "short batch" branch is reachable.
+ if batch > 0 && len(resp.Hits.Hits) > batch {
+ resp.Hits.Hits = resp.Hits.Hits[:batch]
+ }
+ // Only the first request is asked to track the exact total. The
+ // scripted response may already carry a total; the fetcher fills
+ // in scriptedTotal only when the response's total is zero AND the
+ // caller asked for the exact count.
+ if trackTotalHits && resp.Hits.Total.Value == 0 && m.scriptedTotal > 0 {
+ resp.Hits.Total.Value = m.scriptedTotal
+ }
+ return resp, nil
+}
+
+// TestSortValuesEqual pins down the cursor-equality helper. The
+// pagination loop uses it to detect "ES didn't advance" — when the
+// cursor between two consecutive responses is unchanged, the index is
+// exhausted and the loop must stop. False negatives here would loop
+// forever; false positives would terminate early and miss data.
+func TestSortValuesEqual(t *testing.T) {
+ tests := []struct {
+ name string
+ a, b []interface{}
+ want bool
+ }{
+ {name: "both_nil", a: nil, b: nil, want: true},
+ {name: "first_nil", a: nil, b: []interface{}{"x"}, want: false},
+ {name: "second_nil", a: []interface{}{"x"}, b: nil, want: false},
+ {name: "equal_strings", a: []interface{}{"a", "b"}, b: []interface{}{"a", "b"}, want: true},
+ {name: "different_strings", a: []interface{}{"a"}, b: []interface{}{"b"}, want: false},
+ {name: "different_lengths", a: []interface{}{"a"}, b: []interface{}{"a", "b"}, want: false},
+ {name: "mixed_types_equal", a: []interface{}{"x", float64(1)}, b: []interface{}{"x", float64(1)}, want: true},
+ {name: "mixed_types_differ", a: []interface{}{"x", float64(1)}, b: []interface{}{"x", float64(2)}, want: false},
+ }
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ if got := sortValuesEqual(tc.a, tc.b); got != tc.want {
+ t.Errorf("sortValuesEqual(%#v, %#v) = %v, want %v", tc.a, tc.b, got, tc.want)
+ }
+ })
+ }
+}
+
+// TestSearchAfterPaginateSimpleFirstPage exercises the trivial case:
+// offset=0, limit=N, N hits available in one response. The loop must
+// send one request, return the hits, and report the total.
+func TestSearchAfterPaginateSimpleFirstPage(t *testing.T) {
+ m := &mockFetcher{
+ scripted: []SearchResponse{makeResponse(5, 0, 5)},
+ scriptedTotal: 5,
+ }
+ got, total, err := searchAfterPaginate(context.Background(), map[string]interface{}{}, 0, 5, m.fetch)
+ if err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+ if total != 5 {
+ t.Errorf("total = %d, want 5", total)
+ }
+ if len(got) != 5 {
+ t.Fatalf("len(hits) = %d, want 5", len(got))
+ }
+ if m.idx != 1 {
+ t.Errorf("expected 1 fetch call, got %d", m.idx)
+ }
+}
+
+// TestSearchAfterPaginateSkipsDeepOffset covers the regression the
+// `useSearchAfter` bug report flagged: offset=10_500 (past the
+// MAX_RESULT_WINDOW of 10_000) with limit=10 must NOT return the
+// first page — it must walk the result set and return the right slice.
+//
+// We simulate 10,510 total hits, asking for offset=10_500 + limit=10.
+// The mock returns full 1000-hit batches with advancing cursors; the
+// loop should issue 11 batches (10 to skip + 1 to take) and return
+// the last 10 hits (h-10500..h-10509).
+func TestSearchAfterPaginateSkipsDeepOffset(t *testing.T) {
+ const total = 10510
+ const offset = 10500
+ const limit = 10
+
+ // 10 full skip batches: each 1000 hits, cursor advances.
+ // The 11th batch has 510 hits — the skip phase asks for 500
+ // (the remaining 500 to skip), the take phase picks up the
+ // leftover 10. So 11 fetches total cover both phases.
+ m := &mockFetcher{scriptedTotal: total}
+ for i := 0; i < 10; i++ {
+ m.scripted = append(m.scripted, makeResponse(common.SearchAfterBatchSize, i*common.SearchAfterBatchSize, 0))
+ }
+ // Partial batch: 510 hits (10*1000..10*1000+509). Skip uses
+ // the first 500, take uses the last 10.
+ m.scripted = append(m.scripted, makeResponse(510, 10*common.SearchAfterBatchSize, 0))
+ // Defensive: if the loop miscounts and asks for another take
+ // batch, this would surface as a 12th fetch.
+ m.scripted = append(m.scripted, makeResponse(10, 10500, 0))
+
+ got, totalHits, err := searchAfterPaginate(context.Background(), map[string]interface{}{}, offset, limit, m.fetch)
+ if err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+ if totalHits != total {
+ t.Errorf("total = %d, want %d", totalHits, total)
+ }
+ if len(got) != limit {
+ t.Fatalf("len(hits) = %d, want %d", len(got), limit)
+ }
+ // The first returned hit must be h-10500, not h-0 — that is the
+ // entire point of the search_after path.
+ if id, _ := got[0]["id"].(int); id != offset {
+ t.Errorf("first hit id = %d, want %d (search_after must skip the deep offset)", id, offset)
+ }
+ if id, _ := got[limit-1]["id"].(int); id != offset+limit-1 {
+ t.Errorf("last hit id = %d, want %d", id, offset+limit-1)
+ }
+ // 12 fetches: 10 full skip + 1 partial-skip (trims scripted 510
+ // down to 500) + 1 partial-take (the 10 leftover hits the take
+ // phase still needs). The 12th batch in scripted is a defensive
+ // sentinel; if the loop miscounts, that fetch would not be hit.
+ if m.idx != 12 {
+ t.Errorf("expected 12 fetches, got %d", m.idx)
+ }
+}
+
+// TestSearchAfterPaginateExhaustsIndex: when the skip phase reaches
+// the end of the index, the loop must stop, not loop forever waiting
+// for non-empty hits. We simulate a small index that runs out mid-skip.
+func TestSearchAfterPaginateExhaustsIndex(t *testing.T) {
+ // 500 total hits, offset=400, limit=10.
+ // Skip phase: one batch of 500 (the whole index). remainingSkip
+ // becomes 400-500 = -100, loop ends.
+ m := &mockFetcher{
+ scripted: []SearchResponse{
+ makeResponse(500, 0, 500),
+ // We don't expect a second call; if we get one the
+ // loop failed to terminate. Return empty to make
+ // that visible.
+ },
+ scriptedTotal: 500,
+ }
+ got, total, err := searchAfterPaginate(context.Background(), map[string]interface{}{}, 400, 10, m.fetch)
+ if err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+ if total != 500 {
+ t.Errorf("total = %d, want 500", total)
+ }
+ // After skipping all 500 hits, the take phase should issue one
+ // fetch and find 0 hits, returning empty. We check call count
+ // (not m.idx) because the empty response path in the mock
+ // fetcher returns early without incrementing idx.
+ if len(got) != 0 {
+ t.Errorf("len(hits) = %d, want 0 (index exhausted past offset)", len(got))
+ }
+ if len(m.calls) != 2 {
+ t.Errorf("expected 2 fetches (skip+take-empty), got %d", len(m.calls))
+ }
+}
+
+// TestSearchAfterPaginateEmptyResult: zero total hits means the very
+// first response is empty; loop must not loop forever and must
+// return total=0.
+func TestSearchAfterPaginateEmptyResult(t *testing.T) {
+ m := &mockFetcher{
+ scripted: []SearchResponse{makeResponse(0, 0, 0)},
+ scriptedTotal: 0,
+ }
+ got, total, err := searchAfterPaginate(context.Background(), map[string]interface{}{}, 50, 10, m.fetch)
+ if err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+ if total != 0 {
+ t.Errorf("total = %d, want 0", total)
+ }
+ if len(got) != 0 {
+ t.Errorf("len(hits) = %d, want 0", len(got))
+ }
+ if m.idx != 1 {
+ t.Errorf("expected 1 fetch, got %d", m.idx)
+ }
+}
+
+// TestSearchAfterPaginateStopOnUnchangedCursor: the loop must detect
+// the "ES didn't advance" signal (next sort cursor == previous) and
+// stop, rather than spinning. This is a defensive break in case
+// search_after returns identical hits on consecutive requests.
+func TestSearchAfterPaginateStopOnUnchangedCursor(t *testing.T) {
+ // First response advances the cursor; second response is the
+ // same cursor — loop should stop, NOT call a third time.
+ resp1 := makeResponse(1000, 0, 5000)
+ resp1.Hits.Hits[999].Sort = []interface{}{"cursor-1"}
+ resp2 := makeResponse(1000, 1000, 0)
+ resp2.Hits.Hits[999].Sort = []interface{}{"cursor-1"} // same as resp1's last
+ m := &mockFetcher{
+ scripted: []SearchResponse{resp1, resp2},
+ scriptedTotal: 5000,
+ }
+ // offset=500, limit=10.
+ _, _, err := searchAfterPaginate(context.Background(), map[string]interface{}{}, 500, 10, m.fetch)
+ if err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+ // 2 fetches: 1 skip, 1 take that hit the unchanged-cursor
+ // termination. A 3rd fetch would mean we failed to stop.
+ if m.idx != 2 {
+ t.Errorf("expected 2 fetches (loop must stop on unchanged cursor), got %d", m.idx)
+ }
+}
+
+// TestSearchAfterPaginateLimitLargerThanBatchSize: when limit exceeds
+// SearchAfterBatchSize, the take phase must issue multiple iterations.
+func TestSearchAfterPaginateLimitLargerThanBatchSize(t *testing.T) {
+ const limit = 2500 // > 2 * common.SearchAfterBatchSize
+ m := &mockFetcher{
+ scripted: []SearchResponse{
+ // Skip phase empty (offset=0).
+ makeResponse(1000, 0, 10000),
+ makeResponse(1000, 1000, 0),
+ makeResponse(1000, 2000, 0),
+ },
+ scriptedTotal: 10000,
+ }
+ got, total, err := searchAfterPaginate(context.Background(), map[string]interface{}{}, 0, limit, m.fetch)
+ if err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+ if total != 10000 {
+ t.Errorf("total = %d, want 10000", total)
+ }
+ if len(got) != limit {
+ t.Errorf("len(hits) = %d, want %d", len(got), limit)
+ }
+ // offset=0 means the skip loop doesn't run; the take loop
+ // issues 3 fetches (1000 + 1000 + 500 = 2500). The third fetch
+ // is "short" (500 < 1000), which the loop uses to stop early
+ // without over-collecting.
+ if m.idx != 3 {
+ t.Errorf("expected 3 take fetches, got %d", m.idx)
+ }
+ // Hits should be in order h-0..h-2499.
+ wantIDs := make([]int, limit)
+ for i := range wantIDs {
+ wantIDs[i] = i
+ }
+ gotIDs := make([]int, len(got))
+ for i, h := range got {
+ gotIDs[i] = h["id"].(int)
+ }
+ if !reflect.DeepEqual(gotIDs, wantIDs) {
+ t.Errorf("hit order wrong: got %v, want %v", sortedCopy(gotIDs), wantIDs)
+ }
+}
+
+func sortedCopy(in []int) []int {
+ out := append([]int(nil), in...)
+ sort.Ints(out)
+ return out
+}
+
+// TestBuildBoolQueryFromConditionIDFilter is the regression for the
+// "id filter encoded as a nested array inside bool.should" bug.
+//
+// Each branch of the `k == "id"` clause used to append a
+// `[]map[string]interface{}` literal as a single element, so the emitted
+// JSON was `should: [[{...}, {...}]]` (a nested array) instead of
+// `should: [{...}, {...}]`. ES rejects the nested form as malformed.
+// This test pins down the flat shape for the list, string, and int
+// branches.
+func TestBuildBoolQueryFromConditionIDFilter(t *testing.T) {
+ check := func(name string, cond map[string]interface{}, wantFields []string) {
+ t.Helper()
+ got := buildBoolQueryFromCondition(cond, nil, false)
+ outer, ok := got["bool"].(map[string]interface{})
+ if !ok {
+ t.Fatalf("%s: missing bool wrapper: %v", name, got)
+ }
+ should, ok := outer["should"].([]interface{})
+ if !ok {
+ t.Fatalf("%s: should is not []interface{}: %T", name, outer["should"])
+ }
+ if len(should) != len(wantFields) {
+ t.Fatalf("%s: should length = %d, want %d (raw=%v)", name, len(should), len(wantFields), should)
+ }
+ // Every element must be a map (not another slice). A nested slice
+ // would be the bug we're guarding against.
+ seenFields := make(map[string]bool, len(wantFields))
+ for i, el := range should {
+ m, ok := el.(map[string]interface{})
+ if !ok {
+ t.Fatalf("%s: should[%d] is %T, want map[string]interface{} (nested array bug) — raw=%v", name, i, el, el)
+ }
+ if inner, ok := m["term"].(map[string]interface{}); ok {
+ for f := range inner {
+ seenFields[f] = true
+ }
+ continue
+ }
+ if terms, ok := m["terms"].(map[string]interface{}); ok {
+ for f := range terms {
+ seenFields[f] = true
+ }
+ continue
+ }
+ t.Fatalf("%s: should[%d] missing term/terms: %v", name, i, m)
+ }
+ for _, want := range wantFields {
+ if !seenFields[want] {
+ t.Errorf("%s: expected field %q in should clauses, got %v", name, want, seenFields)
+ }
+ }
+ }
+
+ check("list_value", map[string]interface{}{
+ "id": []interface{}{"a", "b", "c"},
+ }, []string{"id", "_id"})
+
+ check("string_value", map[string]interface{}{
+ "id": "doc-42",
+ }, []string{"id", "_id"})
+
+ check("int_value", map[string]interface{}{
+ "id": 42,
+ }, []string{"id", "_id"})
+}
diff --git a/internal/engine/elasticsearch/client.go b/internal/engine/elasticsearch/client.go
index c69d78f935..cee7dff18c 100644
--- a/internal/engine/elasticsearch/client.go
+++ b/internal/engine/elasticsearch/client.go
@@ -33,7 +33,7 @@ import (
"github.com/elastic/go-elasticsearch/v8/esapi"
)
-// Engine Elasticsearch engine implementation
+// elasticsearchEngine is the Elasticsearch engine implementation
type elasticsearchEngine struct {
client *elasticsearch.Client
config *server.ElasticsearchConfig
diff --git a/internal/engine/elasticsearch/meta_filter.go b/internal/engine/elasticsearch/meta_filter.go
new file mode 100644
index 0000000000..61a2f23f07
--- /dev/null
+++ b/internal/engine/elasticsearch/meta_filter.go
@@ -0,0 +1,682 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package elasticsearch
+
+import (
+ "fmt"
+ "regexp"
+ "strconv"
+ "strings"
+
+ "ragflow/internal/common"
+
+ "go.uber.org/zap"
+)
+
+// Field prefix in the doc-metadata ES index
+const metaFieldsPrefix = "meta_fields"
+
+// Date pattern for YYYY-MM-DD
+var dateRegex = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`)
+
+// Supported operators
+var supportedOperators = map[string]bool{
+ "=": true,
+ "≠": true,
+ ">": true,
+ "<": true,
+ "≥": true,
+ "≤": true,
+ "in": true,
+ "not in": true,
+ "contains": true,
+ "not contains": true,
+ "start with": true,
+ "end with": true,
+ "empty": true,
+ "not empty": true,
+}
+
+// Range operators mapping
+var rangeOps = map[string]string{
+ ">": "gt",
+ "<": "lt",
+ "≥": "gte",
+ "≤": "lte",
+}
+
+// Negative operators unsafe for multi-valued fields
+var multivalueUnsafeNegativeOps = map[string]bool{
+ "≠": true,
+ "not in": true,
+}
+
+// UnsupportedMetaFilterError is raised when a filter cannot be expressed as ES DSL
+type UnsupportedMetaFilterError struct {
+ Reason string
+ FilterClause map[string]interface{}
+}
+
+func (e *UnsupportedMetaFilterError) Error() string {
+ return e.Reason
+}
+
+// TranslatedFilter represents a single filter rendered as ES bool clauses
+type TranslatedFilter struct {
+ Must []map[string]interface{}
+ MustNot []map[string]interface{}
+}
+
+// ToClauses converts to ES clauses
+func (f *TranslatedFilter) ToClauses() []map[string]interface{} {
+ if len(f.Must) == 0 && len(f.MustNot) == 0 {
+ return []map[string]interface{}{}
+ }
+ if len(f.MustNot) == 0 {
+ if len(f.Must) == 1 {
+ return []map[string]interface{}{f.Must[0]}
+ }
+ return []map[string]interface{}{{"bool": map[string]interface{}{"must": f.Must}}}
+ }
+ return []map[string]interface{}{{"bool": map[string]interface{}{"must": f.Must, "must_not": f.MustNot}}}
+}
+
+// MetaFilterPushdownPlan represents composed ES bool query body
+type MetaFilterPushdownPlan struct {
+ Logic string
+ translated []*TranslatedFilter
+}
+
+// IsEmpty returns true if plan has no filters
+func (p *MetaFilterPushdownPlan) IsEmpty() bool {
+ return len(p.translated) == 0
+}
+
+// ToQuery renders the full ES query body scoped to given KB IDs
+func (p *MetaFilterPushdownPlan) ToQuery(kbIDs []string) map[string]interface{} {
+ kbClause := map[string]interface{}{"terms": map[string]interface{}{"kb_id": kbIDs}}
+
+ if p.IsEmpty() {
+ return map[string]interface{}{
+ "query": map[string]interface{}{
+ "bool": map[string]interface{}{
+ "filter": []map[string]interface{}{kbClause},
+ },
+ },
+ }
+ }
+
+ var flatClauses []map[string]interface{}
+ for _, t := range p.translated {
+ clauses := t.ToClauses()
+ flatClauses = append(flatClauses, clauses...)
+ }
+
+ var inner map[string]interface{}
+ if p.Logic == "or" {
+ inner = map[string]interface{}{
+ "bool": map[string]interface{}{
+ "should": flatClauses,
+ "minimum_should_match": 1,
+ },
+ }
+ } else {
+ inner = map[string]interface{}{
+ "bool": map[string]interface{}{
+ "must": flatClauses,
+ },
+ }
+ }
+
+ return map[string]interface{}{
+ "query": map[string]interface{}{
+ "bool": map[string]interface{}{
+ "filter": []map[string]interface{}{kbClause, inner},
+ },
+ },
+ }
+}
+
+// MetaFilterTranslator translates filter clauses to ES DSL
+type MetaFilterTranslator struct {
+ prefix string
+}
+
+// NewMetaFilterTranslator creates a new translator
+func NewMetaFilterTranslator() *MetaFilterTranslator {
+ return &MetaFilterTranslator{prefix: metaFieldsPrefix}
+}
+
+func (t *MetaFilterTranslator) fieldName(key string) string {
+ return t.prefix + "." + key
+}
+
+// Translate translates a single filter dict into ES bool clauses
+func (t *MetaFilterTranslator) Translate(flt map[string]interface{}) (*TranslatedFilter, error) {
+ op, _ := flt["op"].(string)
+ key, _ := flt["key"].(string)
+ value := flt["value"]
+
+ if key == "" {
+ return nil, &UnsupportedMetaFilterError{Reason: "filter is missing a string key", FilterClause: flt}
+ }
+ if !supportedOperators[op] {
+ return nil, &UnsupportedMetaFilterError{Reason: fmt.Sprintf("unknown operator %q", op), FilterClause: flt}
+ }
+ if op != "empty" && op != "not empty" {
+ switch v := value.(type) {
+ case nil:
+ return nil, &UnsupportedMetaFilterError{Reason: fmt.Sprintf("operator %q requires a value", op), FilterClause: flt}
+ case string:
+ if strings.TrimSpace(v) == "" {
+ return nil, &UnsupportedMetaFilterError{Reason: fmt.Sprintf("operator %q requires a non-empty value", op), FilterClause: flt}
+ }
+ case []interface{}:
+ if len(v) == 0 {
+ return nil, &UnsupportedMetaFilterError{Reason: fmt.Sprintf("operator %q requires at least one value", op), FilterClause: flt}
+ }
+ }
+ }
+
+ fieldPath := t.fieldName(key)
+
+ switch op {
+ case "empty":
+ return t.translateEmpty(fieldPath), nil
+ case "not empty":
+ return t.translateNotEmpty(fieldPath), nil
+ case "=":
+ return t.translateEqual(fieldPath, value, flt), nil
+ case "≠":
+ return t.translateNotEqual(fieldPath, value, flt), nil
+ case ">", "<", "≥", "≤":
+ return t.translateRange(fieldPath, op, value, flt), nil
+ case "in":
+ return t.translateIn(fieldPath, value, flt), nil
+ case "not in":
+ return t.translateNotIn(fieldPath, value, flt), nil
+ case "contains":
+ return t.translateContains(fieldPath, value, flt), nil
+ case "not contains":
+ return t.translateNotContains(fieldPath, value, flt), nil
+ case "start with":
+ return t.translateStartWith(fieldPath, value, flt), nil
+ case "end with":
+ return t.translateEndWith(fieldPath, value, flt), nil
+ }
+
+ return nil, &UnsupportedMetaFilterError{Reason: fmt.Sprintf("no handler for operator %q", op), FilterClause: flt}
+}
+
+func (t *MetaFilterTranslator) translateEmpty(fieldPath string) *TranslatedFilter {
+ keywordPath := keywordPath(fieldPath)
+ return &TranslatedFilter{
+ Must: []map[string]interface{}{
+ {
+ "bool": map[string]interface{}{
+ "should": []map[string]interface{}{
+ {"bool": map[string]interface{}{"must_not": []map[string]interface{}{{"exists": map[string]interface{}{"field": fieldPath}}}}},
+ {"term": map[string]interface{}{keywordPath: ""}},
+ },
+ "minimum_should_match": 1,
+ },
+ },
+ },
+ }
+}
+
+func (t *MetaFilterTranslator) translateNotEmpty(fieldPath string) *TranslatedFilter {
+ keywordPath := keywordPath(fieldPath)
+ return &TranslatedFilter{
+ Must: []map[string]interface{}{{"exists": map[string]interface{}{"field": fieldPath}}},
+ MustNot: []map[string]interface{}{{"term": map[string]interface{}{keywordPath: ""}}},
+ }
+}
+
+func (t *MetaFilterTranslator) translateEqual(fieldPath string, value interface{}, flt map[string]interface{}) *TranslatedFilter {
+ coerced := coerceScalar(value, flt)
+ return &TranslatedFilter{
+ Must: []map[string]interface{}{termOrMatch(fieldPath, coerced)},
+ }
+}
+
+func (t *MetaFilterTranslator) translateNotEqual(fieldPath string, value interface{}, flt map[string]interface{}) *TranslatedFilter {
+ coerced := coerceScalar(value, flt)
+ return &TranslatedFilter{
+ Must: []map[string]interface{}{{"exists": map[string]interface{}{"field": fieldPath}}},
+ MustNot: []map[string]interface{}{termOrMatch(fieldPath, coerced)},
+ }
+}
+
+func (t *MetaFilterTranslator) translateRange(fieldPath string, op string, value interface{}, flt map[string]interface{}) *TranslatedFilter {
+ coerced := coerceRangeValue(value, flt)
+ sqlOp := rangeOps[op]
+ return &TranslatedFilter{
+ Must: []map[string]interface{}{
+ {"exists": map[string]interface{}{"field": fieldPath}},
+ {"range": map[string]interface{}{fieldPath: map[string]interface{}{sqlOp: coerced}}},
+ },
+ }
+}
+
+func (t *MetaFilterTranslator) translateIn(fieldPath string, value interface{}, flt map[string]interface{}) *TranslatedFilter {
+ members := csvOrList(value, flt)
+ return &TranslatedFilter{
+ Must: []map[string]interface{}{termsStringOrNumeric(fieldPath, members)},
+ }
+}
+
+func (t *MetaFilterTranslator) translateNotIn(fieldPath string, value interface{}, flt map[string]interface{}) *TranslatedFilter {
+ members := csvOrList(value, flt)
+ return &TranslatedFilter{
+ Must: []map[string]interface{}{{"exists": map[string]interface{}{"field": fieldPath}}},
+ MustNot: []map[string]interface{}{termsStringOrNumeric(fieldPath, members)},
+ }
+}
+
+func (t *MetaFilterTranslator) translateContains(fieldPath string, value interface{}, flt map[string]interface{}) *TranslatedFilter {
+ text := coerceString(value, flt)
+ return &TranslatedFilter{
+ Must: []map[string]interface{}{wildcard(fieldPath, "*"+escapeWildcard(text)+"*")},
+ }
+}
+
+func (t *MetaFilterTranslator) translateNotContains(fieldPath string, value interface{}, flt map[string]interface{}) *TranslatedFilter {
+ text := coerceString(value, flt)
+ return &TranslatedFilter{
+ Must: []map[string]interface{}{{"exists": map[string]interface{}{"field": fieldPath}}},
+ MustNot: []map[string]interface{}{wildcard(fieldPath, "*"+escapeWildcard(text)+"*")},
+ }
+}
+
+func (t *MetaFilterTranslator) translateStartWith(fieldPath string, value interface{}, flt map[string]interface{}) *TranslatedFilter {
+ text := coerceString(value, flt)
+ keywordPath := keywordPath(fieldPath)
+ return &TranslatedFilter{
+ Must: []map[string]interface{}{
+ {"prefix": map[string]interface{}{keywordPath: map[string]interface{}{"value": text, "case_insensitive": true}}},
+ },
+ }
+}
+
+func (t *MetaFilterTranslator) translateEndWith(fieldPath string, value interface{}, flt map[string]interface{}) *TranslatedFilter {
+ text := coerceString(value, flt)
+ return &TranslatedFilter{
+ Must: []map[string]interface{}{wildcard(fieldPath, "*"+escapeWildcard(text))},
+ }
+}
+
+// BuildMetaFilterQuery translates filters and renders ES query body
+func BuildMetaFilterQuery(filters []map[string]interface{}, logic string, kbIDs []string) (map[string]interface{}, error) {
+ plan, err := planPushdown(filters, logic)
+ if err != nil {
+ return nil, err
+ }
+ return plan.ToQuery(kbIDs), nil
+}
+
+// PlanPushdown translates every filter and builds a composed plan
+func planPushdown(filters []map[string]interface{}, logic string) (*MetaFilterPushdownPlan, error) {
+ if logic != "and" && logic != "or" {
+ return nil, fmt.Errorf("unsupported logic %q", logic)
+ }
+
+ translator := NewMetaFilterTranslator()
+ plan := &MetaFilterPushdownPlan{Logic: logic}
+ for _, flt := range filters {
+ translated, err := translator.Translate(flt)
+ if err != nil {
+ common.Warn("plan_pushdown failed", zap.String("error", err.Error()))
+ return nil, err
+ }
+ plan.translated = append(plan.translated, translated)
+ }
+ return plan, nil
+}
+
+// IsPushdownSupported checks if all filters can be pushed down
+func IsPushdownSupported(filters []map[string]interface{}) bool {
+ for _, flt := range filters {
+ op, _ := flt["op"].(string)
+ if !supportedOperators[op] {
+ return false
+ }
+ if multivalueUnsafeNegativeOps[op] {
+ return false
+ }
+ key, _ := flt["key"].(string)
+ if key == "" {
+ return false
+ }
+ }
+ return true
+}
+
+// ExtractDocIDs extracts doc IDs from ES search response
+func ExtractDocIDs(esResponse map[string]interface{}) []string {
+ var docIDs []string
+
+ hitsRoot, ok := esResponse["hits"].(map[string]interface{})
+ if !ok {
+ return docIDs
+ }
+
+ rawHits, ok := hitsRoot["hits"].([]interface{})
+ if !ok {
+ return docIDs
+ }
+
+ for _, hit := range rawHits {
+ hitMap, ok := hit.(map[string]interface{})
+ if !ok {
+ continue
+ }
+ docID := ""
+ if id, ok := hitMap["_id"].(string); ok {
+ docID = id
+ } else {
+ source, _ := hitMap["_source"].(map[string]interface{})
+ if source != nil {
+ if id, ok := source["id"].(string); ok {
+ docID = id
+ } else if docID, ok = source["doc_id"].(string); ok {
+ // already set
+ }
+ }
+ }
+ if docID != "" {
+ docIDs = append(docIDs, docID)
+ }
+ }
+
+ return docIDs
+}
+
+// coerceScalar mirrors ast.literal_eval then str.lower() flow
+func coerceScalar(value interface{}, flt map[string]interface{}) interface{} {
+ if value == nil {
+ return nil
+ }
+
+ s := strings.TrimSpace(fmt.Sprintf("%v", value))
+ if dateRegex.MatchString(s) {
+ return s
+ }
+
+ // Try to parse as int first (like Python's ast.literal_eval preserves int vs float)
+ if parsed, err := strconv.ParseInt(s, 10, 64); err == nil {
+ return parsed
+ }
+ if parsed, err := strconv.ParseFloat(s, 64); err == nil {
+ return parsed
+ }
+
+ // It's a string - lower case it
+ return strings.ToLower(s)
+}
+
+// coerceRangeValue handles range comparison values
+func coerceRangeValue(value interface{}, flt map[string]interface{}) interface{} {
+ if value == nil {
+ return nil
+ }
+
+ s := strings.TrimSpace(fmt.Sprintf("%v", value))
+ if dateRegex.MatchString(s) {
+ return s
+ }
+
+ // Try to parse as number
+ if parsed, err := strconv.ParseFloat(s, 64); err == nil {
+ return parsed
+ }
+
+ return s
+}
+
+// coerceString ensures value is a non-empty string
+func coerceString(value interface{}, flt map[string]interface{}) string {
+ if value == nil {
+ return ""
+ }
+ s := fmt.Sprintf("%v", value)
+ if s == "" {
+ return ""
+ }
+ return s
+}
+
+// csvOrList handles in/not in values
+func csvOrList(value interface{}, flt map[string]interface{}) []interface{} {
+ if value == nil {
+ return nil
+ }
+
+ var members []interface{}
+
+ switch v := value.(type) {
+ case []interface{}:
+ members = v
+ case string:
+ // Try to parse as JSON array or split by comma
+ trimmed := strings.TrimSpace(v)
+ if strings.HasPrefix(trimmed, "[") && strings.HasSuffix(trimmed, "]") {
+ // JSON array - parse it
+ parsed := parseJSONArray(trimmed)
+ if parsed != nil {
+ members = parsed
+ }
+ } else {
+ // Comma-separated
+ parts := strings.Split(v, ",")
+ for _, p := range parts {
+ trimmed := strings.TrimSpace(p)
+ if trimmed != "" {
+ members = append(members, strings.ToLower(trimmed))
+ }
+ }
+ }
+ default:
+ members = []interface{}{v}
+ }
+
+ if len(members) == 0 {
+ return nil
+ }
+
+ // Normalize strings to lowercase
+ result := make([]interface{}, len(members))
+ for i, m := range members {
+ if s, ok := m.(string); ok {
+ result[i] = strings.ToLower(strings.TrimSpace(s))
+ } else {
+ result[i] = m
+ }
+ }
+
+ return result
+}
+
+// keywordPath returns .keyword sub-field path
+func keywordPath(fieldPath string) string {
+ return fieldPath + ".keyword"
+}
+
+// termOrMatch creates exact-match clause
+func termOrMatch(fieldPath string, value interface{}) map[string]interface{} {
+ if s, ok := value.(string); ok {
+ return map[string]interface{}{
+ "term": map[string]interface{}{
+ keywordPath(fieldPath): map[string]interface{}{
+ "value": s,
+ "case_insensitive": true,
+ },
+ },
+ }
+ }
+ return map[string]interface{}{
+ "term": map[string]interface{}{fieldPath: value},
+ }
+}
+
+// termsStringOrNumeric creates terms query for in/not in
+func termsStringOrNumeric(fieldPath string, members []interface{}) map[string]interface{} {
+ allNumeric := true
+ for _, m := range members {
+ if _, ok := m.(string); ok {
+ allNumeric = false
+ break
+ }
+ }
+
+ if allNumeric {
+ return map[string]interface{}{
+ "terms": map[string]interface{}{fieldPath: members},
+ }
+ }
+
+ var shouldClauses []map[string]interface{}
+ for _, m := range members {
+ shouldClauses = append(shouldClauses, termOrMatch(fieldPath, m))
+ }
+
+ return map[string]interface{}{
+ "bool": map[string]interface{}{
+ "should": shouldClauses,
+ "minimum_should_match": 1,
+ },
+ }
+}
+
+// wildcard creates wildcard query
+func wildcard(fieldPath string, pattern string) map[string]interface{} {
+ return map[string]interface{}{
+ "wildcard": map[string]interface{}{
+ keywordPath(fieldPath): map[string]interface{}{
+ "value": pattern,
+ "case_insensitive": true,
+ },
+ },
+ }
+}
+
+// escapeWildcard escapes wildcard metacharacters
+func escapeWildcard(text string) string {
+ text = strings.ReplaceAll(text, "\\", "\\\\")
+ text = strings.ReplaceAll(text, "*", "\\*")
+ text = strings.ReplaceAll(text, "?", "\\?")
+ return text
+}
+
+// parseJSONArray parses a simple JSON array string
+func parseJSONArray(s string) []interface{} {
+ // Remove brackets
+ s = strings.TrimSpace(s)
+ if len(s) < 2 {
+ return nil
+ }
+ s = s[1 : len(s)-1]
+
+ var result []interface{}
+ parts := splitJSONParts(s)
+ for _, p := range parts {
+ p = strings.TrimSpace(p)
+ if p == "" {
+ continue
+ }
+ // Remove quotes if present
+ if len(p) >= 2 {
+ if (p[0] == '"' && p[len(p)-1] == '"') || (p[0] == '\'' && p[len(p)-1] == '\'') {
+ p = p[1 : len(p)-1]
+ }
+ }
+ result = append(result, p)
+ }
+ return result
+}
+
+// splitJSONParts splits JSON array parts. It tracks the actual quote
+// character that opened the current quoted region so a double-quoted
+// string isn't terminated by a stray single quote inside it (e.g. an
+// apostrophe) and a single-quoted string isn't split by a comma inside
+// a double-quoted neighbour. Naive `inQuote = !inQuote` was wrong on
+// both counts.
+//
+// Quotes preceded by an odd number of backslashes are treated as
+// escaped literals (JSON `\"` / `\'`) so the comma inside a string like
+// `"a\"b,c"` doesn't trigger a spurious split.
+func splitJSONParts(s string) []string {
+ var parts []string
+ var current strings.Builder
+ var quoteChar rune
+ depth := 0
+
+ runes := []rune(s)
+ for i := 0; i < len(runes); i++ {
+ c := runes[i]
+ if c == '\'' || c == '"' {
+ // Inside a quoted string, count the consecutive backslashes
+ // immediately before this rune. An odd count means this
+ // quote is escaped (e.g. JSON `\"`); an even count (incl. 0)
+ // means it really does toggle the quote state.
+ if quoteChar != 0 {
+ bs := 0
+ for j := i - 1; j >= 0 && runes[j] == '\\'; j-- {
+ bs++
+ }
+ if bs%2 == 1 {
+ current.WriteRune(c)
+ continue
+ }
+ }
+ if quoteChar == 0 {
+ quoteChar = c
+ } else if c == quoteChar {
+ quoteChar = 0
+ }
+ current.WriteRune(c)
+ continue
+ }
+ switch c {
+ case '[', '{':
+ if quoteChar == 0 {
+ depth++
+ }
+ current.WriteRune(c)
+ case ']', '}':
+ if quoteChar == 0 {
+ depth--
+ }
+ current.WriteRune(c)
+ case ',':
+ if quoteChar == 0 && depth == 0 {
+ parts = append(parts, current.String())
+ current.Reset()
+ } else {
+ current.WriteRune(c)
+ }
+ default:
+ current.WriteRune(c)
+ }
+ }
+
+ if current.Len() > 0 {
+ parts = append(parts, current.String())
+ }
+
+ return parts
+}
\ No newline at end of file
diff --git a/internal/engine/elasticsearch/meta_filter_test.go b/internal/engine/elasticsearch/meta_filter_test.go
new file mode 100644
index 0000000000..8e953a47c0
--- /dev/null
+++ b/internal/engine/elasticsearch/meta_filter_test.go
@@ -0,0 +1,213 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package elasticsearch
+
+import (
+ "reflect"
+ "testing"
+)
+
+func TestSplitJSONParts(t *testing.T) {
+ tests := []struct {
+ name string
+ in string
+ want []string
+ }{
+ {
+ name: "empty",
+ in: "",
+ want: nil,
+ },
+ {
+ name: "single_quoted_basic",
+ in: `'a', 'b', 'c'`,
+ want: []string{`'a'`, ` 'b'`, ` 'c'`},
+ },
+ {
+ name: "double_quoted_with_commas_inside",
+ in: `"a,b", "c"`,
+ want: []string{`"a,b"`, ` "c"`},
+ },
+ {
+ name: "apostrophe_inside_double_quoted",
+ in: `"don't", "we, can't"`,
+ want: []string{`"don't"`, ` "we, can't"`},
+ },
+ {
+ name: "nested_brackets_inside_quotes_do_not_count",
+ in: `"{a,b}", [1, 2]`,
+ want: []string{`"{a,b}"`, ` [1, 2]`},
+ },
+ {
+ name: "comma_inside_brackets_outside_quotes_splits",
+ in: `[1, 2], [3, 4]`,
+ want: []string{`[1, 2]`, ` [3, 4]`},
+ },
+ {
+ name: "trailing_comma_drops_empty_part",
+ in: `"a", "b",`,
+ want: []string{`"a"`, ` "b"`},
+ },
+ {
+ // Regression: previously the parser toggled quote state on
+ // every '"', so the comma inside the first element split
+ // the array into three pieces and corrupted in/not in filters.
+ name: "escaped_double_quote_does_not_split",
+ in: `"a\"b,c", "d"`,
+ want: []string{`"a\"b,c"`, ` "d"`},
+ },
+ {
+ // `\\\"` = escaped backslash + escaped quote. The backslash
+ // is a literal, the quote is escaped, so no split.
+ name: "escaped_backslash_then_escaped_quote",
+ in: `"a\\\"b", "c"`,
+ want: []string{`"a\\\"b"`, ` "c"`},
+ },
+ {
+ // Symmetric handling for single-quoted regions.
+ name: "escaped_single_quote_does_not_split",
+ in: `'a\'b,c', 'd'`,
+ want: []string{`'a\'b,c'`, ` 'd'`},
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got := splitJSONParts(tt.in)
+ if !reflect.DeepEqual(got, tt.want) {
+ t.Errorf("splitJSONParts(%q):\n got %#v\n want %#v", tt.in, got, tt.want)
+ }
+ })
+ }
+}
+
+func TestParseJSONArrayDoubleQuotedWithCommas(t *testing.T) {
+ got := parseJSONArray(`["a,b", "c"]`)
+ want := []interface{}{"a,b", "c"}
+ if !reflect.DeepEqual(got, want) {
+ t.Errorf("parseJSONArray([\"a,b\", \"c\"]):\n got %#v\n want %#v", got, want)
+ }
+}
+
+// TestTotalHitsFromESResponse pins down the helper that decides whether
+// the ES push-down response was truncated at metaPushdownMaxSize.
+// Returning a non-nil (total, ok=true) value with total > cap is what
+// drives the caller to fall back to the in-memory meta_filter; getting
+// this wrong (false negative) would let the push-down silently drop docs.
+func TestTotalHitsFromESResponse(t *testing.T) {
+ tests := []struct {
+ name string
+ resp map[string]interface{}
+ wantVal int64
+ wantOk bool
+ }{
+ {
+ name: "missing_hits",
+ resp: map[string]interface{}{},
+ wantVal: 0,
+ wantOk: false,
+ },
+ {
+ name: "missing_total",
+ resp: map[string]interface{}{"hits": map[string]interface{}{}},
+ wantVal: 0,
+ wantOk: false,
+ },
+ {
+ name: "exact_total_under_cap",
+ resp: map[string]interface{}{
+ "hits": map[string]interface{}{
+ "total": map[string]interface{}{"value": float64(42)},
+ },
+ },
+ wantVal: 42,
+ wantOk: true,
+ },
+ {
+ name: "exact_total_at_cap",
+ resp: map[string]interface{}{
+ "hits": map[string]interface{}{
+ "total": map[string]interface{}{"value": float64(metaPushdownMaxSize)},
+ },
+ },
+ wantVal: int64(metaPushdownMaxSize),
+ wantOk: true,
+ },
+ {
+ name: "overflow_total_over_cap",
+ resp: map[string]interface{}{
+ "hits": map[string]interface{}{
+ "total": map[string]interface{}{"value": float64(metaPushdownMaxSize + 1)},
+ },
+ },
+ wantVal: int64(metaPushdownMaxSize + 1),
+ wantOk: true,
+ },
+ {
+ name: "wrong_type",
+ resp: map[string]interface{}{
+ "hits": map[string]interface{}{
+ "total": map[string]interface{}{"value": "10000"},
+ },
+ },
+ wantVal: 0,
+ wantOk: false,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ gotVal, gotOk := totalHitsFromESResponse(tt.resp)
+ if gotOk != tt.wantOk {
+ t.Errorf("ok mismatch: got %v want %v", gotOk, tt.wantOk)
+ }
+ if gotVal != tt.wantVal {
+ t.Errorf("value mismatch: got %d want %d", gotVal, tt.wantVal)
+ }
+ })
+ }
+}
+
+// TestPushdownCapSemantics documents the contract for the overflow path:
+// when total > cap, the slice we'd build from ExtractDocIDs is necessarily
+// a strict subset of the truth. FilterDocIdsByMetaPushdown must return
+// nil in that case so the caller falls back to the in-memory meta_filter.
+//
+// This test exercises the response-shape contract directly rather than
+// spinning up a real ES client; the integration is covered by the
+// service-level tests in internal/service/metadata_filter_test.go.
+func TestPushdownCapSemantics(t *testing.T) {
+ // Build a response that LOOKS like a successful push-down but has
+ // total > cap. totalHitsFromESResponse must report ok=true and the
+ // value must exceed the cap, which is the trigger condition for
+ // returning nil in FilterDocIdsByMetaPushdown.
+ resp := map[string]interface{}{
+ "hits": map[string]interface{}{
+ "total": map[string]interface{}{"value": float64(metaPushdownMaxSize + 1)},
+ "hits": []interface{}{
+ map[string]interface{}{"_id": "doc-1"},
+ },
+ },
+ }
+ total, ok := totalHitsFromESResponse(resp)
+ if !ok {
+ t.Fatal("expected total to be parseable from a well-formed ES response")
+ }
+ if total <= int64(metaPushdownMaxSize) {
+ t.Fatalf("expected total > cap, got total=%d cap=%d", total, metaPushdownMaxSize)
+ }
+}
diff --git a/internal/engine/elasticsearch/metadata.go b/internal/engine/elasticsearch/metadata.go
index 065ffb76ec..bbf83540e4 100644
--- a/internal/engine/elasticsearch/metadata.go
+++ b/internal/engine/elasticsearch/metadata.go
@@ -26,6 +26,8 @@ import (
"github.com/elastic/go-elasticsearch/v8/esapi"
"ragflow/internal/common"
+ "ragflow/internal/dao"
+ "ragflow/internal/engine/types"
"go.uber.org/zap"
)
@@ -166,8 +168,17 @@ func (e *elasticsearchEngine) InsertMetadata(ctx context.Context, metadata []map
return []string{}, nil
}
-// UpdateMetadata updates document metadata in tenant's metadata index
-// The metaFields map will fully replace the existing meta_fields
+// UpdateMetadata updates or inserts document metadata in tenant's metadata index.
+//
+// Examples (existing row → input → resulting meta_fields):
+//
+// {character:["曹操","孙权"], year:2025}
+// + {author:["John","Tom"], category:"tech"}
+// = {character:["曹操","孙权"], year:2025, author:["John","Tom"], category:"tech"}
+//
+// {character:["曹操","孙权"], year:2025}
+// + {year:2026}
+// = {character:["曹操","孙权"], year:2026}
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))
@@ -195,10 +206,15 @@ 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.
updateReq := map[string]interface{}{
"query": query,
"script": map[string]interface{}{
- "source": "ctx._source.meta_fields = params.meta_fields",
+ "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(); }",
+ "lang": "painless",
"params": map[string]interface{}{
"meta_fields": metaFields,
},
@@ -329,9 +345,9 @@ func (e *elasticsearchEngine) DeleteMetadataKeys(ctx context.Context, docID stri
// First, get the current meta_fields to check if it will be empty after deletion
getReq := map[string]interface{}{
- "query": query,
+ "query": query,
"_source": []string{"meta_fields"},
- "size": 1,
+ "size": 1,
}
getBytes, err := json.Marshal(getReq)
@@ -503,4 +519,337 @@ func (e *elasticsearchEngine) DropMetadataStore(ctx context.Context, tenantID st
func (e *elasticsearchEngine) MetadataStoreExists(ctx context.Context, tenantID string) (bool, error) {
indexName := buildMetadataIndexName(tenantID)
return e.indexExists(ctx, indexName)
-}
\ No newline at end of file
+}
+
+// SearchMetadata executes search specifically for metadata indices (ragflow_doc_meta_*)
+func (e *elasticsearchEngine) SearchMetadata(ctx context.Context, req *types.SearchMetadataRequest) (*types.SearchMetadataResult, error) {
+ tenantID := req.TenantID
+ common.Debug("SearchMetadata in Elasticsearch started", zap.String("tenantID", tenantID))
+
+ // Validate inputs
+ if tenantID == "" {
+ return nil, fmt.Errorf("tenantID cannot be empty")
+ }
+
+ indexName := buildMetadataIndexName(tenantID)
+
+ exists, err := e.indexExists(ctx, indexName)
+ if err != nil {
+ common.Warn("Elasticsearch SearchMetadata index existence check failed", zap.String("index", indexName), zap.Error(err))
+ return nil, fmt.Errorf("failed to check metadata index existence: %w", err)
+ }
+ if !exists {
+ common.Debug("Elasticsearch SearchMetadata index absent, returning empty result", zap.String("index", indexName))
+ return &types.SearchMetadataResult{
+ MetadataRecords: []map[string]interface{}{},
+ Total: 0,
+ }, nil
+ }
+
+ offset := req.Offset
+ if offset < 0 {
+ offset = 0
+ }
+ limit := req.Limit
+ if limit <= 0 {
+ limit = 30
+ }
+
+ // Build query for metadata using buildMetadataQueryFromCondition
+ queryBody := make(map[string]interface{})
+ queryBody["query"] = e.buildMetadataQueryFromCondition(req.Filter)
+ if queryBody["query"] == nil {
+ queryBody["query"] = map[string]interface{}{"match_all": map[string]interface{}{}}
+ }
+
+ // Add sorting if order_by specified
+ if req.OrderBy != nil && len(req.OrderBy.Fields) > 0 {
+ sort := parseOrderByExpr(req.OrderBy)
+ if len(sort) > 0 {
+ queryBody["sort"] = sort
+ }
+ }
+
+ // Add _source for field filtering if specified
+ if len(req.SelectFields) > 0 {
+ queryBody["_source"] = req.SelectFields
+ }
+
+ // Apply offset/limit
+ queryBody["size"] = limit
+ queryBody["from"] = offset
+
+ // Serialize query
+ var buf bytes.Buffer
+ if err := json.NewEncoder(&buf).Encode(queryBody); err != nil {
+ return nil, fmt.Errorf("error encoding query: %w", err)
+ }
+
+ common.Info("Elasticsearch SearchMetadata", zap.String("indexName", indexName), zap.Any("dsl", queryBody))
+
+ // Execute search
+ res, err := e.client.Search(
+ e.client.Search.WithContext(ctx),
+ e.client.Search.WithIndex(indexName),
+ e.client.Search.WithBody(&buf),
+ e.client.Search.WithTrackTotalHits(true),
+ )
+ if err != nil {
+ common.Warn("Elasticsearch SearchMetadata query failed", zap.String("index", indexName), zap.Error(err))
+ return nil, fmt.Errorf("search failed: %w", err)
+ }
+ defer res.Body.Close()
+
+ if res.IsError() {
+ bodyBytes, _ := io.ReadAll(res.Body)
+ common.Warn("Elasticsearch SearchMetadata error response", zap.String("index", indexName), zap.String("body", string(bodyBytes)))
+ return nil, fmt.Errorf("search returned error: %s", res.Status())
+ }
+
+ var esResp SearchResponse
+ if err := json.NewDecoder(res.Body).Decode(&esResp); err != nil {
+ common.Warn("Elasticsearch SearchMetadata failed to parse response", zap.String("index", indexName), zap.Error(err))
+ return nil, fmt.Errorf("failed to parse response: %w", err)
+ }
+
+ searchChunks := convertESResponse(&esResp, "")
+ totalHits := esResp.Hits.Total.Value
+
+ common.Debug("SearchMetadata in Elasticsearch completed", zap.Int("returnedRows", len(searchChunks)), zap.Int64("totalHits", totalHits))
+
+ return &types.SearchMetadataResult{
+ MetadataRecords: searchChunks,
+ Total: totalHits,
+ }, nil
+}
+
+// buildMetadataQueryFromCondition builds an ES query for metadata index
+func (e *elasticsearchEngine) buildMetadataQueryFromCondition(condition map[string]interface{}) map[string]interface{} {
+ if len(condition) == 0 {
+ return nil
+ }
+
+ var clauses []map[string]interface{}
+
+ for k, v := range condition {
+ if v == nil {
+ continue
+ }
+
+ switch k {
+ case "kb_id":
+ if listVal, ok := v.([]interface{}); ok {
+ clauses = append(clauses, map[string]interface{}{
+ "terms": map[string]interface{}{k: listVal},
+ })
+ } else if strListVal, ok := v.([]string); ok {
+ clauses = append(clauses, map[string]interface{}{
+ "terms": map[string]interface{}{k: strListVal},
+ })
+ } else {
+ clauses = append(clauses, map[string]interface{}{
+ "term": map[string]interface{}{k: v},
+ })
+ }
+ case "id":
+ if listVal, ok := v.([]interface{}); ok {
+ clauses = append(clauses, map[string]interface{}{
+ "terms": map[string]interface{}{k: listVal},
+ })
+ } else if strListVal, ok := v.([]string); ok {
+ clauses = append(clauses, map[string]interface{}{
+ "terms": map[string]interface{}{k: strListVal},
+ })
+ } else {
+ clauses = append(clauses, map[string]interface{}{
+ "term": map[string]interface{}{k: v},
+ })
+ }
+ default:
+ clauses = append(clauses, map[string]interface{}{
+ "term": map[string]interface{}{k: v},
+ })
+ }
+ }
+
+ if len(clauses) == 0 {
+ return nil
+ }
+
+ if len(clauses) == 1 {
+ return clauses[0]
+ }
+
+ return map[string]interface{}{
+ "bool": map[string]interface{}{
+ "filter": clauses,
+ },
+ }
+}
+
+// metaPushdownMaxSize caps how many doc IDs the metadata push-down is
+// willing to return in one shot. Matches the Python reference
+// (DocMetadataService.filter_doc_ids_by_meta_pushdown, default limit=10000)
+// and ES's default index.max_result_window.
+//
+// When the underlying query matches more than this, the push-down
+// returns nil and the caller falls back to the in-memory meta_filter,
+// which is correct (just slower for very large result sets). Returning
+// a truncated slice as a definitive answer would silently drop docs.
+const metaPushdownMaxSize = 10000
+
+// FilterDocIdsByMetaPushdown runs a metadata filter directly against the ES doc metadata index.
+//
+// Return value convention (matching Python's filter_doc_ids_by_meta_pushdown):
+//
+// nil -> push-down was not viable / errored / result overflowed the
+// push-down cap (caller should fall back to in-memory)
+// []string{} -> push-down succeeded but found 0 matching docs (empty result is definitive)
+func (e *elasticsearchEngine) FilterDocIdsByMetaPushdown(ctx context.Context, kbIDs []string, conditions []map[string]interface{}, logic string) []string {
+ if len(conditions) == 0 || len(kbIDs) == 0 {
+ return nil
+ }
+
+ // Check if push-down is supported
+ if !IsPushdownSupported(conditions) {
+ common.Debug("FilterDocIdsByMetaPushdown: push-down not supported for some filters")
+ return nil
+ }
+
+ // Execute search for each tenant (use first KB to get tenant)
+ kb := kbIDs[0]
+ tenantID, err := dao.GetTenantIDByKBID(kb)
+ if err != nil {
+ common.Warn("FilterDocIdsByMetaPushdown: failed to get tenant for KB", zap.String("kbID", kb), zap.Error(err))
+ return nil
+ }
+ indexName := buildMetadataIndexName(tenantID)
+
+ // Check if index exists
+ exists, err := e.indexExists(ctx, indexName)
+ if err != nil || !exists {
+ return nil
+ }
+
+ // Build the filter query using the full meta_filter logic
+ queryBody, err := BuildMetaFilterQuery(conditions, logic, kbIDs)
+ if err != nil {
+ common.Debug("FilterDocIdsByMetaPushdown: build query failed", zap.String("error", err.Error()))
+ return nil
+ }
+
+ // Use the push-down cap. track_total_hits=true makes hits.total.value
+ // exact (ES otherwise caps the tracked total at 10,000 with relation=gte,
+ // which would let overflow slip through undetected).
+ requestBody := make(map[string]interface{})
+ for k, v := range queryBody {
+ requestBody[k] = v
+ }
+ requestBody["size"] = metaPushdownMaxSize
+ requestBody["track_total_hits"] = true
+ requestBody["_source"] = []string{"id"}
+
+ var buf bytes.Buffer
+ if err := json.NewEncoder(&buf).Encode(requestBody); err != nil {
+ return nil
+ }
+
+ common.Debug("FilterDocIdsByMetaPushdown executing ES query", zap.Any("query", requestBody))
+
+ res, err := e.client.Search(
+ e.client.Search.WithContext(ctx),
+ e.client.Search.WithIndex(indexName),
+ e.client.Search.WithBody(&buf),
+ )
+ if err != nil || res.IsError() {
+ return nil
+ }
+ defer res.Body.Close()
+
+ var result map[string]interface{}
+ if err := json.NewDecoder(res.Body).Decode(&result); err != nil {
+ return nil
+ }
+
+ // Extract doc IDs before the cap check so we can use uniqueDocIDs to
+ // detect silent truncation when the ES total isn't available.
+ docIDs := ExtractDocIDs(result)
+ uniqueDocIDs := dedupeStrings(docIDs)
+
+ // Detect silent truncation: the push-down is a fast path, not the
+ // system of record. If the query matched more than metaPushdownMaxSize
+ // docs, the slice we can build here is necessarily a strict subset of
+ // the truth, and the caller treats non-nil as definitive. Surface a
+ // warning (parity with the Python reference) AND bail out so the
+ // caller falls back to the in-memory meta_filter, which is correct.
+ total, totalOK := totalHitsFromESResponse(result)
+ switch {
+ case !totalOK && len(uniqueDocIDs) >= metaPushdownMaxSize:
+ // ES didn't report a verifiable total but we filled the cap. The
+ // result is possibly truncated and we cannot prove completeness,
+ // so fall back rather than hand the caller a possibly-incomplete
+ // definitive answer.
+ common.Warn("FilterDocIdsByMetaPushdown: ES total is unavailable at cap, falling back to in-memory",
+ zap.Int("uniqueDocCount", len(uniqueDocIDs)),
+ zap.Int("cap", metaPushdownMaxSize),
+ zap.Strings("kbIDs", kbIDs),
+ )
+ return nil
+ case totalOK && total > int64(metaPushdownMaxSize):
+ common.Warn("FilterDocIdsByMetaPushdown: result exceeds push-down cap, falling back to in-memory",
+ zap.Int64("total", total),
+ zap.Int("cap", metaPushdownMaxSize),
+ zap.Strings("kbIDs", kbIDs),
+ )
+ return nil
+ }
+
+ return docIDs
+}
+
+// dedupeStrings returns the unique values of s, preserving first-seen order.
+func dedupeStrings(s []string) []string {
+ if len(s) == 0 {
+ return s
+ }
+ seen := make(map[string]struct{}, len(s))
+ out := make([]string, 0, len(s))
+ for _, v := range s {
+ if _, ok := seen[v]; ok {
+ continue
+ }
+ seen[v] = struct{}{}
+ out = append(out, v)
+ }
+ return out
+}
+
+// totalHitsFromESResponse extracts the exact total hit count from an ES
+// search response. Returns ok=false when the field is missing or in an
+// unexpected shape; callers should treat that as "cannot verify" rather
+// than "no overflow".
+func totalHitsFromESResponse(esResponse map[string]interface{}) (int64, bool) {
+ hitsRoot, ok := esResponse["hits"].(map[string]interface{})
+ if !ok {
+ return 0, false
+ }
+ totalRoot, ok := hitsRoot["total"].(map[string]interface{})
+ if !ok {
+ return 0, false
+ }
+ // ES encodes numeric JSON values as float64 in Go's default decoder.
+ if v, ok := totalRoot["value"].(float64); ok {
+ return int64(v), true
+ }
+ // Some clients / older versions return int64 directly.
+ if v, ok := totalRoot["value"].(int64); ok {
+ return v, true
+ }
+ if v, ok := totalRoot["value"].(json.Number); ok {
+ n, err := v.Int64()
+ if err == nil {
+ return n, true
+ }
+ }
+ return 0, false
+}
diff --git a/internal/engine/engine.go b/internal/engine/engine.go
index ee97e377bc..6cd02d4cd3 100644
--- a/internal/engine/engine.go
+++ b/internal/engine/engine.go
@@ -50,6 +50,7 @@ type DocEngine interface {
DeleteMetadataKeys(ctx context.Context, docID string, datasetID string, keys []string, tenantID string) error
DropMetadataStore(ctx context.Context, tenantID string) error
MetadataStoreExists(ctx context.Context, tenantID string) (bool, error)
+ SearchMetadata(ctx context.Context, req *types.SearchMetadataRequest) (*types.SearchMetadataResult, error)
// Document operations (used by skill indexing)
IndexDocument(ctx context.Context, indexName, docID string, doc interface{}) error
@@ -60,7 +61,9 @@ type DocEngine interface {
GetFields(chunks []map[string]interface{}, fields []string) map[string]map[string]interface{}
GetAggregation(chunks []map[string]interface{}, fieldName string) []map[string]interface{}
GetHighlight(chunks []map[string]interface{}, keywords []string, fieldName string) map[string]string
- GetDocIDs(chunks []map[string]interface{}) []string
+ GetChunkIDs(chunks []map[string]interface{}) []string
+ KNNScores(ctx context.Context, chunks []map[string]interface{}, queryVector []float64, topK int) (map[string]interface{}, error)
+ GetScores(searchResult map[string]interface{}) map[string]float64
// Health check
Ping(ctx context.Context) error
@@ -68,6 +71,12 @@ type DocEngine interface {
// GetType returns the engine type
GetType() string
+
+ // FilterDocIdsByMetaPushdown runs a metadata filter directly against
+ // the doc metadata index, returning matching doc IDs or nil if push-down
+ // is not supported (caller should fall back to in-memory filtering).
+ // conditions is a list of filter objects with keys: key, op, value
+ FilterDocIdsByMetaPushdown(ctx context.Context, kbIDs []string, conditions []map[string]interface{}, logic string) []string
}
// Type returns the engine type (helper method for runtime type checking)
diff --git a/internal/engine/infinity/chunk.go b/internal/engine/infinity/chunk.go
index 5aa469ea09..ad37d51a0e 100644
--- a/internal/engine/infinity/chunk.go
+++ b/internal/engine/infinity/chunk.go
@@ -30,12 +30,14 @@ import (
"sort"
"strconv"
"strings"
- "unicode"
infinity "github.com/infiniflow/infinity-go-sdk"
"go.uber.org/zap"
)
+// ChinesePunctRegex splits on comma, semicolon, Chinese punctuations, and newlines
+var ChinesePunctRegex = regexp.MustCompile(`[,,;;、\r\n]+`)
+
// CreateChunkStore creates a chunk table in Infinity
// baseName is the table name prefix (e.g., "ragflow_<tenant_id>")
// The full table name is built as "{baseName}_{datasetID}"
@@ -47,7 +49,7 @@ func (e *infinityEngine) CreateChunkStore(ctx context.Context, baseName, dataset
var tableName string
var mappingFile string
- tableName = buildChunkTableName(baseName, datasetID)
+ tableName = buildChunkTableName(baseName, datasetID)
if datasetID == "skill" {
mappingFile = "skill_infinity_mapping.json"
common.Info("Creating skill index table", zap.String("tableName", tableName), zap.String("mappingFile", mappingFile))
@@ -574,32 +576,7 @@ func (e *infinityEngine) DeleteChunks(ctx context.Context, condition map[string]
// It supports three matching types: MatchTextExpr (full-text), MatchDenseExpr (vector), and FusionExpr (combined).
// If no match expressions are provided, Search relies solely on filter (e.g., doc_id, available_int) to find results.
func (e *infinityEngine) Search(ctx context.Context, req *types.SearchRequest) (*types.SearchResult, error) {
- common.Debug("Search in Infinity started", zap.Any("indexNames", req.IndexNames))
- if common.IsDebugEnabled() {
- // Format match expressions for logging
- var matchExprsStr string
- for i, expr := range req.MatchExprs {
- switch e := expr.(type) {
- case *types.MatchTextExpr:
- matchExprsStr += fmt.Sprintf(" [%d] MatchTextExpr: fields=%v, matchingText=%s, topN=%d, extraOptions=%v\n", i, e.Fields, e.MatchingText, e.TopN, e.ExtraOptions)
- case *types.MatchDenseExpr:
- matchExprsStr += fmt.Sprintf(" [%d] MatchDenseExpr: vectorColumn=%s, vectorSize=%d, topN=%d, extraOptions=%v\n", i, e.VectorColumnName, len(e.EmbeddingData), e.TopN, e.ExtraOptions)
- case *types.FusionExpr:
- matchExprsStr += fmt.Sprintf(" [%d] FusionExpr: method=%s, topN=%d, fusionParams=%v\n", i, e.Method, e.TopN, e.FusionParams)
- default:
- matchExprsStr += fmt.Sprintf(" [%d] unknown type\n", i)
- }
- }
- common.Debug(fmt.Sprintf("Search request:\n"+
- " indexNames=%v\n"+
- " KbIDs=%v\n"+
- " offset=%d, limit=%d\n"+
- " SelectFields=%v\n"+
- " Filter=%v\n"+
- " MatchExprs:\n%s orderBy=%v\n"+
- " RankFeature=%v",
- req.IndexNames, req.KbIDs, req.Offset, req.Limit, req.SelectFields, req.Filter, matchExprsStr, req.OrderBy, req.RankFeature))
- }
+ types.LogSearchRequest("Infinity", req)
if len(req.IndexNames) == 0 {
return nil, fmt.Errorf("index names cannot be empty")
@@ -621,13 +598,8 @@ func (e *infinityEngine) Search(ctx context.Context, req *types.SearchRequest) (
return nil, fmt.Errorf("failed to get database: %w", err)
}
- isMetadataTable := false
isSkillIndex := false
for _, idx := range req.IndexNames {
- if strings.HasPrefix(idx, "ragflow_doc_meta_") {
- isMetadataTable = true
- break
- }
if strings.HasPrefix(idx, "skill_") {
isSkillIndex = true
break
@@ -635,9 +607,7 @@ func (e *infinityEngine) Search(ctx context.Context, req *types.SearchRequest) (
}
var outputColumns []string
- if isMetadataTable {
- outputColumns = []string{"id", "kb_id", "meta_fields"}
- } else if isSkillIndex {
+ if isSkillIndex {
outputColumns = []string{
"skill_id", "space_id", "folder_id", "name", "tags", "description", "content",
"version", "status", "create_time", "update_time",
@@ -716,23 +686,31 @@ func (e *infinityEngine) Search(ctx context.Context, req *types.SearchRequest) (
outputColumns = append(outputColumns, "row_id()")
}
+ // Strip score pseudo-columns when there's no match expression — Infinity
+ // rejects SCORE()/SCORE_FACTORS() without MATCH TEXT/TENSOR/Fusion with
+ // "InfinityException(3013)". This protects callers (e.g. the no-match
+ // fallback in retrieval.go) that reuse a SelectFields list containing
+ // "_score" across both matched and unmatched queries.
+ if !hasTextMatch && !hasVectorMatch {
+ filtered := outputColumns[:0]
+ for _, c := range outputColumns {
+ switch c {
+ case "_score", "SCORE", "score()", "similarity()":
+ continue
+ }
+ filtered = append(filtered, c)
+ }
+ outputColumns = filtered
+ }
+
outputColumns = convertSelectFields(outputColumns, isSkillIndex)
if hasVectorMatch && matchDense != nil && matchDense.VectorColumnName != "" {
outputColumns = append(outputColumns, matchDense.VectorColumnName)
}
var filterParts []string
- if isMetadataTable && len(req.KbIDs) > 0 && req.KbIDs[0] != "" {
- kbIDs := req.KbIDs
- if len(kbIDs) == 1 {
- filterParts = append(filterParts, fmt.Sprintf("kb_id = '%s'", kbIDs[0]))
- } else {
- kbIDStr := strings.Join(kbIDs, "', '")
- filterParts = append(filterParts, fmt.Sprintf("kb_id IN ('%s')", kbIDStr))
- }
- }
- if !isMetadataTable && (hasTextMatch || hasVectorMatch) {
+ if hasTextMatch || hasVectorMatch {
if req.Filter != nil {
if availInt, ok := req.Filter["available_int"]; ok {
filterParts = append(filterParts, fmt.Sprintf("available_int=%v", availInt))
@@ -756,13 +734,10 @@ func (e *infinityEngine) Search(ctx context.Context, req *types.SearchRequest) (
// Build filter string from req.Filter
if req.Filter != nil {
- filterCopy := req.Filter
- if !isMetadataTable {
- filterCopy = make(map[string]interface{})
- for k, v := range req.Filter {
- if k != "kb_id" {
- filterCopy[k] = v
- }
+ filterCopy := make(map[string]interface{})
+ for k, v := range req.Filter {
+ if k != "kb_id" {
+ filterCopy[k] = v
}
}
@@ -807,8 +782,13 @@ func (e *infinityEngine) Search(ctx context.Context, req *types.SearchRequest) (
}
}
- minMatch := 0.3
-
+ // minMatch comes from matchText.ExtraOptions when set (Python parity).
+ // Mirrors rag/utils/infinity_conn.py which reads
+ // matchExpr.extra_options.get("minimum_should_match", 0.0) — for the
+ // English (non-Chinese) path, the Go Question() builder omits
+ // minimum_should_match, so the default is 0.0 to match Python's
+ // effective 0% threshold for English queries.
+ minMatch := 0.0
var questionText string
var vectorData []float64
textTopN := pageSize
@@ -820,6 +800,19 @@ func (e *infinityEngine) Search(ctx context.Context, req *types.SearchRequest) (
if oq, ok := matchText.ExtraOptions["original_query"].(string); ok {
originalQuery = oq
}
+ if v, ok := matchText.ExtraOptions["minimum_should_match"]; ok {
+ switch x := v.(type) {
+ case float64:
+ minMatch = x
+ case int:
+ minMatch = float64(x)
+ case string:
+ s := strings.TrimSuffix(x, "%")
+ if pct, err := strconv.Atoi(s); err == nil {
+ minMatch = float64(pct) / 100
+ }
+ }
+ }
}
}
if matchDense != nil {
@@ -933,13 +926,21 @@ func (e *infinityEngine) Search(ctx context.Context, req *types.SearchRequest) (
}
}
- if hasTextMatch && fusionExpr == nil {
+ if hasTextMatch {
fieldsStr := strings.Join(convertedFields, ",")
filterFulltext := fmt.Sprintf("filter_fulltext('%s', '%s')", fieldsStr, questionText)
denseFilterStr = fmt.Sprintf("(%s) AND %s", denseFilterStr, filterFulltext)
}
+ threshold := "0.0"
+ if matchDense != nil && matchDense.ExtraOptions != nil {
+ if sim, ok := matchDense.ExtraOptions["similarity"].(float64); ok {
+ threshold = fmt.Sprintf("%g", sim)
+ } else if s, ok := matchDense.ExtraOptions["threshold"].(string); ok {
+ threshold = s
+ }
+ }
extraOptions := map[string]string{
- "threshold": utility.FloatToString(0.0),
+ "threshold": threshold,
"filter": denseFilterStr,
}
@@ -1031,7 +1032,7 @@ func (e *infinityEngine) Search(ctx context.Context, req *types.SearchRequest) (
// Skill index uses different schema
// so we skip the document-specific field mappings
if !isSkillIndex {
- GetFields(searchChunks, nil)
+ applyFieldMappings(searchChunks)
} else {
// For skill index, only handle ROW_ID -> row_id() mapping
for _, chunk := range searchChunks {
@@ -1229,23 +1230,10 @@ func (e *infinityEngine) GetChunk(ctx context.Context, tableName, chunkID string
return chunk, nil
}
-// GetFields applies field mappings to chunks and returns a dict keyed by chunk ID.
-// Equivalent to Python's get_fields() in infinity_conn.py.
-// When fields is nil/empty, returns all fields from chunks.
-func GetFields(chunks []map[string]interface{}, fields []string) map[string]map[string]interface{} {
- result := make(map[string]map[string]interface{})
- if len(chunks) == 0 {
- return result
- }
-
- // If fields is provided, create a set for lookup
- fieldSet := make(map[string]bool)
- for _, f := range fields {
- fieldSet[f] = true
- }
-
+// applyFieldMappings applies field mappings to chunks (side-effect only).
+// Used by Search() to mutate chunks with derived fields before returning.
+func applyFieldMappings(chunks []map[string]interface{}) {
for _, chunk := range chunks {
- // Apply field mappings
// docnm -> docnm_kwd, title_tks, title_sm_tks
if val, ok := chunk["docnm"].(string); ok {
chunk["docnm_kwd"] = val
@@ -1253,12 +1241,12 @@ func GetFields(chunks []map[string]interface{}, fields []string) map[string]map[
chunk["title_sm_tks"] = val
}
- // important_keywords -> important_kwd (split by comma), important_tks
+ // important_keywords -> important_kwd (split by comma/semicolon/Chinese punctuations), important_tks
if val, ok := chunk["important_keywords"].(string); ok {
if val == "" {
chunk["important_kwd"] = []interface{}{}
} else {
- parts := strings.Split(val, ",")
+ parts := ChinesePunctRegex.Split(val, -1)
chunk["important_kwd"] = parts
}
chunk["important_tks"] = val
@@ -1313,7 +1301,7 @@ func GetFields(chunks []map[string]interface{}, fields []string) map[string]map[
"important_kwd": true, "question_kwd": true,
}
arrayFields := []string{
- "doc_type_kwd", "important_kwd", "important_tks", "question_tks",
+ "important_kwd", "important_tks", "question_tks",
"question_kwd", "authors_tks", "authors_sm_tks", "title_tks",
"title_sm_tks", "content_ltks", "content_sm_ltks", "tag_kwd",
}
@@ -1341,27 +1329,302 @@ func GetFields(chunks []map[string]interface{}, fields []string) map[string]map[
chunk["row_id()"] = val
delete(chunk, "ROW_ID")
}
+ }
+}
+
+// GetFields extracts the requested fields from Infinity search results
+func (e *infinityEngine) GetFields(chunks []map[string]interface{}, fields []string) map[string]map[string]interface{} {
+ result := make(map[string]map[string]interface{})
+
+ // Python: if not fields, return {}
+ if len(fields) == 0 {
+ return result
+ }
+
+ if len(chunks) == 0 {
+ return result
+ }
+
+ // Build field set for lookup (Python lines 713-715)
+ fieldsAll := make(map[string]bool)
+ for _, f := range fields {
+ fieldsAll[f] = true
+ }
+ fieldsAll["id"] = true
+
+ // noneColumns is rebuilt per chunk inside the loop below. The
+ // per-chunk "missing → nil" map MUST be fresh for every iteration; if
+ // it's reused, the first chunk that contains a field removes it from
+ // the shared set, and later chunks missing that same field silently
+ // stop getting the nil placeholder, producing inconsistent shapes
+ // per document.
+
+ // Check if important_kwd is needed (for empty_count handling)
+ needImportantKwdEmptyCount := fieldsAll["important_kwd"]
+
+ for _, chunk := range chunks {
+ // Build column map for case-insensitive lookup (Python line 747)
+ columnMap := make(map[string]string)
+ for k := range chunk {
+ columnMap[strings.ToLower(k)] = k
+ }
+
+ // Apply field mappings first (to get derived fields)
+ // docnm -> docnm_kwd, title_tks, title_sm_tks (Python lines 716-719)
+ // Note: Python checks "docnm" in res.columns regardless of whether fields were requested
+ if val, ok := chunk["docnm"].(string); ok {
+ if fieldsAll["docnm_kwd"] {
+ chunk["docnm_kwd"] = val
+ }
+ if fieldsAll["title_tks"] {
+ chunk["title_tks"] = val
+ }
+ if fieldsAll["title_sm_tks"] {
+ chunk["title_sm_tks"] = val
+ }
+ }
+
+ // important_keywords -> important_kwd (split by comma), important_tks (Python lines 720-732)
+ // Python: v.split(",") if v else [] — empty string yields empty list
+ if fieldsAll["important_kwd"] || fieldsAll["important_tks"] {
+ if val, ok := chunk["important_keywords"].(string); ok && val != "" {
+ if fieldsAll["important_kwd"] {
+ if needImportantKwdEmptyCount {
+ // Check for important_kwd_empty_count (Python lines 722-728)
+ if emptyCountVal, hasEmptyCount := chunk["important_kwd_empty_count"]; hasEmptyCount {
+ tokens := strings.Split(val, ",")
+ var emptyCount int
+ switch v := emptyCountVal.(type) {
+ case float64:
+ emptyCount = int(v)
+ case int:
+ emptyCount = v
+ case string:
+ emptyCount, _ = strconv.Atoi(v)
+ }
+ kwdList := make([]interface{}, 0, len(tokens)+emptyCount)
+ for _, t := range tokens {
+ kwdList = append(kwdList, t)
+ }
+ for i := 0; i < emptyCount; i++ {
+ kwdList = append(kwdList, "")
+ }
+ chunk["important_kwd"] = kwdList
+ } else {
+ parts := strings.Split(val, ",")
+ kwdList := make([]interface{}, len(parts))
+ for i, p := range parts {
+ kwdList[i] = p
+ }
+ chunk["important_kwd"] = kwdList
+ }
+ } else {
+ parts := strings.Split(val, ",")
+ kwdList := make([]interface{}, len(parts))
+ for i, p := range parts {
+ kwdList[i] = p
+ }
+ chunk["important_kwd"] = kwdList
+ }
+ }
+ if fieldsAll["important_tks"] {
+ chunk["important_tks"] = val
+ }
+ } else {
+ if fieldsAll["important_kwd"] {
+ chunk["important_kwd"] = []interface{}{}
+ }
+ if fieldsAll["important_tks"] {
+ chunk["important_tks"] = []interface{}{}
+ }
+ }
+ }
+
+ // questions -> question_kwd (split by newline), question_tks (Python lines 733-737)
+ // Python: v.splitlines() — empty string yields empty list
+ if fieldsAll["question_kwd"] || fieldsAll["question_tks"] {
+ if val, ok := chunk["questions"].(string); ok && val != "" {
+ if fieldsAll["question_kwd"] {
+ parts := strings.Split(val, "\n")
+ qList := make([]interface{}, len(parts))
+ for i, p := range parts {
+ qList[i] = p
+ }
+ chunk["question_kwd"] = qList
+ }
+ if fieldsAll["question_tks"] {
+ chunk["question_tks"] = val
+ }
+ } else {
+ if fieldsAll["question_kwd"] {
+ chunk["question_kwd"] = []interface{}{}
+ }
+ if fieldsAll["question_tks"] {
+ chunk["question_tks"] = []interface{}{}
+ }
+ }
+ }
+
+ // content -> content_with_weight, content_ltks, content_sm_ltks (Python lines 738-741)
+ if fieldsAll["content_with_weight"] || fieldsAll["content_ltks"] || fieldsAll["content_sm_ltks"] {
+ if val, ok := chunk["content"].(string); ok {
+ if fieldsAll["content_with_weight"] {
+ chunk["content_with_weight"] = val
+ }
+ if fieldsAll["content_ltks"] {
+ chunk["content_ltks"] = val
+ }
+ if fieldsAll["content_sm_ltks"] {
+ chunk["content_sm_ltks"] = val
+ }
+ }
+ }
+
+ // authors -> authors_tks, authors_sm_tks (Python lines 742-745)
+ if fieldsAll["authors_tks"] || fieldsAll["authors_sm_tks"] {
+ if val, ok := chunk["authors"].(string); ok {
+ if fieldsAll["authors_tks"] {
+ chunk["authors_tks"] = val
+ }
+ if fieldsAll["authors_sm_tks"] {
+ chunk["authors_sm_tks"] = val
+ }
+ }
+ }
+
+ // Post-process fields matching Python lines 758-780
+ // This single loop processes all column transformations in Python order
+ kwdNoSplit := map[string]bool{
+ "knowledge_graph_kwd": true, "docnm_kwd": true,
+ "important_kwd": true, "question_kwd": true,
+ }
+ for field, val := range chunk {
+ fieldLower := strings.ToLower(field)
+
+ // field_keyword: split by "###" (Python lines 760-761)
+ needsSplit := false
+ if fieldLower == "source_id" {
+ needsSplit = true
+ } else if strings.HasSuffix(fieldLower, "_kwd") && !kwdNoSplit[fieldLower] {
+ needsSplit = true
+ }
+ if needsSplit {
+ if strVal, ok := val.(string); ok && strings.Contains(strVal, "###") {
+ parts := strings.Split(strVal, "###")
+ var filtered []interface{}
+ for _, p := range parts {
+ if p != "" {
+ filtered = append(filtered, p)
+ }
+ }
+ chunk[field] = filtered
+ }
+ continue
+ }
+
+ // _feas: JSON parse (Python lines 762-763)
+ if strings.HasSuffix(fieldLower, "_feas") {
+ if strVal, ok := val.(string); ok && strVal != "" {
+ var parsed interface{}
+ if err := json.Unmarshal([]byte(strVal), &parsed); err == nil {
+ chunk[field] = parsed
+ }
+ } else {
+ chunk[field] = map[string]interface{}{}
+ }
+ continue
+ }
+
+ // chunk_data: JSON parse (Python lines 764-766)
+ if fieldLower == "chunk_data" {
+ if strVal, ok := val.(string); ok && strVal != "" {
+ var parsed interface{}
+ if err := json.Unmarshal([]byte(strVal), &parsed); err == nil {
+ chunk[field] = parsed
+ }
+ } else if val == nil {
+ // Keep nil
+ }
+ continue
+ }
+
+ // position_int: hex decode with grouping by 5 (Python lines 767-776)
+ if fieldLower == "position_int" && fieldsAll[fieldLower] {
+ // If already converted to slice by applyFieldMappings, skip
+ if _, isSlice := val.([]interface{}); isSlice {
+ continue
+ }
+ // applyFieldMappings returns [][]int, check that too
+ if _, isIntSlice := val.([][]int); isIntSlice {
+ continue
+ }
+ if strVal, ok := val.(string); ok && strVal != "" {
+ chunk[field] = utility.ConvertHexToPositionIntArray(strVal)
+ } else {
+ chunk[field] = []interface{}{}
+ }
+ continue
+ }
+
+ // page_num_int, top_int: hex decode (Python lines 777-778)
+ if (fieldLower == "page_num_int" || fieldLower == "top_int") && fieldsAll[fieldLower] {
+ // If already converted to slice by applyFieldMappings, skip
+ if _, isSlice := val.([]interface{}); isSlice {
+ continue
+ }
+ // applyFieldMappings returns []int, check that too
+ if _, isIntSlice := val.([]int); isIntSlice {
+ continue
+ }
+ if strVal, ok := val.(string); ok && strVal != "" {
+ chunk[field] = utility.ConvertHexToIntArray(strVal)
+ } else {
+ chunk[field] = []interface{}{}
+ }
+ continue
+ }
+ }
+
+ // Handle row_id mapping (Python lines 748-750)
+ if fieldsAll["row_id()"] {
+ if lowerKey, ok := columnMap["row_id"]; ok {
+ chunk["row_id()"] = chunk[lowerKey]
+ }
+ }
+
+ // Delete base columns after mapping (Python lines 781-783)
+ for _, col := range []string{"docnm", "important_keywords", "questions", "content", "authors"} {
+ delete(chunk, col)
+ }
// Build result map keyed by id
- if id, ok := chunk["id"].(string); ok {
+ if idVal, ok := chunk["id"].(string); ok {
fieldMap := make(map[string]interface{})
+ // Rebuild noneColumns for this chunk so that fields missing
+ // from THIS chunk get a nil placeholder. Reusing a set across
+ // chunks would let the first chunk's contents permanently
+ // remove keys, leaving later chunks with inconsistent shapes.
+ noneColumns := make(map[string]bool, len(fieldsAll))
+ for f := range fieldsAll {
+ noneColumns[strings.ToLower(f)] = true
+ }
for field, value := range chunk {
- if len(fieldSet) == 0 || fieldSet[field] {
+ if fieldsAll[field] {
fieldMap[field] = value
+ delete(noneColumns, strings.ToLower(field))
}
}
- result[id] = fieldMap
+ // Set none_columns to None (Python lines 784-785)
+ for col := range noneColumns {
+ fieldMap[col] = nil
+ }
+ result[idVal] = fieldMap
}
}
return result
}
-// GetFields is a method wrapper for infinityEngine to satisfy DocEngine interface
-func (e *infinityEngine) GetFields(chunks []map[string]interface{}, fields []string) map[string]map[string]interface{} {
- return GetFields(chunks, fields)
-}
-
// GetAggregation aggregates chunk values by field name.
// Input: [{"docnm_kwd": "docA"}, {"docnm_kwd": "docA"}, {"docnm_kwd": "docB"}]
//
@@ -1466,12 +1729,8 @@ func (e *infinityEngine) GetAggregation(chunks []map[string]interface{}, fieldNa
return result
}
-// GetDocIDs extracts document IDs from search results.
-// Extracts "id" field from each chunk and returns as a list.
-func (e *infinityEngine) GetDocIDs(chunks []map[string]interface{}) []string {
- if len(chunks) == 0 {
- return nil
- }
+// GetChunkIDs extracts chunk IDs from Infinity search results.
+func (e *infinityEngine) GetChunkIDs(chunks []map[string]interface{}) []string {
ids := make([]string, 0, len(chunks))
for _, chunk := range chunks {
if id, ok := chunk["id"].(string); ok {
@@ -1489,95 +1748,81 @@ func (e *infinityEngine) GetHighlight(chunks []map[string]interface{}, keywords
return result
}
- // Check if field exists
- hasField := false
- for _, chunk := range chunks {
- if _, ok := chunk[fieldName]; ok {
- hasField = true
- break
- }
+ // For Infinity, scores are already returned in search results (_score column)
+ // So GetScores just extracts scores from chunks, mimicking Python's approach
+ return result
+}
+
+// KNNScores for Infinity - since Infinity normalizes scores during fusion,
+// we just need to return a result structure that GetScores can parse.
+// This matches Python's approach where Infinity doesn't use the two-pass KNN.
+func (e *infinityEngine) KNNScores(ctx context.Context, chunks []map[string]interface{}, queryVector []float64, topK int) (map[string]interface{}, error) {
+ if len(chunks) == 0 {
+ return nil, nil
}
- if !hasField {
- // Try alternative field names
- if fieldName == "content_with_weight" {
- if _, ok := chunks[0]["content"]; ok {
- fieldName = "content"
- hasField = true
+
+ // Build a result structure that GetScores can parse
+ // For Infinity, scores are already in _score field from the first search
+ result := make(map[string]interface{})
+ hitList := make([]interface{}, 0, len(chunks))
+ for _, chunk := range chunks {
+ if id, ok := chunk["id"].(string); ok {
+ hit := map[string]interface{}{
+ "_id": id,
+ "_score": chunk["_score"],
}
+ hitList = append(hitList, hit)
}
}
- if !hasField {
- return result
+ result["hits"] = map[string]interface{}{
+ "hits": hitList,
}
+ return result, nil
+}
- emTag := regexp.MustCompile(`<em>[^<>]+</em>`)
-
- for _, chunk := range chunks {
- id := ""
- if idVal, ok := chunk["id"].(string); ok {
- id = idVal
- }
-
- txt, ok := chunk[fieldName].(string)
- if !ok || txt == "" {
+// GetScores extracts similarity scores from KNN search result.
+// For Infinity, it parses the result from KNNScores and extracts _score values.
+func (e *infinityEngine) GetScores(knnResult map[string]interface{}) map[string]float64 {
+ scores := make(map[string]float64)
+ hits, ok := knnResult["hits"].(map[string]interface{})
+ if !ok {
+ return scores
+ }
+ hitList, ok := hits["hits"].([]interface{})
+ if !ok {
+ return scores
+ }
+ for _, h := range hitList {
+ hit, ok := h.(map[string]interface{})
+ if !ok {
continue
}
-
- // Check if already highlighted
- if emTag.MatchString(txt) {
- result[id] = txt
+ docID, ok := hit["_id"].(string)
+ if !ok {
continue
}
-
- // Replace newlines with spaces
- txt = regexp.MustCompile(`[\r\n]`).ReplaceAllString(txt, " ")
-
- // Split by sentence delimiters
- delimiters := regexp.MustCompile(`[.?!;\n]`)
- segments := delimiters.Split(txt, -1)
-
- var highlightedSegments []string
- for _, segment := range segments {
- // Check if segment is English or contains keywords
- englishCount := 0
- totalCount := 0
- for _, r := range segment {
- if unicode.IsLetter(r) {
- totalCount++
- if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') {
- englishCount++
- }
- }
- }
- isEnglish := totalCount > 0 && float64(englishCount)/float64(totalCount) > 0.5
- segmentToCheck := segment
- if isEnglish {
- // For English: match whole words with boundaries
- for _, kw := range keywords {
- re := regexp.MustCompile(`(^|[ .?/'\"\(\)!,:;-])` + regexp.QuoteMeta(kw) + `([ .?/'\"\(\)!,:;-]|$)`)
- segmentToCheck = re.ReplaceAllString(segmentToCheck, "$1<em>"+kw+"</em>$2")
- }
- } else {
- // For non-English: simple substring match
- for _, kw := range keywords {
- segmentToCheck = strings.ReplaceAll(segmentToCheck, kw, "<em>"+kw+"</em>")
- }
- }
- if strings.Contains(segmentToCheck, "<em>") {
- highlightedSegments = append(highlightedSegments, segmentToCheck)
- }
+ scoreVal := hit["_score"]
+ if scoreVal == nil {
+ scores[docID] = 0.0
+ continue
}
-
- if len(highlightedSegments) > 0 {
- result[id] = strings.Join(highlightedSegments, "...")
+ score, ok := scoreVal.(float64)
+ if !ok {
+ scores[docID] = 0.0
+ continue
}
+ scores[docID] = score
}
-
- return result
+ return scores
}
// convertSelectFields converts field names to Infinity format
// isSkillIndex indicates if this is a skill index (uses skill_id instead of id)
+//
+// Does NOT mutate the input slice — callers (e.g. retrieval.go) reuse the same
+// SelectFields list both for Search() and GetFields(); mutating it would
+// replace logical names like "content_with_weight" with their Infinity column
+// names ("content"), breaking GetFields's field-presence checks.
func convertSelectFields(output []string, isSkillIndex ...bool) []string {
fieldMapping := map[string]string{
"docnm_kwd": "docnm",
@@ -1599,20 +1844,24 @@ func convertSelectFields(output []string, isSkillIndex ...bool) []string {
skillIndex = isSkillIndex[0]
}
+ // Copy + map without mutating the caller's slice.
+ mapped := make([]string, len(output))
needEmptyCount := false
for i, field := range output {
if field == "important_kwd" {
needEmptyCount = true
}
if newField, ok := fieldMapping[field]; ok {
- output[i] = newField
+ mapped[i] = newField
+ } else {
+ mapped[i] = field
}
}
// Remove duplicates
seen := make(map[string]bool)
result := []string{}
- for _, f := range output {
+ for _, f := range mapped {
if f != "" && !seen[f] {
seen[f] = true
result = append(result, f)
@@ -2036,4 +2285,4 @@ func (e *infinityEngine) DropChunkStore(ctx context.Context, baseName, datasetID
// ChunkStoreExists checks if a chunk table exists in Infinity
func (e *infinityEngine) ChunkStoreExists(ctx context.Context, baseName, datasetID string) (bool, error) {
return e.tableExists(ctx, buildChunkTableName(baseName, datasetID))
-}
\ No newline at end of file
+}
diff --git a/internal/engine/infinity/common.go b/internal/engine/infinity/common.go
index ec4b4e4bbc..720e554bbd 100644
--- a/internal/engine/infinity/common.go
+++ b/internal/engine/infinity/common.go
@@ -243,6 +243,17 @@ func buildFilterFromCondition(condition map[string]interface{}, tableColumns map
}
continue
}
+ if strListVal, ok := v.([]string); ok {
+ var inVals []string
+ for _, item := range strListVal {
+ item = strings.ReplaceAll(item, "'", "''")
+ inVals = append(inVals, fmt.Sprintf("'%s'", item))
+ }
+ if len(inVals) > 0 {
+ conditions = append(conditions, fmt.Sprintf("%s IN (%s)", k, strings.Join(inVals, ", ")))
+ }
+ continue
+ }
// Handle exists condition
if k == "exists" {
diff --git a/internal/engine/infinity/meta_filter.go b/internal/engine/infinity/meta_filter.go
new file mode 100644
index 0000000000..8ace5fc8ed
--- /dev/null
+++ b/internal/engine/infinity/meta_filter.go
@@ -0,0 +1,582 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package infinity
+
+import (
+ "fmt"
+ "regexp"
+ "strconv"
+ "strings"
+
+ infinity "github.com/infiniflow/infinity-go-sdk"
+)
+
+// Key pattern for validation
+var keyPattern = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]*$`)
+
+// Supported operators
+var supportedOperators = map[string]bool{
+ "=": true,
+ "≠": true,
+ ">": true,
+ "<": true,
+ "≥": true,
+ "≤": true,
+ "in": true,
+ "not in": true,
+ "contains": true,
+ "not contains": true,
+ "start with": true,
+ "end with": true,
+ "empty": true,
+ "not empty": true,
+}
+
+// Range operators mapping
+var rangeOps = map[string]string{
+ ">": ">",
+ "<": "<",
+ "≥": ">=",
+ "≤": "<=",
+}
+
+// MetaFilterTranslator translates filter clauses to Infinity SQL
+type MetaFilterTranslator struct{}
+
+// NewMetaFilterTranslator creates a new translator
+func NewMetaFilterTranslator() *MetaFilterTranslator {
+ return &MetaFilterTranslator{}
+}
+
+// Translate translates a single filter dict into Infinity SQL filter string
+func (t *MetaFilterTranslator) Translate(flt map[string]interface{}) (string, error) {
+ op, _ := flt["op"].(string)
+ key, _ := flt["key"].(string)
+ value := flt["value"]
+
+ if key == "" {
+ return "", fmt.Errorf("filter is missing a string key")
+ }
+ if !keyPattern.MatchString(key) {
+ return "", fmt.Errorf("invalid key format (must be identifier-like)")
+ }
+ if !supportedOperators[op] {
+ return "", fmt.Errorf("unknown operator %q", op)
+ }
+
+ switch op {
+ case "empty":
+ return t.translateEmpty(key), nil
+ case "not empty":
+ return t.translateNotEmpty(key), nil
+ case "=":
+ return t.translateEqual(key, value, flt), nil
+ case "≠":
+ return t.translateNotEqual(key, value, flt), nil
+ case ">", "<", "≥", "≤":
+ return t.translateRange(key, op, value, flt), nil
+ case "in":
+ return t.translateIn(key, value, flt), nil
+ case "not in":
+ return t.translateNotIn(key, value, flt), nil
+ case "contains":
+ return t.translateContains(key, value, flt)
+ case "not contains":
+ return t.translateNotContains(key, value, flt), nil
+ case "start with":
+ return t.translateStartWith(key, value, flt), nil
+ case "end with":
+ return t.translateEndWith(key, value, flt), nil
+ }
+
+ return "", fmt.Errorf("no handler for operator %q", op)
+}
+
+func (t *MetaFilterTranslator) translateEmpty(key string) string {
+ return fmt.Sprintf("JSON_EXTRACT_STRING(meta_fields, '$.%s') = '\"\"'", key)
+}
+
+func (t *MetaFilterTranslator) translateNotEmpty(key string) string {
+ return fmt.Sprintf("JSON_EXTRACT_STRING(meta_fields, '$.%s') != '\"\"'", key)
+}
+
+func (t *MetaFilterTranslator) translateEqual(key string, value interface{}, flt map[string]interface{}) string {
+ coerced := coerceScalar(value, flt)
+ if s, ok := coerced.(string); ok {
+ escaped := escapeSQLString(s)
+ return fmt.Sprintf("JSON_CONTAINS(meta_fields, '$.%s', '\"%s\"')", key, escaped)
+ }
+ return fmt.Sprintf("JSON_CONTAINS(meta_fields, '$.%s', %v)", key, coerced)
+}
+
+func (t *MetaFilterTranslator) translateNotEqual(key string, value interface{}, flt map[string]interface{}) string {
+ coerced := coerceScalar(value, flt)
+ if s, ok := coerced.(string); ok {
+ escaped := escapeSQLString(s)
+ return fmt.Sprintf("NOT JSON_CONTAINS(meta_fields, '$.%s', '\"%s\"')", key, escaped)
+ }
+ return fmt.Sprintf("NOT JSON_CONTAINS(meta_fields, '$.%s', %v)", key, coerced)
+}
+
+func (t *MetaFilterTranslator) translateRange(key string, op string, value interface{}, flt map[string]interface{}) string {
+ coerced := coerceRangeValue(value, flt)
+ sqlOp := rangeOps[op]
+ if s, ok := coerced.(string); ok {
+ escaped := escapeSQLString(s)
+ return fmt.Sprintf("JSON_EXTRACT_STRING(meta_fields, '$.%s') %s '%s'", key, sqlOp, escaped)
+ }
+ return fmt.Sprintf("JSON_EXTRACT_DOUBLE(meta_fields, '$.%s') %s %v", key, sqlOp, coerced)
+}
+
+func (t *MetaFilterTranslator) translateIn(key string, value interface{}, flt map[string]interface{}) string {
+ members := csvOrList(value, flt)
+
+ var stringParts, numParts []string
+ for _, m := range members {
+ coerced := coerceRangeValue(m, flt)
+ if num, ok := coerceToFloat(coerced); ok {
+ numParts = append(numParts, fmt.Sprintf("JSON_CONTAINS(meta_fields, '$.%s', %v)", key, num))
+ } else if s, ok := coerced.(string); ok {
+ escaped := escapeSQLString(s)
+ stringParts = append(stringParts, fmt.Sprintf("JSON_CONTAINS(meta_fields, '$.%s', '\"%s\"')", key, escaped))
+ }
+ }
+
+ var conditions []string
+ if len(stringParts) > 0 {
+ conditions = append(conditions, "("+strings.Join(stringParts, " OR ")+")")
+ }
+ if len(numParts) > 0 {
+ conditions = append(conditions, "("+strings.Join(numParts, " OR ")+")")
+ }
+
+ return "(" + strings.Join(conditions, " OR ") + ")"
+}
+
+func (t *MetaFilterTranslator) translateNotIn(key string, value interface{}, flt map[string]interface{}) string {
+ members := csvOrList(value, flt)
+
+ var stringParts, numParts []string
+ for _, m := range members {
+ coerced := coerceRangeValue(m, flt)
+ if num, ok := coerceToFloat(coerced); ok {
+ numParts = append(numParts, fmt.Sprintf("NOT JSON_CONTAINS(meta_fields, '$.%s', %v)", key, num))
+ } else if s, ok := coerced.(string); ok {
+ escaped := escapeSQLString(s)
+ stringParts = append(stringParts, fmt.Sprintf("NOT JSON_CONTAINS(meta_fields, '$.%s', '\"%s\"')", key, escaped))
+ }
+ }
+
+ var conditions []string
+ if len(stringParts) > 0 {
+ conditions = append(conditions, "("+strings.Join(stringParts, " AND ")+")")
+ }
+ if len(numParts) > 0 {
+ conditions = append(conditions, "("+strings.Join(numParts, " AND ")+")")
+ }
+
+ return strings.Join(conditions, " AND ")
+}
+
+func (t *MetaFilterTranslator) translateContains(key string, value interface{}, flt map[string]interface{}) (string, error) {
+ // Python guard: if not value and value != 0 -> raise ValueError.
+ // Returning "" here would let the empty fragment slip into the
+ // joined SQL (e.g. "( AND other_condition)"), so we surface the
+ // error instead and let the caller decide how to respond.
+ //
+ // isEmptyValue mirrors Python's `not value` truthiness check so
+ // nil, "", empty slices, and empty maps are all caught — a plain
+ // fmt.Sprintf("%v", ...) == "" test misses those last two.
+ if isEmptyValue(value) && !isNumericZero(value) {
+ return "", fmt.Errorf("contains value is empty: %v", flt)
+ }
+ coerced := coerceRangeValue(value, flt)
+ if num, ok := coerceToFloat(coerced); ok {
+ return fmt.Sprintf("JSON_CONTAINS(meta_fields, '$.%s', %v)", key, num), nil
+ }
+ escaped := escapeSQLString(fmt.Sprintf("%v", value))
+ return fmt.Sprintf("JSON_CONTAINS(meta_fields, '$.%s', '\"%s\"')", key, escaped), nil
+}
+
+func (t *MetaFilterTranslator) translateNotContains(key string, value interface{}, flt map[string]interface{}) string {
+ text := coerceString(value, flt)
+ escaped := escapeSQLString(text)
+ return fmt.Sprintf("NOT JSON_CONTAINS(meta_fields, '$.%s', '\"%s\"')", key, escaped)
+}
+
+func (t *MetaFilterTranslator) translateStartWith(key string, value interface{}, flt map[string]interface{}) string {
+ text := coerceString(value, flt)
+ escaped := escapeSQLString(escapeLikeWildcards(text))
+ return fmt.Sprintf("JSON_EXTRACT_STRING(meta_fields, '$.%s') LIKE '%s%%'", key, escaped)
+}
+
+func (t *MetaFilterTranslator) translateEndWith(key string, value interface{}, flt map[string]interface{}) string {
+ text := coerceString(value, flt)
+ escaped := escapeSQLString(escapeLikeWildcards(text))
+ return fmt.Sprintf("JSON_EXTRACT_STRING(meta_fields, '$.%s') LIKE '%%%s'", key, escaped)
+}
+
+// PlanPushdown translates every filter
+func PlanPushdown(filters []map[string]interface{}, logic string) ([]string, error) {
+ if logic != "and" && logic != "or" {
+ return nil, fmt.Errorf("unknown logic %q", logic)
+ }
+
+ translator := NewMetaFilterTranslator()
+ var result []string
+ for _, flt := range filters {
+ translated, err := translator.Translate(flt)
+ if err != nil {
+ return nil, err
+ }
+ result = append(result, translated)
+ }
+ return result, nil
+}
+
+// BuildInfinityFilter builds the full WHERE clause
+func BuildInfinityFilter(filters []map[string]interface{}, logic string) (string, error) {
+ if len(filters) == 0 {
+ return "1=1", nil
+ }
+
+ fragments, err := PlanPushdown(filters, logic)
+ if err != nil {
+ return "", err
+ }
+
+ joiner := " AND "
+ if logic == "or" {
+ joiner = " OR "
+ }
+
+ return "(" + strings.Join(fragments, joiner) + ")", nil
+}
+
+// IsPushdownSupported checks if all filters can be pushed down
+func IsPushdownSupported(filters []map[string]interface{}) bool {
+ for _, flt := range filters {
+ op, _ := flt["op"].(string)
+ if !supportedOperators[op] {
+ return false
+ }
+ key, _ := flt["key"].(string)
+ if key == "" || !keyPattern.MatchString(key) {
+ return false
+ }
+ }
+ return true
+}
+
+// ExtractDocIDs extracts doc IDs from Infinity result
+func ExtractDocIDs(result interface{}) []string {
+ var docIDs []string
+
+ // Try to handle different result types from Infinity SDK
+ switch v := result.(type) {
+ case map[string]interface{}:
+ if idData, ok := v["id"].([]interface{}); ok {
+ for _, id := range idData {
+ if idStr, ok := id.(string); ok {
+ docIDs = append(docIDs, idStr)
+ }
+ }
+ }
+ case *infinity.QueryResult:
+ if v == nil {
+ break
+ }
+ if data, ok := v.Data["id"]; ok {
+ for _, id := range data {
+ if idStr, ok := id.(string); ok {
+ docIDs = append(docIDs, idStr)
+ }
+ }
+ }
+ }
+
+ return docIDs
+}
+
+// coerceScalar handles scalar comparison values.
+// Mirrors Python's ast.literal_eval: tries int first, then float, then string.
+func coerceScalar(value interface{}, flt map[string]interface{}) interface{} {
+ if value == nil {
+ return nil
+ }
+
+ s := strings.TrimSpace(fmt.Sprintf("%v", value))
+
+ // Try to parse as int first (Python ast.literal_eval preserves int vs float)
+ if parsed, err := strconv.ParseInt(s, 10, 64); err == nil {
+ return parsed
+ }
+ // Then try float
+ if parsed, err := strconv.ParseFloat(s, 64); err == nil {
+ return parsed
+ }
+
+ return s
+}
+
+// coerceRangeValue handles range comparison values.
+// Mirrors Python: tries int first, then float, then string.
+func coerceRangeValue(value interface{}, flt map[string]interface{}) interface{} {
+ if value == nil {
+ return nil
+ }
+
+ s := strings.TrimSpace(fmt.Sprintf("%v", value))
+
+ // Try to parse as int first
+ if parsed, err := strconv.ParseInt(s, 10, 64); err == nil {
+ return parsed
+ }
+ // Then try float
+ if parsed, err := strconv.ParseFloat(s, 64); err == nil {
+ return parsed
+ }
+
+ return s
+}
+
+// coerceString ensures value is a non-empty string
+func coerceString(value interface{}, flt map[string]interface{}) string {
+ if value == nil {
+ return ""
+ }
+ s := fmt.Sprintf("%v", value)
+ if s == "" {
+ return ""
+ }
+ return s
+}
+
+// csvOrList handles in/not in values
+func csvOrList(value interface{}, flt map[string]interface{}) []interface{} {
+ if value == nil {
+ return nil
+ }
+
+ var members []interface{}
+
+ switch v := value.(type) {
+ case []interface{}:
+ members = v
+ case string:
+ trimmed := strings.TrimSpace(v)
+ if strings.HasPrefix(trimmed, "[") && strings.HasSuffix(trimmed, "]") {
+ parsed := parseJSONArray(trimmed)
+ if parsed != nil {
+ members = parsed
+ }
+ } else {
+ parts := strings.Split(v, ",")
+ for _, p := range parts {
+ trimmed := strings.TrimSpace(p)
+ if trimmed != "" {
+ members = append(members, strings.ToLower(trimmed))
+ }
+ }
+ }
+ default:
+ members = []interface{}{v}
+ }
+
+ if len(members) == 0 {
+ return nil
+ }
+
+ result := make([]interface{}, len(members))
+ for i, m := range members {
+ if s, ok := m.(string); ok {
+ result[i] = strings.ToLower(strings.TrimSpace(s))
+ } else {
+ result[i] = m
+ }
+ }
+
+ return result
+}
+
+// escapeSQLString escapes SQL string
+func escapeSQLString(s string) string {
+ return strings.ReplaceAll(s, "'", "''")
+}
+
+// escapeLikeWildcards escapes LIKE wildcards
+func escapeLikeWildcards(text string) string {
+ text = strings.ReplaceAll(text, "\\", "\\\\")
+ text = strings.ReplaceAll(text, "%", "\\%")
+ text = strings.ReplaceAll(text, "_", "\\_")
+ return text
+}
+
+// coerceToFloat tries to convert interface{} to float64
+func coerceToFloat(value interface{}) (float64, bool) {
+ switch v := value.(type) {
+ case float64:
+ return v, true
+ case float32:
+ return float64(v), true
+ case int:
+ return float64(v), true
+ case int64:
+ return float64(v), true
+ case string:
+ if f, err := strconv.ParseFloat(v, 64); err == nil {
+ return f, true
+ }
+ }
+ return 0, false
+}
+
+// isNumericZero checks if value is numeric zero (0, 0.0, etc.)
+func isNumericZero(value interface{}) bool {
+ switch v := value.(type) {
+ case int:
+ return v == 0
+ case int64:
+ return v == 0
+ case float64:
+ return v == 0
+ case float32:
+ return v == 0
+ default:
+ return false
+ }
+}
+
+// isEmptyValue mirrors Python's `not value` truthiness for the small
+// set of types we receive in filter dicts. nil, empty strings, and
+// zero-length slices/maps are all considered "empty" — calling
+// fmt.Sprintf("%v", ...) on an empty slice or map produces "[]" or
+// "map[]", neither of which is the empty string, so a stringly-typed
+// guard would miss them.
+func isEmptyValue(value interface{}) bool {
+ if value == nil {
+ return true
+ }
+ switch v := value.(type) {
+ case string:
+ return v == ""
+ case []string:
+ return len(v) == 0
+ case []interface{}:
+ return len(v) == 0
+ case map[string]interface{}:
+ return len(v) == 0
+ }
+ return false
+}
+
+// parseJSONArray parses a simple JSON array string
+func parseJSONArray(s string) []interface{} {
+ s = strings.TrimSpace(s)
+ if len(s) < 2 {
+ return nil
+ }
+ s = s[1 : len(s)-1]
+
+ var result []interface{}
+ parts := splitJSONParts(s)
+ for _, p := range parts {
+ p = strings.TrimSpace(p)
+ if p == "" {
+ continue
+ }
+ if len(p) >= 2 {
+ if (p[0] == '"' && p[len(p)-1] == '"') || (p[0] == '\'' && p[len(p)-1] == '\'') {
+ p = p[1 : len(p)-1]
+ }
+ }
+ result = append(result, p)
+ }
+ return result
+}
+
+// splitJSONParts splits JSON array parts. It tracks the actual quote
+// character that opened the current quoted region so a double-quoted
+// string isn't terminated by a stray single quote inside it (e.g. an
+// apostrophe) and a single-quoted string isn't split by a comma inside
+// a double-quoted neighbour. Naive `inQuote = !inQuote` was wrong on
+// both counts.
+//
+// Quotes preceded by an odd number of backslashes are treated as
+// escaped literals (JSON `\"` / `\'`) so the comma inside a string like
+// `"a\"b,c"` doesn't trigger a spurious split.
+func splitJSONParts(s string) []string {
+ var parts []string
+ var current strings.Builder
+ var quoteChar rune
+ depth := 0
+
+ runes := []rune(s)
+ for i := 0; i < len(runes); i++ {
+ c := runes[i]
+ if c == '\'' || c == '"' {
+ // Inside a quoted string, count the consecutive backslashes
+ // immediately before this rune. An odd count means this
+ // quote is escaped (e.g. JSON `\"`); an even count (incl. 0)
+ // means it really does toggle the quote state.
+ if quoteChar != 0 {
+ bs := 0
+ for j := i - 1; j >= 0 && runes[j] == '\\'; j-- {
+ bs++
+ }
+ if bs%2 == 1 {
+ current.WriteRune(c)
+ continue
+ }
+ }
+ if quoteChar == 0 {
+ quoteChar = c
+ } else if c == quoteChar {
+ quoteChar = 0
+ }
+ current.WriteRune(c)
+ continue
+ }
+ switch c {
+ case '[', '{':
+ if quoteChar == 0 {
+ depth++
+ }
+ current.WriteRune(c)
+ case ']', '}':
+ if quoteChar == 0 {
+ depth--
+ }
+ current.WriteRune(c)
+ case ',':
+ if quoteChar == 0 && depth == 0 {
+ parts = append(parts, current.String())
+ current.Reset()
+ } else {
+ current.WriteRune(c)
+ }
+ default:
+ current.WriteRune(c)
+ }
+ }
+
+ if current.Len() > 0 {
+ parts = append(parts, current.String())
+ }
+
+ return parts
+}
\ No newline at end of file
diff --git a/internal/engine/infinity/meta_filter_test.go b/internal/engine/infinity/meta_filter_test.go
new file mode 100644
index 0000000000..7726bca4a5
--- /dev/null
+++ b/internal/engine/infinity/meta_filter_test.go
@@ -0,0 +1,290 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package infinity
+
+import (
+ "reflect"
+ "testing"
+)
+
+// TestSplitJSONParts pins down the quote-aware split. The previous
+// implementation only tracked an `inQuote` bool toggled on `'`, so
+// double-quoted strings containing commas were split incorrectly and
+// single quotes inside double-quoted strings (e.g. apostrophes) were
+// also mis-handled.
+func TestSplitJSONParts(t *testing.T) {
+ tests := []struct {
+ name string
+ in string
+ want []string
+ }{
+ {
+ name: "empty",
+ in: "",
+ want: nil,
+ },
+ {
+ name: "single_quoted_basic",
+ in: `'a', 'b', 'c'`,
+ want: []string{`'a'`, ` 'b'`, ` 'c'`},
+ },
+ {
+ name: "double_quoted_with_commas_inside",
+ in: `"a,b", "c"`,
+ want: []string{`"a,b"`, ` "c"`},
+ },
+ {
+ name: "apostrophe_inside_double_quoted",
+ // "don't", "we, can't" — the apostrophes and the comma in
+ // "we, can't" must not break the double-quoted regions.
+ in: `"don't", "we, can't"`,
+ want: []string{`"don't"`, ` "we, can't"`},
+ },
+ {
+ name: "nested_brackets_inside_quotes_do_not_count",
+ in: `"{a,b}", [1, 2]`,
+ want: []string{`"{a,b}"`, ` [1, 2]`},
+ },
+ {
+ name: "comma_inside_brackets_outside_quotes_splits",
+ in: `[1, 2], [3, 4]`,
+ want: []string{`[1, 2]`, ` [3, 4]`},
+ },
+ {
+ name: "trailing_comma_drops_empty_part",
+ in: `"a", "b",`,
+ want: []string{`"a"`, ` "b"`},
+ },
+ {
+ // Regression: previously the parser toggled quote state on
+ // every '"', so the comma inside the first element split
+ // the array into three pieces and corrupted in/not in filters.
+ name: "escaped_double_quote_does_not_split",
+ in: `"a\"b,c", "d"`,
+ want: []string{`"a\"b,c"`, ` "d"`},
+ },
+ {
+ // `\\\"` = escaped backslash + escaped quote. The backslash
+ // is a literal, the quote is escaped, so no split.
+ name: "escaped_backslash_then_escaped_quote",
+ in: `"a\\\"b", "c"`,
+ want: []string{`"a\\\"b"`, ` "c"`},
+ },
+ {
+ // Symmetric handling for single-quoted regions.
+ name: "escaped_single_quote_does_not_split",
+ in: `'a\'b,c', 'd'`,
+ want: []string{`'a\'b,c'`, ` 'd'`},
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got := splitJSONParts(tt.in)
+ if !reflect.DeepEqual(got, tt.want) {
+ t.Errorf("splitJSONParts(%q):\n got %#v\n want %#v", tt.in, got, tt.want)
+ }
+ })
+ }
+}
+
+// TestParseJSONArrayDoubleQuotedWithCommas is the regression case from
+// the bug report: ["a,b", "c"] must yield two elements, not three.
+func TestParseJSONArrayDoubleQuotedWithCommas(t *testing.T) {
+ got := parseJSONArray(`["a,b", "c"]`)
+ want := []interface{}{"a,b", "c"}
+ if !reflect.DeepEqual(got, want) {
+ t.Errorf("parseJSONArray([\"a,b\", \"c\"]):\n got %#v\n want %#v", got, want)
+ }
+}
+
+// TestTranslateContainsEmptyValueErrors pins down the empty-value path
+// in translateContains. The Python reference raises ValueError for
+// `if not value and value != 0`; the Go port must mirror that instead
+// of returning "" (which would join into malformed SQL like
+// "( AND other_condition)" once BuildInfinityFilter is called).
+func TestTranslateContainsEmptyValueErrors(t *testing.T) {
+ tr := NewMetaFilterTranslator()
+ cases := []struct {
+ name string
+ flt map[string]interface{}
+ }{
+ {
+ name: "nil_value",
+ flt: map[string]interface{}{"op": "contains", "key": "author", "value": nil},
+ },
+ {
+ name: "empty_string",
+ flt: map[string]interface{}{"op": "contains", "key": "author", "value": ""},
+ },
+ {
+ name: "empty_slice",
+ flt: map[string]interface{}{"op": "contains", "key": "author", "value": []string{}},
+ },
+ {
+ name: "empty_map",
+ flt: map[string]interface{}{"op": "contains", "key": "author", "value": map[string]interface{}{}},
+ },
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ got, err := tr.Translate(tc.flt)
+ if err == nil {
+ t.Fatalf("expected error for empty value, got %q", got)
+ }
+ if got != "" {
+ t.Errorf("expected empty SQL on error, got %q", got)
+ }
+ })
+ }
+}
+
+// TestTranslateContainsNumericZeroStaysGuarded confirms the
+// "value == 0 is not empty" branch inherited from the Python
+// reference: numeric 0 is a real value to search for, not an
+// empty guard rail.
+func TestTranslateContainsNumericZeroStaysGuarded(t *testing.T) {
+ tr := NewMetaFilterTranslator()
+ flt := map[string]interface{}{"op": "contains", "key": "year", "value": 0}
+ got, err := tr.Translate(flt)
+ if err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+ want := `JSON_CONTAINS(meta_fields, '$.year', 0)`
+ if got != want {
+ t.Errorf("got %q, want %q", got, want)
+ }
+}
+
+// TestBuildInfinityFilterRejectsEmptyContains guards the downstream
+// effect: an empty-value contains must propagate as an error out of
+// BuildInfinityFilter, not silently produce "( AND ...)" SQL.
+func TestBuildInfinityFilterRejectsEmptyContains(t *testing.T) {
+ filters := []map[string]interface{}{
+ {"op": "contains", "key": "author", "value": ""},
+ {"op": "=", "key": "year", "value": 2026},
+ }
+ _, err := BuildInfinityFilter(filters, "and")
+ if err == nil {
+ t.Fatal("expected error from BuildInfinityFilter, got nil")
+ }
+}
+
+// TestTotalHitsFromInfinityExtraInfo pins down the parser that decodes
+// the JSON payload Infinity returns in QueryResult.ExtraInfo when the
+// total_hits_count option is set. The shape isn't part of the public
+// SDK contract, so we accept several spellings; getting the
+// (total > cap) trigger wrong would let the push-down silently drop
+// docs and the caller would never fall back to the in-memory path.
+func TestTotalHitsFromInfinityExtraInfo(t *testing.T) {
+ tests := []struct {
+ name string
+ extra string
+ wantVal int64
+ wantOk bool
+ }{
+ {
+ name: "empty",
+ extra: "",
+ wantVal: 0,
+ wantOk: false,
+ },
+ {
+ name: "invalid_json",
+ extra: "not json",
+ wantVal: 0,
+ wantOk: false,
+ },
+ {
+ name: "total_hits_count_under_cap",
+ extra: `{"total_hits_count": 42}`,
+ wantVal: 42,
+ wantOk: true,
+ },
+ {
+ name: "total_hits_count_at_cap",
+ extra: `{"total_hits_count": 10000}`,
+ wantVal: 10000,
+ wantOk: true,
+ },
+ {
+ name: "total_hits_count_over_cap_triggers_fallback",
+ extra: `{"total_hits_count": 50000}`,
+ wantVal: 50000,
+ wantOk: true,
+ },
+ {
+ name: "camelCase_alias",
+ extra: `{"totalHitsCount": 7}`,
+ wantVal: 7,
+ wantOk: true,
+ },
+ {
+ name: "short_alias",
+ extra: `{"total": 9}`,
+ wantVal: 9,
+ wantOk: true,
+ },
+ {
+ name: "no_recognized_key",
+ extra: `{"unrelated": 1}`,
+ wantVal: 0,
+ wantOk: false,
+ },
+ {
+ name: "negative_value_rejected",
+ extra: `{"total_hits_count": -1}`,
+ wantVal: 0,
+ wantOk: false,
+ },
+ {
+ name: "non_integer_value_rejected",
+ extra: `{"total_hits_count": "many"}`,
+ wantVal: 0,
+ wantOk: false,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ gotVal, gotOk := totalHitsFromInfinityExtraInfo(tt.extra)
+ if gotOk != tt.wantOk {
+ t.Errorf("ok mismatch: got %v want %v (extra=%q)", gotOk, tt.wantOk, tt.extra)
+ }
+ if gotVal != tt.wantVal {
+ t.Errorf("value mismatch: got %d want %d (extra=%q)", gotVal, tt.wantVal, tt.extra)
+ }
+ })
+ }
+}
+
+// TestInfinityPushdownCapSemantics documents the overflow contract for
+// the Infinity path: the query is built with .Limit(metaPushdownMaxSize)
+// and .Option({total_hits_count: true}); when the parsed total exceeds
+// the cap, FilterDocIdsByMetaPushdown must return nil so the caller
+// falls back to the in-memory meta_filter rather than returning a
+// truncated slice as a definitive answer.
+func TestInfinityPushdownCapSemantics(t *testing.T) {
+ extra := `{"total_hits_count": 12345}`
+ total, ok := totalHitsFromInfinityExtraInfo(extra)
+ if !ok {
+ t.Fatal("expected total to be parseable from a well-formed Infinity ExtraInfo payload")
+ }
+ if total <= int64(metaPushdownMaxSize) {
+ t.Fatalf("expected total > cap, got total=%d cap=%d", total, metaPushdownMaxSize)
+ }
+}
diff --git a/internal/engine/infinity/metadata.go b/internal/engine/infinity/metadata.go
index 063c695df7..67d43a5dc6 100644
--- a/internal/engine/infinity/metadata.go
+++ b/internal/engine/infinity/metadata.go
@@ -26,6 +26,8 @@ import (
infinity "github.com/infiniflow/infinity-go-sdk"
"ragflow/internal/common"
+ "ragflow/internal/dao"
+ "ragflow/internal/engine/types"
"ragflow/internal/utility"
"go.uber.org/zap"
@@ -110,6 +112,17 @@ func (e *infinityEngine) CreateMetadataStore(ctx context.Context, tenantID strin
return fmt.Errorf("Failed to create secondary index on kb_id: %w", err)
}
+ // Create secondary index on meta_fields for metadata filter queries
+ _, err = table.CreateIndex(
+ fmt.Sprintf("idx_%s_meta_fields", tableName),
+ infinity.NewIndexInfo("meta_fields", infinity.IndexTypeSecondary, nil),
+ infinity.ConflictTypeIgnore,
+ "",
+ )
+ if err != nil {
+ return fmt.Errorf("Failed to create secondary index on meta_fields: %w", err)
+ }
+
return nil
}
@@ -188,8 +201,31 @@ func (e *infinityEngine) InsertMetadata(ctx context.Context, metadata []map[stri
}
// UpdateMetadata updates or inserts document metadata in tenant's metadata table.
-// If a row with the given docID and datasetID exists, it merges the new metadata with existing.
-// If no row exists, it inserts a new row.
+//
+// "Updates" here means MERGE, not replace. The supplied metaFields are
+// overlaid on top of the row's existing meta_fields map: keys already
+// present are overwritten with the new value, keys not in the input
+// are preserved, and brand-new keys are added. If no row exists for
+// (docID, datasetID), one is inserted containing exactly metaFields.
+//
+// Examples (existing row → input → resulting meta_fields):
+//
+// {character:["曹操","孙权"], year:2025}
+// + {author:["John","Tom"], category:"tech"}
+// = {character:["曹操","孙权"], year:2025, author:["John","Tom"], category:"tech"}
+//
+// {character:["曹操","孙权"], year:2025}
+// + {year:2025}
+// = {character:["曹操","孙权"], year:2025} // year value unchanged, character preserved
+//
+// (empty / row absent) + {author:"Tom"} = {author:"Tom"}
+//
+// Note: this is at odds with the SET-METADATA CLI's name, which a
+// reader naturally parses as "replace". The merge semantics exist so
+// that user-driven metadata edits compose with auto-extracted fields
+// produced by the LLM extraction pipeline. See the CLI parser in
+// internal/cli/user_parser.go (parseSetMeta) for the user-facing
+// surface that drives this engine method.
func (e *infinityEngine) UpdateMetadata(ctx context.Context, docID string, datasetID string, metaFields map[string]interface{}, tenantID string) error {
tableName := buildMetadataTableName(tenantID)
common.Info("InfinityConnection.UpdateMetadata called", zap.String("tableName", tableName), zap.String("docID", docID), zap.String("datasetID", datasetID))
@@ -499,4 +535,314 @@ func (e *infinityEngine) DropMetadataStore(ctx context.Context, tenantID string)
func (e *infinityEngine) MetadataStoreExists(ctx context.Context, tenantID string) (bool, error) {
tableName := buildMetadataTableName(tenantID)
return e.tableExists(ctx, tableName)
-}
\ No newline at end of file
+}
+
+// SearchMetadata executes search specifically for metadata tables
+// This is separate from Search() which handles only chunk tables
+func (e *infinityEngine) SearchMetadata(ctx context.Context, req *types.SearchMetadataRequest) (*types.SearchMetadataResult, error) {
+ tenantID := req.TenantID
+ common.Debug("SearchMetadata in Infinity started", zap.String("tenantID", tenantID))
+
+ // Validate inputs
+ if tenantID == "" {
+ return nil, fmt.Errorf("tenantID cannot be empty")
+ }
+
+ // Build table name from tenantID
+ tableName := buildMetadataTableName(tenantID)
+
+ exists, err := e.tableExists(ctx, tableName)
+ if err != nil {
+ common.Warn("Infinity SearchMetadata table existence check failed", zap.String("table", tableName), zap.Error(err))
+ return nil, fmt.Errorf("failed to check metadata table existence: %w", err)
+ }
+ if !exists {
+ common.Debug("Infinity SearchMetadata table absent, returning empty result", zap.String("table", tableName))
+ // Return an empty (non-nil) slice — Python returns `[]`, and a
+ // nil slice is read by callers as "fall back to in-memory". A
+ // zero-match against an absent table is a definitive answer,
+ // not a missing-data condition.
+ return &types.SearchMetadataResult{
+ MetadataRecords: []map[string]interface{}{},
+ Total: 0,
+ }, nil
+ }
+
+ // Build search request for metadata - simpler than chunk search, no match expressions
+ searchReq := &types.SearchRequest{
+ IndexNames: []string{tableName},
+ Offset: req.Offset,
+ Limit: req.Limit,
+ SelectFields: req.SelectFields,
+ Filter: req.Filter,
+ MatchExprs: nil, // No match expressions for metadata
+ OrderBy: req.OrderBy,
+ RankFeature: nil,
+ }
+
+ result, err := e.Search(ctx, searchReq)
+ if err != nil {
+ return nil, err
+ }
+ return &types.SearchMetadataResult{
+ MetadataRecords: result.Chunks,
+ Total: result.Total,
+ }, nil
+}
+
+// parseLengthPrefixedJSON parses Infinity's length-prefixed JSON format
+// (a sequence of [4-byte little-endian length][JSON] records) and returns
+// each parsed JSON object. This is the same on-the-wire format that the
+// service layer's ParseAllLengthPrefixedJSON understands; duplicated here
+// to keep the engine package free of service-layer dependencies.
+//
+// The format is what Infinity's SDK returns for VARCHAR/TEXT columns
+// when a query matches multiple rows: instead of giving us a list of
+// per-row byte arrays, it concatenates all rows' values into a single
+// blob, prefixing each with a 4-byte little-endian length.
+//
+// Returns nil if `data` is too short to be valid, or if no JSON
+// objects could be extracted.
+func parseLengthPrefixedJSON(data []byte) []map[string]interface{} {
+ if len(data) < 4 {
+ return nil
+ }
+ var results []map[string]interface{}
+ offset := 0
+ for offset+4 <= len(data) {
+ // Read 4-byte length (little-endian)
+ length := uint32(data[offset]) |
+ uint32(data[offset+1])<<8 |
+ uint32(data[offset+2])<<16 |
+ uint32(data[offset+3])<<24
+ if length == 0 || offset+4+int(length) > len(data) {
+ // Length invalid; bail out.
+ break
+ }
+ jsonStart := offset + 4
+ jsonEnd := jsonStart + int(length)
+ var result map[string]interface{}
+ if err := json.Unmarshal(data[jsonStart:jsonEnd], &result); err == nil {
+ results = append(results, result)
+ offset = jsonEnd
+ continue
+ }
+ break
+ }
+ return results
+}
+
+// realignMetaFieldsColumn fixes a column-oriented data-frame
+// misalignment that happens when Infinity's SDK returns the
+// `meta_fields` column for a multi-row query as a single
+// length-prefixed byte array instead of one entry per row. After the
+// column→row loop has run, the first matching chunk holds the entire
+// concatenated blob and the rest are missing the field. This function
+// splits the blob into per-row JSON objects and reattaches them in
+// order to the chunks that need them.
+//
+// Safe no-op when:
+// - there are no chunks
+// - the `meta_fields` column is already aligned (one byte array per
+// chunk), so a length-prefixed parse of any single value yields
+// exactly one object
+// - the byte array doesn't parse as length-prefixed JSON
+func realignMetaFieldsColumn(chunks []map[string]interface{}) {
+ if len(chunks) < 2 {
+ return
+ }
+ firstVal, ok := chunks[0]["meta_fields"]
+ if !ok {
+ return
+ }
+ firstBytes, ok := firstVal.([]byte)
+ if !ok {
+ return
+ }
+ parsed := parseLengthPrefixedJSON(firstBytes)
+ if len(parsed) != len(chunks) {
+ // Either the blob didn't parse as length-prefixed, or it
+ // parsed to a different count than the number of chunks we
+ // built. In either case, don't risk misattributing data.
+ return
+ }
+ for i, meta := range parsed {
+ chunks[i]["meta_fields"] = meta
+ }
+}
+
+// metaPushdownMaxSize caps how many doc IDs the metadata push-down is
+// willing to return in one shot. Matches the Python reference
+// (DocMetadataService.filter_doc_ids_by_meta_pushdown, default limit=10000)
+// and ES's default index.max_result_window.
+//
+// When the underlying query matches more than this, the push-down
+// returns nil and the caller falls back to the in-memory meta_filter,
+// which is correct (just slower for very large result sets). Returning
+// a truncated slice as a definitive answer would silently drop docs.
+const metaPushdownMaxSize = 10000
+
+// FilterDocIdsByMetaPushdown runs a metadata filter directly against the Infinity table.
+//
+// Return value convention (matching Python's filter_doc_ids_by_meta_pushdown):
+//
+// nil -> push-down was not viable / errored / result overflowed the
+// push-down cap (caller should fall back to in-memory)
+// []string{} -> push-down succeeded but found 0 matching docs (empty result is definitive)
+func (e *infinityEngine) FilterDocIdsByMetaPushdown(ctx context.Context, kbIDs []string, conditions []map[string]interface{}, logic string) []string {
+ if len(conditions) == 0 || len(kbIDs) == 0 {
+ return nil
+ }
+
+ // Check if push-down is supported
+ if !IsPushdownSupported(conditions) {
+ common.Debug("FilterDocIdsByMetaPushdown: push-down not supported for some filters")
+ return nil
+ }
+
+ // Get tenant ID from first KB
+ tenantID, err := dao.GetTenantIDByKBID(kbIDs[0])
+ if err != nil {
+ common.Warn("FilterDocIdsByMetaPushdown: failed to get tenant for KB", zap.String("kbID", kbIDs[0]), zap.Error(err))
+ return nil
+ }
+
+ tableName := buildMetadataTableName(tenantID)
+
+ // Build SQL WHERE clause using the full meta_filter logic
+ whereClause, err := BuildInfinityFilter(conditions, logic)
+ if err != nil {
+ common.Debug("FilterDocIdsByMetaPushdown: build filter failed", zap.String("error", err.Error()))
+ return nil
+ }
+
+ // Add KB filter using IN clause. Escape any single quotes in the IDs
+ // defensively — KB IDs are normally UUIDs, but malformed input must
+ // not be able to break out of the literal and alter the query.
+ quotedKBIDs := make([]string, len(kbIDs))
+ for i, kbID := range kbIDs {
+ quotedKBIDs[i] = "'" + strings.ReplaceAll(kbID, "'", "''") + "'"
+ }
+ kbFilter := "kb_id IN (" + strings.Join(quotedKBIDs, ", ") + ")"
+ // Wrap the translated predicate in parens so the AND with the KB clause
+ // doesn't get re-grouped by an internal OR. Without the parens,
+ // `kbFilter AND a OR b` parses as `(kbFilter AND a) OR b`, which can
+ // match rows in other KBs.
+ whereClause = kbFilter + " AND (" + whereClause + ")"
+
+ // Use Infinity connection to execute query
+ db, err := e.client.conn.GetDatabase(e.client.dbName)
+ if err != nil || db == nil {
+ return nil
+ }
+
+ table, err := db.GetTable(tableName)
+ if err != nil || table == nil {
+ return nil
+ }
+
+ // Execute query using chainable API: Output(...).Filter(...)
+ // .Limit(metaPushdownMaxSize) caps the page size, and
+ // .Option({total_hits_count: true}) makes the exact match count
+ // available in QueryResult.ExtraInfo so we can detect overflow and
+ // fall back to the in-memory meta_filter rather than silently
+ // returning a truncated slice (which the caller treats as definitive).
+ common.Debug("FilterDocIdsByMetaPushdown executing Infinity query", zap.String("whereClause", whereClause))
+ queryTable := table.Output([]string{"id"}).Filter(whereClause)
+ queryTable = queryTable.Limit(metaPushdownMaxSize)
+ queryTable = queryTable.Option(map[string]interface{}{"total_hits_count": true})
+ result, err := queryTable.ToResult()
+ if err != nil {
+ return nil
+ }
+
+ qr, ok := result.(*infinity.QueryResult)
+ if !ok || qr == nil {
+ return nil
+ }
+
+ // Detect overflow via the SDK's ExtraInfo payload (a JSON string set
+ // when total_hits_count is requested). If we can't parse it, log
+ // and fall through — the in-memory path is still correct, just
+ // slower.
+ if total, parsed := totalHitsFromInfinityExtraInfo(qr.ExtraInfo); parsed {
+ if total > int64(metaPushdownMaxSize) {
+ common.Warn("FilterDocIdsByMetaPushdown: result exceeds push-down cap, falling back to in-memory",
+ zap.Int64("total", total),
+ zap.Int("cap", metaPushdownMaxSize),
+ zap.Strings("kbIDs", kbIDs),
+ )
+ return nil
+ }
+ } else if qr.ExtraInfo != "" {
+ // ExtraInfo was non-empty but didn't carry total_hits_count in the
+ // expected shape — unusual, but worth flagging so we don't quietly
+ // lose the overflow signal if Infinity changes its payload.
+ common.Debug("FilterDocIdsByMetaPushdown: Infinity ExtraInfo present but total_hits_count missing",
+ zap.String("extraInfo", qr.ExtraInfo),
+ )
+ }
+
+ // Extract doc IDs from the result.
+ docIDs := make([]string, 0)
+ if idData, exists := qr.Data["id"]; exists {
+ for _, id := range idData {
+ if idStr, ok := id.(string); ok {
+ docIDs = append(docIDs, idStr)
+ }
+ }
+ }
+
+ common.Debug("FilterDocIdsByMetaPushdown returned doc IDs", zap.Int("count", len(docIDs)))
+ return docIDs
+}
+
+// totalHitsFromInfinityExtraInfo parses the JSON blob Infinity returns
+// in QueryResult.ExtraInfo when the total_hits_count option is set. The
+// shape is not part of the public SDK contract today (it's a string
+// field with an undocumented layout), so we accept several common
+// spellings and stay tolerant of future changes.
+//
+// Returns (total, true) when a non-negative integer is found, otherwise
+// (0, false) so the caller can decide how to react.
+func totalHitsFromInfinityExtraInfo(extraInfo string) (int64, bool) {
+ if extraInfo == "" {
+ return 0, false
+ }
+ // Try a permissive decode first — Infinity has historically
+ // returned things like {"total_hits_count": 42} but we don't want
+ // to bind to that exact shape forever.
+ var generic map[string]interface{}
+ if err := json.Unmarshal([]byte(extraInfo), &generic); err != nil {
+ return 0, false
+ }
+ for _, key := range []string{"total_hits_count", "totalHitsCount", "total"} {
+ raw, ok := generic[key]
+ if !ok {
+ continue
+ }
+ switch v := raw.(type) {
+ case float64:
+ if v < 0 {
+ return 0, false
+ }
+ return int64(v), true
+ case int64:
+ if v < 0 {
+ return 0, false
+ }
+ return v, true
+ case int:
+ if v < 0 {
+ return 0, false
+ }
+ return int64(v), true
+ case json.Number:
+ n, err := v.Int64()
+ if err == nil && n >= 0 {
+ return n, true
+ }
+ }
+ }
+ return 0, false
+}
diff --git a/internal/engine/types/types.go b/internal/engine/types/types.go
index a7413c018c..dfdf04b1ad 100644
--- a/internal/engine/types/types.go
+++ b/internal/engine/types/types.go
@@ -16,6 +16,14 @@
package types
+import (
+ "fmt"
+
+ "go.uber.org/zap"
+
+ "ragflow/internal/common"
+)
+
// SearchRequest unified search request for all engines
type SearchRequest struct {
// Search target
@@ -46,6 +54,22 @@ type SearchResult struct {
Total int64 // Total number of matches
}
+// SearchMetadataResult unified search result for metadata indices
+type SearchMetadataResult struct {
+ MetadataRecords []map[string]interface{} // Metadata search results
+ Total int64 // Total number of matches
+}
+
+// SearchMetadataRequest unified search request for metadata indices
+type SearchMetadataRequest struct {
+ TenantID string // Tenant ID (index name derived: ragflow_doc_meta_{tenantID})
+ Offset int // Pagination offset
+ Limit int // Pagination limit
+ SelectFields []string // List of field names to return (nil means all fields)
+ Filter map[string]interface{} // Filters for search
+ OrderBy *OrderByExpr // Order by expression
+}
+
type OrderByExpr struct {
Fields []OrderByField
}
@@ -102,3 +126,36 @@ type FusionExpr struct {
TopN int // TopK for fusion
FusionParams map[string]interface{} // Fusion parameters (e.g., {"weights": "0.05,0.95"})
}
+
+// LogSearchRequest logs SearchRequest in debug mode
+func LogSearchRequest(engineName string, req *SearchRequest) {
+ common.Info(fmt.Sprintf("Search in %s started", engineName), zap.Any("indexNames", req.IndexNames))
+
+ if !common.IsDebugEnabled() {
+ return
+ }
+
+ var matchExprsStr string
+ for i, expr := range req.MatchExprs {
+ switch e := expr.(type) {
+ case *MatchTextExpr:
+ matchExprsStr += fmt.Sprintf(" [%d] MatchTextExpr: fields=%v, matchingText=%s, topN=%d, extraOptions=%v\n", i, e.Fields, e.MatchingText, e.TopN, e.ExtraOptions)
+ case *MatchDenseExpr:
+ matchExprsStr += fmt.Sprintf(" [%d] MatchDenseExpr: vectorColumn=%s, vectorSize=%d, topN=%d, extraOptions=%v\n", i, e.VectorColumnName, len(e.EmbeddingData), e.TopN, e.ExtraOptions)
+ case *FusionExpr:
+ matchExprsStr += fmt.Sprintf(" [%d] FusionExpr: method=%s, topN=%d, fusionParams=%v\n", i, e.Method, e.TopN, e.FusionParams)
+ default:
+ matchExprsStr += fmt.Sprintf(" [%d] unknown type\n", i)
+ }
+ }
+
+ common.Debug(fmt.Sprintf("Search request:\n"+
+ " indexNames=%v\n"+
+ " KbIDs=%v\n"+
+ " offset=%d, limit=%d\n"+
+ " SelectFields=%v\n"+
+ " Filter=%v\n"+
+ " MatchExprs:\n%s orderBy=%v\n"+
+ " RankFeature=%v",
+ req.IndexNames, req.KbIDs, req.Offset, req.Limit, req.SelectFields, req.Filter, matchExprsStr, req.OrderBy, req.RankFeature))
+}
diff --git a/internal/entity/models/builtin.go b/internal/entity/models/builtin.go
new file mode 100644
index 0000000000..7c216893a2
--- /dev/null
+++ b/internal/entity/models/builtin.go
@@ -0,0 +1,192 @@
+package models
+
+import (
+ "bytes"
+ "encoding/json"
+ "fmt"
+ "io"
+ "net/http"
+ "os"
+ "strings"
+ "time"
+)
+
+// builtinHTTPClient is a shared client with timeouts for all Builtin model
+// requests. http.DefaultClient has no timeout, so a hung or slow TEI server
+// would otherwise block goroutines indefinitely.
+var builtinHTTPClient = &http.Client{
+ Timeout: 30 * time.Second,
+ Transport: &http.Transport{
+ MaxIdleConnsPerHost: 10,
+ ResponseHeaderTimeout: 10 * time.Second,
+ },
+}
+
+// BuiltinModel implements ModelDriver for Builtin (local embedding models via TEI)
+type BuiltinModel struct {
+ baseURL string
+ model string
+}
+
+// NewBuiltinModel creates a new Builtin model instance
+func NewBuiltinModel(baseURL, model string) *BuiltinModel {
+ return &BuiltinModel{
+ baseURL: baseURL,
+ model: model,
+ }
+}
+
+func (b *BuiltinModel) Name() string {
+ return "builtin"
+}
+
+func (b *BuiltinModel) NewInstance(baseURL map[string]string) ModelDriver {
+ return &BuiltinModel{
+ baseURL: b.baseURL,
+ model: b.model,
+ }
+}
+
+func (b *BuiltinModel) ChatWithMessages(modelName string, messages []Message, apiConfig *APIConfig, chatModelConfig *ChatConfig) (*ChatResponse, error) {
+ return nil, fmt.Errorf("builtin model does not support chat")
+}
+
+func (b *BuiltinModel) ChatStreamlyWithSender(modelName string, messages []Message, apiConfig *APIConfig, modelConfig *ChatConfig, sender func(*string, *string) error) error {
+ return fmt.Errorf("builtin model does not support chat")
+}
+
+// Embed sends texts to a TEI (Text Embeddings Inference) server and returns embeddings
+func (b *BuiltinModel) Embed(modelName *string, texts []string, apiConfig *APIConfig, embeddingConfig *EmbeddingConfig) ([]EmbeddingData, error) {
+ if len(texts) == 0 {
+ return []EmbeddingData{}, nil
+ }
+
+ baseURL := b.baseURL
+ if baseURL == "" {
+ baseURL = "http://localhost:6380"
+ }
+
+ url := fmt.Sprintf("%s/embed", baseURL)
+
+ reqBody := map[string]interface{}{
+ "inputs": texts,
+ }
+
+ // Add dimension if specified
+ if embeddingConfig != nil && embeddingConfig.Dimension > 0 {
+ reqBody["dimensions"] = embeddingConfig.Dimension
+ }
+
+ jsonData, err := json.Marshal(reqBody)
+ if err != nil {
+ return nil, fmt.Errorf("failed to marshal request: %w", err)
+ }
+
+ req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
+ if err != nil {
+ return nil, err
+ }
+
+ req.Header.Set("Content-Type", "application/json")
+ // Note: TEI server typically doesn't require auth for local deployments
+
+ resp, err := builtinHTTPClient.Do(req)
+ if err != nil {
+ return nil, err
+ }
+ defer resp.Body.Close()
+
+ body, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return nil, err
+ }
+
+ if resp.StatusCode != http.StatusOK {
+ return nil, fmt.Errorf("Builtin embeddings API error: status %d, body: %s", resp.StatusCode, string(body))
+ }
+
+ // TEI returns a simple array of embeddings by default
+ var embeddings [][]float64
+ if err = json.Unmarshal(body, &embeddings); err != nil {
+ return nil, fmt.Errorf("failed to parse TEI response: %w, body: %s", err, string(body))
+ }
+
+ result := make([]EmbeddingData, len(embeddings))
+ for i, emb := range embeddings {
+ result[i] = EmbeddingData{
+ Embedding: emb,
+ Index: i,
+ }
+ }
+
+ return result, nil
+}
+
+func (b *BuiltinModel) Rerank(modelName *string, query string, documents []string, apiConfig *APIConfig, rerankConfig *RerankConfig) (*RerankResponse, error) {
+ return nil, fmt.Errorf("builtin model does not support rerank")
+}
+
+func (b *BuiltinModel) TranscribeAudio(modelName *string, file *string, apiConfig *APIConfig, asrConfig *ASRConfig) (*ASRResponse, error) {
+ return nil, fmt.Errorf("builtin model does not support transcription")
+}
+
+func (b *BuiltinModel) TranscribeAudioWithSender(modelName *string, file *string, apiConfig *APIConfig, asrConfig *ASRConfig, sender func(*string, *string) error) error {
+ return fmt.Errorf("builtin model does not support transcription")
+}
+
+func (b *BuiltinModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
+ return nil, fmt.Errorf("builtin model does not support TTS")
+}
+
+func (b *BuiltinModel) AudioSpeechWithSender(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig, sender func(*string, *string) error) error {
+ return fmt.Errorf("builtin model does not support TTS")
+}
+
+func (b *BuiltinModel) OCRFile(modelName *string, content []byte, url *string, apiConfig *APIConfig, ocrConfig *OCRConfig) (*OCRFileResponse, error) {
+ return nil, fmt.Errorf("builtin model does not support OCR")
+}
+
+func (b *BuiltinModel) ParseFile(modelName *string, content []byte, url *string, apiConfig *APIConfig, parseFileConfig *ParseFileConfig) (*ParseFileResponse, error) {
+ return nil, fmt.Errorf("builtin model does not support parse file")
+}
+
+func (b *BuiltinModel) ListModels(apiConfig *APIConfig) ([]string, error) {
+ return []string{b.model}, nil
+}
+
+func (b *BuiltinModel) Balance(apiConfig *APIConfig) (map[string]interface{}, error) {
+ return nil, fmt.Errorf("builtin model does not support balance")
+}
+
+func (b *BuiltinModel) CheckConnection(apiConfig *APIConfig) error {
+ // Try to get model info to verify connection
+ _, err := b.Embed(nil, []string{"test"}, apiConfig, nil)
+ return err
+}
+
+func (b *BuiltinModel) ListTasks(apiConfig *APIConfig) ([]ListTaskStatus, error) {
+ return nil, fmt.Errorf("builtin model does not support tasks")
+}
+
+func (b *BuiltinModel) ShowTask(taskID string, apiConfig *APIConfig) (*TaskResponse, error) {
+ return nil, fmt.Errorf("builtin model does not support tasks")
+}
+
+// GetBuiltinEmbeddingModel returns a Builtin model driver for the given model name
+func GetBuiltinEmbeddingModel(modelName string) ModelDriver {
+ // Get TEI base URL from environment or config
+ // Default to port 6380 where docker-tei-cpu-1 maps internal port 80
+ teiBaseURL := getEnv("TEI_BASE_URL", "http://localhost:6380")
+
+ // Create a builtin model instance with TEI endpoint
+ driver := NewBuiltinModel(teiBaseURL, modelName)
+ return driver
+}
+
+// getEnv is a helper to get environment variable with default
+func getEnv(key, defaultValue string) string {
+ if value := strings.TrimSpace(strings.Replace(os.Getenv(key), "\\", "/", -1)); value != "" {
+ return value
+ }
+ return defaultValue
+}
\ No newline at end of file
diff --git a/internal/entity/tenant.go b/internal/entity/tenant.go
index 0865ab29a0..88129cb6b1 100644
--- a/internal/entity/tenant.go
+++ b/internal/entity/tenant.go
@@ -34,7 +34,7 @@ type Tenant struct {
TTSID *string `gorm:"column:tts_id;size:256;index" json:"tts_id,omitempty"`
TenantTTSID *int64 `gorm:"column:tenant_tts_id;index" json:"tenant_tts_id,omitempty"`
ParserIDs string `gorm:"column:parser_ids;size:256;not null;index" json:"parser_ids"`
- OCRID string `gorm:"column:ocr_id;size:256;not null" json:"ocr_id"`
+ OCRID *string `gorm:"column:ocr_id;size:256" json:"ocr_id,omitempty"`
TenantOCRID *int64 `gorm:"column:tenant_ocr_id" json:"tenant_ocr_id,omitempty"`
Credit int64 `gorm:"column:credit;default:512;index" json:"credit"`
Status *string `gorm:"column:status;size:1;index" json:"status,omitempty"`
diff --git a/internal/handler/chunk.go b/internal/handler/chunk.go
index 71f8df4fde..beef54f1b7 100644
--- a/internal/handler/chunk.go
+++ b/internal/handler/chunk.go
@@ -40,91 +40,6 @@ func NewChunkHandler(chunkService *service.ChunkService, userService *service.Us
}
}
-// RetrievalTest performs retrieval test for chunks
-// @Summary Retrieval Test
-// @Description Test retrieval of chunks based on question and knowledge base
-// @Tags chunks
-// @Accept json
-// @Produce json
-// @Param request body service.RetrievalTestRequest true "retrieval test parameters"
-// @Success 200 {object} map[string]interface{}
-// @Router /api/v1/datasets/search [post]
-func (h *ChunkHandler) RetrievalTest(c *gin.Context) {
- user, errorCode, errorMessage := GetUser(c)
- if errorCode != common.CodeSuccess {
- jsonError(c, errorCode, errorMessage)
- return
- }
-
- // Bind JSON request
- var req service.RetrievalTestRequest
- if err := c.ShouldBindJSON(&req); err != nil {
- c.JSON(http.StatusBadRequest, gin.H{
- "code": 400,
- "message": err.Error(),
- })
- return
- }
-
- // Set default values for optional parameters
- if req.Page == nil {
- defaultPage := 1
- req.Page = &defaultPage
- }
- if req.Size == nil {
- defaultSize := 30
- req.Size = &defaultSize
- }
- if req.TopK == nil {
- defaultTopK := 1024
- req.TopK = &defaultTopK
- }
- if req.UseKG == nil {
- defaultUseKG := false
- req.UseKG = &defaultUseKG
- }
-
- // Validate required fields
- if req.Question == "" {
- c.JSON(http.StatusBadRequest, gin.H{
- "code": 400,
- "message": "question is required",
- })
- return
- }
- if req.Datasets == nil {
- c.JSON(http.StatusBadRequest, gin.H{
- "code": 400,
- "message": "kb_id is required",
- })
- return
- }
-
- if len(req.Datasets) == 0 {
- c.JSON(http.StatusBadRequest, gin.H{
- "code": 400,
- "message": "kb_id array cannot be empty",
- })
- return
- }
-
- // Call service with user ID for permission checks
- resp, err := h.chunkService.RetrievalTest(&req, user.ID)
- if err != nil {
- c.JSON(http.StatusInternalServerError, gin.H{
- "code": 500,
- "message": err.Error(),
- })
- return
- }
-
- c.JSON(http.StatusOK, gin.H{
- "code": 0,
- "data": resp,
- "message": "success",
- })
-}
-
// Get retrieves a chunk by ID
func (h *ChunkHandler) Get(c *gin.Context) {
user, errorCode, errorMessage := GetUser(c)
@@ -134,16 +49,20 @@ func (h *ChunkHandler) Get(c *gin.Context) {
}
chunkID := c.Param("chunk_id")
- if chunkID == "" {
+ datasetID := c.Param("dataset_id")
+ documentID := c.Param("document_id")
+ if chunkID == "" || datasetID == "" || documentID == "" {
c.JSON(http.StatusBadRequest, gin.H{
"code": 400,
- "message": "chunk_id is required",
+ "message": "dataset_id, document_id and chunk_id are required",
})
return
}
req := &service.GetChunkRequest{
- ChunkID: chunkID,
+ ChunkID: chunkID,
+ DocumentID: documentID,
+ DatasetID: datasetID,
}
resp, err := h.chunkService.Get(req, user.ID)
diff --git a/internal/handler/datasets.go b/internal/handler/datasets.go
index ed75b42f81..b1fd260eac 100644
--- a/internal/handler/datasets.go
+++ b/internal/handler/datasets.go
@@ -602,6 +602,86 @@ func (h *DatasetsHandler) ListMetadataFlattened(c *gin.Context) {
jsonResponse(c, common.CodeSuccess, flattenedMeta, "success")
}
+// SearchDatasets searches chunks across datasets based on a question
+// @Summary Search Datasets
+// @Description Search for relevant chunks across one or more datasets based on a question
+// @Tags datasets
+// @Accept json
+// @Produce json
+// @Param request body service.SearchDatasetsRequest true "search parameters"
+// @Success 200 {object} map[string]interface{}
+// @Router /api/v1/datasets/search [post]
+func (h *DatasetsHandler) SearchDatasets(c *gin.Context) {
+ user, errorCode, errorMessage := GetUser(c)
+ if errorCode != common.CodeSuccess {
+ jsonError(c, errorCode, errorMessage)
+ return
+ }
+
+ var req service.SearchDatasetsRequest
+ if err := c.ShouldBindJSON(&req); err != nil {
+ c.JSON(http.StatusBadRequest, gin.H{
+ "code": 400,
+ "message": err.Error(),
+ })
+ return
+ }
+
+ if req.Page == nil {
+ defaultPage := 1
+ req.Page = &defaultPage
+ }
+ if req.Size == nil {
+ defaultSize := 30
+ req.Size = &defaultSize
+ }
+ if req.TopK == nil {
+ defaultTopK := 1024
+ req.TopK = &defaultTopK
+ }
+ if req.UseKG == nil {
+ defaultUseKG := false
+ req.UseKG = &defaultUseKG
+ }
+
+ if req.Question == "" {
+ c.JSON(http.StatusBadRequest, gin.H{
+ "code": 400,
+ "message": "question is required",
+ })
+ return
+ }
+ if req.DatasetIDs == nil {
+ c.JSON(http.StatusBadRequest, gin.H{
+ "code": 400,
+ "message": "kb_id is required",
+ })
+ return
+ }
+
+ if len(req.DatasetIDs) == 0 {
+ c.JSON(http.StatusBadRequest, gin.H{
+ "code": 400,
+ "message": "kb_id array cannot be empty",
+ })
+ return
+ }
+
+ resp, err := h.datasetsService.SearchDatasets(&req, user.ID)
+ if err != nil {
+ c.JSON(http.StatusInternalServerError, gin.H{
+ "code": 500,
+ "message": err.Error(),
+ })
+ return
+ }
+
+ c.JSON(http.StatusOK, gin.H{
+ "code": 0,
+ "data": resp,
+ })
+}
+
func firstStringValue(value interface{}) string {
switch v := value.(type) {
case string:
diff --git a/internal/handler/searchbot.go b/internal/handler/searchbot.go
index b7e75c9128..b23ea1f3f4 100644
--- a/internal/handler/searchbot.go
+++ b/internal/handler/searchbot.go
@@ -42,10 +42,11 @@ type SearchbotRealLLM struct {
}
func (r *SearchbotRealLLM) Chat(tenantID, modelID string, messages []modelModule.Message, config *modelModule.ChatConfig) (*modelModule.ChatResponse, error) {
- chatModel, err := r.Svc.GetChatModel(tenantID, modelID)
+ driver, modelName, apiConfig, _, err := r.Svc.GetModelConfigFromProviderInstance(tenantID, entity.ModelTypeChat, modelID)
if err != nil {
return nil, err
}
+ chatModel := modelModule.NewChatModel(driver, &modelName, apiConfig)
return chatModel.ModelDriver.ChatWithMessages(*chatModel.ModelName, messages, chatModel.APIConfig, config)
}
diff --git a/internal/router/router.go b/internal/router/router.go
index b7585715bf..f8ff3d23af 100644
--- a/internal/router/router.go
+++ b/internal/router/router.go
@@ -235,7 +235,7 @@ func (r *Router) Setup(engine *gin.Engine) {
datasets.DELETE("/:dataset_id/graph", r.datasetsHandler.DeleteKnowledgeGraph)
datasets.POST("", r.datasetsHandler.CreateDataset)
datasets.DELETE("", r.datasetsHandler.DeleteDatasets)
- datasets.POST("/search", r.chunkHandler.RetrievalTest)
+ datasets.POST("/search", r.datasetsHandler.SearchDatasets)
datasets.GET("/metadata/flattened", r.datasetsHandler.ListMetadataFlattened)
// Dataset ingestion logs
diff --git a/internal/service/chat_session.go b/internal/service/chat_session.go
index 659838a72f..fe4de1e5f9 100644
--- a/internal/service/chat_session.go
+++ b/internal/service/chat_session.go
@@ -522,11 +522,26 @@ func (s *ChatSessionService) asyncChatSolo(dialog *entity.Chat, session *entity.
// Process messages - handle attachments and image files
processedMessages := s.processMessages(messages, dialog)
- chatModel, err := s.modelProviderSvc.GetChatModel(dialog.TenantID, dialog.LLMID)
+ var (
+ driver modelModule.ModelDriver
+ modelName string
+ apiConfig *modelModule.APIConfig
+ err error
+ )
+ if dialog.LLMID != "" {
+ driver, modelName, apiConfig, _, err = s.modelProviderSvc.GetModelConfigFromProviderInstance(
+ dialog.TenantID, entity.ModelTypeChat, dialog.LLMID,
+ )
+ } else {
+ driver, modelName, apiConfig, _, err = s.modelProviderSvc.GetTenantDefaultModelByType(
+ dialog.TenantID, entity.ModelTypeChat,
+ )
+ }
if err != nil {
common.Error("asyncChatSolo failed to get chat model", err)
return nil, err
}
+ chatModel := modelModule.NewChatModel(driver, &modelName, apiConfig)
// Convert messages to Message format
var msgs []modelModule.Message
@@ -583,12 +598,27 @@ func (s *ChatSessionService) asyncChatSoloStream(dialog *entity.Chat, session *e
// Process messages
processedMessages := s.processMessages(messages, dialog)
- chatModel, err := s.modelProviderSvc.GetChatModel(dialog.TenantID, dialog.LLMID)
+ var (
+ driver modelModule.ModelDriver
+ modelName string
+ apiConfig *modelModule.APIConfig
+ err error
+ )
+ if dialog.LLMID != "" {
+ driver, modelName, apiConfig, _, err = s.modelProviderSvc.GetModelConfigFromProviderInstance(
+ dialog.TenantID, entity.ModelTypeChat, dialog.LLMID,
+ )
+ } else {
+ driver, modelName, apiConfig, _, err = s.modelProviderSvc.GetTenantDefaultModelByType(
+ dialog.TenantID, entity.ModelTypeChat,
+ )
+ }
if err != nil {
common.Error("asyncChatSoloStream failed to get chat model", err)
resultChan <- s.structureAnswer(session, "**ERROR**: "+err.Error(), messageID, session.ID, reference)
return
}
+ chatModel := modelModule.NewChatModel(driver, &modelName, apiConfig)
// Convert messages to []modelModule.Message for ChatStreamlyWithSender
var chatMessages []modelModule.Message
diff --git a/internal/service/chunk.go b/internal/service/chunk.go
index 6f7d08f540..0a57432145 100644
--- a/internal/service/chunk.go
+++ b/internal/service/chunk.go
@@ -19,19 +19,12 @@ package service
import (
"context"
"fmt"
- "ragflow/internal/common"
- "ragflow/internal/entity"
- "ragflow/internal/entity/models"
- "ragflow/internal/server"
- "strconv"
"strings"
- "go.uber.org/zap"
-
"ragflow/internal/dao"
"ragflow/internal/engine"
"ragflow/internal/engine/types"
- "ragflow/internal/service/nlp"
+ "ragflow/internal/server"
"ragflow/internal/tokenizer"
"ragflow/internal/utility"
)
@@ -61,394 +54,11 @@ func NewChunkService() *ChunkService {
}
}
-// RetrievalTestRequest retrieval test request
-type RetrievalTestRequest struct {
- Datasets []string `json:"dataset_ids" binding:"required"` // string or []string
- Question string `json:"question" binding:"required"`
- Page *int `json:"page,omitempty"`
- Size *int `json:"size,omitempty"`
- DocIDs []string `json:"doc_ids,omitempty"`
- UseKG *bool `json:"use_kg,omitempty"`
- TopK *int `json:"top_k,omitempty"`
- CrossLanguages []string `json:"cross_languages,omitempty"`
- SearchID *string `json:"search_id,omitempty"`
- Filter map[string]interface{} `json:"meta_data_filter,omitempty"`
- TenantRerankID *string `json:"tenant_rerank_id,omitempty"`
- RerankID *string `json:"rerank_id,omitempty"`
- Keyword *bool `json:"keyword,omitempty"`
- SimilarityThreshold *float64 `json:"similarity_threshold,omitempty"`
- VectorSimilarityWeight *float64 `json:"vector_similarity_weight,omitempty"`
-}
-
-// RetrievalTestResponse retrieval test response
-type RetrievalTestResponse struct {
- Chunks []map[string]interface{} `json:"chunks"`
- DocAggs []map[string]interface{} `json:"doc_aggs"`
- Labels *map[string]float64 `json:"labels"`
- Total int64 `json:"total"`
-}
-
-// RetrievalTest performs retrieval test for a given question against specified knowledge bases.
-//
-// Flow:
-// 1. Validate kbs permissions and embedding model
-// 2. Apply metadata filter if specified (auto/semi_auto uses LLM, manual uses provided conditions)
-// 3. Apply cross_languages transformation if requested (translate question)
-// 4. Apply keyword extraction if requested (append keywords to question)
-// 5. Get rank features via LabelQuestion() - tag-based weights or pagerank_fld fallback
-// 6. Call RetrievalService.Retrieval() which:
-// - Computes query embedding
-// - Performs hybrid search (text + vector) with rank features
-// - Reranks results
-// - Builds doc_aggs by aggregating chunks per document
-// 7. knowledge graph retrieval (not implemented)
-// 8. Apply retrieval by children to group child chunks under parent chunks
-func (s *ChunkService) RetrievalTest(req *RetrievalTestRequest, userID string) (*RetrievalTestResponse, error) {
- common.Info("RetrievalTest started", zap.String("userID", userID), zap.Any("kbID", req.Datasets), zap.String("question", req.Question))
-
- common.Debug(fmt.Sprintf("RetrievalTest request:\n"+
- " kbID=%v\n"+
- " question=%s\n"+
- " page=%v, size=%v\n"+
- " docIDs=%v\n"+
- " useKG=%v, topK=%v\n"+
- " crossLanguages=%v\n"+
- " searchID=%v\n"+
- " filter=%v\n"+
- " tenantRerankID=%v\n"+
- " rerankID=%v\n"+
- " keyword=%v\n"+
- " similarityThreshold=%v, vectorSimilarityWeight=%v",
- req.Datasets, req.Question,
- ptrString(req.Page), ptrString(req.Size), req.DocIDs,
- ptrString(req.UseKG), ptrString(req.TopK), req.CrossLanguages, ptrString(req.SearchID),
- req.Filter,
- ptrString(req.TenantRerankID), ptrString(req.RerankID),
- ptrString(req.Keyword),
- ptrString(req.SimilarityThreshold), ptrString(req.VectorSimilarityWeight)))
-
- if req.Question == "" {
- return nil, fmt.Errorf("question is required")
- }
-
- ctx := context.Background()
-
- tenants, err := s.userTenantDAO.GetByUserID(userID)
- if err != nil {
- return nil, fmt.Errorf("failed to get user tenants: %w", err)
- }
- if len(tenants) == 0 {
- return nil, fmt.Errorf("user has no accessible tenants")
- }
- common.Debug("Retrieved user tenants from database", zap.String("userID", userID), zap.Int("tenantCount", len(tenants)))
-
- var tenantIDs []string
- var kbRecords []*entity.Knowledgebase
- for _, datasetID := range req.Datasets {
- found := false
- for _, tenant := range tenants {
- kb, err := s.kbDAO.GetByIDAndTenantID(datasetID, tenant.TenantID)
- if err == nil && kb != nil {
- common.Debug("Found knowledge base in database",
- zap.String("datasetID", datasetID),
- zap.String("tenantID", tenant.TenantID),
- zap.String("kbName", kb.Name),
- zap.String("embdID", kb.EmbdID))
- tenantIDs = append(tenantIDs, tenant.TenantID)
- kbRecords = append(kbRecords, kb)
- found = true
- break
- }
- }
- if !found {
- return nil, fmt.Errorf("only owner of dataset is authorized for this operation")
- }
- }
-
- // Check if all kbs have the same embedding model
- if len(kbRecords) > 1 {
- firstEmbdID := kbRecords[0].EmbdID
- for i := 1; i < len(kbRecords); i++ {
- if kbRecords[i].EmbdID != firstEmbdID {
- return nil, fmt.Errorf("cannot retrieve across datasets with different embedding models")
- }
- }
- }
-
- // Determine meta_data_filter
- var chatID string
- var chatModelForFilter *models.ChatModel
- filter := req.Filter
-
- if req.SearchID != nil && *req.SearchID != "" {
- // If search_id is set, get meta_data_filter and chat_id from search_config
- searchDetail, err := s.searchService.GetDetail(*req.SearchID)
- if err != nil {
- common.Warn("Failed to get search detail for search_id, proceeding without it", zap.String("searchID", *req.SearchID), zap.Error(err))
- } else if searchConfig, ok := searchDetail["search_config"].(entity.JSONMap); ok && searchConfig != nil {
- if searchMetaFilter, ok := searchConfig["meta_data_filter"].(map[string]interface{}); ok {
- filter = searchMetaFilter
- }
- chatID, _ = searchConfig["chat_id"].(string)
- } else {
- common.Warn("No search_config found in search detail", zap.String("searchID", *req.SearchID))
- }
- }
-
- // If meta_data_filter method is auto/semi_auto, get chat model
- if filter != nil {
- method, _ := filter["method"].(string)
- if method == "auto" || method == "semi_auto" {
- modelProviderSvc := NewModelProviderService()
- if chatID != "" {
- // Use chat_id from search_config (it's actually the model name)
- chatModelForFilter, err = modelProviderSvc.GetChatModel(tenantIDs[0], chatID)
- if err != nil {
- common.Warn("Failed to get chat model from search_config chat_id, using tenant default", zap.String("chatID", chatID), zap.Error(err))
- } else {
- common.Info("Fetched chat model (from search_config) for metadata filter",
- zap.String("chatID", chatID),
- zap.String("tenantID", tenantIDs[0]))
- }
- }
-
- // If no chatID from search_config, or chatModel not found, use tenant default
- if chatModelForFilter == nil {
- tenantSvc := NewTenantService()
- modelName, err := tenantSvc.GetDefaultModelName(tenantIDs[0], entity.ModelTypeChat)
- if err != nil || modelName == "" {
- common.Warn("Failed to get tenant default chat model name for meta_data_filter", zap.Error(err))
- } else {
- chatModelForFilter, err = modelProviderSvc.GetChatModel(tenantIDs[0], modelName)
- if err != nil {
- common.Warn("Failed to get chat model for meta_data_filter", zap.Error(err))
- } else {
- common.Info("Fetched chat model (tenant default) for metadata filter",
- zap.String("tenantID", tenantIDs[0]),
- zap.String("modelName", modelName))
- }
- }
- }
- }
- }
-
- // Apply meta_data_filter to get filtered doc_ids (filter by metadata before retrieval)
- docIDs := make([]string, len(req.DocIDs))
- copy(docIDs, req.DocIDs)
- if filter != nil {
- // Get flattened metadata
- metadataSvc := NewMetadataService()
- flattedMeta, err := metadataSvc.GetFlattedMetaByKBs(req.Datasets)
- if err != nil {
- common.Warn("Failed to get flatted metadata", zap.Error(err))
- } else {
- common.Info("metadata filter conditions", zap.Any("filter", filter))
- filteredDocIDs, _ := ApplyMetaDataFilter(ctx, filter, flattedMeta, req.Question, chatModelForFilter, req.DocIDs)
- docIDs = filteredDocIDs
- common.Info("ApplyMetaDataFilter result", zap.Strings("docIDs", docIDs))
- }
- }
-
- // Apply cross_languages and keyword extraction with tenant default chat model
- modifiedQuestion := req.Question
- var chatModel *models.ChatModel
-
- // Get chat model for cross_languages and keyword_extraction
- if len(req.CrossLanguages) > 0 || (req.Keyword != nil && *req.Keyword) {
- tenantSvc := NewTenantService()
- modelProviderSvc := NewModelProviderService()
- modelName, err := tenantSvc.GetDefaultModelName(tenantIDs[0], "chat")
- if err != nil || modelName == "" {
- common.Warn("Failed to get default chat model name for LLM transformations", zap.Error(err))
- } else {
- chatModel, err = modelProviderSvc.GetChatModel(tenantIDs[0], modelName)
- if err != nil {
- common.Warn("Failed to get chat model for LLM transformations", zap.Error(err))
- } else {
- common.Info("Fetched chat model (tenant default) for cross_languages/keyword_extraction",
- zap.String("tenantID", tenantIDs[0]),
- zap.String("modelName", modelName))
- }
- }
- }
-
- // Apply cross_languages on the question (translate question)
- if chatModel != nil && len(req.CrossLanguages) > 0 {
- translated, err := CrossLanguages(ctx, chatModel, req.Question, req.CrossLanguages)
- if err != nil {
- common.Warn("Failed to translate question", zap.Error(err))
- } else {
- modifiedQuestion = translated
- }
- }
-
- // Apply keyword extraction on the question (append keywords to question)
- if chatModel != nil && req.Keyword != nil && *req.Keyword {
- extractedKeywords, err := KeywordExtraction(ctx, chatModel, modifiedQuestion, 3)
- if err != nil {
- common.Warn("Failed to extract keywords from question", zap.Error(err))
- } else if extractedKeywords != "" {
- modifiedQuestion = modifiedQuestion + " " + extractedKeywords
- }
- }
-
- if modifiedQuestion != req.Question {
- common.Info("Modified question after transformations",
- zap.String("originalQuestion", req.Question),
- zap.String("modifiedQuestion", modifiedQuestion),
- zap.Strings("crossLanguages", req.CrossLanguages),
- zap.Bool("keywordExtraction", req.Keyword != nil && *req.Keyword))
- }
-
- // Get tag-based rank features via LabelQuestion
- metadataSvc := NewMetadataService()
- labels := metadataSvc.LabelQuestion(modifiedQuestion, kbRecords)
- common.Debug("LabelQuestion result", zap.Any("labels", labels))
-
- // Determine embedding model
- var embdID string
- var tenantLLM *entity.TenantLLM
- if kbRecords[0].TenantEmbdID != nil && *kbRecords[0].TenantEmbdID > 0 {
- tenantLLM, embdID, err = dao.LookupTenantLLMByID(dao.NewTenantLLMDAO(), *kbRecords[0].TenantEmbdID)
- if err != nil {
- return nil, fmt.Errorf("failed to get embedding model by tenant_embd_id: %w", err)
- }
- } else if kbRecords[0].EmbdID != "" {
- parts := strings.Split(kbRecords[0].EmbdID, "@")
- if len(parts) == 2 && parts[1] != "" {
- tenantLLM, embdID, err = dao.LookupTenantLLMByFactory(dao.NewTenantLLMDAO(), tenantIDs[0], parts[1], parts[0], entity.ModelTypeEmbedding)
- } else {
- tenantLLM, embdID, err = dao.LookupTenantLLMByName(dao.NewTenantLLMDAO(), tenantIDs[0], kbRecords[0].EmbdID, entity.ModelTypeEmbedding)
- }
- if err != nil {
- return nil, fmt.Errorf("failed to get embedding model by embd_id: %w", err)
- }
- } else {
- tenantLLM, err = dao.NewTenantLLMDAO().GetByTenantAndType(tenantIDs[0], entity.ModelTypeEmbedding)
- if err != nil {
- return nil, fmt.Errorf("failed to get tenant default embedding model: %w", err)
- }
- if tenantLLM == nil || tenantLLM.LLMName == nil || *tenantLLM.LLMName == "" {
- return nil, fmt.Errorf("no default embedding model found for tenant %s", tenantIDs[0])
- }
- embdID = fmt.Sprintf("%s@%s", *tenantLLM.LLMName, tenantLLM.LLMFactory)
- }
-
- // Get embedding model for the tenant
- modelProviderSvc := NewModelProviderService()
- embeddingModel, err := modelProviderSvc.GetEmbeddingModel(tenantIDs[0], embdID)
- if err != nil {
- return nil, fmt.Errorf("failed to get embedding model: %w", err)
- }
- common.Info("Fetched embedding model for retrieval",
- zap.String("tenantID", tenantIDs[0]),
- zap.String("embdID", embdID))
-
- // Get rerank model if RerankID is specified
- var rerankModel *models.RerankModel
- var rerankCompositeName string
- if req.TenantRerankID != nil && *req.TenantRerankID != "" {
- tenantRerankIDInt, parseErr := strconv.ParseInt(*req.TenantRerankID, 10, 64)
- if parseErr != nil {
- return nil, fmt.Errorf("invalid tenant_rerank_id: %w", parseErr)
- }
- _, rerankCompositeName, err = dao.LookupTenantLLMByID(dao.NewTenantLLMDAO(), tenantRerankIDInt)
- if err != nil {
- return nil, fmt.Errorf("failed to get rerank model by tenant_rerank_id: %w", err)
- }
- } else if req.RerankID != nil && *req.RerankID != "" {
- _, rerankCompositeName, err = dao.LookupTenantLLMByName(dao.NewTenantLLMDAO(), tenantIDs[0], *req.RerankID, entity.ModelTypeRerank)
- if err != nil {
- return nil, fmt.Errorf("failed to get rerank model by rerank_id: %w", err)
- }
- }
- if rerankCompositeName != "" {
- rerankModel, err = modelProviderSvc.GetRerankModel(tenantIDs[0], rerankCompositeName)
- if err != nil {
- return nil, fmt.Errorf("failed to get rerank model: %w", err)
- }
- }
-
- if rerankModel != nil {
- common.Info("Fetched rerank model",
- zap.String("tenantID", tenantIDs[0]),
- zap.String("rerankCompositeName", rerankCompositeName))
- }
-
- retrievalReq := &nlp.RetrievalRequest{
- TenantIDs: tenantIDs,
- Question: modifiedQuestion,
- KbIDs: req.Datasets,
- DocIDs: docIDs,
- Page: getPageNum(req.Page, 1),
- PageSize: getPageSize(req.Size, 30),
- Top: req.TopK,
- SimilarityThreshold: req.SimilarityThreshold,
- VectorSimilarityWeight: req.VectorSimilarityWeight,
- RerankModel: rerankModel,
- RankFeature: &labels,
- EmbeddingModel: embeddingModel,
- }
-
- // Call RetrievalService to perform retrieval
- retrievalResult, err := nlp.NewRetrievalService(s.docEngine, s.documentDAO).Retrieval(ctx, retrievalReq)
- if err != nil {
- return nil, fmt.Errorf("retrieval search failed: %w", err)
- }
-
- filteredChunks := retrievalResult.Chunks
-
- // Handle knowledge graph retrieval
- // TODO: KG retrieval requires GraphRAG infrastructure which is not yet implemented in Go
- if req.UseKG != nil && *req.UseKG {
- common.Warn("use_kg is not yet implemented in Go - skipping KG retrieval")
- }
-
- // Apply retrieval_by_children - aggregate child chunks into parent chunks
- filteredChunks = nlp.RetrievalByChildren(filteredChunks, tenantIDs, s.docEngine, ctx)
-
- // Remove vector field from each chunk
- for i := range filteredChunks {
- delete(filteredChunks[i], "vector")
- }
-
- common.Info("RetrievalTest completed", zap.String("userID", userID), zap.Any("kbID", req.Datasets), zap.String("question", req.Question), zap.Int64("chunkCount", int64(len(filteredChunks))))
-
- return &RetrievalTestResponse{
- Chunks: filteredChunks,
- DocAggs: retrievalResult.DocAggs,
- Labels: &labels,
- Total: int64(len(filteredChunks)),
- }, nil
-}
-
-// Helper functions
-
-// ptrString converts a pointer to a formatted string
-func ptrString[T any](p *T) string {
- if p == nil {
- return "<nil>"
- }
- return fmt.Sprintf("%v", *p)
-}
-
-func getPageNum(page *int, defaultVal int) int {
- if page != nil && *page > 0 {
- return *page
- }
- return defaultVal
-}
-
-func getPageSize(size *int, defaultVal int) int {
- if size != nil && *size > 0 {
- return *size
- }
- return defaultVal
-}
-
-// GetChunkRequest request for getting a chunk by ID
+// GetChunkRequest request for getting a chunk by ID.
type GetChunkRequest struct {
- ChunkID string `json:"chunk_id"`
+ ChunkID string `json:"chunk_id"`
+ DocumentID string `json:"document_id"`
+ DatasetID string `json:"dataset_id"`
}
// GetChunkResponse response for getting a chunk
@@ -465,10 +75,27 @@ func (s *ChunkService) Get(req *GetChunkRequest, userID string) (*GetChunkRespon
if req.ChunkID == "" {
return nil, fmt.Errorf("chunk_id is required")
}
+ if req.DatasetID == "" {
+ return nil, fmt.Errorf("dataset_id is required")
+ }
+ if req.DocumentID == "" {
+ return nil, fmt.Errorf("document_id is required")
+ }
ctx := context.Background()
- // Get user's tenants
+ // Verify the document exists and belongs to the dataset
+ docDAO := dao.NewDocumentDAO()
+ doc, err := docDAO.GetByID(req.DocumentID)
+ if err != nil || doc == nil {
+ return nil, fmt.Errorf("document not found")
+ }
+ if doc.KbID != req.DatasetID {
+ return nil, fmt.Errorf("document does not belong to this dataset")
+ }
+
+ // Resolve the tenant that owns this dataset. A dataset belongs to
+ // exactly one tenant, so the loop breaks on the first match.
tenants, err := s.userTenantDAO.GetByUserID(userID)
if err != nil {
return nil, fmt.Errorf("failed to get user tenants: %w", err)
@@ -476,75 +103,87 @@ func (s *ChunkService) Get(req *GetChunkRequest, userID string) (*GetChunkRespon
if len(tenants) == 0 {
return nil, fmt.Errorf("user has no accessible tenants")
}
-
- // Try each tenant to find the chunk
- var chunk map[string]interface{}
+ var targetTenantID string
for _, tenant := range tenants {
- // Get kbIDs for this tenant
- kbIDs, err := s.kbDAO.GetKBIDsByTenantID(tenant.TenantID)
- if err != nil {
- continue
+ kb, err := s.kbDAO.GetByIDAndTenantID(req.DatasetID, tenant.TenantID)
+ if err == nil && kb != nil {
+ targetTenantID = tenant.TenantID
+ break
}
+ }
+ if targetTenantID == "" {
+ return nil, fmt.Errorf("user does not have access to this dataset")
+ }
+
+ indexName := fmt.Sprintf("ragflow_%s", targetTenantID)
+ raw, err := s.docEngine.GetChunk(ctx, indexName, req.ChunkID, []string{req.DatasetID})
+ if err != nil {
+ return nil, fmt.Errorf("failed to get chunk: %w", err)
+ }
+ if raw == nil {
+ return nil, fmt.Errorf("chunk not found")
+ }
+ chunk, ok := raw.(map[string]interface{})
+ if !ok {
+ return nil, fmt.Errorf("invalid chunk format: expected map[string]interface{}, got %T", raw)
+ }
+
+ if actual, _ := chunk["kb_id"].(string); actual != req.DatasetID {
+ return nil, fmt.Errorf("chunk does not belong to dataset %q", req.DatasetID)
+ }
+ if actual, _ := chunk["doc_id"].(string); actual != req.DocumentID {
+ return nil, fmt.Errorf("chunk does not belong to document %q", req.DocumentID)
+ }
- indexName := fmt.Sprintf("ragflow_%s", tenant.TenantID)
+ return &GetChunkResponse{Chunk: formatChunkForGet(chunk)}, nil
+}
- doc, err := s.docEngine.GetChunk(ctx, indexName, req.ChunkID, kbIDs)
- if err != nil {
+// formatChunkForGet normalizes a raw engine chunk into the public
+// response shape: it strips internal fields (_vec / _tks / _ltks / _sm_
+// suffixes and a handful of score / id fields), renames a few legacy
+// keys, and coerces numeric fields to JSONFloat64 so the JSON encoder
+// preserves precision.
+func formatChunkForGet(chunk map[string]interface{}) map[string]interface{} {
+ result := make(map[string]interface{})
+ skipFields := map[string]bool{
+ "id": true, "authors": true, "_score": true, "SCORE": true,
+ }
+ for k, v := range chunk {
+ if skipFields[k] || strings.HasSuffix(k, "_vec") || strings.Contains(k, "_sm_") || strings.HasSuffix(k, "_tks") || strings.HasSuffix(k, "_ltks") {
continue
}
-
- if doc != nil {
- chunk, ok := doc.(map[string]interface{})
- if ok {
- result := make(map[string]interface{})
- skipFields := map[string]bool{
- "id": true, "authors": true, "_score": true, "SCORE": true,
- }
- for k, v := range chunk {
- if skipFields[k] || strings.HasSuffix(k, "_vec") || strings.Contains(k, "_sm_") || strings.HasSuffix(k, "_tks") || strings.HasSuffix(k, "_ltks") {
- continue
- }
- switch k {
- case "content":
- result["content_with_weight"] = v
- case "docnm":
- result["docnm_kwd"] = v
- case "important_keywords":
- utility.SetFieldArray(result, "important_kwd", v)
- case "questions":
- utility.SetFieldArray(result, "question_kwd", v)
- case "entities_kwd", "entity_kwd", "entity_type_kwd", "from_entity_kwd",
- "name_kwd", "raptor_kwd", "removed_kwd", "source_id", "tag_kwd",
- "to_entity_kwd", "toc_kwd", "authors_tks", "doc_type_kwd":
- if utility.IsEmpty(v) {
- result[k] = []interface{}{}
- } else {
- result[k] = v
- }
- case "tag_feas":
- if utility.IsEmpty(v) {
- result[k] = map[string]interface{}{}
- } else {
- result[k] = v
- }
- case "create_timestamp_flt", "rank_flt", "weight_flt":
- if floatVal, ok := utility.ToFloat64(v); ok {
- result[k] = utility.JSONFloat64(floatVal)
- }
- default:
- result[k] = v
- }
- }
- return &GetChunkResponse{Chunk: result}, nil
+ switch k {
+ case "content":
+ result["content_with_weight"] = v
+ case "docnm":
+ result["docnm_kwd"] = v
+ case "important_keywords":
+ utility.SetFieldArray(result, "important_kwd", v)
+ case "questions":
+ utility.SetFieldArray(result, "question_kwd", v)
+ case "entities_kwd", "entity_kwd", "entity_type_kwd", "from_entity_kwd",
+ "name_kwd", "raptor_kwd", "removed_kwd", "source_id", "tag_kwd",
+ "to_entity_kwd", "toc_kwd":
+ if utility.IsEmpty(v) {
+ result[k] = []interface{}{}
+ } else {
+ result[k] = v
+ }
+ case "tag_feas":
+ if utility.IsEmpty(v) {
+ result[k] = map[string]interface{}{}
+ } else {
+ result[k] = v
}
+ case "create_timestamp_flt", "rank_flt", "weight_flt":
+ if floatVal, ok := utility.ToFloat64(v); ok {
+ result[k] = utility.JSONFloat64(floatVal)
+ }
+ default:
+ result[k] = v
}
}
-
- if chunk == nil {
- return nil, fmt.Errorf("chunk not found")
- }
-
- return &GetChunkResponse{Chunk: chunk}, nil
+ return result
}
// ListChunksRequest request for listing chunks
@@ -677,7 +316,7 @@ func (s *ChunkService) List(req *ListChunksRequest, userID string) (*ListChunksR
utility.SetFieldArray(result, "question_kwd", v)
case "entities_kwd", "entity_kwd", "entity_type_kwd", "from_entity_kwd",
"name_kwd", "raptor_kwd", "removed_kwd",
- "source_id", "tag_kwd", "to_entity_kwd", "toc_kwd", "doc_type_kwd":
+ "source_id", "tag_kwd", "to_entity_kwd", "toc_kwd":
if utility.IsEmpty(v) {
result[k] = []interface{}{}
} else {
@@ -808,10 +447,12 @@ func (s *ChunkService) UpdateChunk(req *UpdateChunkRequest, userID string) error
if err != nil {
return fmt.Errorf("failed to get existing chunk: %w", err)
}
-
+ if existingChunk == nil {
+ return fmt.Errorf("chunk %q not found in dataset %q", req.ChunkID, req.DatasetID)
+ }
existing, ok := existingChunk.(map[string]interface{})
if !ok {
- return fmt.Errorf("invalid chunk format")
+ return fmt.Errorf("invalid chunk format: expected map[string]interface{}, got %T", existingChunk)
}
// Build update dict
diff --git a/internal/service/dataset.go b/internal/service/dataset.go
index e098c25322..7dde5458fa 100644
--- a/internal/service/dataset.go
+++ b/internal/service/dataset.go
@@ -17,19 +17,24 @@
package service
import (
+ "context"
"encoding/json"
"errors"
"fmt"
+ "ragflow/internal/common"
+ "ragflow/internal/dao"
+ "ragflow/internal/engine"
"ragflow/internal/entity"
+ "ragflow/internal/entity/models"
+ "ragflow/internal/server"
+ "ragflow/internal/service/nlp"
"ragflow/internal/utility"
"strings"
"time"
"github.com/google/uuid"
+ "go.uber.org/zap"
"gorm.io/gorm"
-
- "ragflow/internal/common"
- "ragflow/internal/dao"
)
var (
@@ -73,10 +78,16 @@ type DatasetService struct {
tenantDAO *dao.TenantDAO
tenantLLMDAO *dao.TenantLLMDAO
pipelineLogDAO *dao.PipelineOperationLogDAO
+ userTenantDAO *dao.UserTenantDAO
+ searchService *SearchService
+ docEngine engine.DocEngine
+ embeddingCache *utility.EmbeddingLRU
+ engineType server.EngineType
}
// NewDatasetService creates a new datasets service.
func NewDatasetService() *DatasetService {
+ cfg := server.GetConfig()
return &DatasetService{
kbDAO: dao.NewKnowledgebaseDAO(),
documentDAO: dao.NewDocumentDAO(),
@@ -84,7 +95,416 @@ func NewDatasetService() *DatasetService {
tenantDAO: dao.NewTenantDAO(),
tenantLLMDAO: dao.NewTenantLLMDAO(),
pipelineLogDAO: dao.NewPipelineOperationLogDAO(),
+ userTenantDAO: dao.NewUserTenantDAO(),
+ searchService: NewSearchService(),
+ docEngine: engine.Get(),
+ embeddingCache: utility.NewEmbeddingLRU(1000),
+ engineType: cfg.DocEngine.Type,
+ }
+}
+
+// SearchDatasetsRequest is the request structure for searching chunks across datasets.
+type SearchDatasetsRequest struct {
+ DatasetIDs []string `json:"dataset_ids" binding:"required"`
+ Question string `json:"question" binding:"required"`
+ Page *int `json:"page,omitempty"`
+ Size *int `json:"size,omitempty"`
+ DocIDs []string `json:"doc_ids,omitempty"`
+ UseKG *bool `json:"use_kg,omitempty"`
+ TopK *int `json:"top_k,omitempty"`
+ CrossLanguages []string `json:"cross_languages,omitempty"`
+ SearchID *string `json:"search_id,omitempty"`
+ MetadataFilter map[string]interface{} `json:"meta_data_filter,omitempty"`
+ RerankID *string `json:"rerank_id,omitempty"`
+ Keyword *bool `json:"keyword,omitempty"`
+ SimilarityThreshold *float64 `json:"similarity_threshold,omitempty"`
+ VectorSimilarityWeight *float64 `json:"vector_similarity_weight,omitempty"`
+}
+
+// SearchDatasetsResponse is the response structure for dataset search results.
+type SearchDatasetsResponse struct {
+ Chunks []map[string]interface{} `json:"chunks"`
+ DocAggs []map[string]interface{} `json:"doc_aggs"`
+ Labels *map[string]float64 `json:"labels"`
+ Total int64 `json:"total"`
+}
+
+// SearchDatasets searches chunks across one or more knowledge bases based on a question.
+// It retrieves relevant chunks using embedding and optional reranking, applying filters,
+// cross-language translation, and keyword extraction as configured.
+func (s *DatasetService) SearchDatasets(req *SearchDatasetsRequest, userID string) (*SearchDatasetsResponse, error) {
+ if req.Question == "" {
+ return nil, fmt.Errorf("question is required")
+ }
+ if len(req.DatasetIDs) == 0 {
+ return nil, fmt.Errorf("dataset_ids is required")
+ }
+ common.Info("SearchDatasets started", zap.String("userID", userID), zap.Any("datasets", req.DatasetIDs), zap.String("question", req.Question))
+
+ page := 1
+ if req.Page != nil {
+ page = *req.Page
+ }
+ pageSize := 30
+ if req.Size != nil {
+ pageSize = *req.Size
+ }
+ useKG := false
+ if req.UseKG != nil {
+ useKG = *req.UseKG
+ }
+ similarityThreshold := 0.0
+ if req.SimilarityThreshold != nil {
+ similarityThreshold = *req.SimilarityThreshold
+ }
+ vectorSimilarityWeight := 0.3
+ if req.VectorSimilarityWeight != nil {
+ vectorSimilarityWeight = *req.VectorSimilarityWeight
+ }
+ topK := 1024
+ if req.TopK != nil {
+ topK = *req.TopK
+ }
+ if topK < 1 {
+ topK = 1
+ } else if topK > 2048 {
+ topK = 2048
+ }
+ keyword := false
+ if req.Keyword != nil {
+ keyword = *req.Keyword
+ }
+ searchID := ""
+ if req.SearchID != nil {
+ searchID = *req.SearchID
+ }
+ rerankID := ""
+ if req.RerankID != nil {
+ rerankID = *req.RerankID
+ }
+
+ question := req.Question
+ datasetIDs := req.DatasetIDs
+ metadataFilter := req.MetadataFilter
+ crossLanguages := req.CrossLanguages
+
+ common.Debug(fmt.Sprintf("SearchDatasets request:\n"+
+ " datasetIDs=%v\n"+
+ " question=%s\n"+
+ " page=%v, pageSize=%v\n"+
+ " docIDs=%v\n"+
+ " useKG=%v, topK=%v\n"+
+ " crossLanguages=%v\n"+
+ " searchID=%v\n"+
+ " metadataFilter=%v\n"+
+ " rerankID=%v\n"+
+ " keyword=%v\n"+
+ " similarityThreshold=%v, vectorSimilarityWeight=%v",
+ datasetIDs, req.Question,
+ ptrString(req.Page), ptrString(req.Size), req.DocIDs,
+ useKG, topK, crossLanguages, searchID,
+ metadataFilter,
+ rerankID,
+ keyword,
+ similarityThreshold, vectorSimilarityWeight))
+
+ ctx := context.Background()
+ modelProviderSvc := NewModelProviderService()
+
+ // Access check for all datasets
+ var tenantIDs []string
+ var kbRecords []*entity.Knowledgebase
+ seenTenants := make(map[string]bool)
+ for _, datasetID := range datasetIDs {
+ if !s.kbDAO.Accessible(datasetID, userID) {
+ common.Warn("SearchDatasets access denied", zap.String("datasetID", datasetID), zap.String("userID", userID))
+ return nil, fmt.Errorf("only owner of dataset %s is authorized for this operation", datasetID)
+ }
+
+ kb, err := s.kbDAO.GetByID(datasetID)
+ if err != nil || kb == nil {
+ common.Warn("SearchDatasets dataset not found", zap.String("datasetID", datasetID))
+ return nil, fmt.Errorf("dataset %s not found", datasetID)
+ }
+ if !seenTenants[kb.TenantID] {
+ seenTenants[kb.TenantID] = true
+ tenantIDs = append(tenantIDs, kb.TenantID)
+ }
+ kbRecords = append(kbRecords, kb)
+ }
+
+ // Check if all kbs have the same embedding model
+ if len(kbRecords) > 1 {
+ firstEmbdID := kbRecords[0].EmbdID
+ for i := 1; i < len(kbRecords); i++ {
+ if kbRecords[i].EmbdID != firstEmbdID {
+ return nil, fmt.Errorf("cannot retrieve across datasets with different embedding models")
+ }
+ }
+ }
+
+ // Override request fields with values from saved search config (if search_id is provided)
+ var chatID string
+ if searchID != "" {
+ searchDetail, err := s.searchService.GetDetail(searchID)
+ if err != nil {
+ common.Warn("Failed to get search detail for search_id, proceeding without it", zap.String("searchID", searchID), zap.Error(err))
+ } else if searchConfig, ok := searchDetail["search_config"].(map[string]interface{}); ok && searchConfig != nil {
+ if scMetadataFilter, ok := searchConfig["meta_data_filter"].(map[string]interface{}); ok {
+ metadataFilter = scMetadataFilter
+ }
+ if scST, ok := searchConfig["similarity_threshold"].(float64); ok {
+ similarityThreshold = scST
+ }
+ if scVSW, ok := searchConfig["vector_similarity_weight"].(float64); ok {
+ vectorSimilarityWeight = scVSW
+ }
+ if scTopK, ok := searchConfig["top_k"].(float64); ok {
+ topK = int(scTopK)
+ if topK < 1 {
+ topK = 1
+ } else if topK > 2048 {
+ topK = 2048
+ }
+ }
+ if scUseKG, ok := searchConfig["use_kg"].(bool); ok {
+ useKG = scUseKG
+ }
+ if scLangs, ok := searchConfig["cross_languages"].([]interface{}); ok {
+ crossLanguages = make([]string, len(scLangs))
+ for i, l := range scLangs {
+ if s, ok := l.(string); ok {
+ crossLanguages[i] = s
+ }
+ }
+ }
+ if scKeyword, ok := searchConfig["keyword"].(bool); ok {
+ keyword = scKeyword
+ }
+ if scRerankID, ok := searchConfig["rerank_id"].(string); ok {
+ rerankID = scRerankID
+ }
+ chatID, _ = searchConfig["chat_id"].(string)
+
+ common.Debug("SearchDatasets loaded Search config",
+ zap.String("searchID", searchID),
+ zap.Strings("datasetIDs", datasetIDs),
+ zap.Float64("vectorSimilarityWeight", vectorSimilarityWeight),
+ zap.Float64("fullTextWeight", 1-vectorSimilarityWeight),
+ zap.Float64("similarityThreshold", similarityThreshold),
+ zap.Int("topK", topK),
+ zap.Strings("crossLanguages", crossLanguages),
+ zap.Bool("keyword", keyword),
+ zap.String("rerankID", rerankID),
+ zap.String("chatID", chatID),
+ zap.Bool("useKG", useKG))
+ } else {
+ common.Warn("No search_config found in search detail", zap.String("searchID", searchID))
+ }
+ }
+
+ // If meta_data_filter method is auto/semi_auto, get chat model
+ var err error
+ var chatModelForFilter *models.ChatModel
+ if metadataFilter != nil {
+ method, _ := metadataFilter["method"].(string)
+ if method == "auto" || method == "semi_auto" {
+ if chatID != "" {
+ driver, modelName, apiConfig, _, err := modelProviderSvc.GetModelConfigFromProviderInstance(tenantIDs[0], entity.ModelTypeChat, chatID)
+ if err != nil {
+ common.Warn("Failed to get chat model config from search_config chat_id, using tenant default", zap.String("chatID", chatID), zap.Error(err))
+ } else {
+ chatModelForFilter = models.NewChatModel(driver, &modelName, apiConfig)
+ common.Info("Fetched chat model (from search_config) for metadata filter",
+ zap.String("chatID", chatID),
+ zap.String("tenantID", tenantIDs[0]))
+ }
+ }
+
+ if chatModelForFilter == nil {
+ driver, modelName, apiConfig, _, err := modelProviderSvc.GetTenantDefaultModelByType(tenantIDs[0], entity.ModelTypeChat)
+ if err != nil {
+ common.Warn("Failed to get tenant default chat model for meta_data_filter", zap.Error(err))
+ } else {
+ chatModelForFilter = models.NewChatModel(driver, &modelName, apiConfig)
+ common.Info("Fetched chat model (tenant default) for metadata filter",
+ zap.String("tenantID", tenantIDs[0]))
+ }
+ }
+ }
+ }
+
+ // Apply meta_data_filter to get filtered doc_ids
+ docIDs := make([]string, len(req.DocIDs))
+ copy(docIDs, req.DocIDs)
+ if metadataFilter != nil {
+ metadataSvc := NewMetadataService()
+ flattedMeta, err := metadataSvc.GetFlattedMetaByKBs(datasetIDs)
+ if err != nil {
+ common.Warn("Failed to get flatted metadata, using empty metadata for filter", zap.Error(err))
+ flattedMeta = make(common.MetaData)
+ }
+ common.Info("Metadata filter conditions", zap.Any("filter", metadataFilter))
+ filteredDocIDs, _ := ApplyMetaDataFilter(ctx, metadataFilter, flattedMeta, question, chatModelForFilter, req.DocIDs, datasetIDs)
+ docIDs = filteredDocIDs
+ common.Info("ApplyMetaDataFilter result", zap.Strings("docIDs", docIDs))
+ }
+
+ // Apply cross_languages and keyword extraction
+ modifiedQuestion := question
+ if len(crossLanguages) > 0 {
+ // Pass tenantID and empty llmID so CrossLanguages can fetch default if needed
+ // This matches Python's cross_languages(tenant_id, llm_id, query, languages)
+ common.Info("CrossLanguages: dispatching translation",
+ zap.String("tenantID", tenantIDs[0]),
+ zap.String("llmID", ""),
+ zap.Strings("crossLanguages", crossLanguages))
+ translated, err := CrossLanguages(ctx, tenantIDs[0], "", question, crossLanguages)
+ if err != nil {
+ common.Warn("Failed to translate question", zap.String("llmID", ""), zap.Error(err))
+ } else {
+ modifiedQuestion = translated
+ }
+ }
+ if keyword {
+ driver, modelName, apiConfig, _, err := modelProviderSvc.GetTenantDefaultModelByType(tenantIDs[0], entity.ModelTypeChat)
+ if err != nil {
+ common.Warn("Failed to get default chat model for LLM transformations", zap.Error(err))
+ } else {
+ chatModel := models.NewChatModel(driver, &modelName, apiConfig)
+ common.Info("Fetched chat model (tenant default) for keyword_extraction",
+ zap.String("tenantID", tenantIDs[0]))
+
+ extractedKeywords, err := KeywordExtraction(ctx, chatModel, modifiedQuestion, 3)
+ if err != nil {
+ common.Warn("Failed to extract keywords from question", zap.Error(err))
+ } else if extractedKeywords != "" {
+ modifiedQuestion = modifiedQuestion + extractedKeywords
+ }
+ }
+ }
+ if modifiedQuestion != question {
+ common.Info("Modified question after transformations",
+ zap.String("originalQuestion", question),
+ zap.String("modifiedQuestion", modifiedQuestion),
+ zap.Strings("crossLanguages", crossLanguages),
+ zap.Bool("keywordExtraction", keyword))
+ }
+
+ // Get tag-based rank features via LabelQuestion
+ metadataSvc := NewMetadataService()
+ labels := metadataSvc.LabelQuestion(modifiedQuestion, kbRecords)
+ if len(labels) > 0 {
+ common.Debug("LabelQuestion result", zap.Any("labels", labels))
+ }
+
+ // Determine embedding model
+ var embeddingModel *models.EmbeddingModel
+ if kbRecords[0].EmbdID != "" {
+ driver, modelName, apiConfig, maxTokens, embErr := modelProviderSvc.GetModelConfigFromProviderInstance(tenantIDs[0], entity.ModelTypeEmbedding, kbRecords[0].EmbdID)
+ if embErr != nil {
+ return nil, fmt.Errorf("failed to get embedding model by embd_id: %w", embErr)
+ }
+ embeddingModel = models.NewEmbeddingModel(driver, &modelName, apiConfig, maxTokens)
+ } else {
+ driver, modelName, apiConfig, maxTokens, err := modelProviderSvc.GetTenantDefaultModelByType(tenantIDs[0], entity.ModelTypeEmbedding)
+ if err != nil {
+ return nil, fmt.Errorf("failed to get tenant default embedding model: %w", err)
+ }
+ embeddingModel = models.NewEmbeddingModel(driver, &modelName, apiConfig, maxTokens)
+ }
+ modelNameStr := ""
+ if embeddingModel.ModelName != nil {
+ modelNameStr = *embeddingModel.ModelName
+ }
+ common.Info("Fetched embedding model for retrieval",
+ zap.String("tenantID", tenantIDs[0]),
+ zap.String("modelName", modelNameStr))
+
+ // Get rerank model if rerankID is specified
+ var rerankModel *models.RerankModel
+
+ if rerankID != "" {
+ driver, modelName, apiConfig, _, rErr := modelProviderSvc.GetModelConfigFromProviderInstance(tenantIDs[0], entity.ModelTypeRerank, rerankID)
+ if rErr != nil {
+ return nil, fmt.Errorf("failed to get rerank model by rerank_id: %w", rErr)
+ }
+ rerankModel = models.NewRerankModel(driver, &modelName, apiConfig)
+ }
+
+ if rerankModel != nil {
+ common.Info("Fetched rerank model",
+ zap.String("tenantID", tenantIDs[0]),
+ zap.String("modelName", *rerankModel.ModelName))
+ }
+
+ retrievalReq := &nlp.RetrievalRequest{
+ TenantIDs: tenantIDs,
+ Question: modifiedQuestion,
+ KbIDs: datasetIDs,
+ DocIDs: docIDs,
+ Page: page,
+ PageSize: pageSize,
+ Top: &topK,
+ SimilarityThreshold: &similarityThreshold,
+ VectorSimilarityWeight: &vectorSimilarityWeight,
+ RerankModel: rerankModel,
+ RankFeature: &labels,
+ EmbeddingModel: embeddingModel,
+ }
+
+ retrievalResult, err := nlp.NewRetrievalService(s.docEngine, s.documentDAO).Retrieval(ctx, retrievalReq)
+ if err != nil {
+ return nil, fmt.Errorf("retrieval search failed: %w", err)
+ }
+
+ filteredChunks := retrievalResult.Chunks
+
+ if useKG {
+ common.Warn("use_kg is not yet implemented in Go - skipping KG retrieval")
+ }
+
+ filteredChunks = nlp.RetrievalByChildren(filteredChunks, tenantIDs, s.docEngine, ctx)
+
+ for i := range filteredChunks {
+ delete(filteredChunks[i], "vector")
+ }
+
+ common.Info("SearchDatasets completed", zap.String("userID", userID), zap.Any("kbID", datasetIDs), zap.String("question", question), zap.Int64("chunkCount", int64(len(filteredChunks))))
+
+ // Convert all float64 values to PyFloat64 for Python-compatible JSON serialization
+ pyChunks := common.ConvertFloatsToPyFormat(filteredChunks).([]map[string]interface{})
+
+ return &SearchDatasetsResponse{
+ Chunks: pyChunks,
+ DocAggs: retrievalResult.DocAggs,
+ Labels: &labels,
+ Total: retrievalResult.Total,
+ }, nil
+}
+
+// Helper functions
+
+// ptrString converts a pointer to a formatted string
+func ptrString[T any](p *T) string {
+ if p == nil {
+ return "<nil>"
+ }
+ return fmt.Sprintf("%v", *p)
+}
+
+func getPageNum(page *int, defaultVal int) int {
+ if page != nil && *page > 0 {
+ return *page
+ }
+ return defaultVal
+}
+
+func getPageSize(size *int, defaultVal int) int {
+ if size != nil && *size > 0 {
+ return *size
}
+ return defaultVal
}
// AutoMetadataField mirrors the REST dataset auto metadata field schema.
diff --git a/internal/service/document.go b/internal/service/document.go
index ceaef93ba8..7eee8aacc3 100644
--- a/internal/service/document.go
+++ b/internal/service/document.go
@@ -44,15 +44,15 @@ import (
// DocumentService document service
type DocumentService struct {
- documentDAO *dao.DocumentDAO
- kbDAO *dao.KnowledgebaseDAO
- ingestionTaskDAO *dao.IngestionDAO
- ingestionLogDAO *dao.IngestionLogDAO
- docEngine engine.DocEngine
- engineType server.EngineType
- metadataSvc *MetadataService
- taskDAO *dao.TaskDAO
- file2DocumentDAO *dao.File2DocumentDAO
+ documentDAO *dao.DocumentDAO
+ kbDAO *dao.KnowledgebaseDAO
+ ingestionTaskDAO *dao.IngestionDAO
+ ingestionLogDAO *dao.IngestionLogDAO
+ docEngine engine.DocEngine
+ engineType server.EngineType
+ metadataSvc *MetadataService
+ taskDAO *dao.TaskDAO
+ file2DocumentDAO *dao.File2DocumentDAO
}
// NewDocumentService create document service
@@ -915,7 +915,7 @@ func (s *DocumentService) GetMetadataSummary(kbID string, docIDs []string) (map[
}
// Aggregate metadata from results
- return aggregateMetadata(searchResult.Chunks), nil
+ return aggregateMetadata(searchResult.MetadataRecords), nil
}
// SetDocumentMetadata sets metadata for a document in the document engine
@@ -1012,9 +1012,9 @@ func (s *DocumentService) GetDocumentMetadataByID(docID string) (map[string]inte
}
// Return metadata if found
- if len(searchResult.Chunks) > 0 {
- chunk := searchResult.Chunks[0]
- return ExtractMetaFields(chunk)
+ if len(searchResult.MetadataRecords) > 0 {
+ metadata := searchResult.MetadataRecords[0]
+ return ExtractMetaFields(metadata)
}
return make(map[string]interface{}), nil
@@ -1032,20 +1032,20 @@ func (s *DocumentService) GetMetadataByKBs(kbIDs []string) (map[string]interface
}
flattenedMeta := make(map[string]map[string][]string)
- numChunks := len(searchResult.Chunks)
+ numMetadata := len(searchResult.MetadataRecords)
var allMetaFields []map[string]interface{}
- if numChunks > 1 && len(searchResult.Chunks) > 0 {
- firstChunk := searchResult.Chunks[0]
- if metaFieldsVal := firstChunk["meta_fields"]; metaFieldsVal != nil {
+ if numMetadata > 1 && len(searchResult.MetadataRecords) > 0 {
+ firstMetadata := searchResult.MetadataRecords[0]
+ if metaFieldsVal := firstMetadata["meta_fields"]; metaFieldsVal != nil {
if v, ok := metaFieldsVal.([]byte); ok {
allMetaFields = ParseAllLengthPrefixedJSON(v)
}
}
}
- for idx, chunk := range searchResult.Chunks {
- docID, ok := ExtractDocumentID(chunk)
+ for idx, metadata := range searchResult.MetadataRecords {
+ docID, ok := ExtractDocumentID(metadata)
if !ok {
continue
}
@@ -1058,7 +1058,7 @@ func (s *DocumentService) GetMetadataByKBs(kbIDs []string) (map[string]interface
metaFields = allMetaFields[idx]
} else {
// Normal case - get from chunk
- metaFieldsVal = chunk["meta_fields"]
+ metaFieldsVal = metadata["meta_fields"]
if metaFieldsVal != nil {
switch v := metaFieldsVal.(type) {
case string:
diff --git a/internal/service/generator.go b/internal/service/generator.go
index 074eab6599..ede56dee39 100644
--- a/internal/service/generator.go
+++ b/internal/service/generator.go
@@ -20,6 +20,7 @@ import (
"context"
"fmt"
"ragflow/internal/common"
+ "ragflow/internal/entity"
"regexp"
"strings"
@@ -94,9 +95,45 @@ func KeywordExtraction(ctx context.Context, chatModel *modelModule.ChatModel, co
}
// CrossLanguages translates a question into multiple languages using LLM.
-func CrossLanguages(ctx context.Context, chatModel *modelModule.ChatModel, query string, languages []string) (string, error) {
+// The model is fetched internally based on llmID:
+// - If llmID is empty, fetches tenant's default chat model
+// - If llmID is not empty, fetches the specified model (or image2text if type matches)
+func CrossLanguages(ctx context.Context, tenantID string, llmID string, query string, languages []string) (string, error) {
+ common.Debug("CrossLanguages invoked",
+ zap.String("tenantID", tenantID),
+ zap.String("llmID", llmID),
+ zap.Strings("languages", languages))
+
+ modelProviderSvc := NewModelProviderService()
+ var chatModel *modelModule.ChatModel
+ var err error
+
+ if llmID != "" {
+ modelTypes, err := modelProviderSvc.GetModelTypeByName(tenantID, llmID)
+ if err != nil {
+ return query, fmt.Errorf("failed to get model type: %w", err)
+ }
+ resolvedType := entity.ModelTypeChat
+ for _, mt := range modelTypes {
+ if mt == entity.ModelTypeImage2Text {
+ resolvedType = entity.ModelTypeImage2Text
+ break
+ }
+ }
+ driver, modelName, apiConfig, _, err := modelProviderSvc.GetModelConfigFromProviderInstance(tenantID, resolvedType, llmID)
+ if err != nil {
+ return query, fmt.Errorf("failed to get chat model: %w", err)
+ }
+ chatModel = modelModule.NewChatModel(driver, &modelName, apiConfig)
+ } else {
+ driver, modelName, apiConfig, _, err := modelProviderSvc.GetTenantDefaultModelByType(tenantID, entity.ModelTypeChat)
+ if err != nil {
+ return query, fmt.Errorf("failed to get default chat model: %w", err)
+ }
+ chatModel = modelModule.NewChatModel(driver, &modelName, apiConfig)
+ }
if chatModel == nil {
- return "", fmt.Errorf("chat model is nil")
+ return query, fmt.Errorf("failed to get chat model: nil chat model")
}
if query == "" {
@@ -149,20 +186,15 @@ func CrossLanguages(ctx context.Context, chatModel *modelModule.ChatModel, query
result := *response.Answer
// Clean up response - remove think tags and trim
- result = strings.TrimSpace(result)
result = thinkBlockRE.ReplaceAllString(result, "")
- result = strings.TrimSpace(result)
if strings.Contains(result, "**ERROR**") {
return query, nil
}
// Parse response
- result = strings.TrimPrefix(result, "Output:")
- result = strings.TrimPrefix(result, "output:")
result = regexp.MustCompile(`(?i)^output:\s*`).ReplaceAllString(result, "")
result = regexp.MustCompile(`\n+`).ReplaceAllString(result, "")
- result = strings.TrimSpace(result)
parts := strings.Split(result, "===")
var translations []string
diff --git a/internal/service/load_prompt.go b/internal/service/load_prompt.go
index 138a88822e..80a8e898e0 100644
--- a/internal/service/load_prompt.go
+++ b/internal/service/load_prompt.go
@@ -31,8 +31,28 @@ var (
promptsBaseDir string
)
-// thinkBlockRE is used to strip think blocks from LLM responses
-var thinkBlockRE = regexp.MustCompile(`<think>[\s\S]*?`)
+// thinkBlockRE strips think blocks from LLM responses.
+//
+// This mirrors the Python original
+//
+// re.sub(r"^.*</think>", "", ans, flags=re.DOTALL)
+//
+// exactly: `.*` with re.DOTALL is greedy, so the regex consumes
+// everything from the start of the string up to and including the
+// LAST </think> on the input. The Go form uses [\s\S] (Go's
+// newline-aware any-char) and a greedy `*`. A non-greedy `*?` here
+// would diverge for responses containing more than one </think>
+// (e.g. malformed streams that re-emit a partial think block
+// after the real answer), stripping only the first and leaving the
+// rest of the response invisible to the caller.
+var thinkBlockRE = regexp.MustCompile(`^[\s\S]*</think>`)
+
+// jsonFenceRE matches markdown code fences around JSON responses.
+// Mirrors Python's re.sub(r"(`{3}json\n|`{3}\n*$)", ..., flags=re.DOTALL).
+// Note: `\n*` is intentionally narrower than Go's `\s*` — Python only
+// matches newlines, not other whitespace, so a closing fence followed
+// by trailing spaces (e.g. "``` \n") is left intact.
+var jsonFenceRE = regexp.MustCompile("```json\\n|```\\n*$")
func init() {
// Strategy 1: Check working directory first (most reliable during development/tests)
diff --git a/internal/service/load_prompt_test.go b/internal/service/load_prompt_test.go
new file mode 100644
index 0000000000..81b0f32899
--- /dev/null
+++ b/internal/service/load_prompt_test.go
@@ -0,0 +1,180 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package service
+
+import "testing"
+
+// TestThinkBlockREParityWithPython pins down the exact behaviour of
+// thinkBlockRE so it stays in sync with the Python original
+//
+// re.sub(r"^.*</think>", "", ans, flags=re.DOTALL)
+//
+// The Python regex is greedy: with re.DOTALL the leading `.*` matches
+// as much as possible, so for an input with more than one </think>
+// the substitution strips everything up to and including the LAST
+// marker. A non-greedy `*?` would diverge here and leave the
+// tail-visible-portion of the response behind.
+// TestJSONFenceREParityWithPython pins down jsonFenceRE against the Python
+// original
+//
+// re.sub(r"(^.*</think>|```json\n|```\n*$)", "", ans, flags=re.DOTALL)
+//
+// The Go port is split across thinkBlockRE (run first in callers) and
+// jsonFenceRE; this test exercises jsonFenceRE in isolation. The trailing
+// alternative uses \n* (not \s*) so a closing fence followed by other
+// whitespace — e.g. "``` \n" — is preserved exactly as Python does.
+func TestJSONFenceREParityWithPython(t *testing.T) {
+ tests := []struct {
+ name string
+ in string
+ want string
+ }{
+ {
+ name: "opening_json_fence_stripped",
+ in: "```json\n{\"a\":1}\n",
+ want: "{\"a\":1}\n",
+ },
+ {
+ name: "opening_json_fence_no_content_newline",
+ in: "```json\n{\"a\":1}",
+ want: "{\"a\":1}", // \n is the separator between ```json and the body
+ },
+ {
+ name: "trailing_fence_with_newline_stripped",
+ in: "{\"a\":1}\n```\n",
+ want: "{\"a\":1}\n",
+ },
+ {
+ name: "trailing_fence_no_newline_stripped",
+ in: "{\"a\":1}\n```",
+ want: "{\"a\":1}\n", // \n* matches the existing \n before ```
+ },
+ {
+ name: "trailing_fence_no_preceding_newline_stripped",
+ in: "{\"a\":1}```",
+ want: "{\"a\":1}",
+ },
+ {
+ name: "trailing_fence_with_spaces_preserved",
+ // Python leaves this alone: \n* does not match spaces.
+ // A `\s*` form would strip it.
+ in: "``` \n",
+ want: "``` \n",
+ },
+ {
+ name: "bare_fence_stripped",
+ in: "```",
+ want: "",
+ },
+ {
+ name: "two_opening_fences_both_stripped",
+ // The opening alternative is not anchored, so a second
+ // ```json\n mid-stream is also stripped.
+ in: "```json\n{\"a\":1}\n```\n```json\n{\"a\":2}",
+ want: "{\"a\":1}\n```\n{\"a\":2}",
+ },
+ {
+ name: "mid_text_closing_fence_not_stripped",
+ // The closing alternative has a $ anchor, so a bare
+ // ```\n in the middle of the string is NOT stripped.
+ in: "{\"a\":1}\n```\n{\"a\":2}",
+ want: "{\"a\":1}\n```\n{\"a\":2}",
+ },
+ {
+ name: "plain_text_unchanged",
+ in: "plain text response",
+ want: "plain text response",
+ },
+ {
+ name: "open_close_pair",
+ in: "```json\n{\"a\":1}\n```",
+ want: "{\"a\":1}\n",
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got := jsonFenceRE.ReplaceAllString(tt.in, "")
+ if got != tt.want {
+ t.Errorf("input: %q\n got: %q\n want: %q", tt.in, got, tt.want)
+ }
+ })
+ }
+}
+
+func TestThinkBlockREParityWithPython(t *testing.T) {
+ tests := []struct {
+ name string
+ in string
+ want string
+ }{
+ {
+ name: "no_think_tag_unchanged",
+ in: "plain answer, no tags",
+ want: "plain answer, no tags",
+ },
+ {
+ name: "single_think_block_stripped",
+ in: "<think>hidden reasoning</think>visible answer",
+ want: "visible answer",
+ },
+ {
+ name: "multiline_single_think_block",
+ in: "<think>\nline 1\nline 2\n</think>\nvisible",
+ want: "\nvisible",
+ },
+ {
+ name: "two_think_blocks_greedy_strips_to_last",
+ // Python: greedy `^.*</think>` strips
+ // "<think>A</think>part1<think>B</think>", leaving "part2".
+ // A non-greedy form would have left "part1<think>B</think>part2".
+ in: "<think>A</think>part1<think>B</think>part2",
+ want: "part2",
+ },
+ {
+ name: "two_think_blocks_with_answer_greedy",
+ // Mirrors a real-world malformed stream where the model
+ // re-emits a stray </think> after the answer.
+ in: "<think>reasoning</think>Answer<think>noise</think>real tail",
+ want: "real tail",
+ },
+ {
+ name: "unclosed_think_tag_does_not_match",
+ // No </think> at all — the regex requires the closing tag,
+ // so nothing is stripped and the original passes through.
+ in: "<think>no closing tag here",
+ want: "<think>no closing tag here",
+ },
+ {
+ name: "lookalike_closing_tag_strips_everything",
+ // Quirky but intentional parity case: the regex only requires
+ // the substring </think> to be present, regardless of whether
+ // an opening <think> exists. The Python original has the same
+ // behaviour, so the Go port must match — this is a
+ // pre-existing limitation, not a new bug.
+ in: "use </tag> to mean end, not the same as </think>",
+ want: "",
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got := thinkBlockRE.ReplaceAllString(tt.in, "")
+ if got != tt.want {
+ t.Errorf("input: %q\n got: %q\n want: %q", tt.in, got, tt.want)
+ }
+ })
+ }
+}
diff --git a/internal/service/metadata.go b/internal/service/metadata.go
index 109be64ad2..0f8c1c1e20 100644
--- a/internal/service/metadata.go
+++ b/internal/service/metadata.go
@@ -57,11 +57,7 @@ func BuildMetadataIndexName(tenantID string) string {
// GetTenantIDByKBID retrieves tenant ID from knowledge base ID
func (s *MetadataService) GetTenantIDByKBID(kbID string) (string, error) {
- kb, err := s.kbDAO.GetByID(kbID)
- if err != nil {
- return "", fmt.Errorf("knowledgebase not found: %w", err)
- }
- return kb.TenantID, nil
+ return dao.GetTenantIDByKBID(kbID)
}
// GetTenantIDByKBIDs retrieves tenant ID from the first knowledge base ID in the list
@@ -69,48 +65,42 @@ func (s *MetadataService) GetTenantIDByKBIDs(kbIDs []string) (string, error) {
if len(kbIDs) == 0 {
return "", fmt.Errorf("no kb_ids provided")
}
- kb, err := s.kbDAO.GetByID(kbIDs[0])
- if err != nil {
- return "", fmt.Errorf("knowledgebase not found: %w", err)
- }
- return kb.TenantID, nil
+ return dao.GetTenantIDByKBID(kbIDs[0])
}
-// SearchMetadataResult holds the result of a metadata search
-type SearchMetadataResult struct {
+// SearchMetadataResponse holds the result of a metadata search
+type SearchMetadataResponse struct {
IndexName string
- Chunks []map[string]interface{}
+ MetadataRecords []map[string]interface{}
}
// SearchMetadata searches the metadata index with the given parameters
-func (s *MetadataService) SearchMetadata(kbID, tenantID string, docIDs []string, size int) (*SearchMetadataResult, error) {
- indexName := BuildMetadataIndexName(tenantID)
-
- searchReq := &types.SearchRequest{
- IndexNames: []string{indexName},
- KbIDs: []string{kbID},
- Offset: 0,
- Limit: size,
+func (s *MetadataService) SearchMetadata(kbID, tenantID string, docIDs []string, size int) (*SearchMetadataResponse, error) {
+ searchReq := &types.SearchMetadataRequest{
+ TenantID: tenantID,
+ Offset: 0,
+ Limit: size,
Filter: map[string]interface{}{
- "doc_id": docIDs,
+ "id": docIDs,
+ "kb_id": kbID,
},
}
- searchResult, err := s.docEngine.Search(context.Background(), searchReq)
+ searchResult, err := s.docEngine.SearchMetadata(context.Background(), searchReq)
if err != nil {
return nil, fmt.Errorf("search failed: %w", err)
}
- return &SearchMetadataResult{
- IndexName: indexName,
- Chunks: searchResult.Chunks,
+ return &SearchMetadataResponse{
+ IndexName: BuildMetadataIndexName(tenantID),
+ MetadataRecords: searchResult.MetadataRecords,
}, nil
}
// SearchMetadataByKBs searches the metadata index for multiple knowledge bases
-func (s *MetadataService) SearchMetadataByKBs(kbIDs []string, size int) (*SearchMetadataResult, error) {
+func (s *MetadataService) SearchMetadataByKBs(kbIDs []string, size int) (*SearchMetadataResponse, error) {
if len(kbIDs) == 0 {
- return &SearchMetadataResult{Chunks: []map[string]interface{}{}}, nil
+ return &SearchMetadataResponse{MetadataRecords: []map[string]interface{}{}}, nil
}
tenantID, err := s.GetTenantIDByKBIDs(kbIDs)
@@ -118,23 +108,23 @@ func (s *MetadataService) SearchMetadataByKBs(kbIDs []string, size int) (*Search
return nil, err
}
- indexName := BuildMetadataIndexName(tenantID)
-
- searchReq := &types.SearchRequest{
- IndexNames: []string{indexName},
- KbIDs: kbIDs,
- Offset: 0,
- Limit: size,
+ searchReq := &types.SearchMetadataRequest{
+ TenantID: tenantID,
+ Offset: 0,
+ Limit: size,
+ Filter: map[string]interface{}{
+ "kb_id": kbIDs,
+ },
}
- searchResult, err := s.docEngine.Search(context.Background(), searchReq)
+ searchResult, err := s.docEngine.SearchMetadata(context.Background(), searchReq)
if err != nil {
return nil, fmt.Errorf("search failed: %w", err)
}
- return &SearchMetadataResult{
- IndexName: indexName,
- Chunks: searchResult.Chunks,
+ return &SearchMetadataResponse{
+ IndexName: BuildMetadataIndexName(tenantID),
+ MetadataRecords: searchResult.MetadataRecords,
}, nil
}
@@ -153,7 +143,7 @@ func (s *MetadataService) GetFlattedMetaByKBs(kbIDs []string) (common.MetaData,
flattedMeta := make(common.MetaData)
- for _, chunk := range result.Chunks {
+ for _, chunk := range result.MetadataRecords {
// Extract doc_id from chunk
docID := ""
if id, ok := chunk["id"].(string); ok {
@@ -293,7 +283,7 @@ func (s *MetadataService) FetchDocMetaByKB(docIDsByKB KBDocIDsMap, tenantID stri
if err != nil {
continue
}
- for docID, meta := range ConvertSearchResultToDocMeta(result.Chunks) {
+ for docID, meta := range ConvertSearchResultToDocMeta(result.MetadataRecords) {
metaByDoc[docID] = meta
}
}
diff --git a/internal/service/metadata_filter.go b/internal/service/metadata_filter.go
index d8b8dd161c..9919f1f571 100644
--- a/internal/service/metadata_filter.go
+++ b/internal/service/metadata_filter.go
@@ -22,7 +22,9 @@ import (
"fmt"
"os"
"ragflow/internal/common"
+ "ragflow/internal/engine"
"regexp"
+ "strconv"
"strings"
"time"
@@ -33,9 +35,9 @@ import (
// MetaFilterCondition represents a single filter condition
type MetaFilterCondition struct {
- Key string `json:"key"`
- Value string `json:"value"`
- Op string `json:"op"`
+ Key string `json:"key"`
+ Value interface{} `json:"value"`
+ Op string `json:"op"`
}
// MetaFilterResult represents the result of LLM-generated filter
@@ -44,6 +46,40 @@ type MetaFilterResult struct {
Logic string `json:"logic"`
}
+// compareValues compares two metadata values for relational operators
+// (>, <, >=, <=). It attempts numeric comparison first so that
+// lexicographic ordering like "10" < "2" or "100" < "20" no longer
+// produces wrong results for years, prices, counts, etc. If either
+// operand is not a valid number, it falls back to lexicographic string
+// comparison so non-numeric metadata (names, tags, etc.) still works.
+func compareValues(val1, val2, op string) bool {
+ if f1, err1 := strconv.ParseFloat(val1, 64); err1 == nil {
+ if f2, err2 := strconv.ParseFloat(val2, 64); err2 == nil {
+ switch op {
+ case ">":
+ return f1 > f2
+ case "<":
+ return f1 < f2
+ case ">=":
+ return f1 >= f2
+ case "<=":
+ return f1 <= f2
+ }
+ }
+ }
+ switch op {
+ case ">":
+ return val1 > val2
+ case "<":
+ return val1 < val2
+ case ">=":
+ return val1 >= val2
+ case "<=":
+ return val1 <= val2
+ }
+ return false
+}
+
// ManualValueResolver is a callback function to transform manual filter values
type ManualValueResolver func(map[string]interface{}) map[string]interface{}
@@ -166,8 +202,8 @@ func GenMetaFilter(ctx context.Context, chatModel *modelModule.ChatModel, metaDa
if err != nil {
common.Warn("ChatWithMessages failed for GenMetaFilter",
zap.String("model",
-
- *chatModel.ModelName),
+
+ *chatModel.ModelName),
zap.Error(err))
return nil, fmt.Errorf("failed to generate meta filter: %w", err)
}
@@ -179,19 +215,20 @@ func GenMetaFilter(ctx context.Context, chatModel *modelModule.ChatModel, metaDa
// Clean up response
responseStr := strings.TrimSpace(*response.Answer)
responseStr = thinkBlockRE.ReplaceAllString(responseStr, "")
+ responseStr = jsonFenceRE.ReplaceAllString(responseStr, "")
responseStr = strings.TrimSpace(responseStr)
- // Remove markdown code blocks if present
- responseStr = strings.TrimPrefix(responseStr, "```json")
- responseStr = strings.TrimPrefix(responseStr, "```")
- responseStr = strings.TrimSuffix(responseStr, "```")
- responseStr = strings.TrimSpace(responseStr)
-
- // Parse JSON
+ // Parse JSON with repair — try standard parsing first, then attempt repairs
var result MetaFilterResult
if err := json.Unmarshal([]byte(responseStr), &result); err != nil {
- common.Warn("Failed to parse meta filter response, returning empty conditions", zap.Error(err))
- return &MetaFilterResult{Conditions: []MetaFilterCondition{}, Logic: "and"}, nil
+ // Attempt JSON repair for common LLM output issues
+ repaired := repairJSON(responseStr)
+ if err2 := json.Unmarshal([]byte(repaired), &result); err2 != nil {
+ common.Warn("Failed to parse meta filter response after repair",
+ zap.String("raw", responseStr[:min(len(responseStr), 200)]),
+ zap.Error(err))
+ return &MetaFilterResult{Conditions: []MetaFilterCondition{}, Logic: "and"}, nil
+ }
}
common.Info("GenMetaFilter result", zap.Any("conditions", result.Conditions), zap.String("logic", result.Logic))
@@ -239,22 +276,303 @@ func convertToMetaCondition(f MetaFilterCondition) common.MetaCondition {
}
switch f.Op {
case "in", "not in":
- parts := strings.Split(f.Value, ",")
+ strVal, _ := f.Value.(string)
+ parts := strings.Split(strVal, ",")
arr := make([]interface{}, 0, len(parts))
for _, p := range parts {
if trimmed := strings.TrimSpace(p); trimmed != "" { arr = append(arr, trimmed) }
+ }
+ mc.Value = arr
+ }
+ return mc
+}
+
+// applySingleCondition applies a single filter condition and returns matching doc IDs
+func applySingleCondition(metaData map[string]interface{}, condition MetaFilterCondition) []string {
+ key := condition.Key
+ rawValue := condition.Value
+ op := condition.Op
+
+ // For most operators, value is a single string; only "in" / "not in" accept lists.
+ strValue := fmt.Sprintf("%v", rawValue)
+
+ valueMap, ok := metaData[key].(map[string]interface{})
+ if !ok {
+ return []string{}
+ }
+
+ var result []string
+
+ switch op {
+ case "=", "==":
+ if docIDs, exists := valueMap[strValue]; exists {
+ switch v := docIDs.(type) {
+ case []interface{}:
+ for _, id := range v {
+ if idStr, ok := id.(string); ok {
+ result = append(result, idStr)
+ }
+ }
+ case []string:
+ result = append(result, v...)
+ }
+ }
+ case "!=", "≠":
+ for val, docIDs := range valueMap {
+ if strings.EqualFold(val, strValue) {
+ continue
+ }
+ switch v := docIDs.(type) {
+ case []interface{}:
+ for _, id := range v {
+ if idStr, ok := id.(string); ok {
+ result = append(result, idStr)
+ }
+ }
+ case []string:
+ result = append(result, v...)
+ }
+ }
+ case "contains":
+ for val, docIDs := range valueMap {
+ if strings.Contains(strings.ToLower(val), strings.ToLower(strValue)) {
+ switch v := docIDs.(type) {
+ case []interface{}:
+ for _, id := range v {
+ if idStr, ok := id.(string); ok {
+ result = append(result, idStr)
+ }
+ }
+ case []string:
+ result = append(result, v...)
+ }
+ }
+ }
+ case "not contains":
+ for val, docIDs := range valueMap {
+ if !strings.Contains(strings.ToLower(val), strings.ToLower(strValue)) {
+ switch v := docIDs.(type) {
+ case []interface{}:
+ for _, id := range v {
+ if idStr, ok := id.(string); ok {
+ result = append(result, idStr)
+ }
+ }
+ case []string:
+ result = append(result, v...)
+ }
+ }
+ }
+ case "in":
+ inValues := metaFilterValues(rawValue)
+ for _, v := range inValues {
+ if docIDs, exists := valueMap[v]; exists {
+ switch ids := docIDs.(type) {
+ case []interface{}:
+ for _, id := range ids {
+ if idStr, ok := id.(string); ok {
+ result = append(result, idStr)
+ }
+ }
+ case []string:
+ result = append(result, ids...)
+ }
+ }
+ }
+ case "not in":
+ excludeValues := make(map[string]bool)
+ for _, v := range metaFilterValues(rawValue) {
+ excludeValues[strings.ToLower(v)] = true
+ }
+ for val, docIDs := range valueMap {
+ if !excludeValues[strings.ToLower(val)] {
+ switch v := docIDs.(type) {
+ case []interface{}:
+ for _, id := range v {
+ if idStr, ok := id.(string); ok {
+ result = append(result, idStr)
+ }
+ }
+ case []string:
+ result = append(result, v...)
+ }
+ }
+ }
+ case "start with":
+ for val, docIDs := range valueMap {
+ if strings.HasPrefix(strings.ToLower(val), strings.ToLower(strValue)) {
+ switch v := docIDs.(type) {
+ case []interface{}:
+ for _, id := range v {
+ if idStr, ok := id.(string); ok {
+ result = append(result, idStr)
+ }
+ }
+ case []string:
+ result = append(result, v...)
+ }
+ }
+ }
+ case "end with":
+ for val, docIDs := range valueMap {
+ if strings.HasSuffix(strings.ToLower(val), strings.ToLower(strValue)) {
+ switch v := docIDs.(type) {
+ case []interface{}:
+ for _, id := range v {
+ if idStr, ok := id.(string); ok {
+ result = append(result, idStr)
+ }
+ }
+ case []string:
+ result = append(result, v...)
+ }
+ }
+ }
+ case "empty":
+ if len(valueMap) == 0 {
+ return []string{}
+ }
+ case "not empty":
+ if len(valueMap) > 0 {
+ for _, docIDs := range valueMap {
+ switch v := docIDs.(type) {
+ case []interface{}:
+ for _, id := range v {
+ if idStr, ok := id.(string); ok {
+ result = append(result, idStr)
+ }
+ }
+ case []string:
+ result = append(result, v...)
+ }
+ }
+ }
+ case ">":
+ for val, docIDs := range valueMap {
+ if compareValues(val, strValue, ">") {
+ switch v := docIDs.(type) {
+ case []interface{}:
+ for _, id := range v {
+ if idStr, ok := id.(string); ok {
+ result = append(result, idStr)
+ }
+ }
+ case []string:
+ result = append(result, v...)
+ }
+ }
+ }
+ case "<":
+ for val, docIDs := range valueMap {
+ if compareValues(val, strValue, "<") {
+ switch v := docIDs.(type) {
+ case []interface{}:
+ for _, id := range v {
+ if idStr, ok := id.(string); ok {
+ result = append(result, idStr)
+ }
+ }
+ case []string:
+ result = append(result, v...)
+ }
+ }
+ }
+ case ">=":
+ for val, docIDs := range valueMap {
+ if compareValues(val, strValue, ">=") {
+ switch v := docIDs.(type) {
+ case []interface{}:
+ for _, id := range v {
+ if idStr, ok := id.(string); ok {
+ result = append(result, idStr)
+ }
+ }
+ case []string:
+ result = append(result, v...)
+ }
+ }
+ }
+ case "<=":
+ for val, docIDs := range valueMap {
+ if compareValues(val, strValue, "<=") {
+ switch v := docIDs.(type) {
+ case []interface{}:
+ for _, id := range v {
+ if idStr, ok := id.(string); ok {
+ result = append(result, idStr)
+ }
+ }
+ case []string:
+ result = append(result, v...)
+ }
+ }
+ }
+ default:
+ // Default to equality check
+ if docIDs, exists := valueMap[strValue]; exists {
+ switch v := docIDs.(type) {
+ case []interface{}:
+ for _, id := range v {
+ if idStr, ok := id.(string); ok {
+ result = append(result, idStr)
+ }
+ }
+ case []string:
+ result = append(result, v...)
+ }
}
- mc.Value = arr
}
- return mc
+ return result
}
+// metaFilterValues extracts string values from a MetaFilterCondition Value which can
+// be a single string, a []string, or a []interface{} (used by "in" / "not in" operators).
+func metaFilterValues(value interface{}) []string {
+ switch v := value.(type) {
+ case string:
+ parts := strings.Split(v, ",")
+ result := make([]string, 0, len(parts))
+ for _, p := range parts {
+ p = strings.TrimSpace(p)
+ if p != "" {
+ result = append(result, p)
+ }
+ }
+ return result
+ case []interface{}:
+ result := make([]string, 0, len(v))
+ for _, item := range v {
+ if s, ok := item.(string); ok {
+ s = strings.TrimSpace(s)
+ if s != "" {
+ result = append(result, s)
+ }
+ }
+ }
+ return result
+ case []string:
+ result := make([]string, 0, len(v))
+ for _, s := range v {
+ s = strings.TrimSpace(s)
+ if s != "" {
+ result = append(result, s)
+ }
+ }
+ return result
+ default:
+ return []string{fmt.Sprintf("%v", v)}
+ }
+}
// ApplyMetaDataFilter applies metadata filtering rules and returns filtered doc_ids
// Supports three modes:
// - auto: generate filter conditions via LLM
// - semi_auto: generate conditions using selected metadata keys only via LLM
// - manual: directly filter based on provided conditions
+//
+// When kbIDs is supplied, metadata filters are pushed down to the doc metadata
+// index (ES/Infinity) via FilterDocIdsByMetaPushdown instead of being evaluated
+// in-memory. The in-memory meta_filter path remains the fallback.
func ApplyMetaDataFilter(
ctx context.Context,
metaDataFilter map[string]interface{},
@@ -262,6 +580,7 @@ func ApplyMetaDataFilter(
question string,
chatModel *modelModule.ChatModel,
baseDocIDs []string,
+ kbIDs []string,
manualValueResolver ...ManualValueResolver,
) ([]string, bool) {
if metaDataFilter == nil {
@@ -273,6 +592,41 @@ func ApplyMetaDataFilter(
method, _ := metaDataFilter["method"].(string)
+ // Helper to run metadata filter with push-down fallback
+ // runMetadataFilter executes filter conditions via push-down (ES/Infinity)
+ // when possible, falling back to in-memory filtering when push-down is not
+ // viable or fails.
+ //
+ // The nil-vs-empty-slice convention (matching Python):
+ // nil -> push-down was not viable / errored -> fall back to in-memory
+ // []string{} -> push-down succeeded but found 0 matching docs -> definitive,
+ // do NOT fall back to in-memory (empty result is authoritative)
+ runMetadataFilter := func(conditions []MetaFilterCondition, logic string) []string {
+ // Try ES/Infinity push-down first
+ if len(conditions) > 0 && len(kbIDs) > 0 {
+ docEngine := engine.Get()
+ if docEngine != nil {
+ // Convert []MetaFilterCondition to []map[string]interface{}
+ condMaps := make([]map[string]interface{}, len(conditions))
+ for i, c := range conditions {
+ condMaps[i] = map[string]interface{}{
+ "key": c.Key,
+ "op": c.Op,
+ "value": c.Value,
+ }
+ }
+ pushdownIDs := docEngine.FilterDocIdsByMetaPushdown(ctx, kbIDs, condMaps, logic)
+ // nil = push-down not viable / errored -> fall back to in-memory
+ // non-nil (including empty slice) = push-down definitive -> use as-is
+ if pushdownIDs != nil {
+ return pushdownIDs
+ }
+ }
+ }
+ // Fall back to in-memory filter
+ return ApplyMetaFilter(metaData, conditions, logic)
+ }
+
switch method {
case "auto":
filters, err := GenMetaFilter(ctx, chatModel, metaData, question, nil)
@@ -280,7 +634,7 @@ func ApplyMetaDataFilter(
common.Warn("Failed to generate meta filter", zap.Error(err))
return docIDs, false
}
- filteredIDs := ApplyMetaFilter(metaData, filters.Conditions, filters.Logic)
+ filteredIDs := runMetadataFilter(filters.Conditions, filters.Logic)
docIDs = append(docIDs, filteredIDs...)
if len(docIDs) == 0 {
return nil, true // Return nil to indicate auto filter returned empty
@@ -321,7 +675,7 @@ func ApplyMetaDataFilter(
common.Warn("Failed to generate meta filter", zap.Error(err))
return docIDs, false
}
- filteredIDs := ApplyMetaFilter(metaData, filters.Conditions, filters.Logic)
+ filteredIDs := runMetadataFilter(filters.Conditions, filters.Logic)
docIDs = append(docIDs, filteredIDs...)
if len(docIDs) == 0 {
return nil, true
@@ -355,7 +709,7 @@ func ApplyMetaDataFilter(
if key, ok := cond["key"].(string); ok {
condition.Key = key
}
- if value, ok := cond["value"].(string); ok {
+ if value, exists := cond["value"]; exists {
condition.Value = value
}
if op, ok := cond["op"].(string); ok {
@@ -365,7 +719,7 @@ func ApplyMetaDataFilter(
}
}
- filteredIDs := ApplyMetaFilter(metaData, conditions, logic)
+ filteredIDs := runMetadataFilter(conditions, logic)
docIDs = append(docIDs, filteredIDs...)
if len(manualFilters) > 0 && len(docIDs) == 0 {
return []string{"-999"}, false
@@ -374,3 +728,49 @@ func ApplyMetaDataFilter(
return docIDs, false
}
+
+// repairJSON attempts to fix common JSON formatting issues in LLM output.
+// This mirrors Python's json_repair.loads() behavior for the most common issues:
+// - Trailing commas in arrays/objects
+// - Unquoted or single-quoted keys
+// - Extra content after closing brace
+func repairJSON(s string) string {
+ s = strings.TrimSpace(s)
+
+ // Find the outermost JSON object { ... }
+ start := strings.Index(s, "{")
+ end := strings.LastIndex(s, "}")
+ if start == -1 || end == -1 || end <= start {
+ return s
+ }
+ s = s[start : end+1]
+
+ // Remove trailing commas before ] or }
+ s = removeTrailingCommas(s)
+
+ // Fix single-quoted keys and values: 'key' -> "key"
+ // This is a simplification — only handles the outermost level
+ s = fixQuotes(s)
+
+ return s
+}
+
+// removeTrailingCommas removes commas that appear immediately before ] or }
+func removeTrailingCommas(s string) string {
+ // Remove , followed by optional whitespace and then ] or }
+ re := regexp.MustCompile(`,(\s*[}\]])`)
+ return re.ReplaceAllString(s, "$1")
+}
+
+// fixQuotes converts single quotes to double quotes for JSON keys.
+// Only handles simple cases: 'word' -> "word"
+func fixQuotes(s string) string {
+ // Replace single-quoted keys: 'key': -> "key":
+ re := regexp.MustCompile(`'(\w+)'(\s*):`)
+ s = re.ReplaceAllString(s, `"$1"$2:`)
+ // Replace single-quoted string values: : 'value' -> : "value"
+ // (only when preceded by colon and optional whitespace)
+ re2 := regexp.MustCompile(`:\s*'([^']*)'(\s*[,}\]])`)
+ s = re2.ReplaceAllString(s, `: "$1"$2`)
+ return s
+}
diff --git a/internal/service/metadata_filter_test.go b/internal/service/metadata_filter_test.go
index cccb067ea4..33e96ddd91 100644
--- a/internal/service/metadata_filter_test.go
+++ b/internal/service/metadata_filter_test.go
@@ -372,3 +372,138 @@ func TestConvertToMetaCondition_InMixedSpaces(t *testing.T) {
t.Errorf("expected [A B C], got %v", vals)
}
}
+
+// buildValueMap constructs the valueMap shape that applySingleCondition
+// expects: metaData[key] is a map[string]interface{} where each value is
+// the list of doc IDs that carry that metadata value.
+func buildValueMap(pairs map[string][]string) map[string]interface{} {
+ m := make(map[string]interface{}, len(pairs))
+ for k, ids := range pairs {
+ m[k] = ids
+ }
+ return m
+}
+
+func asSortedIDs(ids []string) []string {
+ out := append([]string(nil), ids...)
+ sort.Strings(out)
+ return out
+}
+
+// TestApplySingleConditionRelationalNumericOrdering pins down the fix for
+// lexicographic ordering in relational operators. Before the fix, "10" < "2"
+// evaluated true, so a filter like `year > 2` against values {2, 10, 20, 100}
+// would have produced {10, 100, 20} (lexicographic) instead of {10, 20, 100}
+// (numeric). The same bug applied to <, >=, <=.
+func TestApplySingleConditionRelationalNumericOrdering(t *testing.T) {
+ // valueMap[year] = { value -> [docID,...] }
+ metaData := map[string]interface{}{
+ "year": buildValueMap(map[string][]string{
+ "2": {"d-2"},
+ "10": {"d-10"},
+ "20": {"d-20"},
+ "100": {"d-100"},
+ }),
+ }
+
+ tests := []struct {
+ name string
+ op string
+ val string
+ want []string
+ }{
+ {name: "gt_returns_strictly_greater", op: ">", val: "2", want: []string{"d-10", "d-20", "d-100"}},
+ {name: "gt_lexicographic_trap", op: ">", val: "20", want: []string{"d-100"}}, // "100" > "20" numerically, not lex
+ {name: "lt_returns_strictly_lesser", op: "<", val: "20", want: []string{"d-2", "d-10"}},
+ {name: "lt_lexicographic_trap", op: "<", val: "10", want: []string{"d-2"}}, // "2" < "10" numerically, not lex
+ {name: "gte_includes_equal", op: ">=", val: "10", want: []string{"d-10", "d-20", "d-100"}},
+ {name: "lte_includes_equal", op: "<=", val: "10", want: []string{"d-2", "d-10"}},
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ cond := MetaFilterCondition{Key: "year", Op: tt.op, Value: tt.val}
+ got := asSortedIDs(applySingleCondition(metaData, cond))
+ want := asSortedIDs(tt.want)
+ if len(got) != len(want) {
+ t.Fatalf("len = %d (%v), want %d (%v)", len(got), got, len(want), want)
+ }
+ for i := range got {
+ if got[i] != want[i] {
+ t.Fatalf("[%d] = %q, want %q (full got=%v want=%v)", i, got[i], want[i], got, want)
+ }
+ }
+ })
+ }
+}
+
+// TestApplySingleConditionRelationalFallsBackToString ensures non-numeric
+// metadata (names, tags, etc.) still works via lexicographic comparison when
+// at least one side isn't a valid number.
+func TestApplySingleConditionRelationalFallsBackToString(t *testing.T) {
+ metaData := map[string]interface{}{
+ "tag": buildValueMap(map[string][]string{
+ "apple": {"d-a"},
+ "banana": {"d-b"},
+ "cherry": {"d-c"},
+ }),
+ }
+
+ tests := []struct {
+ op string
+ val string
+ want []string
+ }{
+ {op: ">", val: "banana", want: []string{"d-c"}}, // "cherry" > "banana"
+ {op: "<", val: "cherry", want: []string{"d-a", "d-b"}}, // apple, banana
+ {op: ">=", val: "banana", want: []string{"d-b", "d-c"}},
+ {op: "<=", val: "banana", want: []string{"d-a", "d-b"}},
+ }
+ for _, tt := range tests {
+ t.Run(tt.op+tt.val, func(t *testing.T) {
+ cond := MetaFilterCondition{Key: "tag", Op: tt.op, Value: tt.val}
+ got := asSortedIDs(applySingleCondition(metaData, cond))
+ want := asSortedIDs(tt.want)
+ if len(got) != len(want) {
+ t.Fatalf("op=%q val=%q: got %v, want %v", tt.op, tt.val, got, want)
+ }
+ for i := range got {
+ if got[i] != want[i] {
+ t.Fatalf("op=%q val=%q [%d] = %q, want %q", tt.op, tt.val, i, got[i], want[i])
+ }
+ }
+ })
+ }
+}
+
+// TestCompareValuesDirectly exercises the helper in isolation for
+// completeness — in particular, the numeric-only path and the mixed
+// numeric/string fallback path.
+func TestCompareValuesDirectly(t *testing.T) {
+ cases := []struct {
+ v1, v2, op string
+ want bool
+ }{
+ // Numeric ordering — the bug we are fixing
+ {"10", "2", ">", true},
+ {"2", "10", ">", false},
+ {"100", "20", "<", false},
+ {"20", "100", "<", true},
+ {"10", "10", ">=", true},
+ {"10", "10", "<=", true},
+ // String fallback
+ {"banana", "apple", ">", true},
+ {"apple", "banana", ">", false},
+ // Mixed: one side is a number, the other isn't → fallback
+ {"10", "banana", ">", false},
+ {"banana", "10", ">", true},
+ // Unknown op
+ {"1", "2", "==", false},
+ }
+ for _, tt := range cases {
+ got := compareValues(tt.v1, tt.v2, tt.op)
+ if got != tt.want {
+ t.Errorf("compareValues(%q, %q, %q) = %v, want %v", tt.v1, tt.v2, tt.op, got, tt.want)
+ }
+ }
+}
diff --git a/internal/service/model_service.go b/internal/service/model_service.go
index ab2399bfe5..aa8b166bed 100644
--- a/internal/service/model_service.go
+++ b/internal/service/model_service.go
@@ -20,6 +20,7 @@ import (
"encoding/json"
"errors"
"fmt"
+ "os"
"ragflow/internal/common"
"ragflow/internal/dao"
"ragflow/internal/entity"
@@ -27,6 +28,7 @@ import (
"ragflow/internal/utility"
"strings"
+ "go.uber.org/zap"
"gorm.io/gorm"
)
@@ -1946,16 +1948,7 @@ func (m *ModelProviderService) GetEmbeddingModel(tenantID, compositeModelName st
return modelModule.NewEmbeddingModel(driver, &modelName, apiConfig, maxTokens), nil
}
-// GetRerankModel returns a RerankModel wrapper for the given tenant
-func (m *ModelProviderService) GetRerankModel(tenantID, compositeModelName string) (*modelModule.RerankModel, error) {
- driver, modelName, apiConfig, _, err := m.getModelConfig(tenantID, compositeModelName)
- if err != nil {
- return nil, err
- }
- return modelModule.NewRerankModel(driver, &modelName, apiConfig), nil
-}
-
-// GetChatModel returns a ChatModel wrapper for the given tenant
+// GetChatModel returns a ChatModel wrapper for the given tenant
func (m *ModelProviderService) GetChatModel(tenantID, compositeModelName string) (*modelModule.ChatModel, error) {
driver, modelName, apiConfig, _, err := m.getModelConfig(tenantID, compositeModelName)
if err != nil {
@@ -1970,6 +1963,100 @@ type AddModelRequest struct {
Models []ModelRequest `json:"models"`
}
+func (m *ModelProviderService) GetTenantDefaultModelByType(tenantID string, modelType entity.ModelType) (modelModule.ModelDriver, string, *modelModule.APIConfig, int, error) {
+ if modelType == entity.ModelTypeOCR {
+ return nil, "", nil, 0, fmt.Errorf("OCR model name is required")
+ }
+
+ tenantSvc := NewTenantService()
+ modelName, err := tenantSvc.GetDefaultModelName(tenantID, modelType)
+ if err != nil {
+ return nil, "", nil, 0, fmt.Errorf("failed to get default model name for type %s: %w", modelType, err)
+ }
+ if modelName == "" {
+ return nil, "", nil, 0, fmt.Errorf("no default %s model is set", modelType)
+ }
+
+ return m.GetModelConfigFromProviderInstance(tenantID, modelType, modelName)
+}
+
+// GetModelTypeByName returns the list of model types the given model is enrolled as.
+func (m *ModelProviderService) GetModelTypeByName(tenantID, modelName string) ([]entity.ModelType, error) {
+ common.Debug("GetModelTypeByName",
+ zap.String("tenantID", tenantID),
+ zap.String("modelName", modelName))
+
+ pureModelName, instanceName, providerName, err := parseModelName(modelName)
+ if err != nil {
+ return nil, err
+ }
+
+ // Direct provider lookup
+ provider, provErr := m.modelProviderDAO.GetByTenantIDAndProviderName(tenantID, providerName)
+ if provErr != nil {
+ return nil, fmt.Errorf("provider %q lookup failed: %w", providerName, provErr)
+ }
+ if provider == nil {
+ return nil, fmt.Errorf("provider %q not found for model %q", providerName, modelName)
+ }
+
+ // Direct instance lookup
+ instance, instErr := m.modelInstanceDAO.GetByProviderIDAndInstanceName(provider.ID, instanceName)
+ if instErr != nil {
+ return nil, fmt.Errorf("instance %q lookup failed: %w", instanceName, instErr)
+ }
+ if instance == nil {
+ return nil, fmt.Errorf("instance %q not found for model %q", instanceName, modelName)
+ }
+
+ // Direct model lookup
+ modelObjs, modelErr := m.modelDAO.GetModelsByProviderIDAndInstanceIDAndModelName(provider.ID, instance.ID, pureModelName)
+ if modelErr == nil && len(modelObjs) > 0 {
+ types := make([]entity.ModelType, 0, len(modelObjs))
+ for _, obj := range modelObjs {
+ types = append(types, entity.ModelType(obj.ModelType))
+ }
+ return types, nil
+ }
+
+ // Fallback: factory LLM catalog
+ var extra map[string]string
+ _ = json.Unmarshal([]byte(instance.Extra), &extra)
+ region := extra["region"]
+
+ targetFactoryName := providerName
+ if region == "intl" && strings.EqualFold(providerName, "siliconflow") {
+ targetFactoryName = "siliconflow_intl"
+ }
+ // Use the case-insensitive FindProvider / findModel helpers. The Go's
+ // conf/models/*.json files use mixed case ("SiliconFlow") while the
+ // Python's conf/llm_factories.json uses all-caps ("SILICONFLOW"); a strict
+ // `==` here would fail for any case mismatch. The rest of the Go codebase
+ // uses these helpers too.
+ targetProvider := dao.GetModelProviderManager().FindProvider(targetFactoryName)
+ if targetProvider == nil {
+ return nil, fmt.Errorf("model provider config not found: %s", providerName)
+ }
+ for i := range targetProvider.Models {
+ if strings.EqualFold(targetProvider.Models[i].Name, pureModelName) {
+ if len(targetProvider.Models[i].ModelTypes) == 0 {
+ return nil, fmt.Errorf("model %q has no model_types in factory catalog", pureModelName)
+ }
+ return []entity.ModelType{entity.ModelType(targetProvider.Models[i].ModelTypes[0])}, nil
+ }
+ }
+ return nil, fmt.Errorf("model %q not found for model %q", pureModelName, modelName)
+}
+
+type AddCustomModelRequest struct {
+ ProviderName string `json:"provider_name"`
+ InstanceName string `json:"instance_name"`
+ ModelName string `json:"model_name"`
+ ModelTypes []string `json:"model_types"`
+ MaxTokens int `json:"max_tokens"`
+ Thinking *bool `json:"thinking"`
+}
+
type ModelRequest struct {
ModelName string `json:"model_name"`
ModelTypes []string `json:"model_types"`
@@ -2072,39 +2159,219 @@ func (m *ModelProviderService) AddModel(request *AddModelRequest, userID string)
return common.CodeSuccess, nil
}
-// getModelConfig returns the model driver, model name, API config, and max tokens for a model
-func (m *ModelProviderService) getModelConfig(tenantID, compositeModelName string) (modelModule.ModelDriver, string, *modelModule.APIConfig, int, error) {
- modelName, instanceName, providerName, err := parseModelName(compositeModelName)
+// modelName must be a composite name of the form "model@instance@provider" or
+// "model@provider" — the provider is required and is looked up directly via
+// tenant_model_provider. For 2-part names the instance defaults to "default".
+// If the model is enrolled in tenant_model, that row is used (and INACTIVE rows
+// raise). Otherwise the factory's LLM catalog is consulted, with
+// region=intl + siliconflow redirected to the siliconflow_intl factory.
+func (m *ModelProviderService) GetModelConfigFromProviderInstance(tenantID string, modelType entity.ModelType, modelName string) (modelModule.ModelDriver, string, *modelModule.APIConfig, int, error) {
+ common.Debug("GetModelConfigFromProviderInstance",
+ zap.String("tenantID", tenantID),
+ zap.String("modelName", modelName),
+ zap.String("modelType", string(modelType)))
+
+ // TEI builtin embedding short-circuit
+ if modelType == entity.ModelTypeEmbedding && strings.Contains(os.Getenv("COMPOSE_PROFILES"), "tei-") {
+ parts := strings.Split(modelName, "@")
+ teiPure := parts[0]
+ teiProvider := ""
+ switch len(parts) {
+ case 2:
+ teiProvider = parts[1]
+ case 3:
+ teiProvider = parts[2]
+ }
+ if teiPure == os.Getenv("TEI_MODEL") && (teiProvider == "Builtin" || teiProvider == "") {
+ builtinDriver := modelModule.GetBuiltinEmbeddingModel(teiPure)
+ if builtinDriver == nil {
+ return nil, "", nil, 0, fmt.Errorf("builtin (TEI) embedding model %q not found", teiPure)
+ }
+ teiBaseURL := os.Getenv("TEI_BASE_URL")
+ apiConfig := &modelModule.APIConfig{ApiKey: nil, Region: nil, BaseURL: &teiBaseURL}
+ return builtinDriver, teiPure, apiConfig, 0, nil
+ }
+ }
+
+ // Generic Builtin provider short-circuit. The tenant_model_provider /
+ // tenant_model_instance tables don't have a row for "Builtin" because it's
+ // a local service, not a tenant-enrolled provider — so the direct lookups
+ // below would raise. Mirrors the private getModelConfig's Builtin branch.
+ //
+ // Gated on ModelTypeEmbedding because the Builtin driver here is the TEI
+ // embedding endpoint; the underlying BuiltinModel's Chat/Rerank/AudioSpeech
+ // /OCR methods all return hard "not supported" errors. A chat/rerank/etc.
+ // request that names a Builtin provider must fall through to the standard
+ // branch, which surfaces an accurate "provider not found" instead of
+ // handing back an embedding-only driver.
+ parts := strings.Split(modelName, "@")
+ if modelType == entity.ModelTypeEmbedding && len(parts) >= 2 && parts[len(parts)-1] == "Builtin" {
+ pureModelName := parts[0]
+ builtinDriver := modelModule.GetBuiltinEmbeddingModel(pureModelName)
+ if builtinDriver == nil {
+ return nil, "", nil, 0, fmt.Errorf("builtin embedding model %q not found", pureModelName)
+ }
+ apiKey := ""
+ region := ""
+ apiConfig := &modelModule.APIConfig{ApiKey: &apiKey, Region: &region}
+ maxTokens := 0
+ if mi, _ := dao.GetModelProviderManager().GetModelByName("Builtin", pureModelName); mi != nil {
+ maxTokens = mi.MaxTokens
+ }
+ return builtinDriver, pureModelName, apiConfig, maxTokens, nil
+ }
+
+ pureModelName, instanceName, providerName, err := parseModelName(modelName)
if err != nil {
return nil, "", nil, 0, err
}
- // Check if provider exists
- provider, err := m.modelProviderDAO.GetByTenantIDAndProviderName(tenantID, providerName)
- if err != nil {
- return nil, "", nil, 0, err
+ // Direct provider lookup
+ provider, provErr := m.modelProviderDAO.GetByTenantIDAndProviderName(tenantID, providerName)
+ if provErr != nil {
+ return nil, "", nil, 0, fmt.Errorf("provider %q lookup failed: %w", providerName, provErr)
}
if provider == nil {
- return nil, "", nil, 0, fmt.Errorf("provider %s not found", providerName)
+ return nil, "", nil, 0, fmt.Errorf("provider %q not found for model %q", providerName, modelName)
}
- instance, err := m.modelInstanceDAO.GetByProviderIDAndInstanceName(provider.ID, instanceName)
- if err != nil {
- return nil, "", nil, 0, err
+ // Direct instance lookup
+ instance, instErr := m.modelInstanceDAO.GetByProviderIDAndInstanceName(provider.ID, instanceName)
+ if instErr != nil {
+ return nil, "", nil, 0, fmt.Errorf("instance %q lookup failed: %w", instanceName, instErr)
}
if instance == nil {
- return nil, "", nil, 0, fmt.Errorf("instance %s not found for provider %s", instanceName, providerName)
+ return nil, "", nil, 0, fmt.Errorf("instance %q not found for model %q", instanceName, modelName)
}
+ // Decode api_key and extra fields from the instance row
+ apiKey := instance.APIKey
var extra map[string]string
- err = json.Unmarshal([]byte(instance.Extra), &extra)
+ _ = json.Unmarshal([]byte(instance.Extra), &extra)
+ region := extra["region"]
+ baseURL := extra["base_url"]
+
+ // Direct model lookup
+ modelObj, modelErr := m.modelDAO.GetByProviderIDAndInstanceIDAndModelTypeAndModelName(
+ provider.ID, instance.ID, string(modelType), pureModelName,
+ )
+ switch {
+ case modelErr == nil:
+ // Happy path: tenant enrolled this model.
+ // INACTIVE check
+ if modelObj.Status == "inactive" {
+ return nil, "", nil, 0, fmt.Errorf("model %q is disabled", modelName)
+ }
+
+ providerInfo := dao.GetModelProviderManager().FindProvider(providerName)
+ if providerInfo == nil {
+ return nil, "", nil, 0, fmt.Errorf("provider %q driver not found", providerName)
+ }
+ driver, driverErr := newModelDriverForBaseURL(providerInfo.ModelDriver, providerName, region, baseURL)
+ if driverErr != nil {
+ return nil, "", nil, 0, driverErr
+ }
+ maxTokens := 0
+ if mi, _ := dao.GetModelProviderManager().GetModelByName(providerName, pureModelName); mi != nil {
+ maxTokens = mi.MaxTokens
+ }
+ apiConfig := &modelModule.APIConfig{ApiKey: &apiKey, Region: &region, BaseURL: &baseURL}
+ return driver, modelObj.ModelName, apiConfig, maxTokens, nil
+ case errors.Is(modelErr, gorm.ErrRecordNotFound):
+ // Tenant hasn't enrolled this model. Fall through to the factory catalog.
+ common.Debug("GetModelConfigFromProviderInstance: tenant has no row for model, falling back to factory catalog",
+ zap.String("tenantID", tenantID),
+ zap.String("providerName", providerName),
+ zap.String("instanceName", instanceName),
+ zap.String("modelType", string(modelType)),
+ zap.String("modelName", pureModelName))
+ default:
+ // Surface unexpected DAO errors (e.g. transient DB failure) instead of
+ // silently resolving from the factory catalog — that would mask disabled
+ // or tenant-specific configurations.
+ return nil, "", nil, 0, fmt.Errorf("model %q lookup failed: %w", modelName, modelErr)
+ }
+
+ // Fallback: factory LLM catalog
+ targetFactoryName := providerName
+ if region == "intl" && strings.EqualFold(providerName, "siliconflow") {
+ targetFactoryName = "siliconflow_intl"
+ }
+ // Use the case-insensitive FindProvider / findModel helpers. The Go's
+ // conf/models/*.json files use mixed case ("SiliconFlow") while the
+ // Python's conf/llm_factories.json uses all-caps ("SILICONFLOW"); a strict
+ // `==` here would fail for any case mismatch even though the Python passes
+ // (its FACTORY_LLM_INFOS and the parsed provider_name happen to agree on
+ // casing). The rest of the Go codebase uses these helpers too.
+ targetProvider := dao.GetModelProviderManager().FindProvider(targetFactoryName)
+ if targetProvider == nil {
+ return nil, "", nil, 0, fmt.Errorf("model provider config not found: %s", providerName)
+ }
+ var llmInfo *entity.Model
+ for i := range targetProvider.Models {
+ if strings.EqualFold(targetProvider.Models[i].Name, pureModelName) {
+ llmInfo = targetProvider.Models[i]
+ break
+ }
+ }
+ if llmInfo == nil {
+ return nil, "", nil, 0, fmt.Errorf("model config not found: %s", modelName)
+ }
+ driver, driverErr := newModelDriverForBaseURL(targetProvider.ModelDriver, providerName, region, baseURL)
+ if driverErr != nil {
+ return nil, "", nil, 0, driverErr
+ }
+ apiConfig := &modelModule.APIConfig{ApiKey: &apiKey, Region: &region, BaseURL: &baseURL}
+ return driver, llmInfo.Name, apiConfig, llmInfo.MaxTokens, nil
+}
+
+// getModelConfig returns the model driver, model name, API config, and max tokens for a model
+func (m *ModelProviderService) getModelConfig(tenantID, compositeModelName string) (modelModule.ModelDriver, string, *modelModule.APIConfig, int, error) {
+ modelName, instanceName, providerName, err := parseModelName(compositeModelName)
if err != nil {
return nil, "", nil, 0, err
}
- region := extra["region"]
+
+ // Check if provider exists (skip for Builtin provider)
+ var providerID string
+ if providerName != "Builtin" {
+ provider, err := m.modelProviderDAO.GetByTenantIDAndProviderName(tenantID, providerName)
+ if err != nil {
+ return nil, "", nil, 0, err
+ }
+ if provider == nil {
+ return nil, "", nil, 0, fmt.Errorf("provider %s not found", providerName)
+ }
+ providerID = provider.ID
+ } else {
+ common.Debug("getModelConfig skipping provider lookup for Builtin")
+ }
+
+ // Get instance (skip for Builtin provider since it doesn't use tenant_model_instance)
+ var instance *entity.TenantModelInstance
+ if providerName != "Builtin" {
+ instance, err = m.modelInstanceDAO.GetByProviderIDAndInstanceName(providerID, instanceName)
+ if err != nil {
+ return nil, "", nil, 0, err
+ }
+ if instance == nil {
+ return nil, "", nil, 0, fmt.Errorf("instance %s not found for provider %s", instanceName, providerName)
+ }
+ common.Debug("getModelConfig instance found", zap.String("instanceName", instanceName))
+ }
+
+ var extra map[string]string
+ var region string
+ if instance != nil {
+ err = json.Unmarshal([]byte(instance.Extra), &extra)
+ if err != nil {
+ return nil, "", nil, 0, err
+ }
+ region = extra["region"]
+ }
providerInfo := dao.GetModelProviderManager().FindProvider(providerName)
- if providerInfo == nil {
+ if providerName != "Builtin" && providerInfo == nil {
return nil, "", nil, 0, fmt.Errorf("provider %s not found", providerName)
}
@@ -2115,17 +2382,29 @@ func (m *ModelProviderService) getModelConfig(tenantID, compositeModelName strin
maxTokens = modelInfo.MaxTokens
}
- _, err = m.modelDAO.GetModelByProviderIDAndInstanceIDAndModelName(provider.ID, instance.ID, modelName)
- if err != nil {
- _, err = dao.GetModelProviderManager().GetModelByName(providerName, modelName)
+ // For Builtin provider, use empty APIKey and skip tenant_model lookup
+ var apiKey string
+ if providerName == "Builtin" {
+ apiKey = ""
+ // For Builtin, we need to get the ModelDriver from somewhere
+ // Since Builtin models are handled locally, we return a special driver
+ builtinDriver := modelModule.GetBuiltinEmbeddingModel(modelName)
+ if builtinDriver == nil {
+ return nil, "", nil, 0, fmt.Errorf("builtin embedding model %s not found", modelName)
+ }
+ apiConfig := &modelModule.APIConfig{ApiKey: &apiKey, Region: &region}
+ return builtinDriver, modelName, apiConfig, maxTokens, nil
+ } else {
+ _, err = m.modelDAO.GetModelByProviderIDAndInstanceIDAndModelName(providerID, instance.ID, modelName)
if err != nil {
- return nil, "", nil, 0, fmt.Errorf("provider %s model %s not found", providerName, modelName)
+ _, err = dao.GetModelProviderManager().GetModelByName(providerName, modelName)
+ if err != nil {
+ return nil, "", nil, 0, fmt.Errorf("provider %s model %s not found", providerName, modelName)
+ }
}
-
- apiConfig := &modelModule.APIConfig{ApiKey: &instance.APIKey, Region: &region}
- return providerInfo.ModelDriver, modelName, apiConfig, maxTokens, nil
+ apiKey = instance.APIKey
}
- apiConfig := &modelModule.APIConfig{ApiKey: &instance.APIKey, Region: &region}
+ apiConfig := &modelModule.APIConfig{ApiKey: &apiKey, Region: &region}
return providerInfo.ModelDriver, modelName, apiConfig, maxTokens, nil
}
diff --git a/internal/service/nlp/query_builder.go b/internal/service/nlp/query_builder.go
index 991bcdb53d..25a16d5a1f 100644
--- a/internal/service/nlp/query_builder.go
+++ b/internal/service/nlp/query_builder.go
@@ -229,7 +229,7 @@ func (qb *QueryBuilder) Question(txt string, tbl string, minMatch float64) (*typ
// Replace punctuation and special characters with space
// Reference: rag/nlp/query.py L44-48
// re is the regex pattern for matching punctuation and special characters.
- re := regexp.MustCompile(`[ :|\r\n\t,,.。??/\` + "`" + `!!&^%()\[\]{}<>]+`)
+ re := regexp.MustCompile(`[ :|\r\n\t,,.。??/\` + "`" + `!!&^%()\[\]{}<>*~'"\\]+`)
// txtCleaned is the text after removing punctuation and special characters.
txtCleaned := re.ReplaceAllString(txtSimplified, " ")
@@ -312,10 +312,12 @@ func (qb *QueryBuilder) Question(txt string, tbl string, minMatch float64) (*typ
for _, syn := range tkSyns {
syn = strings.TrimSpace(syn)
if syn != "" {
- synParts = append(synParts, fmt.Sprintf(`"%s"^%.1f`, syn, tw.w/4.0))
+ synParts = append(synParts, fmt.Sprintf(`"%s"^%.4f`, syn, tw.w/4.0))
}
}
syns[i] = strings.Join(synParts, " ")
+ // Extend keywords with synonyms
+ keywords = append(keywords, tkSyns...)
} else {
syns[i] = ""
}
@@ -333,7 +335,7 @@ func (qb *QueryBuilder) Question(txt string, tbl string, minMatch float64) (*typ
continue
}
// Format: (token^weight synonym)
- q = append(q, fmt.Sprintf("(%s^%.1f %s)", tk, w, syns[i]))
+ q = append(q, fmt.Sprintf("(%s^%.4f %s)", tk, w, syns[i]))
}
// Add phrase queries for adjacent tokens
@@ -349,7 +351,7 @@ func (qb *QueryBuilder) Question(txt string, tbl string, minMatch float64) (*typ
if tksW[i].w > maxW {
maxW = tksW[i].w
}
- q = append(q, fmt.Sprintf(`"%s %s"^%.1f`, left, right, maxW*2))
+ q = append(q, fmt.Sprintf(`"%s %s"^%.4f`, left, right, maxW*2))
}
if len(q) == 0 {
@@ -440,7 +442,7 @@ func (qb *QueryBuilder) Question(txt string, tbl string, minMatch float64) (*typ
for _, m := range sm {
m = specialCharRe.ReplaceAllString(m, "")
m = qb.SubSpecialChar(m)
- if len(m) > 1 {
+ if len([]rune(m)) > 1 {
cleanSm = append(cleanSm, m)
}
}
@@ -622,28 +624,6 @@ func (qb *QueryBuilder) Paragraph(contentTks string, keywords []string, keywords
}
}
-// Similarity calculates similarity between two term weight dictionaries.
-// Algorithm: s = sum(qtwt[k] for k in qtwt if k in dtwt) / sum(qtwt[k])
-func (qb *QueryBuilder) Similarity(qtwt map[string]float64, dtwt map[string]float64) float64 {
- if len(qtwt) == 0 {
- return 0.0
- }
- var sum float64
- for k, v := range qtwt {
- if _, ok := dtwt[k]; ok {
- sum += v
- }
- }
- var total float64
- for _, v := range qtwt {
- total += v
- }
- if total == 0 {
- return 0.0
- }
- return sum / total
-}
-
// TokenSimilarity calculates similarity between query terms and multiple document term sets.
// To be implemented: requires term weight processing module.
func (qb *QueryBuilder) TokenSimilarity(atks string, btkss []string) []float64 {
diff --git a/internal/service/nlp/query_builder_test.go b/internal/service/nlp/query_builder_test.go
index 238a40317a..fae13a697d 100644
--- a/internal/service/nlp/query_builder_test.go
+++ b/internal/service/nlp/query_builder_test.go
@@ -395,32 +395,6 @@ func TestQueryBuilder_Paragraph(t *testing.T) {
}
}
-func TestQueryBuilder_Similarity(t *testing.T) {
- qb := NewQueryBuilder()
- tests := []struct {
- name string
- qtwt map[string]float64
- dtwt map[string]float64
- expected float64
- }{
- {"Empty query", map[string]float64{}, map[string]float64{"a": 1.0}, 0.0},
- {"Empty doc", map[string]float64{"a": 1.0}, map[string]float64{}, 0.0},
- {"Exact match", map[string]float64{"a": 1.0, "b": 2.0}, map[string]float64{"a": 5.0, "b": 3.0}, 1.0},
- {"Partial match", map[string]float64{"a": 1.0, "b": 2.0, "c": 3.0}, map[string]float64{"a": 1.0, "c": 1.0}, (1.0 + 3.0) / (1.0 + 2.0 + 3.0)}, // sum=4, total=6 => 0.666...
- {"No match", map[string]float64{"a": 1.0}, map[string]float64{"b": 2.0}, 0.0},
- {"Zero total weight", map[string]float64{"a": 0.0, "b": 0.0}, map[string]float64{"a": 1.0}, 0.0},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- result := qb.Similarity(tt.qtwt, tt.dtwt)
- // Use tolerance for floating point
- if result < tt.expected-1e-9 || result > tt.expected+1e-9 {
- t.Errorf("Similarity(%v, %v) = %v, want %v", tt.qtwt, tt.dtwt, result, tt.expected)
- }
- })
- }
-}
-
func TestQueryBuilder_TokenSimilarity(t *testing.T) {
qb := NewQueryBuilder()
// Currently placeholder returns zero slice
diff --git a/internal/service/nlp/reranker.go b/internal/service/nlp/reranker.go
index 2e18d5f89c..2d77eabef8 100644
--- a/internal/service/nlp/reranker.go
+++ b/internal/service/nlp/reranker.go
@@ -67,7 +67,7 @@ func Rerank(
) (sim []float64, tsim []float64, vsim []float64) {
// If reranker model is provided and there are results, use model reranking
if rerankModel != nil && total > 0 {
- return RerankByModel(rerankModel, chunks, query, tkWeight, vtWeight, cfield, qb, rankFeature)
+ return RerankByModel(rerankModel, chunks, nil, nil, query, tkWeight, vtWeight, cfield, qb, rankFeature)
}
// Otherwise, use fallback logic based on engine type
@@ -89,6 +89,8 @@ func Rerank(
func RerankByModel(
rerankModel *models.RerankModel,
chunks []map[string]interface{},
+ ids []string,
+ field map[string]map[string]interface{},
query string,
tkWeight, vtWeight float64,
cfield string,
@@ -114,7 +116,18 @@ func RerankByModel(
insTw := make([][]string, 0, chunkCount)
docs := make([]string, 0, chunkCount)
- for _, chunk := range chunks {
+ // Process chunks in id order
+ for i, chunkID := range ids {
+ chunk, ok := field[chunkID]
+ if !ok {
+ // Fallback to chunks[i] if id not found in field
+ if i < len(chunks) {
+ chunk = chunks[i]
+ } else {
+ continue
+ }
+ }
+
contentLtks := extractContentTokens(chunk, cfield)
titleTks := extractTitleTokens(chunk)
importantKwd := extractImportantKeywords(chunk)
@@ -134,30 +147,33 @@ func RerankByModel(
// Calculate token similarity
tsim = TokenSimilarity(keywords, insTw, qb)
- var modelSim []float64
// Get similarity scores from reranker model
rerankResponse, err := rerankModel.ModelDriver.Rerank(rerankModel.ModelName, query, docs, rerankModel.APIConfig, &models.RerankConfig{})
if err != nil {
common.Error("RerankByModel: rerankModel.Rerank failed; falling back to token-only similarity", err)
// If model fails, fall back to token similarity only
- modelSim = make([]float64, len(tsim))
+ rerankResponse = &models.RerankResponse{}
}
- loopCount := min(chunkCount, len(rerankResponse.Data))
- for i := 0; i < loopCount; i++ {
- modelSim = append(modelSim, rerankResponse.Data[i].RelevanceScore)
+ // Use the Index field from the response to place scores in the correct position,
+ // matching the original document order
+ modelSim := make([]float64, len(insTw))
+ for _, result := range rerankResponse.Data {
+ if result.Index >= 0 && result.Index < len(modelSim) {
+ modelSim[result.Index] = result.RelevanceScore
+ }
}
// Combine token similarity with model similarity
// Model similarity is treated as vector similarity component
- sim = make([]float64, chunkCount)
+ sim = make([]float64, len(insTw))
for i := range tsim {
sim[i] = tkWeight*tsim[i] + vtWeight*modelSim[i]
}
// Apply rank feature scores (tag_score * 10 + pagerank)
// Always apply pageranks, even when rankFeature is nil/empty
- sim = applyRankFeatureScores(chunks, sim, rankFeature)
+ sim = applyRankFeatureScoresForIDs(ids, field, sim, rankFeature)
common.Info("RerankByModel completed")
return sim, tsim, modelSim
@@ -235,7 +251,7 @@ func RerankStandard(
// Always apply pageranks, even when rankFeature is nil/empty
sim = applyRankFeatureScores(chunks, sim, rankFeature)
- common.Info("RerankStandard completed")
+ common.Info("RerankStandard completed", zap.Int("outputChunks", len(sim)))
return sim, tsim, vsim
}
@@ -296,9 +312,10 @@ func HybridSimilarity(
}
// Combine similarities
+ // rerank_with_knn: sim = tkweight * tksim + vtweight * vtsim
sim = make([]float64, len(tsim))
for i := range tsim {
- sim[i] = vsim[i]*vtWeight + tsim[i]*tkWeight
+ sim[i] = tkWeight*tsim[i] + vtWeight*vsim[i]
}
return sim, tsim, vsim
@@ -306,63 +323,90 @@ func HybridSimilarity(
// TokenSimilarity calculates token-based similarity
func TokenSimilarity(atks []string, btkss [][]string, qb *QueryBuilder) []float64 {
- atksDict := tokensToDict(atks, qb)
+ atksDict, atksKeyOrder := tokensToDict(atks, qb)
btkssDicts := make([]map[string]float64, len(btkss))
for i, btks := range btkss {
- btkssDicts[i] = tokensToDict(btks, qb)
+ btkssDicts[i], _ = tokensToDict(btks, qb)
}
similarities := make([]float64, len(btkssDicts))
for i, btkDict := range btkssDicts {
- similarities[i] = tokenDictSimilarity(atksDict, btkDict)
+ similarities[i] = tokenDictSimilarity(atksDict, btkDict, atksKeyOrder)
}
return similarities
}
-// tokensToDict converts tokens to a weighted dictionary
-func tokensToDict(tks []string, qb *QueryBuilder) map[string]float64 {
+// tokensToDict converts tokens to a weighted dictionary.
+// Also returns the insertion order of keys to match Python's dict insertion order.
+func tokensToDict(tks []string, qb *QueryBuilder) (map[string]float64, []string) {
d := make(map[string]float64)
+ var keyOrder []string
if qb == nil || qb.termWeight == nil {
- return d
+ return d, keyOrder
}
wts := qb.termWeight.Weights(tks, false)
for i, tw := range wts {
t := tw.Term
c := tw.Weight
+ if _, exists := d[t]; !exists {
+ keyOrder = append(keyOrder, t)
+ }
d[t] += c * 0.4
if i+1 < len(wts) {
_t := wts[i+1].Term
_c := wts[i+1].Weight
- d[t+_t] += math.Max(c, _c) * 0.6
+ k := t + _t
+ if _, exists := d[k]; !exists {
+ keyOrder = append(keyOrder, k)
+ }
+ d[k] += math.Max(c, _c) * 0.6
}
}
-
- return d
+ return d, keyOrder
}
-// tokenDictSimilarity calculates similarity between two token dictionaries
-func tokenDictSimilarity(qtwt, dtwt map[string]float64) float64 {
+// tokenDictSimilarity calculates similarity between two token dictionaries.
+// Uses the query key order (from tokensToDict) to match Python's dict insertion order.
+// Python iterates dict.items() in insertion order (guaranteed since Python 3.7).
+// Floating-point addition is non-associative, so the iteration order matters.
+func tokenDictSimilarity(qtwt, dtwt map[string]float64, qKeyOrder []string) float64 {
if len(qtwt) == 0 || len(dtwt) == 0 {
return 0.0
}
+ // Use qKeyOrder if provided, otherwise fall back to sorted keys
+ keys := qKeyOrder
+ if len(keys) == 0 {
+ keys = make([]string, 0, len(qtwt))
+ for k := range qtwt {
+ keys = append(keys, k)
+ }
+ sort.Strings(keys)
+ }
+
// s = sum of query weights for matching tokens
+ // NOTE: Use naive left-to-right summation (not PairwiseSum) to match Python's
+ // exact float64 behavior in query.py similarity(). Python iterates dict.items()
+ // in insertion order with simple +=, which is left-to-right accumulation.
s := 1e-9
- for t, qw := range qtwt {
+ matchCount := 0
+ for _, t := range keys {
if _, ok := dtwt[t]; ok {
- s += qw
+ s += qtwt[t]
+ matchCount++
}
}
// q = sum of all query weights (L1 normalization)
q := 1e-9
- for _, qw := range qtwt {
- q += qw
+ for _, t := range keys {
+ q += qtwt[t]
}
- return s / q
+ result := s / q
+ return result
}
// ArgsortDescending returns indices sorted by values in descending order
@@ -414,10 +458,7 @@ func extractContentTokens(fields map[string]interface{}, cfield string) []string
return []string{}
}
- // Remove redundant spaces first to handle irregular spacing in Chinese text
- v = RemoveRedundantSpaces(v)
-
- // Now split by whitespace to get individual tokens
+ // Split by whitespace to get individual tokens
seen := make(map[string]bool)
var result []string
for _, t := range strings.Fields(v) {
@@ -435,8 +476,7 @@ func extractTitleTokens(fields map[string]interface{}) []string {
if !ok {
return []string{}
}
- // Remove redundant spaces first
- v = RemoveRedundantSpaces(v)
+ // NOTE: Do NOT call RemoveRedundantSpaces here - it removes spaces between Chinese chars
var result []string
for _, t := range strings.Fields(v) {
if t != "" {
@@ -486,24 +526,31 @@ func extractImportantKeywords(fields map[string]interface{}) []string {
}
}
-// cosineSimilarity calculates cosine similarity between two vectors
+// cosineSimilarity calculates cosine similarity between two vectors.
+// Three parallel pairwise sums (dot, ||a||², ||b||²) keep precision
+// comparable to numpy's float64 reductions.
func cosineSimilarity(a, b []float64) float64 {
if len(a) != len(b) {
return 0.0
}
- var dot, normA, normB float64
+ dBuf := make([]float64, len(a))
+ aBuf := make([]float64, len(a))
+ bBuf := make([]float64, len(a))
for i := range a {
- dot += a[i] * b[i]
- normA += a[i] * a[i]
- normB += b[i] * b[i]
+ dBuf[i] = a[i] * b[i]
+ aBuf[i] = a[i] * a[i]
+ bBuf[i] = b[i] * b[i]
}
+ dot := common.PairwiseSum(dBuf)
+ normA := common.PairwiseSum(aBuf)
+ normB := common.PairwiseSum(bBuf)
if normA == 0 || normB == 0 {
return 0.0
}
- return dot / (math.Sqrt(normA) * math.Sqrt(normB))
+ return dot / (common.PySqrt(normA) * common.PySqrt(normB))
}
// RemoveRedundantSpaces removes redundant spaces from text
@@ -552,13 +599,32 @@ func applyRankFeatureScores(chunks []map[string]interface{}, sim []float64, rank
}
// Compute query denominator: sqrt(sum of squares of query rank feature weights, excluding pagerank)
- qDenor := 0.0
- for t, s := range rankFeature {
+ // Sort keys for deterministic float accumulation (Go map iteration is randomized)
+ rankFeatureKeys := make([]string, 0, len(rankFeature))
+ for k := range rankFeature {
+ rankFeatureKeys = append(rankFeatureKeys, k)
+ }
+ sort.Strings(rankFeatureKeys)
+
+ qDenorBuf := make([]float64, 0, len(rankFeatureKeys))
+ for _, t := range rankFeatureKeys {
if t != common.PAGERANK_FLD {
- qDenor += s * s
+ s := rankFeature[t]
+ qDenorBuf = append(qDenorBuf, s*s)
}
}
- qDenor = math.Sqrt(qDenor)
+ qDenor := common.PySqrt(common.PairwiseSum(qDenorBuf))
+
+ // If the query has no usable tag-feature weights (e.g. pagerank-only), fall
+ // back to pageranks-only. Mirrors Python's `if q_denor == 0: return pageranks`
+ // in _rank_feature_scores(); otherwise the later `... / qDenor` divides by 0
+ // and turns matching chunks into NaN, contaminating the final ranking.
+ if qDenor == 0 {
+ for i := range sim {
+ sim[i] += pageranks[i]
+ }
+ return sim
+ }
// Compute tag score for each chunk
tagScores := make([]float64, len(chunks))
@@ -570,18 +636,150 @@ func applyRankFeatureScores(chunks []map[string]interface{}, sim []float64, rank
}
// Parse tag_feas JSON string: {"tag1": 0.5, "tag2": 0.3}
- nor, denor := 0.0, 0.0
tagFeaMap := parseTagFeasRerank(tagFeaStr)
- for t, sc := range tagFeaMap {
+ // Sort keys for deterministic float accumulation
+ tagFeaKeys := make([]string, 0, len(tagFeaMap))
+ for k := range tagFeaMap {
+ tagFeaKeys = append(tagFeaKeys, k)
+ }
+ sort.Strings(tagFeaKeys)
+ norBuf := make([]float64, 0, len(tagFeaKeys))
+ denorBuf := make([]float64, 0, len(tagFeaKeys))
+ for _, t := range tagFeaKeys {
+ sc := tagFeaMap[t]
+ if weight, exists := rankFeature[t]; exists {
+ norBuf = append(norBuf, weight*sc)
+ }
+ denorBuf = append(denorBuf, sc*sc)
+ }
+ // NOTE: Use naive left-to-right summation to match Python's exact float64
+ // behavior in _rank_feature_scores(). Python uses nor += ... and denor += ...
+ // in dict iteration order, which is simple left-to-right accumulation.
+ var nor, denor float64
+ for _, v := range norBuf {
+ nor += v
+ }
+ for _, v := range denorBuf {
+ denor += v
+ }
+ if denor == 0 {
+ tagScores[i] = 0
+ } else {
+ tagScores[i] = nor / common.PySqrt(denor) / qDenor
+ }
+ }
+
+ // Final score: tag_score * 10 + pagerank
+ for i := range sim {
+ sim[i] += tagScores[i]*10 + pageranks[i]
+ }
+
+ return sim
+}
+
+// applyRankFeatureScoresForIDs applies rank feature scores using field map (by chunk IDs)
+// This is used when we have the field map from search results
+func applyRankFeatureScoresForIDs(ids []string, field map[string]map[string]interface{}, sim []float64, rankFeature map[string]float64) []float64 {
+ if len(ids) == 0 || len(sim) == 0 {
+ return sim
+ }
+
+ // Collect pageranks from each chunk via field map
+ pageranks := make([]float64, len(ids))
+ for i, chunkID := range ids {
+ chunk, ok := field[chunkID]
+ if !ok {
+ pageranks[i] = 0
+ continue
+ }
+ if pr, ok := chunk[common.PAGERANK_FLD]; ok {
+ if f, ok := toFloat64(pr); ok {
+ pageranks[i] = f
+ }
+ }
+ }
+
+ // If no query rank features (no tag features), just add pageranks to sim
+ if len(rankFeature) == 0 {
+ for i := range sim {
+ sim[i] += pageranks[i]
+ }
+ return sim
+ }
+
+ // Compute query denominator: sqrt(sum of squares of query rank feature weights, excluding pagerank)
+ // Sort keys for deterministic float accumulation (Go map iteration is randomized)
+ rankFeatureKeys := make([]string, 0, len(rankFeature))
+ for k := range rankFeature {
+ rankFeatureKeys = append(rankFeatureKeys, k)
+ }
+ sort.Strings(rankFeatureKeys)
+
+ qDenorBuf := make([]float64, 0, len(rankFeatureKeys))
+ for _, t := range rankFeatureKeys {
+ if t != common.PAGERANK_FLD {
+ s := rankFeature[t]
+ qDenorBuf = append(qDenorBuf, s*s)
+ }
+ }
+ // NOTE: Python uses np.sum([s*s for...]) which is pairwise, so PairwiseSum is correct here
+ qDenor := common.PySqrt(common.PairwiseSum(qDenorBuf))
+
+ // If the query has no usable tag-feature weights (e.g. pagerank-only), fall
+ // back to pageranks-only. Mirrors Python's `if q_denor == 0: return pageranks`
+ // in _rank_feature_scores(); otherwise the later `... / qDenor` divides by 0
+ // and turns matching chunks into NaN, contaminating the final ranking.
+ if qDenor == 0 {
+ for i := range sim {
+ sim[i] += pageranks[i]
+ }
+ return sim
+ }
+
+ // Compute tag score for each chunk
+ tagScores := make([]float64, len(ids))
+ for i, chunkID := range ids {
+ chunk, ok := field[chunkID]
+ if !ok {
+ tagScores[i] = 0
+ continue
+ }
+ tagFeaStr, ok := chunk[common.TAG_FLD].(string)
+ if !ok || tagFeaStr == "" {
+ tagScores[i] = 0
+ continue
+ }
+
+ // Parse tag_feas JSON string: {"tag1": 0.5, "tag2": 0.3}
+ tagFeaMap := parseTagFeasRerank(tagFeaStr)
+ // Sort keys for deterministic float accumulation
+ tagFeaKeys := make([]string, 0, len(tagFeaMap))
+ for k := range tagFeaMap {
+ tagFeaKeys = append(tagFeaKeys, k)
+ }
+ sort.Strings(tagFeaKeys)
+ norBuf := make([]float64, 0, len(tagFeaKeys))
+ denorBuf := make([]float64, 0, len(tagFeaKeys))
+ for _, t := range tagFeaKeys {
+ sc := tagFeaMap[t]
if weight, exists := rankFeature[t]; exists {
- nor += weight * sc
+ norBuf = append(norBuf, weight*sc)
}
- denor += sc * sc
+ denorBuf = append(denorBuf, sc*sc)
+ }
+ // NOTE: Use naive left-to-right summation to match Python's exact float64
+ // behavior in _rank_feature_scores(). Python uses nor += ... and denor += ...
+ var nor, denor float64
+ for _, v := range norBuf {
+ nor += v
+ }
+ for _, v := range denorBuf {
+ denor += v
}
if denor == 0 {
tagScores[i] = 0
} else {
- tagScores[i] = nor / math.Sqrt(denor) / qDenor
+ tagScores[i] = nor / common.PySqrt(denor) / qDenor
}
}
@@ -593,6 +791,153 @@ func applyRankFeatureScores(chunks []map[string]interface{}, sim []float64, rank
return sim
}
+// RerankWithKNN performs reranking using KNN scores (ES two-pass approach)
+// Matches Python's rerank_with_knn()
+//
+// TWO-PASS APPROACH (matching Python's Dealer._knn_scores + get_scores pattern):
+//
+// PASS 1 (KNNScores / _knn_scores):
+// - First search returns text-matched chunks with hybrid scores (BM25 + vector fusion)
+// - Second KNN-only search filtered by those chunk IDs
+// - ES computes cosine similarity between query vector and stored chunk vectors
+// - Vectors stay in ES index (no need to ship them to application)
+// - Returns raw KNN search result containing _id -> _score mappings
+//
+// PASS 2 (GetScores / get_scores):
+// - Extracts doc_id -> score from the KNN result
+// - Produces the clean vector similarity scores needed for reranking
+//
+// RERANK (RerankWithKNN / rerank_with_knn):
+// - Combines token similarity (keyword overlap) with vector similarity (cosine)
+// - Formula: sim = tkWeight * tksim + vtWeight * vtsim + rank_features
+// - Token weighting: content + title*2 + important_kwd*5 + question_tks*6
+// - Rank features: tag_score * 10 + pagerank (per chunk)
+//
+// Python equivalent in rag/nlp/search.py:
+//
+// knn_scores = await self._knn_scores(sres, idx_names, kb_ids) # Pass 1
+// knn_scores = self.dataStore.get_scores(res) # Pass 2
+// sim, tsim, vsim = self.rerank_with_knn(sres, question, knn_scores, ...) # Rerank
+//
+// Parameters:
+// - chunks: search results from first pass (used for fallback)
+// - ids: ordered chunk IDs from search results
+// - field: field map (chunk_id -> chunk fields)
+// - knnScores: cosine similarity scores from GetScores (doc_id -> score)
+// - query: search query string
+// - tkWeight: token similarity weight (typically 0.3)
+// - vtWeight: vector similarity weight (typically 0.7)
+// - cfield: content field name (default "content_ltks")
+// - qb: QueryBuilder for token processing
+// - rankFeature: rank feature weights (e.g., {"pagerank_fea": 10.0})
+func RerankWithKNN(
+ chunks []map[string]interface{},
+ ids []string,
+ field map[string]map[string]interface{},
+ knnScores map[string]float64,
+ query string,
+ tkWeight, vtWeight float64,
+ cfield string,
+ qb *QueryBuilder,
+ rankFeature map[string]float64,
+) (sim []float64, tsim []float64, vsim []float64) {
+ if len(ids) == 0 {
+ return []float64{}, []float64{}, []float64{}
+ }
+
+ common.Info("RerankWithKNN started", zap.Int("chunkCount", len(ids)), zap.Float64("tkWeight", tkWeight), zap.Float64("vtWeight", vtWeight))
+
+ // Normalize important_kwd - Python checks if it's a string and wraps in list
+ // for i in sres.ids:
+ // if isinstance(sres.field[i].get("important_kwd", []), str):
+ // sres.field[i]["important_kwd"] = [sres.field[i]["important_kwd"]]
+ for _, chunkID := range ids {
+ chunk, ok := field[chunkID]
+ if !ok {
+ continue
+ }
+ if v, exists := chunk["important_kwd"]; exists {
+ if _, isString := v.(string); isString {
+ field[chunkID]["important_kwd"] = []string{v.(string)}
+ }
+ }
+ }
+
+ // Extract keywords from query
+ keywords := []string{}
+ if qb != nil {
+ _, keywords = qb.Question(query, "qa", 0.6)
+ }
+ common.Info("RerankWithKNN keywords", zap.Any("keywords", keywords))
+
+ // Build token lists matching Python's OrderedDict approach
+ insTw := make([][]string, 0, len(ids))
+ for i, chunkID := range ids {
+ chunk, ok := field[chunkID]
+ if !ok {
+ if i < len(chunks) {
+ chunk = chunks[i]
+ } else {
+ insTw = append(insTw, []string{})
+ continue
+ }
+ }
+
+ // Normalize text content - split, dedupe while preserving order (OrderedDict effect)
+ contentLtks := extractContentTokens(chunk, cfield)
+ titleTks := extractTitleTokens(chunk)
+ questionTks := extractQuestionTokens(chunk)
+ importantKwd := extractImportantKeywords(chunk)
+
+ // Combine tokens with weights: content + title*2 + important_kwd*5 + question_tks*6
+ // This matches Python: tks = content_ltks + title_tks * 2 + important_kwd * 5 + question_tks * 6
+ tks := make([]string, 0, len(contentLtks)+len(titleTks)*2+len(importantKwd)*5+len(questionTks)*6)
+ tks = append(tks, contentLtks...)
+ for i := 0; i < 2; i++ {
+ tks = append(tks, titleTks...)
+ }
+ for i := 0; i < 5; i++ {
+ tks = append(tks, importantKwd...)
+ }
+ for i := 0; i < 6; i++ {
+ tks = append(tks, questionTks...)
+ }
+ insTw = append(insTw, tks)
+ }
+
+ // Calculate token similarity
+ tsim = TokenSimilarity(keywords, insTw, qb)
+ common.Info("RerankWithKNN tsim", zap.Float64s("tsim", tsim))
+
+ // Build vector similarity from knnScores - matches Python's np.array([knn_scores.get(chunk_id, 0.0) for chunk_id in sres.ids])
+ vsim = make([]float64, len(ids))
+ for i, chunkID := range ids {
+ vsim[i] = knnScores[chunkID] // Returns 0.0 if not found (Go map default)
+ }
+ common.Info("RerankWithKNN knnScores", zap.Int("knnScoreCount", len(knnScores)), zap.Float64s("vsim", vsim), zap.Strings("ids", ids), zap.Float64s("knnScores", func() []float64 {
+ scores := make([]float64, 0, len(knnScores))
+ for _, id := range ids {
+ if s, ok := knnScores[id]; ok {
+ scores = append(scores, s)
+ }
+ }
+ return scores
+ }()))
+
+ // Apply rank feature scores
+ sim = make([]float64, len(tsim))
+ for i := range tsim {
+ sim[i] = tkWeight*tsim[i] + vtWeight*vsim[i]
+ }
+
+ // Apply rank feature scores (tag_score * 10 + pagerank)
+ sim = applyRankFeatureScoresForIDs(ids, field, sim, rankFeature)
+ common.Info("RerankWithKNN rankFeatureScores", zap.Any("rankFeature", rankFeature), zap.Any("simAfterRank", sim))
+
+ common.Info("RerankWithKNN completed", zap.Int("outputChunks", len(sim)))
+ return sim, tsim, vsim
+}
+
// toFloat64 converts various numeric types to float64
func toFloat64(v interface{}) (float64, bool) {
switch val := v.(type) {
diff --git a/internal/service/nlp/retrieval.go b/internal/service/nlp/retrieval.go
index 4cfd197f89..0050b0eaa5 100644
--- a/internal/service/nlp/retrieval.go
+++ b/internal/service/nlp/retrieval.go
@@ -66,6 +66,7 @@ type RetrievalRequest struct {
type RetrievalResult struct {
Chunks []map[string]interface{}
DocAggs []map[string]interface{} // Aggregated document counts, sorted by count desc
+ Total int64 // Post-pagination chunk count (matches Python's len(ranks["chunks"]))
}
// Retrieval performs hybrid search + reranking + pagination
@@ -76,8 +77,9 @@ type RetrievalResult struct {
// - Build chunks
// - Build document aggregation if specified
func (s *RetrievalService) Retrieval(ctx context.Context, req *RetrievalRequest) (*RetrievalResult, error) {
+ common.Info("Retrieval START", zap.String("question", req.Question), zap.Int("page", req.Page), zap.Int("pageSize", req.PageSize))
if req.Question == "" {
- return &RetrievalResult{Chunks: []map[string]interface{}{}, DocAggs: []map[string]interface{}{}}, nil
+ return &RetrievalResult{Chunks: []map[string]interface{}{}, DocAggs: []map[string]interface{}{}, Total: 0}, nil
}
// Apply default values
@@ -85,7 +87,7 @@ func (s *RetrievalService) Retrieval(ctx context.Context, req *RetrievalRequest)
req.Top = func() *int { v := 1024; return &v }()
}
if req.SimilarityThreshold == nil {
- req.SimilarityThreshold = func() *float64 { v := 0.0; return &v }()
+ req.SimilarityThreshold = func() *float64 { v := 0.2; return &v }()
}
if req.VectorSimilarityWeight == nil {
req.VectorSimilarityWeight = func() *float64 { v := 0.3; return &v }()
@@ -154,29 +156,130 @@ func (s *RetrievalService) Retrieval(ctx context.Context, req *RetrievalRequest)
return nil, fmt.Errorf("PruneDeletedChunks failed: %w", err)
}
if searchResult.Total == 0 {
- return &RetrievalResult{Chunks: []map[string]interface{}{}, DocAggs: []map[string]interface{}{}}, nil
+ return &RetrievalResult{Chunks: []map[string]interface{}{}, DocAggs: []map[string]interface{}{}, Total: 0}, nil
}
- vtWeight := *req.VectorSimilarityWeight
- tkWeight := 1.0 - vtWeight
+ // sim = tkWeight*tsim + vtWeight*vsim
+ vtWeightOrig := *req.VectorSimilarityWeight
+ tkWeightOrig := 1.0 - vtWeightOrig
+ tkWeight := tkWeightOrig
+ vtWeight := vtWeightOrig
qb := GetQueryBuilder()
useInfinity := engine.GetEngineType() != engine.EngineElasticsearch
- sim, term_similarity, vector_similarity := Rerank(
- req.RerankModel,
- searchResult.Chunks,
- int(searchResult.Total),
- nil,
- searchResult.QueryVector,
- req.Question,
- tkWeight,
- vtWeight,
- useInfinity,
- "content_ltks",
- qb,
- *req.RankFeature,
- )
+ useOceanBase := false // TODO: add OceanBase detection when supported
+
+ // For ES path: call GetScores() for second-pass KNN to get clean cosine similarity
+ // For Infinity path: use _score directly (scores already normalized during fusion)
+ // For OceanBase path: extract vectors and compute locally
+ var sim []float64
+ var term_similarity []float64
+ var vector_similarity []float64
+
+ if req.RerankModel != nil && searchResult.Total > 0 {
+ // External rerank model path - use RerankByModel
+ sim, term_similarity, vector_similarity = RerankByModel(
+ req.RerankModel,
+ searchResult.Chunks,
+ searchResult.IDs,
+ searchResult.Field,
+ req.Question,
+ tkWeight,
+ vtWeight,
+ "content_ltks",
+ qb,
+ *req.RankFeature,
+ )
+ } else if useInfinity {
+ // Infinity: scores already normalized before fusion, just extract _score
+ sim = make([]float64, len(searchResult.IDs))
+ for i, id := range searchResult.IDs {
+ if chunk, ok := searchResult.Field[id]; ok {
+ if score, ok := chunk["_score"].(float64); ok {
+ sim[i] = score
+ } else if score, ok := chunk["SCORE"].(float64); ok {
+ sim[i] = score
+ } else if score, ok := chunk["SIMILARITY"].(float64); ok {
+ sim[i] = score
+ } else {
+ sim[i] = 0.0
+ }
+ } else {
+ sim[i] = 0.0
+ }
+ }
+ term_similarity = sim
+ vector_similarity = sim
+ } else if useOceanBase {
+ // OceanBase: extract vectors and compute locally (not implemented)
+ sim = make([]float64, len(searchResult.IDs))
+ for i := range searchResult.IDs {
+ sim[i] = 0.0
+ }
+ term_similarity = sim
+ vector_similarity = sim
+ } else {
+ // ES PATH: Two-pass KNN approach for clean cosine similarity scores
+ //
+ // Python's equivalent flow (rag/nlp/search.py L656-669):
+ // 1. First search returns text+vector matched chunks (hybrid BM25 + KNN fusion)
+ // 2. _knn_scores: second KNN-only query filtered by those chunk IDs
+ // - ES computes cosine similarity between query vector and stored vectors
+ // - Vectors stay in ES index (not shipped to application)
+ // - Returns raw KNN result with _id -> _score mappings
+ // 3. get_scores: extracts doc_id -> score from the KNN result
+ // 4. rerank_with_knn: combines token similarity + vector similarity + rank features
+ //
+ // Go implementation mirrors this exactly:
+ // KNNScores() -> performs second KNN query (ES-specific, on DocEngine interface)
+ // GetScores() -> extracts doc_id -> score from result (matches Python's get_scores)
+ // RerankWithKNN() -> combines tksim + vtsim + rank_features (matches rerank_with_knn)
+ //
+ // Why two passes?
+ // - First search uses fusion (BM25 * vector_similarity_weight + KNN * (1-vector_similarity_weight))
+ // - The fusion score is not a clean cosine similarity - it's a weighted combination
+ // - Second KNN-only query gives us the pure cosine similarity for reranking
+ // - This keeps vectors in ES (no need to extract them) while getting clean scores
+
+ // PASS 1: Second KNN query to get clean cosine similarities
+ // KNNScores() performs the ES-specific KNN search and returns raw result
+ knnResult, err := s.docEngine.KNNScores(ctx, searchResult.Chunks, searchResult.QueryVector, len(searchResult.IDs))
+ if err != nil {
+ common.Warn("KNNScores failed for ES, falling back to local computation", zap.Error(err))
+ // Fallback: RerankStandard computes vector similarity locally (requires shipping vectors)
+ sim, term_similarity, vector_similarity = RerankStandard(
+ searchResult.Chunks,
+ nil, // keywords computed internally
+ searchResult.QueryVector,
+ req.Question,
+ tkWeight,
+ vtWeight,
+ "content_ltks",
+ qb,
+ *req.RankFeature,
+ )
+ } else {
+ // PASS 2: Extract scores from KNN result
+ // GetScores() mirrors Python's get_scores() - maps doc_id -> _score
+ knnScores := s.docEngine.GetScores(knnResult)
+
+ // RERANK: Combine token + vector + rank feature similarities
+ // Matches Python's rerank_with_knn(): sim = tkweight * tksim + vtweight * vtsim + rank_fea
+ sim, term_similarity, vector_similarity = RerankWithKNN(
+ searchResult.Chunks,
+ searchResult.IDs,
+ searchResult.Field,
+ knnScores,
+ req.Question,
+ tkWeight,
+ vtWeight,
+ "content_ltks",
+ qb,
+ *req.RankFeature,
+ )
+ }
+ }
if len(sim) == 0 {
- return &RetrievalResult{Chunks: []map[string]interface{}{}, DocAggs: []map[string]interface{}{}}, nil
+ return &RetrievalResult{Chunks: []map[string]interface{}{}, DocAggs: []map[string]interface{}{}, Total: 0}, nil
}
// Sort indices (positions into search results) by score descending
@@ -189,15 +292,14 @@ func (s *RetrievalService) Retrieval(ctx context.Context, req *RetrievalRequest)
for i, s := range sim {
idxScores = append(idxScores, idxScore{idx: i, score: s})
}
- sort.Slice(idxScores, func(i, j int) bool {
+ // Use SliceStable for deterministic ordering when scores are tied
+ sort.SliceStable(idxScores, func(i, j int) bool {
return idxScores[i].score > idxScores[j].score
})
// When vector_similarity_weight is 0, similarity_threshold is not meaningful for term-only scores
- // When doc_ids is explicitly provided (metadata or document filtering), bypass threshold
- // User wants those specific documents regardless of their relevance score
postThreshold := *req.SimilarityThreshold
- if *req.VectorSimilarityWeight <= 0 || len(req.DocIDs) > 0 {
+ if vtWeight <= 0 {
postThreshold = 0.0
}
@@ -209,7 +311,7 @@ func (s *RetrievalService) Retrieval(ctx context.Context, req *RetrievalRequest)
}
}
if len(validIdx) == 0 {
- return &RetrievalResult{Chunks: []map[string]interface{}{}, DocAggs: []map[string]interface{}{}}, nil
+ return &RetrievalResult{Chunks: []map[string]interface{}{}, DocAggs: []map[string]interface{}{}, Total: 0}, nil
}
// Calculate pagination
@@ -225,8 +327,8 @@ func (s *RetrievalService) Retrieval(ctx context.Context, req *RetrievalRequest)
}
pageIdx = validIdx[begin:end]
}
- common.Debug("Pagination result info", zap.Int("totalValid", len(validIdx)), zap.Int("begin", begin),
- zap.Int("end", end), zap.Int("chunkCount", len(pageIdx)))
+ common.Info("Pagination result info", zap.Int("totalValid", len(validIdx)), zap.Int("begin", begin),
+ zap.Int("end", end), zap.Int("chunkCount", len(pageIdx)), zap.Float64("postThreshold", postThreshold))
// Build chunks for pageIdx, transforms raw search results into the API response format
var filteredChunks []map[string]interface{}
@@ -274,15 +376,36 @@ func (s *RetrievalService) Retrieval(ctx context.Context, req *RetrievalRequest)
}
if v, ok := chunk["img_id"]; ok {
resultChunk["image_id"] = v
+ } else {
+ resultChunk["image_id"] = ""
}
- if v, ok := chunk["position_int"]; ok {
+ if v, ok := chunk["position_int"]; ok && v != nil {
resultChunk["positions"] = v
+ } else {
+ resultChunk["positions"] = []interface{}{}
}
- if v, ok := chunk["doc_type_kwd"]; ok {
- resultChunk["doc_type_kwd"] = v
- }
- if v, ok := chunk["mom_id"]; ok {
- resultChunk["mom_id"] = v
+ if v, ok := chunk["doc_type_kwd"]; ok && v != nil {
+ if s, ok := v.(string); ok {
+ if s == "" {
+ // Infinity's whitespace-# analyzer returns empty string as [] in Python SDK
+ // but as "" in Go SDK. Both Infinity and Elasticsearch paths normalize
+ // to None on the Python side (the test converts [] to None), so use nil
+ // here for parity instead of []interface{}{}.
+ resultChunk["doc_type_kwd"] = nil
+ } else {
+ resultChunk["doc_type_kwd"] = s
+ }
+ } else if sliceVal, ok := v.([]interface{}); ok {
+ if len(sliceVal) == 0 {
+ resultChunk["doc_type_kwd"] = nil
+ } else {
+ resultChunk["doc_type_kwd"] = sliceVal
+ }
+ } else {
+ resultChunk["doc_type_kwd"] = nil
+ }
+ } else {
+ resultChunk["doc_type_kwd"] = nil
}
// row_id: row identifier (for structured data like tables)
if v, ok := chunk["row_id()"]; ok {
@@ -291,6 +414,29 @@ func (s *RetrievalService) Retrieval(ctx context.Context, req *RetrievalRequest)
resultChunk["similarity"] = sim[i]
resultChunk["term_similarity"] = term_similarity[i]
resultChunk["vector_similarity"] = vector_similarity[i]
+
+ // Always set these fields even if empty, to match Python response format
+ if v, ok := chunk["important_kwd"]; ok {
+ resultChunk["important_kwd"] = v
+ } else {
+ resultChunk["important_kwd"] = []string{}
+ }
+ if v, ok := chunk["mom_id"]; ok {
+ resultChunk["mom_id"] = v
+ } else {
+ resultChunk["mom_id"] = ""
+ }
+ if v, ok := chunk["row_id()"]; ok {
+ resultChunk["row_id"] = v
+ } else {
+ resultChunk["row_id"] = nil
+ }
+ if v, ok := chunk["tag_kwd"]; ok {
+ resultChunk["tag_kwd"] = v
+ } else {
+ resultChunk["tag_kwd"] = []string{}
+ }
+
vectorColumn := fmt.Sprintf("q_%d_vec", dim)
if v, ok := chunk[vectorColumn]; ok {
resultChunk["vector"] = v
@@ -377,6 +523,7 @@ func (s *RetrievalService) Retrieval(ctx context.Context, req *RetrievalRequest)
return &RetrievalResult{
Chunks: filteredChunks,
DocAggs: docAggs,
+ Total: int64(len(filteredChunks)),
}, nil
}
@@ -407,6 +554,7 @@ type RetrievalSearchResult struct {
Keywords []string // Keywords from query
Aggregation []map[string]interface{} // Doc aggregation by field
Options map[string]interface{} // Engine-specific options (e.g., total from get_total)
+ IndexNames []string // Index names for second-pass queries (e.g., KNN scores)
}
// Search performs search based on question and EmbeddingModel:
@@ -420,6 +568,9 @@ func (s *RetrievalService) Search(ctx context.Context, req *RetrievalSearchReque
req.Highlight = func() *bool { v := false; return &v }()
}
filters := req.GetFilters()
+ if _, ok := filters["available_int"]; !ok {
+ filters["available_int"] = 1
+ }
pg := req.Page - 1
if pg < 0 {
pg = 0
@@ -440,6 +591,7 @@ func (s *RetrievalService) Search(ctx context.Context, req *RetrievalSearchReque
"doc_id", "chunk_order_int", "page_num_int", "top_int", "create_timestamp_flt", "knowledge_graph_kwd",
"question_kwd", "question_tks", "doc_type_kwd",
"available_int", "content_with_weight", "mom_id", "pagerank_fea", "tag_feas", "row_id()",
+ "_score",
}
kwds := make(map[string]struct{})
@@ -526,11 +678,22 @@ func (s *RetrievalService) Search(ctx context.Context, req *RetrievalSearchReque
if err != nil {
return nil, fmt.Errorf("Search failed: %w", err)
}
- // If result is empty, retry with lower min_match
+ // If result is empty, retry with relaxed conditions
if engineResult.Total == 0 {
_, hasDocIDFilter := filters["doc_id"]
if hasDocIDFilter {
- // Fallback without vector query when doc_id filter is present
+ // When a doc_id filter is present (e.g. from metadata filter like era=960)
+ // and the hybrid search returns no results, fall back to a filter-only
+ // search (no text match, no vector match). This ensures that when a
+ // metadata filter restricts the search to a specific set of documents
+ // that happen to have no relevant content for the query, we still
+ // return those documents' chunks (ordered by the request's sort/order).
+ //
+ // Example: searching "打虎" with metadata filter era≠960 limits the
+ // search to Three Kingdoms documents (era=220). Since "打虎" only
+ // appears in Water Margin (era=960), the hybrid search returns 0
+ // results. This fallback returns all chunks from Three Kingdoms
+ // documents instead of returning an empty result.
searchRequest.SelectFields = src
searchRequest.MatchExprs = []interface{}{}
searchRequest.RankFeature = nil
@@ -540,7 +703,10 @@ func (s *RetrievalService) Search(ctx context.Context, req *RetrievalSearchReque
return nil, fmt.Errorf("Search retry failed: %w", err)
}
} else {
- // Retry with lower min_match via QueryBuilder
+ // No doc_id filter — retry with lower min_match (0.1 vs default 0.3)
+ // and lower vector similarity threshold (0.17 vs default 0.1-0.2).
+ // This provides a second chance for queries that were too strict
+ // on the first attempt.
matchText, _ := GetQueryBuilder().Question(req.Question, "qa", 0.1)
matchDense.ExtraOptions["similarity"] = 0.17
searchRequest.MatchExprs = []interface{}{matchText, matchDense, fusionExpr}
@@ -573,7 +739,8 @@ func (s *RetrievalService) Search(ctx context.Context, req *RetrievalSearchReque
}
searchResult := engineResult
- ids := s.docEngine.GetDocIDs(searchResult.Chunks)
+ ids := s.docEngine.GetChunkIDs(searchResult.Chunks)
+ common.Info("GetChunkIDs result", zap.Int("count", len(ids)), zap.Strings("ids", ids))
// Build Keywords list from kwds set
keywordsList := make([]string, 0, len(kwds))
@@ -581,8 +748,14 @@ func (s *RetrievalService) Search(ctx context.Context, req *RetrievalSearchReque
keywordsList = append(keywordsList, k)
}
- // Build Field map
- fieldMap := s.docEngine.GetFields(searchResult.Chunks, nil)
+ fieldMap := s.docEngine.GetFields(searchResult.Chunks, src)
+ common.Info("GetFields result", zap.Int("count", len(fieldMap)), zap.Strings("keys", func() []string {
+ keys := make([]string, 0, len(fieldMap))
+ for k := range fieldMap {
+ keys = append(keys, k)
+ }
+ return keys
+ }()), zap.Strings("ids_from_GetDocIDs", ids))
// Build Aggregation
aggregation := s.docEngine.GetAggregation(searchResult.Chunks, "docnm_kwd")
@@ -602,6 +775,7 @@ func (s *RetrievalService) Search(ctx context.Context, req *RetrievalSearchReque
IDs: ids,
Keywords: keywordsList,
Aggregation: aggregation,
+ IndexNames: searchRequest.IndexNames,
}, nil
}
@@ -708,12 +882,13 @@ func RetrievalByChildren(chunks []map[string]interface{}, tenantIDs []string, do
}
// Calculate average similarity
- var totalSim float64
+ simBuf := make([]float64, 0, len(childList))
for _, c := range childList {
if sim, ok := c.chunk["similarity"].(float64); ok {
- totalSim += sim
+ simBuf = append(simBuf, sim)
}
}
+ totalSim := common.PairwiseSum(simBuf)
avgSim := totalSim / float64(len(childList))
// Collect content_ltks from children
@@ -738,9 +913,13 @@ func RetrievalByChildren(chunks []map[string]interface{}, tenantIDs []string, do
}
// Build aggregated chunk
- docTypeKwd := parentMap["doc_type_kwd"]
- if v, ok := docTypeKwd.(string); ok && v == "" {
- docTypeKwd = []interface{}{}
+ docTypeKwd := ""
+ if v, ok := parentMap["doc_type_kwd"].(string); ok {
+ docTypeKwd = v
+ }
+ imgID := parentMap["img_id"]
+ if imgID == nil || imgID == "" {
+ imgID = ""
}
aggregated := map[string]interface{}{
"chunk_id": momID,
@@ -750,7 +929,7 @@ func RetrievalByChildren(chunks []map[string]interface{}, tenantIDs []string, do
"docnm_kwd": parentMap["docnm_kwd"],
"kb_id": parentMap["kb_id"],
"important_kwd": allImportantKwd,
- "image_id": parentMap["img_id"],
+ "image_id": imgID,
"similarity": avgSim,
"vector_similarity": avgSim,
"term_similarity": avgSim,
diff --git a/internal/service/nlp/synonym.go b/internal/service/nlp/synonym.go
index 335e252590..de53366dfa 100644
--- a/internal/service/nlp/synonym.go
+++ b/internal/service/nlp/synonym.go
@@ -20,6 +20,7 @@ import (
"path/filepath"
"ragflow/internal/common"
"regexp"
+ "sort"
"strings"
"time"
@@ -195,6 +196,9 @@ func (s *Synonym) Lookup(tk string, topN int) []string {
}
}
+ // Sort for deterministic results (map iteration order is non-deterministic in Go)
+ sort.Strings(wnRes)
+
if len(wnRes) > topN {
return wnRes[:topN]
}
diff --git a/internal/service/nlp/term_weight.go b/internal/service/nlp/term_weight.go
index e72463674d..9c45b4ed39 100644
--- a/internal/service/nlp/term_weight.go
+++ b/internal/service/nlp/term_weight.go
@@ -343,12 +343,20 @@ func (d *TermWeightDealer) Weights(tks []string, preprocess bool) []TermWeight {
}
if s == 0 && len([]rune(t)) >= 4 {
// Try fine-grained tokenization
- fgTokens, _ := tokenizer.Tokenize(t)
+ fgTokens, _ := tokenizer.FineGrainedTokenize(t)
tokens := strings.Fields(fgTokens)
+ // Filter: only keep tokens with length > 1
+ var filteredTokens []string
+ for _, tt := range tokens {
+ if len([]rune(tt)) > 1 {
+ filteredTokens = append(filteredTokens, tt)
+ }
+ }
+
var validTokens []float64
- if len(tokens) > 1 {
- for _, tt := range tokens {
+ if len(filteredTokens) > 1 {
+ for _, tt := range filteredTokens {
f := freq(tt)
validTokens = append(validTokens, f)
}
@@ -381,12 +389,21 @@ func (d *TermWeightDealer) Weights(tks []string, preprocess bool) []TermWeight {
return 300
}
if len([]rune(t)) >= 4 {
- fgTokens, _ := tokenizer.Tokenize(t)
+ // Use fine-grained tokenization
+ fgTokens, _ := tokenizer.FineGrainedTokenize(t)
tokens := strings.Fields(fgTokens)
+ // Filter: only keep tokens with length > 1
+ var filteredTokens []string
+ for _, tt := range tokens {
+ if len([]rune(tt)) > 1 {
+ filteredTokens = append(filteredTokens, tt)
+ }
+ }
+
var validTokens []float64
- if len(tokens) > 1 {
- for _, tt := range tokens {
+ if len(filteredTokens) > 1 {
+ for _, tt := range filteredTokens {
f := df(tt)
validTokens = append(validTokens, f)
}
@@ -404,8 +421,14 @@ func (d *TermWeightDealer) Weights(tks []string, preprocess bool) []TermWeight {
}
// idf function
+ // Uses common.PyLog10 (C library's log10 via cgo) instead of
+ // math.Log10 to match Python's math.log10 exactly. Go's pure-Go
+ // math.Log10 can differ by 1 ULP from glibc's log10, causing parity
+ // test failures.
idf := func(s, N float64) float64 {
- return math.Log10(10 + ((N - s + 0.5) / (s + 0.5)))
+ arg := 10 + ((N - s + 0.5) / (s + 0.5))
+ result := common.PyLog10(arg)
+ return result
}
tw := []TermWeight{}
@@ -466,10 +489,12 @@ func (d *TermWeightDealer) Weights(tks []string, preprocess bool) []TermWeight {
return tw
}
- S := 0.0
- for _, twItem := range tw {
- S += twItem.Weight
+ // Use PairwiseSum to match Python's np.sum() which uses pairwise summation
+ weightBuf := make([]float64, len(tw))
+ for i, twItem := range tw {
+ weightBuf[i] = twItem.Weight
}
+ S := common.PairwiseSum(weightBuf)
if S > 0 {
for i := range tw {
diff --git a/internal/service/search.go b/internal/service/search.go
index 901cebb423..dcc8f7f122 100644
--- a/internal/service/search.go
+++ b/internal/service/search.go
@@ -118,7 +118,7 @@ func (s *SearchService) toSearchAppResponse(search *entity.Search) map[string]in
"status": search.Status,
"create_time": search.CreateTime,
"update_time": search.UpdateTime,
- "search_config": search.SearchConfig,
+ "search_config": map[string]interface{}(search.SearchConfig),
}
if search.Avatar != nil {
@@ -348,7 +348,7 @@ func (s *SearchService) GetDetail(searchID string) (map[string]interface{}, erro
"status": search.Status,
"create_time": search.CreateTime,
"update_time": search.UpdateTime,
- "search_config": search.SearchConfig,
+ "search_config": map[string]interface{}(search.SearchConfig),
}
if search.Avatar != nil {
diff --git a/internal/service/tenant.go b/internal/service/tenant.go
index 6166559980..708b7995cc 100644
--- a/internal/service/tenant.go
+++ b/internal/service/tenant.go
@@ -396,9 +396,15 @@ func (s *TenantService) GetDefaultModelName(tenantID string, modelType entity.Mo
case entity.ModelTypeImage2Text:
modelID = tenant.Img2TxtID
case entity.ModelTypeTTS:
+ if tenant.TTSID == nil {
+ return "", fmt.Errorf("tenant TTS model not configured")
+ }
modelID = *tenant.TTSID
case entity.ModelTypeOCR:
- modelID = tenant.OCRID
+ if tenant.OCRID == nil {
+ return "", fmt.Errorf("tenant OCR model not configured")
+ }
+ modelID = *tenant.OCRID
default:
return "", fmt.Errorf("invalid model type: %s", modelType)
}
@@ -537,7 +543,11 @@ func (s *TenantService) ListTenantDefaultModels(userID string) ([]ModelItem, err
})
}
- defaultOCRModelProvider, defaultOCRModelInstance, defaultOCRModelName, defaultOCRModelEnable, err := s.GetModelInfo(ownedTenant.TenantID, ownedTenant.OCRID, "ocr")
+ if ownedTenant.OCRID == nil {
+ return result, nil
+ }
+
+ defaultOCRModelProvider, defaultOCRModelInstance, defaultOCRModelName, defaultOCRModelEnable, err := s.GetModelInfo(ownedTenant.TenantID, *ownedTenant.OCRID, "ocr")
if err == nil {
result = append(result, ModelItem{
ModelProvider: defaultOCRModelProvider,
diff --git a/internal/tokenizer/tokenizer_concurrent_test.go b/internal/tokenizer/tokenizer_concurrent_test.go
index ea4b793cee..35b5a40143 100644
--- a/internal/tokenizer/tokenizer_concurrent_test.go
+++ b/internal/tokenizer/tokenizer_concurrent_test.go
@@ -30,7 +30,7 @@ import (
func init() {
// Initialize logger for tests
- if err := common.Init("info"); err != nil {
+ if err := common.Init("info", ""); err != nil {
fmt.Printf("Failed to initialize logger: %v\n", err)
}
}
diff --git a/rag/nlp/search.py b/rag/nlp/search.py
index a32d2cacba..e20cece79b 100644
--- a/rag/nlp/search.py
+++ b/rag/nlp/search.py
@@ -522,6 +522,7 @@ class Dealer:
sres.field[i]["important_kwd"] = [sres.field[i]["important_kwd"]]
ins_tw = []
for i in sres.ids:
+ #content_ltks = list(OrderedDict.fromkeys(sres.field[i][cfield].split()))
content_ltks = sres.field[i][cfield].split()
title_tks = [t for t in sres.field[i].get("title_tks", "").split() if t]
important_kwd = sres.field[i].get("important_kwd", [])
@@ -673,7 +674,8 @@ class Dealer:
ranks["doc_aggs"] = []
return ranks
- sorted_idx = np.argsort(sim_np * -1)
+ # Use stable sort for deterministic ordering when scores are tied
+ sorted_idx = np.argsort(sim_np * -1, kind='stable')
# When vector_similarity_weight is 0, similarity_threshold is not meaningful for term-only scores.
post_threshold = 0.0 if vector_similarity_weight <= 0 else similarity_threshold
diff --git a/rag/prompts/generator.py b/rag/prompts/generator.py
index 83309cc76f..edba89c808 100644
--- a/rag/prompts/generator.py
+++ b/rag/prompts/generator.py
@@ -297,10 +297,12 @@ async def cross_languages(tenant_id, llm_id, query, languages=[]):
ans = await chat_mdl.async_chat(rendered_sys_prompt, [{"role": "user", "content": rendered_user_prompt}],
{"temperature": 0.2})
- ans = re.sub(r"^.*</think>", "", ans, flags=re.DOTALL)
if ans.find("**ERROR**") >= 0:
+ logging.info("[cross_languages] LLM returned error, falling back to original query")
return query
- return "\n".join([a for a in re.sub(r"(^Output:|\n+)", "", ans, flags=re.DOTALL).split("===") if a.strip()])
+ ans = re.sub(r"^.*\*\*ERROR\*\*", "", ans, flags=re.DOTALL)
+ result = "\n".join([a for a in re.sub(r"(^Output:|\n+)", "", ans, flags=re.DOTALL).split("===") if a.strip()])
+ return result
async def content_tagging(chat_mdl, content, all_tags, examples, topn=3):
diff --git a/test/testcases/configs.py b/test/testcases/configs.py
index d6428d5527..466eab4034 100644
--- a/test/testcases/configs.py
+++ b/test/testcases/configs.py
@@ -23,6 +23,10 @@ ZHIPU_AI_API_KEY = os.getenv("ZHIPU_AI_API_KEY")
if ZHIPU_AI_API_KEY is None:
pytest.exit("Error: Environment variable ZHIPU_AI_API_KEY must be set")
+SILICONFLOW_API_KEY = os.getenv("SILICONFLOW_API_KEY")
+if SILICONFLOW_API_KEY is None:
+ pytest.exit("Error: Environment variable SILICONFLOW_API_KEY must be set")
+
# password is "123"
PASSWORD = """ctAseGvejiaSWWZ88T/m4FQVOpQyUvP+x7sXtdv3feqZACiQleuewkUi35E16wSd5C5QcnkkcV9cYc8TKPTRZlxappDuirxghxoOvFcJxFU4ixLsD
diff --git a/test/testcases/conftest.py b/test/testcases/conftest.py
index 41e9fc62c3..9ae399603f 100644
--- a/test/testcases/conftest.py
+++ b/test/testcases/conftest.py
@@ -93,7 +93,7 @@ _install_scholarly_stub()
import pytest
import requests
-from configs import EMAIL, HOST_ADDRESS, PASSWORD, VERSION, ZHIPU_AI_API_KEY
+from configs import EMAIL, HOST_ADDRESS, PASSWORD, VERSION, ZHIPU_AI_API_KEY, SILICONFLOW_API_KEY
MARKER_EXPRESSIONS = {
"p1": "p1",
@@ -196,6 +196,33 @@ def get_added_models(auth, factory_name):
return False
+def get_tenant_llm_added(auth, factory_name, model_name, model_type="rerank"):
+ """
+ Check whether a specific (factory, model_name, model_type) tenant_llm row exists.
+
+ Legacy /v1/llm/my_llms response shape:
+ {
+ "ZHIPU-AI": {"tags": ..., "llm": [{"name": ..., "type": ...}, ...]},
+ "SILICONFLOW": {"tags": ..., "llm": [{"name": ..., "type": ...}, ...]},
+ }
+ so we navigate by factory key first, then look through its llm list.
+ """
+ url = HOST_ADDRESS + f"/{VERSION}/llm/my_llms"
+ authorization = {"Authorization": auth}
+ response = requests.get(url=url, headers=authorization)
+ res = response.json()
+ if res.get("code") != 0:
+ return False
+ data = res.get("data") or {}
+ factory_data = data.get(factory_name) or {}
+ for m in factory_data.get("llm", []) or []:
+ if m.get("name") != model_name:
+ continue
+ if model_type is None or m.get("type") == model_type:
+ return True
+ return False
+
+
def add_models(auth):
# todo deprecated
url = HOST_ADDRESS + f"/{VERSION}/llm/set_api_key"
@@ -215,25 +242,86 @@ def add_models(auth):
def add_model_instance(auth):
add_provider_api = HOST_ADDRESS + "/api/v1/providers"
authorization = {"Authorization": auth}
- add_provider_response = requests.put(url=add_provider_api, headers=authorization, json={"provider_name": "ZHIPU-AI"})
- add_provider_res = add_provider_response.json()
- if add_provider_res.get("code") != 0:
- pytest.exit(f"Critical error in add model provider: {add_provider_res.get('message')}")
-
- add_instance_api = HOST_ADDRESS + "/api/v1/providers/ZHIPU-AI/instances"
- add_instance_response = requests.post(url=add_instance_api, headers=authorization, json={
- "instance_name": "CI",
- "api_key": ZHIPU_AI_API_KEY,
- "region": "default",
- "base_url": ""
- })
- add_instance_res = add_instance_response.json()
- if add_instance_res.get("code") != 0:
- pytest.exit(f"Critical error in add model instance: {add_instance_res.get('message')}")
-
- add_success = get_added_models(auth, "ZHIPU-AI")
- if not add_success:
- pytest.exit("Critical error in check added model: add model failed")
+
+ providers = [
+ ("ZHIPU-AI", ZHIPU_AI_API_KEY),
+ ("SILICONFLOW", SILICONFLOW_API_KEY),
+ ]
+
+ for provider_name, api_key in providers:
+ if not get_added_models(auth, provider_name):
+ add_provider_response = requests.put(url=add_provider_api, headers=authorization, json={"provider_name": provider_name})
+ add_provider_res = add_provider_response.json()
+ if add_provider_res.get("code") != 0:
+ pytest.exit(f"Critical error in add model provider: {add_provider_res.get('message')}")
+
+ # Register both "CI" (used by glm-4-flash@CI@ZHIPU-AI in configs.py
+ # and BAAI/bge-reranker-v2-m3@CI@SILICONFLOW) and "default".
+ for instance_name in ("CI", "default"):
+ add_instance_api = HOST_ADDRESS + f"/api/v1/providers/{provider_name}/instances"
+ add_instance_response = requests.post(url=add_instance_api, headers=authorization, json={
+ "instance_name": instance_name,
+ "api_key": api_key,
+ "region": "default",
+ "base_url": ""
+ })
+ add_instance_res = add_instance_response.json()
+ if add_instance_res.get("code") != 0:
+ msg = add_instance_res.get("message", "")
+ # Instance may already exist with a different API key from a
+ # prior test run; that's fine — skip instead of failing.
+ if "Already exist instance" in msg or "already exist" in msg.lower():
+ print(f"Note: {provider_name}/{instance_name} already exists, skipping")
+ continue
+ # Python API blocks creating instances named "default".
+ # The test_retrieval_parity test handles this by inserting
+ # "default" directly into the DB for SILICONFLOW.
+ if "cannot be 'default'" in msg:
+ print(f"Note: {provider_name}/{instance_name} blocked by API (name reserved), skipping")
+ continue
+ pytest.exit(
+ f"Critical error in add model instance {provider_name}/{instance_name}: "
+ f"{msg}"
+ )
+
+ add_success = get_added_models(auth, provider_name)
+ if not add_success:
+ pytest.exit(f"Critical error in check added model: {provider_name} add model failed")
+
+
+def add_siliconflow_rerank_llm(auth):
+ """
+ Register the BAAI/bge-reranker-v2-m3 rerank model under factory=SILICONFLOW / instance=CI.
+
+ This is the model referenced as `BAAI/bge-reranker-v2-m3@CI@SILICONFLOW` in
+ test_retrieval_parity.py. The /v1/llm/add_llm endpoint validates the key by
+ issuing a real rerank request, so the call requires network access to SiliconFlow
+ and a valid SILICONFLOW_API_KEY.
+ """
+ factory = "SILICONFLOW"
+ model_name = "BAAI/bge-reranker-v2-m3"
+ if get_tenant_llm_added(auth, factory, model_name, "rerank"):
+ return
+
+ url = HOST_ADDRESS + f"/{VERSION}/llm/add_llm"
+ authorization = {"Authorization": auth}
+ payload = {
+ "llm_factory": factory,
+ "llm_name": model_name,
+ "model_type": "rerank",
+ "api_key": SILICONFLOW_API_KEY,
+ "api_base": "",
+ }
+ response = requests.post(url=url, headers=authorization, json=payload)
+ res = response.json()
+ if res.get("code") != 0:
+ pytest.exit(
+ f"Critical error adding {factory} rerank model {model_name}: "
+ f"code={res.get('code')} message={res.get('message')} data={res.get('data')}"
+ )
+
+ if not get_tenant_llm_added(auth, factory, model_name, "rerank"):
+ pytest.exit(f"Failed to confirm {factory}/{model_name} rerank row was added")
def get_tenant_info(auth):
@@ -249,7 +337,7 @@ def get_tenant_info(auth):
@pytest.fixture(scope="session", autouse=True)
def set_tenant_info(auth):
- if not get_added_models(auth, "ZHIPU-AI"):
+ if not get_added_models(auth, "ZHIPU-AI") or not get_added_models(auth, "SILICONFLOW"):
try:
add_model_instance(auth)
except Exception as e:
@@ -282,3 +370,24 @@ def set_tenant_info(auth):
embd_res = set_default_embedding_response.json()
if embd_res.get("code") != 0:
raise Exception(embd_res.get("message"))
+
+
[email protected](scope="session", autouse=True)
+def set_tenant_siliconflow_rerank(auth):
+ """
+ Ensure the SiliconFlow BAAI/bge-reranker-v2-m3 rerank model is registered
+ for the test tenant. Used by test_retrieval_parity.py as
+ `BAAI/bge-reranker-v2-m3@CI@SILICONFLOW`.
+
+ Runs after `set_tenant_info` so the SILICONFLOW provider+CI instance
+ already exist when the /add_llm call is made.
+
+ If /add_llm is blocked (e.g. factory not in allowed list), the rerank
+ model config is resolved from FACTORY_LLM_INFOS at search time, so the
+ test can still proceed.
+ """
+ try:
+ add_siliconflow_rerank_llm(auth)
+ except Exception as e:
+ print(f"Note: Could not register SILICONFLOW rerank model via /add_llm: {e}")
+ print("The model config will be resolved from FACTORY_LLM_INFOS at runtime.")
diff --git a/test/testcases/restful_api/test_search_datasets_consistency.py b/test/testcases/restful_api/test_search_datasets_consistency.py
new file mode 100644
index 0000000000..13736a3bf6
--- /dev/null
+++ b/test/testcases/restful_api/test_search_datasets_consistency.py
@@ -0,0 +1,594 @@
+#
+#
+# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+"""
+Search-datasets consistency tests between Python (port 9380) and Go (port 9384) servers.
+Compares /api/v1/datasets/search endpoint responses for consistency between Python and Go.
+
+When an LLM is involved (rerank_id, keyword, or cross_languages is set), both sides
+call the LLM independently and the chunk *count*, ordering, and scores can drift
+across runs and between Python/Go. The test logs the per-chunk chunk_id + similarity
+for human inspection but skips strict count/order/score comparisons in those cases.
+
+When no LLM is involved, both servers run the same deterministic retrieval path
+with no non-deterministic dependencies, so the responses are expected to be
+byte-identical: same chunk count, same chunk order, and the same per-field
+values (with the empty-value normalization done in compare_chunks).
+
+All datasets and documents are created once at module level, then each test unit
+runs against the pre-built data. Cleanup happens automatically at module teardown.
+"""
+import logging
+import os
+import sys
+import pytest
+import requests
+import tempfile
+import time
+import uuid
+
+# Logging setup
+# Default is silent. Set LOG_LEVEL=INFO locally to see logger.info() messages.
+_LOG_LEVEL = os.environ.get("LOG_LEVEL", "WARNING").upper()
+_log_handler = logging.StreamHandler(sys.stderr)
+_log_handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
+logger = logging.getLogger("search_datasets_consistency")
+logger.setLevel(getattr(logging, _LOG_LEVEL, logging.WARNING))
+logger.addHandler(_log_handler)
+logger.propagate = False
+
+from test.testcases.utils import wait_for
+
+PYTHON_HOST = "http://localhost:9380"
+GO_HOST = "http://localhost:9384"
+
+# ---------------------------------------------------------------------------
+# Shared test documents
+# ---------------------------------------------------------------------------
+THREE_KINGDOMS_TXT = """
+曹操(155 年—220 年)
+作为曹魏政权的奠基者,曹操展现出卓越的政治、军事和文学才能。在政治上,
+他挟天子以令诸侯,稳固自身政治地位,推行一系列政策,如推行屯田制,不仅
+解决了粮食短缺问题,还使大量流民得以安置,促进了农业生产的恢复与发展 ;
+在军事方面,曹操一生征战无数,官渡之战以少胜多击败袁绍,统一北方,压制
+匈奴等异族势力,为北方地区带来相对稳定的局面 。然而,曹操的评价褒贬不
+一,"治世能臣"体现他在治理国家、施展政治抱负方面的才能;"乱世奸雄"
+则反映他在乱世中为达目的不择手段的一面 。
+
+刘备(161 年—223 年)
+蜀汉开国皇帝刘备,以"仁德"著称。他的一生充满传奇色彩,早年与关羽、张
+飞桃园结义,奠定兄弟情谊,三人患难与共,为兴复汉室而努力 。刘备求贤若
+渴,三顾茅庐请出诸葛亮,得到这位智谋之士的辅佐,为蜀汉政权的建立与发展
+奠定根基 。刘备善于笼络人心,凭借自身的人格魅力吸引了众多人才,在乱世
+中逐渐崛起,建立蜀汉,与曹魏、东吴形成三足鼎立之势 。
+
+孙权(182 年—252 年)
+东吴的建立者孙权,年少继承父兄基业,展现出非凡的领导才能 。他擅长用人
+制衡,麾下人才济济,周瑜、鲁肃、陆逊等皆是东吴的栋梁之才 。孙权依托长
+江天险,制定了稳健的战略,致力于开发江南经济与海外贸易,使东吴在三国中
+占据重要地位 。在赤壁之战中,孙权与刘备联军击败曹操,奠定三国鼎立的基
+础;之后又在夷陵之战中击败刘备,巩固了东吴在江南的统治 。
+
+赵云(?—229 年)
+赵云一生未败,长坂坡单骑救主,在曹操大军中七进七出,如入无人之境,成功
+救出刘备之子刘禅,被誉为"常胜将军" 。赵云跟随刘备多年,忠心耿耿,多
+次在危难时刻挺身而出,保护刘备及其家人的安全 。他武艺高强,为人正直,
+深受刘备和蜀军将士的敬重 。
+"""
+
+WATER_MARGIN_TXT = """
+宋江(?—?)
+宋江是梁山泊一百零八将之首,人称"及时雨"。他原为郓城县押司,因仗义疏财、
+济人贫苦,在江湖上广结英雄豪杰。宋江性格矛盾,一方面讲究忠义,一心想着
+招安报国;另一方面又领导梁山好汉对抗朝廷。他带领梁山好汉两赢童贯、三败
+高俅,最终接受朝廷招安,率军征讨辽国、田虎、王庆、方腊,立下赫赫战功。
+
+武松(?—?)
+武松因其排行第二,人称"武二郎"。景阳冈打虎,使他一举成名。武松武艺高强,
+力大无穷,是梁山泊步军头领。他因兄长武大郎被西门庆、潘金莲毒害,怒杀二
+人,被发配孟州。在孟州,他醉打蒋门神,帮助施恩夺回快活林。大闹飞云浦、
+血溅鸳鸯楼后,武松走上反抗道路,最终加入梁山。
+
+李逵(?—?)
+李逵是梁山泊好汉中最具反抗精神的代表,人称"黑旋风"。他性格鲁莽直率,
+嫉恶如仇,对宋江忠心耿耿。李逵使用两把板斧,在战场上所向披靡,屡立战功。
+他曾在江州劫法场救宋江,在沂岭杀四虎,展现了惊人的勇气和力量。
+"""
+
+ENGLISH_DOCS = """
+Artificial Intelligence
+Artificial intelligence (AI) is intelligence demonstrated by machines, in contrast to
+the natural intelligence displayed by humans and animals. Leading AI textbooks define
+the field as the study of "intelligent agents": any system that perceives its environment
+and takes actions that maximize its chance of successfully achieving its goals.
+AI applications include advanced web search engines (e.g., Google Search), recommendation
+systems (used by YouTube, Amazon, and Netflix), understanding human speech (such as Siri
+and Alexa), self-driving cars (e.g., Waymo), generative and creative tools (ChatGPT and
+AI art), and superhuman play and analysis in strategy games (such as chess and Go).
+
+Machine Learning
+Machine learning (ML) is a field of inquiry devoted to understanding and building methods
+that "learn" – that is, methods that leverage data to improve performance on some set of
+tasks. It is seen as a part of artificial intelligence. Machine learning algorithms build
+a model based on sample data, known as training data, in order to make predictions or
+decisions without being explicitly programmed to do so. Machine learning algorithms are
+used in a wide variety of applications, such as in medicine, email filtering, speech
+recognition, agriculture, and computer vision, where it is difficult or unfeasible to
+develop conventional algorithms to perform the needed tasks.
+
+Deep Learning
+Deep learning is part of a broader family of machine learning methods based on artificial
+neural networks with representation learning. Learning can be supervised, semi-supervised
+or unsupervised. Deep-learning architectures such as deep neural networks, deep belief
+networks, deep reinforcement learning, recurrent neural networks, convolutional neural
+networks and transformers have been applied to fields including computer vision, speech
+recognition, natural language processing, machine translation, bioinformatics, drug design,
+medical image analysis, climate science, material inspection and board game programs,
+where they have produced results comparable to and in some cases surpassing human expert
+performance.
+"""
+# ---------------------------------------------------------------------------
+# Helpers
+# ---------------------------------------------------------------------------
+def compare_chunks(python_chunk, go_chunk):
+ """Compare a single chunk between Python and Go responses."""
+ all_keys = set(python_chunk.keys()) | set(go_chunk.keys())
+
+ for field in all_keys:
+ p_val = python_chunk.get(field)
+ g_val = go_chunk.get(field)
+
+ # Normalize empty values to None so the comparison treats
+ # "no value" shapes interchangeably across engines:
+ # Python Infinity: [] (keyword list, infinity_conn.py:793-794)
+ # Python Elasticsearch: "" (keyword, es_conn.py:626-644)
+ # Go (both engines): None (intentional — see retrieval.go:403-424)
+ if isinstance(p_val, (list, str)) and not p_val:
+ p_val = None
+ if isinstance(g_val, (list, str)) and not g_val:
+ g_val = None
+
+ if p_val is None or g_val is None:
+ if p_val != g_val:
+ raise AssertionError(f"Field '{field}': python={p_val}, go={g_val}")
+ continue
+
+ if field in ("similarity", "term_similarity", "vector_similarity"):
+ if p_val != g_val:
+ raise AssertionError(
+ f"Field '{field}' differs: python={p_val}, go={g_val}, diff={abs(p_val - g_val)}"
+ )
+ elif isinstance(p_val, (list, dict)):
+ if p_val != g_val:
+ raise AssertionError(f"Field '{field}' mismatch")
+ else:
+ if p_val != g_val:
+ raise AssertionError(f"Field '{field}': python={p_val}, go={g_val}")
+
+
+def search_and_compare(rest_client, dataset_ids, cfg):
+ """Perform search on both servers and compare results.
+
+ dataset_ids can be a single dataset ID string or a list of dataset IDs.
+ cfg is a dict with keys matching the search payload:
+ question (required), top_k (default 5), rerank_id, search_id, keyword,
+ vector_similarity_weight, similarity_threshold, use_kg, cross_languages,
+ page, size, meta_data_filter
+ """
+ headers = {"Authorization": f"Bearer {rest_client.token}"}
+ if isinstance(dataset_ids, str):
+ ids = [dataset_ids]
+ else:
+ ids = list(dataset_ids)
+
+ search_payload = {
+ "dataset_ids": ids,
+ "question": cfg["question"],
+ "top_k": cfg.get("top_k", 5),
+ }
+ optional_fields = [
+ "rerank_id", "search_id", "keyword", "vector_similarity_weight",
+ "similarity_threshold", "use_kg", "cross_languages", "page", "size",
+ "meta_data_filter", "doc_ids",
+ ]
+ for field in optional_fields:
+ value = cfg.get(field)
+ if value is not None:
+ search_payload[field] = value
+
+ # Call Python server
+ python_res = rest_client.post("/datasets/search", json=search_payload)
+ assert python_res.status_code == 200, f"Python server error: {python_res.status_code}, body: {python_res.text}"
+ python_data = python_res.json()
+ assert python_data["code"] == 0, f"Python payload error: {python_data}"
+
+ # Call Go server with same auth
+ go_res = requests.post(
+ f"{GO_HOST}/api/v1/datasets/search",
+ json=search_payload,
+ headers=headers,
+ timeout=30,
+ )
+ assert go_res.status_code == 200, f"Go server error: {go_res.status_code}, body: {go_res.text}"
+ go_data = go_res.json()
+ assert go_data["code"] == 0, f"Go payload error: {go_data}"
+
+ python_chunks = python_data["data"]["chunks"]
+ go_chunks = go_data["data"]["chunks"]
+
+ logger.info(f"python_chunks={len(python_chunks)}, go_chunks={len(go_chunks)}")
+ logger.info(f" Python chunks: {[(c.get('chunk_id','?'), c.get('similarity',0)) for c in python_chunks]}")
+ logger.info(f" Go chunks: {[(c.get('chunk_id','?'), c.get('similarity',0)) for c in go_chunks]}")
+
+ llm_involved = bool(cfg.get("rerank_id") or cfg.get("keyword") or cfg.get("cross_languages"))
+ if not llm_involved:
+ assert len(python_chunks) == len(go_chunks), \
+ f"Chunk count differs: python={len(python_chunks)}, go={len(go_chunks)}"
+ for i, (p_chunk, g_chunk) in enumerate(zip(python_chunks, go_chunks)):
+ try:
+ compare_chunks(p_chunk, g_chunk)
+ except AssertionError as e:
+ raise AssertionError(f"Chunk {i} comparison failed: {e}")
+ python_total = python_data["data"].get("total", 0)
+ go_total = go_data["data"].get("total", 0)
+ if python_total != go_total:
+ raise AssertionError(f"total differs: python={python_total}, go={go_total}")
+
+ return len(python_chunks), len(python_chunks)
+
+
+def _upload_and_parse(rest_client, dataset_id, text, filename="doc.txt"):
+ """Upload text as a file and wait for parsing to complete. Returns document_id."""
+ with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False, encoding='utf-8') as f:
+ f.write(text)
+ temp_path = f.name
+
+ with open(temp_path, 'rb') as f:
+ files = [("file", (filename, f))]
+ upload_res = rest_client.post(f"/datasets/{dataset_id}/documents", files=files)
+ assert upload_res.status_code == 200, f"Failed to upload {filename}: {upload_res.text}"
+ doc_id = upload_res.json()["data"][0]["id"]
+
+ parse_res = rest_client.post(
+ f"/datasets/{dataset_id}/documents/parse",
+ json={"document_ids": [doc_id]},
+ )
+ assert parse_res.status_code == 200, f"Failed to start parsing {filename}: {parse_res.text}"
+
+ @wait_for(120, 2, f"Document parsing timeout for {filename}")
+ def check_parsed():
+ doc_res = rest_client.get(f"/datasets/{dataset_id}/documents", params={"id": doc_id})
+ if doc_res.status_code != 200:
+ return False
+ docs = doc_res.json()["data"]["docs"]
+ return bool(docs) and docs[0].get("run") == "DONE"
+
+ check_parsed()
+ return doc_id
+
+
+def _get_chunk_count(rest_client, dataset_id, doc_id):
+ """Return the number of chunks for a parsed document."""
+ doc_res = rest_client.get(f"/datasets/{dataset_id}/documents", params={"id": doc_id})
+ docs = doc_res.json()["data"]["docs"]
+ return docs[0].get("chunk_count", 0)
+
+
+def _set_metadata(rest_client, dataset_id, doc_id, updates):
+ """Set metadata key-value pairs on a document."""
+ headers = {"Authorization": f"Bearer {rest_client.token}"}
+ formatted_updates = [{"key": k, "value": v} for k, v in updates.items()]
+ res = requests.patch(
+ f"{PYTHON_HOST}/api/v1/datasets/{dataset_id}/documents/metadatas",
+ json={
+ "selector": {"document_ids": [doc_id]},
+ "updates": formatted_updates,
+ },
+ headers=headers,
+ timeout=30,
+ )
+ assert res.status_code == 200, f"Failed to set metadata: {res.text}"
+ assert res.json()["code"] == 0, f"Metadata update error: {res.json()}"
+ meta_str = ", ".join(f"{k}={v}" for k, v in updates.items())
+ logger.info(f" metadata set on doc {doc_id}: {meta_str}")
+
+
+# ---------------------------------------------------------------------------
+# Module-level fixture: create all datasets once, delete at the end
+# ---------------------------------------------------------------------------
[email protected](scope="module")
+def all_datasets(rest_client):
+ """Set up all datasets and documents once for the module.
+
+ Returns a dict with:
+ ds_chinese : 1 dataset with 2 files (Three Kingdoms + Water Margin, with metadata)
+ ds_chinese_doc1 : doc_id for Three Kingdoms
+ ds_chinese_doc2 : doc_id for Water Margin
+
+ ds_chinese_2 : 1 dataset with Three Kingdoms only
+ ds_3k_doc : doc_id
+
+ ds_english : 1 dataset with English text
+ ds_english_doc : doc_id
+ """
+ logger.info("\n[SETUP] Creating all datasets for search-datasets consistency tests...")
+
+ data = {}
+
+ # -----------------------------------------------------------------------
+ # 1) 1 dataset with 2 files (Chinese)
+ # -----------------------------------------------------------------------
+ create_res = rest_client.post("/datasets", json={
+ "name": "consistency_chinese",
+ "embedding_model": "BAAI/bge-small-en-v1.5@Builtin",
+ "parser_config": {"chunk_token_num": 1, "delimiter": "`\n\n`"},
+ })
+ assert create_res.status_code == 200, create_res.text
+ assert create_res.json()["code"] == 0, create_res.json()
+ ds_chinese_id = create_res.json()["data"]["id"]
+
+ doc1 = _upload_and_parse(rest_client, ds_chinese_id, THREE_KINGDOMS_TXT, "three_kingdoms.txt")
+ doc2 = _upload_and_parse(rest_client, ds_chinese_id, WATER_MARGIN_TXT, "water_margin.txt")
+ logger.info(f" ds_chinese: {ds_chinese_id} (3k={doc1}, wm={doc2})")
+ logger.info(f" 3K chunks: {_get_chunk_count(rest_client, ds_chinese_id, doc1)}")
+ logger.info(f" WM chunks: {_get_chunk_count(rest_client, ds_chinese_id, doc2)}")
+
+ # Set metadata on individual documents
+ _set_metadata(rest_client, ds_chinese_id, doc1, {"era": 220, "source": "luo", "character": ["曹操", "刘备", "孙权", "赵云"]})
+ _set_metadata(rest_client, ds_chinese_id, doc2, {"era": 960, "source": "shi", "character": ["宋江", "武松", "李逵"]})
+
+ data["ds_chinese"] = ds_chinese_id
+ data["ds_chinese_doc1"] = doc1
+ data["ds_chinese_doc2"] = doc2
+
+ # -----------------------------------------------------------------------
+ # 2) 1 dataset with Three Kingdoms only
+ # -----------------------------------------------------------------------
+ create_res = rest_client.post("/datasets", json={
+ "name": "consistency_three_kingdoms",
+ "embedding_model": "BAAI/bge-small-en-v1.5@Builtin",
+ "parser_config": {"chunk_token_num": 1, "delimiter": "`\n\n`"},
+ })
+ assert create_res.status_code == 200, create_res.text
+ assert create_res.json()["code"] == 0, create_res.json()
+ ds_3k_id = create_res.json()["data"]["id"]
+
+ doc_3k = _upload_and_parse(rest_client, ds_3k_id, THREE_KINGDOMS_TXT, "three_kingdoms.txt")
+ logger.info(f" ds_chinese_2: {ds_3k_id} (doc={doc_3k})")
+ logger.info(f" chunks: {_get_chunk_count(rest_client, ds_3k_id, doc_3k)}")
+
+ data["ds_chinese_2"] = ds_3k_id
+ data["ds_3k_doc"] = doc_3k
+
+ # Wait for metadata to be indexed
+ time.sleep(2)
+
+ # -----------------------------------------------------------------------
+ # 3) 1 dataset with English text
+ # -----------------------------------------------------------------------
+ create_res = rest_client.post("/datasets", json={
+ "name": "consistency_english",
+ "embedding_model": "BAAI/bge-small-en-v1.5@Builtin",
+ "parser_config": {"chunk_token_num": 1, "delimiter": "`\n\n`"},
+ })
+ assert create_res.status_code == 200, create_res.text
+ assert create_res.json()["code"] == 0, create_res.json()
+ ds_en_id = create_res.json()["data"]["id"]
+
+ doc_en = _upload_and_parse(rest_client, ds_en_id, ENGLISH_DOCS, "english_docs.txt")
+ logger.info(f" ds_english: {ds_en_id} (doc={doc_en})")
+ logger.info(f" chunks: {_get_chunk_count(rest_client, ds_en_id, doc_en)}")
+
+ data["ds_english"] = ds_en_id
+ data["ds_english_doc"] = doc_en
+
+ logger.info("[SETUP] All datasets ready.\n")
+
+ yield data
+
+ # Teardown: delete all datasets
+ logger.info("\n[TEARDOWN] Deleting all datasets...")
+ all_ids = [
+ data["ds_chinese"],
+ data["ds_chinese_2"],
+ data["ds_english"],
+ ]
+ res = rest_client.delete("/datasets", json={"ids": all_ids})
+ assert res.status_code == 200, f"Teardown failed: {res.text}"
+ logger.info("[TEARDOWN] Done.")
+
+
+# Skip every test in this module from CI. Remove the next line to re-enable.
+pytestmark = pytest.mark.skipif(
+ os.getenv("CI") == "true",
+ reason="GO server is not started in CI",
+)
+
+# ---------------------------------------------------------------------------
+# Test Unit 1: Search consistency — 1 dataset with 2 files
+# ---------------------------------------------------------------------------
+def test_search_datasets_consistency_basic(rest_client, all_datasets):
+ """
+ Compare /api/v1/datasets/search responses between Python and Go servers for consistency.
+ Tests the single dataset (ds_chinese) which contains 2 files.
+ """
+ dataset_id = all_datasets["ds_chinese"]
+ doc1 = all_datasets["ds_chinese_doc1"]
+ doc2 = all_datasets["ds_chinese_doc2"]
+ logger.info(f"Using dataset (Chinese, 2 files): {dataset_id} (doc1={doc1}, doc2={doc2})")
+
+ search_configs = [
+ {"question": "曹操"},
+ {"question": "曹操", "page": 2, "size": 2},
+ {"question": "曹操", "top_k": 2},
+ {"question": "曹操", "similarity_threshold": 0.0},
+ {"question": "曹操", "similarity_threshold": 0.5},
+ {"question": "曹操", "vector_similarity_weight": 0.0},
+ {"question": "曹操", "vector_similarity_weight": 0.7},
+ {"question": "曹操", "keyword": True},
+ {"question": "努力发展农业", "keyword": True},
+ {"question": "political status", "cross_languages": ["Chinese"]},
+ {"question": "诸葛亮"},
+ {"question": "努力发展农业"},
+ {"question": "曹操 诸葛亮 周瑜"},
+ {"question": "曹操", "top_k": 3, "rerank_id": "BAAI/bge-reranker-v2-m3@CI@SILICONFLOW"},
+ {"question": "曹操", "doc_ids": [doc1]},
+ {"question": "曹操", "doc_ids": [doc2]},
+ {"question": "曹操", "doc_ids": []},
+ ]
+
+ for cfg in search_configs:
+ cfg_str = ", ".join(f"{k}={v}" for k, v in cfg.items())
+ logger.info(f"\n--- Testing: {cfg_str} ---")
+ total, chunk_count = search_and_compare(rest_client, dataset_id, cfg)
+ logger.info(f"SUCCESS: Python and Go responses match for {chunk_count} chunks, total={total}")
+
+
+# ---------------------------------------------------------------------------
+# Test Unit 2: Metadata filter consistency
+# ---------------------------------------------------------------------------
+def test_search_datasets_consistency_metadata_filter(rest_client, all_datasets):
+ """
+ Compare Python vs Go search with metadata filtering on the Chinese dataset for consistency.
+ Uses ds_chinese which has 2 documents with metadata:
+ - doc1: era=220, source=luo, character=[曹操,刘备,孙权,赵云]
+ - doc2: era=960, source=shi, character=[宋江,武松,李逵]
+ """
+ dataset_id = all_datasets["ds_chinese"]
+ logger.info(f"Using dataset (Chinese, metadata): {dataset_id}")
+
+ search_configs = [
+ # Manual filters
+ {"question": "打虎", "meta_data_filter": {"method": "manual", "manual": [{"key": "era", "op": "=", "value": 960}]}},
+ {"question": "曹操", "meta_data_filter": {"method": "manual", "manual": [{"key": "source", "op": "=", "value": "luo"}]}},
+ {"question": "打虎", "meta_data_filter": {"method": "manual", "manual": [{"key": "era", "op": "≠", "value": 960}]}},
+ {"question": "打虎", "meta_data_filter": {"method": "manual", "manual": [{"key": "era", "op": ">", "value": 220}]}},
+ {"question": "曹操", "meta_data_filter": {"method": "manual", "manual": [{"key": "source", "op": "contains", "value": "luo"}]}},
+ {"question": "努力发展农业", "meta_data_filter": {"method": "manual", "manual": [{"key": "character", "op": "in", "value": ["曹操","孙权"]}]}},
+ {"question": "打虎", "meta_data_filter": {"method": "manual", "manual": [{"key": "character", "op": "=", "value": "武松"}]}},
+ ]
+
+ for cfg in search_configs:
+ cfg_str = ", ".join(f"{k}={v}" for k, v in cfg.items())
+ logger.info(f"\n--- Metadata filter: {cfg_str} ---")
+ total, chunk_count = search_and_compare(rest_client, dataset_id, cfg)
+ logger.info(f"SUCCESS: {chunk_count} chunks, total={total}")
+
+
+# ---------------------------------------------------------------------------
+# Test Unit 3: Search with search_id
+# ---------------------------------------------------------------------------
+def test_search_datasets_consistency_with_search_id(rest_client, all_datasets):
+ """
+ Compare Python vs Go with search_id parameter for consistency.
+ Creates a search config with multiple parameters set, then tests that
+ both servers honor the stored config overrides.
+ """
+ dataset_id = all_datasets["ds_chinese"]
+ logger.info(f"Using dataset (Chinese): {dataset_id}")
+
+ # Create a search config first
+ search_name = f"consistency_search_{uuid.uuid4().hex[:8]}"
+ search_res = rest_client.post("/searches", json={"name": search_name, "description": "consistency test search"})
+ assert search_res.status_code == 200, f"Failed to create search: {search_res.text}"
+ search_payload = search_res.json()
+ assert search_payload["code"] == 0, f"Search creation error: {search_payload}"
+ search_id = search_payload["data"]["search_id"]
+ logger.info(f"Created search_id: {search_id}")
+
+ # Update the search config with multiple parameters
+ search_config = {
+ "similarity_threshold": 0.2,
+ "vector_similarity_weight": 0.7,
+ "top_k": 3,
+ "use_kg": False,
+ "keyword": False,
+ }
+ update_res = rest_client.put(
+ f"/searches/{search_id}",
+ json={"name": search_name, "search_config": search_config},
+ )
+ assert update_res.status_code == 200, f"Failed to update search config: {update_res.text}"
+ assert update_res.json()["code"] == 0, f"Search config update error: {update_res.json()}"
+ logger.info(f"Updated search config: {search_config}")
+
+ # Search with search_id — config overrides should apply
+ search_configs = [
+ {"question": "曹操", "search_id": search_id},
+ {"question": "曹操 诸葛亮", "search_id": search_id},
+ ]
+
+ for cfg in search_configs:
+ cfg_str = ", ".join(f"{k}={v}" for k, v in cfg.items())
+ logger.info(f"\n--- Testing with search_id: {cfg_str} ---")
+ total, chunk_count = search_and_compare(rest_client, dataset_id, cfg)
+ logger.info(f"SUCCESS: Python and Go responses match for {chunk_count} chunks, total={total}")
+
+
+# ---------------------------------------------------------------------------
+# Test Unit 4: Multi-dataset search
+# ---------------------------------------------------------------------------
+def test_search_datasets_consistency_multi_dataset(rest_client, all_datasets):
+ """
+ Compare Python vs Go when searching across 2 datasets simultaneously for consistency.
+ Uses ds_chinese and ds_chinese_2.
+ """
+ both_ids = [all_datasets["ds_chinese"], all_datasets["ds_chinese_2"]]
+ logger.info(f"Using datasets (multi): ds_chinese={all_datasets['ds_chinese']}, ds_chinese_2={all_datasets['ds_chinese_2']}")
+
+ search_configs = [
+ {"question": "武松打虎"},
+ {"question": "努力发展农业"},
+ {"question": "曹操 宋江"},
+ ]
+
+ for cfg in search_configs:
+ cfg_str = ", ".join(f"{k}={v}" for k, v in cfg.items())
+ logger.info(f"\n--- Multi-dataset Testing: {cfg_str} ---")
+ total, chunk_count = search_and_compare(rest_client, both_ids, cfg)
+ logger.info(f"SUCCESS: Python and Go responses match for {chunk_count} chunks, total={total}")
+
+
+# ---------------------------------------------------------------------------
+# Test Unit 5: English search consistency
+# ---------------------------------------------------------------------------
+def test_search_datasets_consistency_english(rest_client, all_datasets):
+ """
+ Compare Python vs Go with English text documents for consistency.
+ Uses ds_english.
+ """
+ dataset_id = all_datasets["ds_english"]
+ logger.info(f"Using English dataset: {dataset_id}")
+
+ search_configs = [
+ {"question": "artificial intelligence"},
+ {"question": "neural networks and deep learning"},
+ {"question": "neural networks", "keyword": True},
+ {"question": "convolutional neural networks", "keyword": True},
+ {"question": "人工智能", "cross_languages": ["English"]},
+ {"question": "机器学习", "cross_languages": ["English"]},
+ ]
+
+ for cfg in search_configs:
+ cfg_str = ", ".join(f"{k}={v}" for k, v in cfg.items())
+ logger.info(f"\n--- Testing (English): {cfg_str} ---")
+ total, chunk_count = search_and_compare(rest_client, dataset_id, cfg)
+ logger.info(f"SUCCESS: Python and Go responses match for {chunk_count} chunks, total={total}")