Struct index and searching
- Simplicity
go get github.com/brianvoe/gofindit
import "github.com/brianvoe/gofindit"
type Test struct {
Name string `find:"name"` // Tag with find
Age int // or field name Age is used
}
// Create a new index
index := gofindit.New()
// Create a new document
doc := Test{
Name: "Test",
Age: 10,
}
// Index the document
id := "1"
err := index.Index(id, doc)
if err != nil {
fmt.Println(err)
return
}
// Get the document
docGet, err := index.Get(id)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%+v", docGet)
// Output: {Name:Test Age:10}
// Create a new index
index := gofindit.New()
// Add all your documents
for i := 0; i < 1000; i++ {
// Add to index
// ... Code here
}
// Create a search query
search := SearchQuery{
Limit: 10, // default 10
Skip: 0, // default 0
Sort: "", // "", asc or desc
SortBy: "", // field name
// Search fields
Fields: []SearchQueryField{
{
Field: "name", // find tag
Type: "partial", // match, partial or range
Value: "billy", // Case insensitive
},
},
}
// Search for the document
results, err := index.Search(search)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%+v", results)
// Output: [{Name:Billy Age:10}]