fix(retrieval): bound automatic graph retrieval
This commit is contained in:
@@ -17,6 +17,44 @@ type Neo4jRepository struct {
|
||||
nodePrefix string
|
||||
}
|
||||
|
||||
const (
|
||||
maxGraphSearchEntities = 8
|
||||
maxGraphSearchRows = 100
|
||||
)
|
||||
|
||||
func normalizeSearchNodes(nodes []string) []string {
|
||||
result := make([]string, 0, min(len(nodes), maxGraphSearchEntities))
|
||||
seen := make(map[string]struct{}, len(nodes))
|
||||
for _, node := range nodes {
|
||||
node = strings.TrimSpace(node)
|
||||
if node == "" {
|
||||
continue
|
||||
}
|
||||
key := strings.ToLower(node)
|
||||
if _, ok := seen[key]; ok {
|
||||
continue
|
||||
}
|
||||
seen[key] = struct{}{}
|
||||
result = append(result, node)
|
||||
if len(result) == maxGraphSearchEntities {
|
||||
break
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func buildSearchNodeQuery(labelExpr string) string {
|
||||
return `
|
||||
MATCH (n:` + labelExpr + `)-[r]-(m:` + labelExpr + `)
|
||||
WHERE ANY(nodeText IN $nodes WHERE n.name CONTAINS nodeText)
|
||||
WITH n, r, m,
|
||||
ANY(nodeText IN $nodes WHERE n.name = nodeText) AS exact_match
|
||||
ORDER BY exact_match DESC, n.name ASC, type(r) ASC, m.name ASC
|
||||
LIMIT $row_limit
|
||||
RETURN n, r, m
|
||||
`
|
||||
}
|
||||
|
||||
// NewNeo4jRepository creates a new Neo4j repository
|
||||
func NewNeo4jRepository(driver neo4j.Driver) interfaces.RetrieveGraphRepository {
|
||||
return &Neo4jRepository{driver: driver, nodePrefix: "ENTITY"}
|
||||
@@ -170,17 +208,20 @@ func (n *Neo4jRepository) SearchNode(
|
||||
logger.Warnf(ctx, "NOT SUPPORT RETRIEVE GRAPH")
|
||||
return nil, nil
|
||||
}
|
||||
nodes = normalizeSearchNodes(nodes)
|
||||
if len(nodes) == 0 {
|
||||
return &types.GraphData{}, nil
|
||||
}
|
||||
session := n.driver.NewSession(ctx, neo4j.SessionConfig{AccessMode: neo4j.AccessModeRead})
|
||||
defer session.Close(ctx)
|
||||
|
||||
result, err := session.ExecuteRead(ctx, func(tx neo4j.ManagedTransaction) (interface{}, error) {
|
||||
labelExpr := n.Label(namespace)
|
||||
query := `
|
||||
MATCH (n:` + labelExpr + `)-[r]-(m:` + labelExpr + `)
|
||||
WHERE ANY(nodeText IN $nodes WHERE n.name CONTAINS nodeText)
|
||||
RETURN n, r, m
|
||||
`
|
||||
params := map[string]interface{}{"nodes": nodes}
|
||||
query := buildSearchNodeQuery(labelExpr)
|
||||
params := map[string]interface{}{
|
||||
"nodes": nodes,
|
||||
"row_limit": maxGraphSearchRows,
|
||||
}
|
||||
result, err := tx.Run(ctx, query, params)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to run query: %v", err)
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package neo4j
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNormalizeSearchNodesDropsBlankDeduplicatesAndCaps(t *testing.T) {
|
||||
got := normalizeSearchNodes([]string{
|
||||
" CRA ", "", "cra", "控制柜", "control", "CONTROL", "A", "B", "C", "D", "E", "F",
|
||||
})
|
||||
want := []string{"CRA", "控制柜", "control", "A", "B", "C", "D", "E"}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("normalizeSearchNodes() = %#v, want %#v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildSearchNodeQueryIsDeterministicAndBounded(t *testing.T) {
|
||||
query := buildSearchNodeQuery("ENTITY_KB")
|
||||
for _, required := range []string{
|
||||
"exact_match",
|
||||
"ORDER BY exact_match DESC",
|
||||
"LIMIT $row_limit",
|
||||
"RETURN n, r, m",
|
||||
} {
|
||||
if !strings.Contains(query, required) {
|
||||
t.Fatalf("query must contain %q:\n%s", required, query)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user