feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
54
backend/infra/contract/document/searchstore/dsl.go
Normal file
54
backend/infra/contract/document/searchstore/dsl.go
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* 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 searchstore
|
||||
|
||||
import "fmt"
|
||||
|
||||
type DSL struct {
|
||||
Op Op
|
||||
Field string
|
||||
Value interface{} // builtin types / []*DSL
|
||||
}
|
||||
|
||||
type Op string
|
||||
|
||||
const (
|
||||
OpEq Op = "eq"
|
||||
OpNe Op = "ne"
|
||||
OpLike Op = "like"
|
||||
OpIn Op = "in"
|
||||
|
||||
OpAnd Op = "and"
|
||||
OpOr Op = "or"
|
||||
)
|
||||
|
||||
func (d *DSL) DSL() map[string]any {
|
||||
return map[string]any{"dsl": d}
|
||||
}
|
||||
|
||||
func LoadDSL(src map[string]any) (*DSL, error) {
|
||||
if src == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
dsl, ok := src["dsl"].(*DSL)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("load dsl failed")
|
||||
}
|
||||
|
||||
return dsl, nil
|
||||
}
|
||||
82
backend/infra/contract/document/searchstore/manager.go
Normal file
82
backend/infra/contract/document/searchstore/manager.go
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* 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 searchstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type Manager interface {
|
||||
Create(ctx context.Context, req *CreateRequest) error
|
||||
|
||||
Drop(ctx context.Context, req *DropRequest) error
|
||||
|
||||
GetType() SearchStoreType
|
||||
|
||||
GetSearchStore(ctx context.Context, collectionName string) (SearchStore, error)
|
||||
}
|
||||
|
||||
type CreateRequest struct {
|
||||
CollectionName string
|
||||
Fields []*Field
|
||||
CollectionMeta map[string]string
|
||||
}
|
||||
|
||||
type DropRequest struct {
|
||||
CollectionName string
|
||||
}
|
||||
|
||||
type GetSearchStoreRequest struct {
|
||||
CollectionName string
|
||||
}
|
||||
|
||||
type Field struct {
|
||||
Name FieldName
|
||||
Type FieldType
|
||||
Description string
|
||||
|
||||
Nullable bool
|
||||
IsPrimary bool
|
||||
|
||||
Indexing bool
|
||||
}
|
||||
|
||||
type SearchStoreType string
|
||||
|
||||
const (
|
||||
TypeVectorStore SearchStoreType = "vector"
|
||||
TypeTextStore SearchStoreType = "text"
|
||||
)
|
||||
|
||||
type FieldName = string
|
||||
|
||||
// 内置 field name
|
||||
const (
|
||||
FieldID FieldName = "id" // int64
|
||||
FieldCreatorID FieldName = "creator_id" // int64
|
||||
FieldTextContent FieldName = "text_content" // string
|
||||
)
|
||||
|
||||
type FieldType int64
|
||||
|
||||
const (
|
||||
FieldTypeUnknown FieldType = 0
|
||||
FieldTypeInt64 FieldType = 1
|
||||
FieldTypeText FieldType = 2
|
||||
FieldTypeDenseVector FieldType = 3
|
||||
FieldTypeSparseVector FieldType = 4
|
||||
)
|
||||
87
backend/infra/contract/document/searchstore/options.go
Normal file
87
backend/infra/contract/document/searchstore/options.go
Normal file
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* 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 searchstore
|
||||
|
||||
import (
|
||||
"github.com/cloudwego/eino/components/indexer"
|
||||
"github.com/cloudwego/eino/components/retriever"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/infra/contract/document/progressbar"
|
||||
)
|
||||
|
||||
type IndexerOptions struct {
|
||||
PartitionKey *string
|
||||
Partition *string // 存储分片映射
|
||||
IndexingFields []string
|
||||
ProgressBar progressbar.ProgressBar
|
||||
}
|
||||
|
||||
type RetrieverOptions struct {
|
||||
MultiMatch *MultiMatch // 多 field 查询
|
||||
PartitionKey *string
|
||||
Partitions []string // 查询分片映射
|
||||
}
|
||||
|
||||
type MultiMatch struct {
|
||||
Fields []string
|
||||
Query string
|
||||
}
|
||||
|
||||
func WithIndexerPartitionKey(key string) indexer.Option {
|
||||
return indexer.WrapImplSpecificOptFn(func(o *IndexerOptions) {
|
||||
o.PartitionKey = &key
|
||||
})
|
||||
}
|
||||
|
||||
func WithPartition(partition string) indexer.Option {
|
||||
return indexer.WrapImplSpecificOptFn(func(o *IndexerOptions) {
|
||||
o.Partition = &partition
|
||||
})
|
||||
}
|
||||
|
||||
func WithIndexingFields(fields []string) indexer.Option {
|
||||
return indexer.WrapImplSpecificOptFn(func(o *IndexerOptions) {
|
||||
o.IndexingFields = fields
|
||||
})
|
||||
}
|
||||
|
||||
func WithProgressBar(progressBar progressbar.ProgressBar) indexer.Option {
|
||||
return indexer.WrapImplSpecificOptFn(func(o *IndexerOptions) {
|
||||
o.ProgressBar = progressBar
|
||||
})
|
||||
}
|
||||
|
||||
func WithMultiMatch(fields []string, query string) retriever.Option {
|
||||
return retriever.WrapImplSpecificOptFn(func(o *RetrieverOptions) {
|
||||
o.MultiMatch = &MultiMatch{
|
||||
Fields: fields,
|
||||
Query: query,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func WithRetrieverPartitionKey(key string) retriever.Option {
|
||||
return retriever.WrapImplSpecificOptFn(func(o *RetrieverOptions) {
|
||||
o.PartitionKey = &key
|
||||
})
|
||||
}
|
||||
|
||||
func WithPartitions(partitions []string) retriever.Option {
|
||||
return retriever.WrapImplSpecificOptFn(func(o *RetrieverOptions) {
|
||||
o.Partitions = partitions
|
||||
})
|
||||
}
|
||||
32
backend/infra/contract/document/searchstore/searchstore.go
Normal file
32
backend/infra/contract/document/searchstore/searchstore.go
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* 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 searchstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/cloudwego/eino/components/indexer"
|
||||
"github.com/cloudwego/eino/components/retriever"
|
||||
)
|
||||
|
||||
type SearchStore interface {
|
||||
indexer.Indexer
|
||||
|
||||
retriever.Retriever
|
||||
|
||||
Delete(ctx context.Context, ids []string) error
|
||||
}
|
||||
Reference in New Issue
Block a user