refactor: SearchEngine interface, remove nilable qdrantStore
This commit is contained in:
1 parent
98aa7dd68c
commit
ffdc023065
5 files changed
+134
-118
No files matched your search
@@ -6,36 +6,20 @@ import (
|
||||
"strconv"
|
||||
|
||||
"github.com/qdrant/go-client/qdrant"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
const collectionName = "flashcard"
|
||||
|
||||
type entry struct {
|
||||
ID int64 `json:"_id"`
|
||||
PublicID int64 `json:"public_id"`
|
||||
Version int `json:"version"`
|
||||
Note string `json:"note"`
|
||||
Repos []string `json:"repos,omitempty"`
|
||||
Host string `json:"host,omitempty"`
|
||||
Tags []string `json:"tags,omitempty"`
|
||||
User string `json:"user,omitempty"`
|
||||
Context string `json:"context,omitempty"`
|
||||
DeleteTimestamp string `json:"delete_timestamp,omitempty"`
|
||||
Created string `json:"created"`
|
||||
}
|
||||
|
||||
type searchResult struct {
|
||||
Entry entry `json:",inline"`
|
||||
Score float32 `json:"score"`
|
||||
}
|
||||
|
||||
type qdrantStore struct {
|
||||
client *qdrant.Client
|
||||
embedder *embedder
|
||||
sqlite *sqliteStore
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
func newQdrantStore(client *qdrant.Client, embedder *embedder) *qdrantStore {
|
||||
return &qdrantStore{client: client, embedder: embedder}
|
||||
func newQdrantStore(client *qdrant.Client, embedder *embedder, sqlite *sqliteStore, logger *zap.Logger) *qdrantStore {
|
||||
return &qdrantStore{client: client, embedder: embedder, sqlite: sqlite, logger: logger}
|
||||
}
|
||||
|
||||
func (s *qdrantStore) ensureCollection(ctx context.Context) error {
|
||||
@@ -61,7 +45,7 @@ func (s *qdrantStore) ensureCollection(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *qdrantStore) index(ctx context.Context, id, publicID int64, version int, note string) error {
|
||||
func (s *qdrantStore) Index(ctx context.Context, id, publicID int64, version int, note string) error {
|
||||
vec, err := s.embedder.embedText(ctx, note)
|
||||
if err != nil {
|
||||
return fmt.Errorf("embed text: %w", err)
|
||||
@@ -85,13 +69,12 @@ func (s *qdrantStore) index(ctx context.Context, id, publicID int64, version int
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("upsert point: %w", err)
|
||||
s.logger.Warn("qdrant index failed (entry in sqlite)", zap.Int64("id", id), zap.Error(err))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *qdrantStore) delete(ctx context.Context, publicID int64) error {
|
||||
func (s *qdrantStore) Delete(ctx context.Context, id, publicID int64) error {
|
||||
wait := true
|
||||
_, err := s.client.Delete(ctx, &qdrant.DeletePoints{
|
||||
CollectionName: collectionName,
|
||||
@@ -101,12 +84,12 @@ func (s *qdrantStore) delete(ctx context.Context, publicID int64) error {
|
||||
),
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("delete point: %w", err)
|
||||
s.logger.Warn("qdrant delete failed (tombstone in sqlite)", zap.Int64("public_id", publicID), zap.Error(err))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *qdrantStore) search(ctx context.Context, query string, limit uint64) ([]searchResult, error) {
|
||||
func (s *qdrantStore) Search(ctx context.Context, query string, limit uint64) ([]searchResult, error) {
|
||||
vec, err := s.embedder.embedText(ctx, query)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("embed query: %w", err)
|
||||
@@ -140,6 +123,10 @@ func (s *qdrantStore) search(ctx context.Context, query string, limit uint64) ([
|
||||
}
|
||||
}
|
||||
|
||||
if cached, err := s.sqlite.getByID(ctx, r.Entry.ID); err == nil && cached != nil {
|
||||
r.Entry = *cached
|
||||
}
|
||||
|
||||
searchResults = append(searchResults, r)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user