feat: manually mirror opencoze's code from bytedance

Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
fanlv
2025-07-20 17:36:12 +08:00
commit 890153324f
14811 changed files with 1923430 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
/*
* 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 es
import (
"context"
)
type Client interface {
Create(ctx context.Context, index, id string, document any) error
Update(ctx context.Context, index, id string, document any) error
Delete(ctx context.Context, index, id string) error
Search(ctx context.Context, index string, req *Request) (*Response, error)
Exists(ctx context.Context, index string) (bool, error)
CreateIndex(ctx context.Context, index string, properties map[string]any) error
DeleteIndex(ctx context.Context, index string) error
Types() Types
NewBulkIndexer(index string) (BulkIndexer, error)
}
type Types interface {
NewLongNumberProperty() any
NewTextProperty() any
NewUnsignedLongNumberProperty() any
}
type BulkIndexer interface {
Add(ctx context.Context, item BulkIndexerItem) error
Close(ctx context.Context) error
}

View File

@@ -0,0 +1,73 @@
/*
* 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 es
import (
"encoding/json"
"io"
"github.com/elastic/go-elasticsearch/v8/typedapi/types/enums/totalhitsrelation"
)
type BulkIndexerItem struct {
Index string
Action string
DocumentID string
Routing string
Version *int64
VersionType string
Body io.ReadSeeker
RetryOnConflict *int
}
type Request struct {
Size *int
Query *Query
MinScore *float64
Sort []SortFiled
SearchAfter []any
From *int
}
type SortFiled struct {
Field string
Asc bool
}
type Response struct {
Hits HitsMetadata `json:"hits"`
MaxScore *float64 `json:"max_score,omitempty"`
}
type HitsMetadata struct {
Hits []Hit `json:"hits"`
MaxScore *float64 `json:"max_score,omitempty"`
// Total Total hit count information, present only if `track_total_hits` wasn't
// `false` in the search request.
Total *TotalHits `json:"total,omitempty"`
}
type Hit struct {
Id_ *string `json:"_id,omitempty"`
Score_ *float64 `json:"_score,omitempty"`
Source_ json.RawMessage `json:"_source,omitempty"`
}
type TotalHits struct {
Relation totalhitsrelation.TotalHitsRelation `json:"relation"`
Value int64 `json:"value"`
}

View File

@@ -0,0 +1,111 @@
/*
* 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 es
const (
QueryTypeEqual = "equal"
QueryTypeMatch = "match"
QueryTypeMultiMatch = "multi_match"
QueryTypeNotExists = "not_exists"
QueryTypeContains = "contains"
QueryTypeIn = "in"
)
type KV struct {
Key string
Value any
}
type QueryType string
type Query struct {
KV KV
Type QueryType
MultiMatchQuery MultiMatchQuery
Bool *BoolQuery
}
type BoolQuery struct {
Filter []Query
Must []Query
MustNot []Query
Should []Query
MinimumShouldMatch *int
}
type MultiMatchQuery struct {
Fields []string
Type string // best_fields
Query string
Operator string
}
const (
Or = "or"
And = "and"
)
func NewEqualQuery(k string, v any) Query {
return Query{
KV: KV{Key: k, Value: v},
Type: QueryTypeEqual,
}
}
func NewMatchQuery(k string, v any) Query {
return Query{
KV: KV{Key: k, Value: v},
Type: QueryTypeMatch,
}
}
func NewMultiMatchQuery(fields []string, query, typeStr, operator string) Query {
return Query{
Type: QueryTypeMultiMatch,
MultiMatchQuery: MultiMatchQuery{
Fields: fields,
Query: query,
Operator: operator,
Type: typeStr,
},
}
}
func NewNotExistsQuery(k string) Query {
return Query{
KV: KV{Key: k},
Type: QueryTypeNotExists,
}
}
func NewContainsQuery(k string, v any) Query {
return Query{
KV: KV{Key: k, Value: v},
Type: QueryTypeContains,
}
}
func NewInQuery[T any](k string, v []T) Query {
arr := make([]any, 0, len(v))
for _, item := range v {
arr = append(arr, item)
}
return Query{
KV: KV{Key: k, Value: arr},
Type: QueryTypeIn,
}
}