From 2e3047127ee8f1207aff20ce5b9ff34974f6d282 Mon Sep 17 00:00:00 2001 From: zwwhdls Date: Wed, 11 Dec 2024 21:00:59 +0800 Subject: [PATCH] fix filter docs when has no record by filter & optimize update and create document api Signed-off-by: zwwhdls --- pkg/models/doc/document.go | 1 + pkg/service/chain.go | 3 +++ pkg/store/docstore/meili.go | 44 +++++++++++++++++++++++-------------- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/pkg/models/doc/document.go b/pkg/models/doc/document.go index 3104762..d487976 100644 --- a/pkg/models/doc/document.go +++ b/pkg/models/doc/document.go @@ -30,6 +30,7 @@ type DocPtrInterface interface { ID() string EntryID() string Type() string + String() string } type Document struct { diff --git a/pkg/service/chain.go b/pkg/service/chain.go index ffc4a00..71bad69 100644 --- a/pkg/service/chain.go +++ b/pkg/service/chain.go @@ -228,6 +228,9 @@ func (c *Chain) Search(ctx context.Context, query *doc.DocumentQuery, attrQuerie for _, attr := range attrs { ids = append(ids, attr.EntryId) } + if len(ids) == 0 && len(attrQueries) != 0 { + return nil, nil + } query.AttrQueries = append(query.AttrQueries, &doc.AttrQuery{ Attr: "kind", diff --git a/pkg/store/docstore/meili.go b/pkg/store/docstore/meili.go index f438d7d..c955bd8 100644 --- a/pkg/store/docstore/meili.go +++ b/pkg/store/docstore/meili.go @@ -73,19 +73,22 @@ func NewMeiliClient(conf config.Config) (DocStoreInterface, error) { } func (c *MeiliClient) Store(ctx context.Context, docPtr doc.DocPtrInterface) error { + c.log.Debugf("store entryId %s %s: %s", docPtr.EntryID(), docPtr.Type(), docPtr.String()) task, err := c.index.AddDocuments(docPtr, "id") if err != nil { c.log.Error(err) return err } - if err := c.wait(ctx, task.TaskUID); err != nil { - c.log.Errorf("store document with entryId %s error: %s", docPtr.EntryID(), err) - return err - } + go func() { + if err := c.wait(ctx, task.TaskUID); err != nil { + c.log.Errorf("store document with entryId %s error: %s", docPtr.EntryID(), err) + } + }() return nil } func (c *MeiliClient) FilterAttr(ctx context.Context, query *doc.DocumentAttrQuery) (doc.DocumentAttrList, error) { + c.log.Debugf("query document attr : [%s]", query.String()) rep, err := c.index.Search("", query.ToRequest()) if err != nil { return nil, err @@ -105,6 +108,7 @@ func (c *MeiliClient) FilterAttr(ctx context.Context, query *doc.DocumentAttrQue } func (c *MeiliClient) Search(ctx context.Context, query *doc.DocumentQuery) (doc.DocumentList, error) { + c.log.Debugf("search document: [%s] query: [%s]", query.Search, query.String()) rep, err := c.index.Search(query.Search, query.ToRequest()) if err != nil { return nil, err @@ -124,32 +128,37 @@ func (c *MeiliClient) Search(ctx context.Context, query *doc.DocumentQuery) (doc } func (c *MeiliClient) Update(ctx context.Context, document *doc.Document) error { + c.log.Debugf("update document: %s", document.ID()) t, err := c.index.UpdateDocuments(document) if err != nil { c.log.Error(err) return err } - if err := c.wait(ctx, t.TaskUID); err != nil { - c.log.Errorf("update document %s error: %s", document.ID, err) - return err - } + go func() { + if err := c.wait(ctx, t.TaskUID); err != nil { + c.log.Errorf("update document %s error: %s", document.ID, err) + } + }() return nil } func (c *MeiliClient) Delete(ctx context.Context, docId string) error { + c.log.Debugf("delete document: %s", docId) t, err := c.index.DeleteDocument(docId) if err != nil { c.log.Error(err) return err } - if err := c.wait(ctx, t.TaskUID); err != nil { - c.log.Errorf("delete document %s error: %s", docId, err) - return err - } + go func() { + if err := c.wait(ctx, t.TaskUID); err != nil { + c.log.Errorf("delete document %s error: %s", docId, err) + } + }() return nil } func (c *MeiliClient) DeleteByFilter(ctx context.Context, aqs doc.DocumentAttrQuery) error { + c.log.Debugf("delete by filter: %+v", aqs.String()) filter := []interface{}{} for _, aq := range aqs.AttrQueries { filter = append(filter, aq.ToFilter()) @@ -160,15 +169,16 @@ func (c *MeiliClient) DeleteByFilter(ctx context.Context, aqs doc.DocumentAttrQu c.log.Error(err) return err } - if err := c.wait(ctx, t.TaskUID); err != nil { - c.log.Errorf("delete document by filter error: %s", err) - return err - } + go func() { + if err := c.wait(ctx, t.TaskUID); err != nil { + c.log.Errorf("delete document by filter error: %s", err) + } + }() return nil } func (c *MeiliClient) wait(ctx context.Context, taskUID int64) error { - t := time.NewTicker(1 * time.Second) + t := time.NewTicker(100 * time.Millisecond) defer t.Stop() for { select {