meilisearch/index.go

55 lines
956 B
Go
Raw Normal View History

2022-10-27 03:03:35 +00:00
package meilisearch
import (
"errors"
ms "github.com/meilisearch/meilisearch-go"
)
const defaultPrimaryKey = "_id"
type Index struct {
// Index name
Name string
// Default key: _id
PrimaryKey string
}
2022-10-29 08:33:07 +00:00
func (i *Index) validate() error {
2022-10-27 03:03:35 +00:00
if i.Name == "" {
return errors.New("missing index name")
}
// set default key
if i.PrimaryKey == "" {
i.PrimaryKey = defaultPrimaryKey
}
return nil
}
// Create new index
2022-10-29 08:33:07 +00:00
func (i *Index) Create() error {
2022-10-27 03:03:35 +00:00
if err := i.validate(); err != nil {
return err
}
_, err := client.CreateIndex(&ms.IndexConfig{
Uid: i.Name,
PrimaryKey: i.PrimaryKey,
})
return err
}
// UpdateSettings ...
2022-10-29 08:33:07 +00:00
func (i *Index) UpdateSettings(setting IndexSettings) error {
2022-10-27 03:03:35 +00:00
_, err := getIndex(i.Name).UpdateSettings(&ms.Settings{
SearchableAttributes: setting.SearchableAttributes,
FilterableAttributes: setting.FilterableAttributes,
SortableAttributes: setting.SortableAttributes,
})
return err
}