46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
package search
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/luxsin/app-api/internal/config"
|
|
"github.com/meilisearch/meilisearch-go"
|
|
)
|
|
|
|
var modelListAttributes = []string{"rig", "form", "name", "brand_name", "source", "eq_key"}
|
|
|
|
type Client struct {
|
|
index meilisearch.IndexManager
|
|
}
|
|
|
|
func NewClient(cfg config.MeilisearchConfig) *Client {
|
|
ms := meilisearch.New(cfg.Host, meilisearch.WithAPIKey(cfg.APIKey))
|
|
return &Client{index: ms.Index(cfg.Index)}
|
|
}
|
|
|
|
func (c *Client) ModelList(ctx context.Context, key string, count int) ([]map[string]any, error) {
|
|
resp, err := c.index.SearchWithContext(ctx, key, &meilisearch.SearchRequest{
|
|
Limit: int64(count),
|
|
AttributesToRetrieve: modelListAttributes,
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("meilisearch search: %w", err)
|
|
}
|
|
|
|
if len(resp.Hits) == 0 {
|
|
return []map[string]any{}, nil
|
|
}
|
|
|
|
list := make([]map[string]any, 0, len(resp.Hits))
|
|
for _, hit := range resp.Hits {
|
|
item := make(map[string]any)
|
|
if err := hit.DecodeInto(&item); err != nil {
|
|
return nil, fmt.Errorf("decode meilisearch hit: %w", err)
|
|
}
|
|
list = append(list, item)
|
|
}
|
|
|
|
return list, nil
|
|
}
|