Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix filter docs when has no record by filter & optimize update and create document api #54

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading