55 lines
956 B
Go
55 lines
956 B
Go
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
|
|
}
|
|
|
|
func (i *Index) validate() error {
|
|
if i.Name == "" {
|
|
return errors.New("missing index name")
|
|
}
|
|
|
|
// set default key
|
|
if i.PrimaryKey == "" {
|
|
i.PrimaryKey = defaultPrimaryKey
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Create new index
|
|
func (i *Index) Create() error {
|
|
if err := i.validate(); err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err := client.CreateIndex(&ms.IndexConfig{
|
|
Uid: i.Name,
|
|
PrimaryKey: i.PrimaryKey,
|
|
})
|
|
|
|
return err
|
|
}
|
|
|
|
// UpdateSettings ...
|
|
func (i *Index) UpdateSettings(setting IndexSettings) error {
|
|
_, err := getIndex(i.Name).UpdateSettings(&ms.Settings{
|
|
SearchableAttributes: setting.SearchableAttributes,
|
|
FilterableAttributes: setting.FilterableAttributes,
|
|
SortableAttributes: setting.SortableAttributes,
|
|
})
|
|
return err
|
|
}
|