gofindit

package module
v0.0.0-...-d5193cb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 12, 2024 License: MIT Imports: 13 Imported by: 0

README

Gofindit

Gofindit Go Report Card Test GoDoc license

Struct index and searching

ko-fi

Buy Me A Coffee

Features

  • Simplicity

Installation

go get github.com/brianvoe/gofindit

Simple Usage

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}

Search Usage

// 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}]

Documentation

Overview

Example
type Test struct {
	Name string `find:"Name" index:"leve_string"`
	Age  int    `find:"Age"`
}

// Create a new index
index := 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}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Tokenize

func Tokenize(str string, filters []FilterFunc) ([]string, error)

Tokenize takes a string and returns an array of tokens

Types

type Cache

type Cache struct {
}

TODO: Implement a cache that based upon the search query, generate an id based upon the search query and store the results in a map[string]any

type Document

type Document struct {
	Original any
	Values   map[string]fields.Field
}

func NewDoc

func NewDoc(doc any) (*Document, error)
Example
type Test struct {
	Name string `find:"Name"`
	Age  int    `find:"Age"`
}

// Create a new document
doc := Test{
	Name: "Test",
	Age:  10,
}

// Create a new document
document, err := NewDoc(doc)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Println(document.Values)
Output:

map[Age:{int 10 []} Name:{string Test [test]}]

func (*Document) GetFieldType

func (d *Document) GetFieldType(field string) (string, bool)

func (*Document) GetFieldValue

func (d *Document) GetFieldValue(field string) (any, bool)
Example
type Test struct {
	Name string `find:"name"`
	Age  int    `find:"age"`
}

// Create a new document
doc := Test{
	Name: "Test",
	Age:  10,
}

// Create a new document
document, _ := NewDoc(doc)

value, _ := document.GetFieldValue("name")
fmt.Println(value)

value, _ = document.GetFieldValue("age")
fmt.Println(value)
Output:

test true
10 true

type Index

type Index struct {
	Documents map[string]*Document

	Filters []FilterFunc

	// Cache
	Cache     bool
	CacheSize int
	// contains filtered or unexported fields
}

func New

func New() *Index

func NewOptions

func NewOptions(options Options) *Index

NewOptions returns a new index with the given options

func (*Index) Get

func (i *Index) Get(id string) (any, error)

Get returns the document with the given ID

func (*Index) Index

func (i *Index) Index(id string, doc any) error

func (*Index) Random

func (i *Index) Random() (string, any)

func (*Index) Search

func (i *Index) Search(searchQuery SearchQuery) ([]any, error)

Search returns a array of documents

Example
type Test struct {
	Name string `find:"Name"`
	Age  int    `find:"Age"`
}

// Create a new index
index := New()

// Create a new document
doc := Test{
	Name: "Billy is my friend",
	Age:  10,
}

// Index the document
id := "1"
err := index.Index(id, doc)
if err != nil {
	fmt.Println(err)
	return
}

// Create a search query
search := SearchQuery{
	Fields: []SearchQueryField{
		{
			Field: "Name",
			Type:  "partial",
			Value: "friend",
		},
	},
}

// Search for the document
results, err := index.Search(search)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Printf("%+v", results)
Output:

[{Name:Billy is my friend Age:10}]

func (*Index) SearchStr

func (i *Index) SearchStr(search string) ([]any, error)

type Options

type Options struct {
	// Cache
	Cache     bool // Whether or not to cache search results
	CacheSize int  // The maximum number of search results to cache

	// Filters
	Filters []FilterFunc // The filters to apply to strings
}

type SearchQuery

type SearchQuery struct {
	Limit  uint               `json:"limit"`
	Skip   uint               `json:"skip"`
	Sort   string             `json:"sort"`    // asc or desc
	SortBy string             `json:"sort_by"` // Field to sort by
	Fields []SearchQueryField `json:"fields"`
}

func JsontoSearchQueries

func JsontoSearchQueries(jsonBytes []byte) (*SearchQuery, error)

func StringToSearchQuery

func StringToSearchQuery(input string) (*SearchQuery, error)

Take full raw query string and parse it into a SearchQuery struct Field types are a flat structure of the field and type of field

func (*SearchQuery) Sanatize

func (sq *SearchQuery) Sanatize()

func (*SearchQuery) Validate

func (sq *SearchQuery) Validate() error

type SearchQueryField

type SearchQueryField struct {
	Field string
	Type  string // "match", "partial", "range"
	Value any
}

func (*SearchQueryField) Sanatize

func (dq *SearchQueryField) Sanatize()

func (*SearchQueryField) Validate

func (dq *SearchQueryField) Validate() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL