Skip to content

Commit

Permalink
fix filter docs when has no record by filter & optimize update and cr…
Browse files Browse the repository at this point in the history
…eate document api

Signed-off-by: zwwhdls <zww@hdls.me>
  • Loading branch information
zwwhdls committed Dec 11, 2024
1 parent e120da1 commit 2e30471
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
1 change: 1 addition & 0 deletions pkg/models/doc/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type DocPtrInterface interface {
ID() string
EntryID() string
Type() string
String() string
}

type Document struct {
Expand Down
3 changes: 3 additions & 0 deletions pkg/service/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
44 changes: 27 additions & 17 deletions pkg/store/docstore/meili.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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())
Expand All @@ -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 {
Expand Down

0 comments on commit 2e30471

Please sign in to comment.