Go to file
namhq1989 b8d9d6d5a6 add method UpdateDocuments 2022-11-15 09:57:02 +07:00
.gitignore init 2022-10-27 10:03:35 +07:00
README.md move search function to struct 2022-11-14 10:11:11 +07:00
config.go init 2022-10-27 10:03:35 +07:00
document.go add method UpdateDocuments 2022-11-15 09:57:02 +07:00
go.mod upgrade to go 1.19 2022-11-14 10:25:57 +07:00
go.sum upgrade to go 1.19 2022-11-14 10:25:57 +07:00
index.go fix typo 2022-10-29 15:33:07 +07:00
meilisearch.go init 2022-10-27 10:03:35 +07:00
search.go move search function to struct 2022-11-14 10:12:55 +07:00

README.md

meilisearch

package main

import (
	"fmt"
	"git.selly.red/Selly-Modules/logger"
	"git.selly.red/Selly-Modules/meilisearch"
)

func main() {
	logger.Init("demo", "hello")

	if err := meilisearch.Connect(meilisearch.Config{
		URL:    "http://localhost:7700",
		APIKey: "meili_key",
	}); err != nil {
		panic(err)
	}

	demo()
}

func demo() {
	type Player struct {
		ID     string `json:"_id"`
		Name   string `json:"name"`
		Age    int    `json:"age"`
		Number int    `json:"number"`
	}

	var index = meilisearch.Index{
		Name: "player",
	}

	if err := index.Create(); err != nil {
		fmt.Println("error when create index")
		panic(err)
	}

	if err := index.UpdateSettings(meilisearch.IndexSettings{
		SearchableAttributes: []string{"name"},
		FilterableAttributes: []string{"age"},
		SortableAttributes:   []string{"number"},
	}); err != nil {
		fmt.Println("error when update index setting")
		panic(err)
	}

	if err := index.AddDocuments([]Player{
		{ID: "1", Name: "Messi", Age: 34, Number: 30},
		{ID: "2", Name: "Ronaldo", Age: 37, Number: 7},
		{ID: "3", Name: "Kevin De Bruyne", Age: 30, Number: 17},
		{ID: "4", Name: "Ronaldo De Lima", Age: 46, Number: 10},
	}); err != nil {
		fmt.Println("error when insert documents")
		panic(err)
	}

	var data = make([]Player, 0)
	total, err := index.Search(meilisearch.SearchPay  load{
		Query: "ronaldo",
		Filter: [][]string{
			[]string{"age > 40"},
		},
		Pagination: meilisearch.SearchPagination{
			Offset: 0,
			Limit:  5,
		},
		Sort: nil,
	}, &data)

	fmt.Println("total", total)
	fmt.Println("err", err)
	fmt.Println("data", data)

	//if err := index.DeleteDocumentsByPrimaryKeys([]string{"2", "3"}); err != nil {
	//	fmt.Println("error when delete documents")
	//	panic(err)
	//}
}