Refactor QueryDSL and search types to utilize GOU types directly
- Updated the `QueryDSLGenerator` interface to use `gou.QueryDSL` and `model.Model` types for improved compatibility with Yao's query system. - Revised the `Request` struct to incorporate GOU types for `Wheres` and `Orders`, enhancing the integration with the GOU QueryDSL format. - Removed deprecated `QueryWhere` and `QueryOrder` types, streamlining the codebase and reducing redundancy. - Enhanced documentation in `DESIGN.md` to reflect these changes and provide guidance on using GOU types directly.
This commit is contained in:
parent
d264c7a784
commit
f8ba875cd3
3 changed files with 68 additions and 105 deletions
|
|
@ -447,7 +447,8 @@ type KeywordExtractor interface {
|
||||||
// QueryDSLGenerator generates QueryDSL for DB search
|
// QueryDSLGenerator generates QueryDSL for DB search
|
||||||
type QueryDSLGenerator interface {
|
type QueryDSLGenerator interface {
|
||||||
// Generate converts natural language to QueryDSL
|
// Generate converts natural language to QueryDSL
|
||||||
Generate(ctx *context.Context, query string, schemas []*types.ModelSchema) (*types.QueryDSL, error)
|
// Uses GOU types directly: model.Model and gou.QueryDSL
|
||||||
|
Generate(query string, models []*model.Model) (*gou.QueryDSL, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note: Embedding is handled by KB collection's own config (embedding provider + model),
|
// Note: Embedding is handled by KB collection's own config (embedding provider + model),
|
||||||
|
|
@ -480,6 +481,10 @@ All types are defined in `search/types/` package to prevent circular dependencie
|
||||||
```go
|
```go
|
||||||
package types
|
package types
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/yaoapp/gou/query/gou"
|
||||||
|
)
|
||||||
|
|
||||||
// SearchType represents the type of search
|
// SearchType represents the type of search
|
||||||
type SearchType string
|
type SearchType string
|
||||||
|
|
||||||
|
|
@ -516,28 +521,17 @@ type Request struct {
|
||||||
Graph bool `json:"graph,omitempty"` // Enable graph association
|
Graph bool `json:"graph,omitempty"` // Enable graph association
|
||||||
|
|
||||||
// Database search specific
|
// Database search specific
|
||||||
Models []string `json:"models,omitempty"` // Model IDs (e.g., "user", "agents.mybot.product")
|
// Uses GOU QueryDSL types directly for compatibility with Yao's query system
|
||||||
Wheres []QueryWhere `json:"wheres,omitempty"` // Pre-defined filters (optional)
|
// See: github.com/yaoapp/gou/query/gou/types.go
|
||||||
Orders []QueryOrder `json:"orders,omitempty"` // Sort orders (optional)
|
Models []string `json:"models,omitempty"` // Model IDs (e.g., "user", "agents.mybot.product")
|
||||||
Select []string `json:"select,omitempty"` // Fields to return (optional)
|
Wheres []gou.Where `json:"wheres,omitempty"` // Pre-defined filters (optional), uses GOU QueryDSL Where
|
||||||
|
Orders gou.Orders `json:"orders,omitempty"` // Sort orders (optional), uses GOU QueryDSL Orders
|
||||||
|
Select []string `json:"select,omitempty"` // Fields to return (optional)
|
||||||
|
|
||||||
// Reranking
|
// Reranking
|
||||||
Rerank *RerankOptions `json:"rerank,omitempty"`
|
Rerank *RerankOptions `json:"rerank,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// QueryWhere represents a filter condition for DB search
|
|
||||||
type QueryWhere struct {
|
|
||||||
Field string `json:"field"` // Field name
|
|
||||||
Op string `json:"op,omitempty"` // Operator: "=", "like", ">", "<", "in", etc. (default: "=")
|
|
||||||
Value interface{} `json:"value"` // Filter value
|
|
||||||
}
|
|
||||||
|
|
||||||
// QueryOrder represents a sort order for DB search
|
|
||||||
type QueryOrder struct {
|
|
||||||
Field string `json:"field"` // Field name
|
|
||||||
Order string `json:"order,omitempty"` // "asc" or "desc" (default: "desc")
|
|
||||||
}
|
|
||||||
|
|
||||||
// RerankOptions controls result reranking
|
// RerankOptions controls result reranking
|
||||||
// Reranker type is determined by uses.rerank in agent/agent.yml
|
// Reranker type is determined by uses.rerank in agent/agent.yml
|
||||||
type RerankOptions struct {
|
type RerankOptions struct {
|
||||||
|
|
@ -589,38 +583,20 @@ type ResultItem struct {
|
||||||
|
|
||||||
// ProcessedQuery represents a processed query ready for execution
|
// ProcessedQuery represents a processed query ready for execution
|
||||||
type ProcessedQuery struct {
|
type ProcessedQuery struct {
|
||||||
Type SearchType `json:"type"`
|
Type SearchType `json:"type"`
|
||||||
Keywords []string `json:"keywords,omitempty"` // For web search
|
Keywords []string `json:"keywords,omitempty"` // For web search
|
||||||
Vector []float32 `json:"vector,omitempty"` // For KB search
|
Vector []float32 `json:"vector,omitempty"` // For KB search
|
||||||
DSL *QueryDSL `json:"dsl,omitempty"` // For DB search
|
DSL *gou.QueryDSL `json:"dsl,omitempty"` // For DB search, uses GOU QueryDSL
|
||||||
}
|
}
|
||||||
|
|
||||||
// QueryDSL represents a Yao QueryDSL for database search
|
// Note: For QueryDSL and Model types, use GOU types directly:
|
||||||
type QueryDSL struct {
|
// - github.com/yaoapp/gou/query/gou.QueryDSL
|
||||||
Model string `json:"model"` // Target model
|
// - github.com/yaoapp/gou/model.Model
|
||||||
Select []string `json:"select,omitempty"` // Fields to return
|
// - github.com/yaoapp/gou/model.Column
|
||||||
Wheres []QueryWhere `json:"wheres,omitempty"` // Filter conditions
|
|
||||||
Orders []QueryOrder `json:"orders,omitempty"` // Sort orders
|
|
||||||
Limit int `json:"limit,omitempty"` // Max results
|
|
||||||
}
|
|
||||||
|
|
||||||
// ModelSchema represents a Yao Model schema for DSL generation
|
|
||||||
type ModelSchema struct {
|
|
||||||
ID string `json:"id"` // Model ID
|
|
||||||
Name string `json:"name"` // Model name
|
|
||||||
Description string `json:"description"` // Model description
|
|
||||||
Fields []FieldSchema `json:"fields"` // Field definitions
|
|
||||||
}
|
|
||||||
|
|
||||||
// FieldSchema represents a field in the model schema
|
|
||||||
type FieldSchema struct {
|
|
||||||
Name string `json:"name"` // Field name
|
|
||||||
Type string `json:"type"` // Field type
|
|
||||||
Description string `json:"description"` // Field description
|
|
||||||
Searchable bool `json:"searchable"` // Whether field is searchable
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
> **Note**: `Wheres` and `Orders` use GOU QueryDSL types directly (`gou.Where` and `gou.Orders`) for full compatibility with Yao's query system. See `github.com/yaoapp/gou/query/gou/types.go` for the complete type definitions.
|
||||||
|
|
||||||
### Graph Types (`types/graph.go`)
|
### Graph Types (`types/graph.go`)
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|
@ -920,22 +896,33 @@ interface KBOptions {
|
||||||
|
|
||||||
interface DBOptions {
|
interface DBOptions {
|
||||||
models?: string[]; // Model IDs (default: use assistant's db.models)
|
models?: string[]; // Model IDs (default: use assistant's db.models)
|
||||||
wheres?: QueryWhere[]; // Pre-defined filters
|
wheres?: Where[]; // Pre-defined filters, uses GOU QueryDSL Where format
|
||||||
orders?: QueryOrder[]; // Sort orders
|
orders?: Order[]; // Sort orders, uses GOU QueryDSL Order format
|
||||||
select?: string[]; // Fields to return
|
select?: string[]; // Fields to return
|
||||||
limit?: number; // Max results (default: 10)
|
limit?: number; // Max results (default: 10)
|
||||||
rerank?: RerankOptions;
|
rerank?: RerankOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface QueryWhere {
|
// GOU QueryDSL Where condition
|
||||||
field: string;
|
// See: github.com/yaoapp/gou/query/gou/types.go
|
||||||
op?: string; // "=", "like", ">", "<", "in", etc.
|
interface Where {
|
||||||
value: any;
|
field: Expression; // Field expression
|
||||||
|
value?: any; // Match value
|
||||||
|
op: string; // Operator: "=", "like", ">", "<", ">=", "<=", "in", "is null", etc.
|
||||||
|
or?: boolean; // true for OR condition, default AND
|
||||||
|
wheres?: Where[]; // Nested conditions for grouping
|
||||||
}
|
}
|
||||||
|
|
||||||
interface QueryOrder {
|
// GOU QueryDSL Order
|
||||||
field: string;
|
interface Order {
|
||||||
order?: string; // "asc" or "desc"
|
field: Expression; // Field expression
|
||||||
|
sort?: string; // "asc" or "desc"
|
||||||
|
}
|
||||||
|
|
||||||
|
// GOU Expression (simplified)
|
||||||
|
interface Expression {
|
||||||
|
field?: string; // Field name
|
||||||
|
table?: string; // Table name (optional)
|
||||||
}
|
}
|
||||||
|
|
||||||
interface RerankOptions {
|
interface RerankOptions {
|
||||||
|
|
@ -1687,14 +1674,15 @@ func NewQueryDSLGenerator(usesQueryDSL string, cfg *types.QueryDSLConfig) *Query
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate converts natural language to QueryDSL
|
// Generate converts natural language to QueryDSL
|
||||||
func (g *QueryDSLGenerator) Generate(ctx *context.Context, query string, schemas []*types.ModelSchema) (*types.QueryDSL, error) {
|
// Uses GOU types directly: model.Model and gou.QueryDSL
|
||||||
|
func (g *QueryDSLGenerator) Generate(query string, models []*model.Model) (*gou.QueryDSL, error) {
|
||||||
switch {
|
switch {
|
||||||
case g.usesQueryDSL == "builtin" || g.usesQueryDSL == "":
|
case g.usesQueryDSL == "builtin" || g.usesQueryDSL == "":
|
||||||
return g.builtinGenerate(query, schemas)
|
return g.builtinGenerate(query, models)
|
||||||
case strings.HasPrefix(g.usesQueryDSL, "mcp:"):
|
case strings.HasPrefix(g.usesQueryDSL, "mcp:"):
|
||||||
return g.mcpGenerate(ctx, query, schemas)
|
return g.mcpGenerate(query, models)
|
||||||
default:
|
default:
|
||||||
return g.agentGenerate(ctx, query, schemas)
|
return g.agentGenerate(query, models)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
package interfaces
|
package interfaces
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/yaoapp/gou/model"
|
||||||
|
"github.com/yaoapp/gou/query/gou"
|
||||||
"github.com/yaoapp/yao/agent/search/types"
|
"github.com/yaoapp/yao/agent/search/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -13,7 +15,7 @@ type KeywordExtractor interface {
|
||||||
// QueryDSLGenerator generates QueryDSL for DB search
|
// QueryDSLGenerator generates QueryDSL for DB search
|
||||||
type QueryDSLGenerator interface {
|
type QueryDSLGenerator interface {
|
||||||
// Generate converts natural language to QueryDSL
|
// Generate converts natural language to QueryDSL
|
||||||
Generate(query string, schemas []*types.ModelSchema) (*types.QueryDSL, error)
|
Generate(query string, models []*model.Model) (*gou.QueryDSL, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note: Embedding is handled by KB collection's own config (embedding provider + model),
|
// Note: Embedding is handled by KB collection's own config (embedding provider + model),
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,13 @@
|
||||||
package types
|
package types
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/yaoapp/gou/query/gou"
|
||||||
|
)
|
||||||
|
|
||||||
// SearchType represents the type of search
|
// SearchType represents the type of search
|
||||||
type SearchType string
|
type SearchType string
|
||||||
|
|
||||||
|
// SearchType constants
|
||||||
const (
|
const (
|
||||||
SearchTypeWeb SearchType = "web" // Web/Internet search
|
SearchTypeWeb SearchType = "web" // Web/Internet search
|
||||||
SearchTypeKB SearchType = "kb" // Knowledge base vector search
|
SearchTypeKB SearchType = "kb" // Knowledge base vector search
|
||||||
|
|
@ -12,6 +17,7 @@ const (
|
||||||
// SourceType represents where the search result came from
|
// SourceType represents where the search result came from
|
||||||
type SourceType string
|
type SourceType string
|
||||||
|
|
||||||
|
// SourceType constants
|
||||||
const (
|
const (
|
||||||
SourceUser SourceType = "user" // User-provided DataContent (highest priority)
|
SourceUser SourceType = "user" // User-provided DataContent (highest priority)
|
||||||
SourceHook SourceType = "hook" // Hook ctx.search.*() results
|
SourceHook SourceType = "hook" // Hook ctx.search.*() results
|
||||||
|
|
@ -36,28 +42,15 @@ type Request struct {
|
||||||
Graph bool `json:"graph,omitempty"` // Enable graph association
|
Graph bool `json:"graph,omitempty"` // Enable graph association
|
||||||
|
|
||||||
// Database search specific
|
// Database search specific
|
||||||
Models []string `json:"models,omitempty"` // Model IDs (e.g., "user", "agents.mybot.product")
|
Models []string `json:"models,omitempty"` // Model IDs (e.g., "user", "agents.mybot.product")
|
||||||
Wheres []QueryWhere `json:"wheres,omitempty"` // Pre-defined filters (optional)
|
Wheres []gou.Where `json:"wheres,omitempty"` // Pre-defined filters (optional), uses GOU QueryDSL Where
|
||||||
Orders []QueryOrder `json:"orders,omitempty"` // Sort orders (optional)
|
Orders gou.Orders `json:"orders,omitempty"` // Sort orders (optional), uses GOU QueryDSL Orders
|
||||||
Select []string `json:"select,omitempty"` // Fields to return (optional)
|
Select []string `json:"select,omitempty"` // Fields to return (optional)
|
||||||
|
|
||||||
// Reranking
|
// Reranking
|
||||||
Rerank *RerankOptions `json:"rerank,omitempty"`
|
Rerank *RerankOptions `json:"rerank,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// QueryWhere represents a filter condition for DB search
|
|
||||||
type QueryWhere struct {
|
|
||||||
Field string `json:"field"` // Field name
|
|
||||||
Op string `json:"op,omitempty"` // Operator: "=", "like", ">", "<", "in", etc. (default: "=")
|
|
||||||
Value interface{} `json:"value"` // Filter value
|
|
||||||
}
|
|
||||||
|
|
||||||
// QueryOrder represents a sort order for DB search
|
|
||||||
type QueryOrder struct {
|
|
||||||
Field string `json:"field"` // Field name
|
|
||||||
Order string `json:"order,omitempty"` // "asc" or "desc" (default: "desc")
|
|
||||||
}
|
|
||||||
|
|
||||||
// RerankOptions controls result reranking
|
// RerankOptions controls result reranking
|
||||||
// Reranker type is determined by uses.rerank in agent/agent.yml
|
// Reranker type is determined by uses.rerank in agent/agent.yml
|
||||||
type RerankOptions struct {
|
type RerankOptions struct {
|
||||||
|
|
@ -109,33 +102,13 @@ type ResultItem struct {
|
||||||
|
|
||||||
// ProcessedQuery represents a processed query ready for execution
|
// ProcessedQuery represents a processed query ready for execution
|
||||||
type ProcessedQuery struct {
|
type ProcessedQuery struct {
|
||||||
Type SearchType `json:"type"`
|
Type SearchType `json:"type"`
|
||||||
Keywords []string `json:"keywords,omitempty"` // For web search
|
Keywords []string `json:"keywords,omitempty"` // For web search
|
||||||
Vector []float32 `json:"vector,omitempty"` // For KB search
|
Vector []float32 `json:"vector,omitempty"` // For KB search
|
||||||
DSL *QueryDSL `json:"dsl,omitempty"` // For DB search
|
DSL *gou.QueryDSL `json:"dsl,omitempty"` // For DB search, uses GOU QueryDSL
|
||||||
}
|
}
|
||||||
|
|
||||||
// QueryDSL represents a Yao QueryDSL for database search
|
// Note: For QueryDSL and Model types, use GOU types directly:
|
||||||
type QueryDSL struct {
|
// - github.com/yaoapp/gou/query/gou.QueryDSL
|
||||||
Model string `json:"model"` // Target model
|
// - github.com/yaoapp/gou/model.Model
|
||||||
Select []string `json:"select,omitempty"` // Fields to return
|
// - github.com/yaoapp/gou/model.Column
|
||||||
Wheres []QueryWhere `json:"wheres,omitempty"` // Filter conditions
|
|
||||||
Orders []QueryOrder `json:"orders,omitempty"` // Sort orders
|
|
||||||
Limit int `json:"limit,omitempty"` // Max results
|
|
||||||
}
|
|
||||||
|
|
||||||
// ModelSchema represents a Yao Model schema for DSL generation
|
|
||||||
type ModelSchema struct {
|
|
||||||
ID string `json:"id"` // Model ID
|
|
||||||
Name string `json:"name"` // Model name
|
|
||||||
Description string `json:"description"` // Model description
|
|
||||||
Fields []FieldSchema `json:"fields"` // Field definitions
|
|
||||||
}
|
|
||||||
|
|
||||||
// FieldSchema represents a field in the model schema
|
|
||||||
type FieldSchema struct {
|
|
||||||
Name string `json:"name"` // Field name
|
|
||||||
Type string `json:"type"` // Field type
|
|
||||||
Description string `json:"description"` // Field description
|
|
||||||
Searchable bool `json:"searchable"` // Whether field is searchable
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue