move search function to struct

This commit is contained in:
namhq1989 2022-11-14 10:11:11 +07:00
parent a1c8dad070
commit e0e23e8a29
2 changed files with 5 additions and 6 deletions

View File

@ -59,7 +59,7 @@ func demo() {
} }
var data = make([]Player, 0) var data = make([]Player, 0)
total, err := meilisearch.Search("player", meilisearch.SearchPayload{ total, err := index.Search(meilisearch.SearchPay load{
Query: "ronaldo", Query: "ronaldo",
Filter: [][]string{ Filter: [][]string{
[]string{"age > 40"}, []string{"age > 40"},

View File

@ -9,9 +9,8 @@ import (
ms "github.com/meilisearch/meilisearch-go" ms "github.com/meilisearch/meilisearch-go"
) )
// Search return result from database func (i Index) Search(payload SearchPayload, response interface{}) (int64, error) {
func Search(index string, payload SearchPayload, response interface{}) (int64, error) { result, err := getIndex(i.Name).Search(payload.Query, &ms.SearchRequest{
result, err := client.Index(index).Search(payload.Query, &ms.SearchRequest{
Offset: payload.Pagination.Offset, Offset: payload.Pagination.Offset,
Limit: payload.Pagination.Limit, Limit: payload.Pagination.Limit,
Filter: payload.Filter, Filter: payload.Filter,
@ -23,7 +22,7 @@ func Search(index string, payload SearchPayload, response interface{}) (int64, e
Source: "meilisearch.Search", Source: "meilisearch.Search",
Message: err.Error(), Message: err.Error(),
Data: logger.Map{ Data: logger.Map{
"index": index, "index": i.Name,
"payload": payload, "payload": payload,
}, },
}) })
@ -43,7 +42,7 @@ func Search(index string, payload SearchPayload, response interface{}) (int64, e
Source: "meilisearch.Search", Source: "meilisearch.Search",
Message: err.Error(), Message: err.Error(),
Data: logger.Map{ Data: logger.Map{
"index": index, "index": i.Name,
"payload": payload, "payload": payload,
"hits": result.Hits, "hits": result.Hits,
}, },