56 lines
1.1 KiB
Go
56 lines
1.1 KiB
Go
package meilisearch
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
|
|
"git.selly.red/Selly-Modules/logger"
|
|
|
|
ms "github.com/meilisearch/meilisearch-go"
|
|
)
|
|
|
|
func (i *Index) Search(payload SearchPayload, response interface{}) (int64, error) {
|
|
result, err := getIndex(i.Name).Search(payload.Query, &ms.SearchRequest{
|
|
Offset: payload.Pagination.Offset,
|
|
Limit: payload.Pagination.Limit,
|
|
Filter: payload.Filter,
|
|
Sort: payload.Sort,
|
|
})
|
|
|
|
if err != nil {
|
|
logger.Error("search", logger.LogData{
|
|
Source: "meilisearch.Search",
|
|
Message: err.Error(),
|
|
Data: logger.Map{
|
|
"index": i.Name,
|
|
"payload": payload,
|
|
},
|
|
})
|
|
|
|
return 0, errors.New("cannot search data")
|
|
}
|
|
|
|
// return if no result
|
|
if len(result.Hits) == 0 {
|
|
return 0, nil
|
|
}
|
|
|
|
// map data to response variable
|
|
b, _ := json.Marshal(result.Hits)
|
|
if err = json.Unmarshal(b, &response); err != nil {
|
|
logger.Error("unmarshal result", logger.LogData{
|
|
Source: "meilisearch.Search",
|
|
Message: err.Error(),
|
|
Data: logger.Map{
|
|
"index": i.Name,
|
|
"payload": payload,
|
|
"hits": result.Hits,
|
|
},
|
|
})
|
|
return 0, err
|
|
}
|
|
|
|
// return
|
|
return result.EstimatedTotalHits, nil
|
|
}
|