sled

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

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

Go to latest
Published: Sep 9, 2024 License: Apache-2.0 Imports: 21 Imported by: 0

README

Example

  1. generate fake data and reload index go run example/main.go -fake -reload or on subsequent runs with the same data go run example/main.go
  2. search via http://localhost:8080/?q=shirt

Configuration

index

type IndexConfig struct {
	ShardNum    int                           // number of shards to use
	ShardPath   string                        // filepath to store shard index (if not in-memory)
	IdField     string                        // data field to be used as doc _id
	StoreFields []string                      // fields to be stored in index; if not set, just use composite "_all"
	AnalyzerConfig analyzer.ConfigMap // analyzer config to use per field. use "*" for any field
}
type SearchConfig struct {
	Limit          int                           // limit number of results returned; 0 will return all
	From           int                           // offset for paging results (to be used with limit)
	SearchFields   []string                      // fields to search the query; if not set, search composite "_all"
	ReturnFields   []string                      // stored fields to return when getting search results; see IndexConfig.StoreFields to manage fields youre storing
	QueryConfig    QueryConfig                   // this will have no effect if SearchConfig.SearchFields are not set
	ScoreThreshold float64                       // filter results below specified score. if not set, includes all
	AnalyzerConfig analyzer.ConfigMap            // analyzer config to use per field. use "*" for any field
}

Quickstart

with defaults

// example with german analyzer config and default index and search config
ac := analyzer.NewConfig(analyzer.German).WithoutStem().WithLength(3, 15)
// make sure to use proper field names in index and search config
indexConfig := sled.NewDefaultIndexConfig("my-index", "id", false, *ac)
// in this case were using all of the fields to search and returning only "image", "title", "infos", "brand"
searchConfig := sled.NewDefaultSearchConfig(*ac,[]string{"image", "title", "infos", "brand"})

initialize the index

index, err := sled.NewIndex(indexConfig)

load data into the index

f, err := os.Open("data.json")
if err != nil {
  return nil, err
}
defer f.Close()
var data []map[string]interface{}
if err := json.NewDecoder(f).Decode(&data); err != nil {
  return nil, err
}
if err := index.BatchInsert(data); err != nil {
  return nil, err
}

search the index

results, err := index.Search(ctx, q, searchConfig)
for _, hit := range results.Hits {
  // do something with the hits
}

Language support note

  • In the current implementation its advised to use a single language per index
  • For multiple languages in a single index, for valid results, one would need to specify language dependant fields (and thus cannot use _all for searching)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateFakeData

func GenerateFakeData(num int, savePath string) ([]map[string]any, error)

Types

type Config

type Config struct {
	IndexConfig  IndexConfig
	SearchConfig SearchConfig
}

type Hit

type Hit struct {
	Id     string
	Score  float64
	Values map[string]string
}

type Index

type Index struct {
	// contains filtered or unexported fields
}

func NewIndex

func NewIndex(ic IndexConfig) (*Index, error)

func (Index) BatchDelete

func (i Index) BatchDelete(ids []string) error

func (Index) BatchInsert

func (i Index) BatchInsert(data []map[string]any) error

func (*Index) Purge

func (i *Index) Purge() error

purge any saved paths

func (Index) Search

func (i Index) Search(ctx context.Context, query string, sc *SearchConfig) (combined SearchResult, err error)

func (Index) Update

func (i Index) Update(datum map[string]any) error

func (Index) Upsert

func (i Index) Upsert(data []map[string]any) error

type IndexConfig

type IndexConfig struct {
	ShardNum       int                `yaml:"shard_num,omitempty" json:"shard_num,omitempty"`             // number of shards to use
	ShardPath      string             `yaml:"shard_path,omitempty" json:"shard_path,omitempty"`           // filepath to store shard index (if not in-memory)
	IdField        string             `yaml:"id_field,omitempty" json:"id_field,omitempty"`               // data field to be used as doc _id
	StoreFields    []string           `yaml:"store_fields,omitempty" json:"store_fields,omitempty"`       // fields to be stored in index; if not set, just use composite "_all"
	AnalyzerConfig analyzer.ConfigMap `yaml:"analyzer_config,omitempty" json:"analyzer_config,omitempty"` // analyzer config to use per field. use "*" for any field
}

func NewDefaultIndexConfig

func NewDefaultIndexConfig(name, idField string, inMemory bool, ac analyzer.Config) IndexConfig

index config with opinionated defaults

type QueryConfig

type QueryConfig struct {
	ImproveFuzziness map[string]bool    // improve fuzziness when searching specific fields
	FieldBoost       map[string]float64 // boost results when searching specific fields
}

func (QueryConfig) GetBoost

func (qc QueryConfig) GetBoost(field string) float64

func (QueryConfig) GetFuzzyness

func (qc QueryConfig) GetFuzzyness(field string) int

type SearchConfig

type SearchConfig struct {
	Limit          int                `yaml:"limit,omitempty" json:"limit,omitempty"`                     // limit number of results returned; 0 will return all
	From           int                `yaml:"from,omitempty" json:"from,omitempty"`                       // offset for paging results (to be used with limit)
	SearchFields   []string           `yaml:"search_fields,omitempty" json:"search_fields,omitempty"`     // fields to search the query; if not set, search composite "_all"
	ReturnFields   []string           `yaml:"return_fields,omitempty" json:"return_fields,omitempty"`     // stored fields to return when getting search results; see IndexConfig.StoreFields to manage fields youre storing
	QueryConfig    QueryConfig        `yaml:"query_config,omitempty" json:"query_config,omitempty"`       // this will have no effect if SearchConfig.SearchFields are not set
	ScoreThreshold float64            `yaml:"score_threshold,omitempty" json:"score_threshold,omitempty"` // filter results below specified score. if not set, includes all
	AnalyzerConfig analyzer.ConfigMap `yaml:"analyzer_config,omitempty" json:"analyzer_config,omitempty"` // analyzer config to use per field. use "*" for any field
}

func NewDefaultSearchConfig

func NewDefaultSearchConfig(ac analyzer.Config, returnFields []string) SearchConfig

search config with opinionated defaults note: ImproveFuzziness will impact query speed but improves results

type SearchResult

type SearchResult struct {
	HitNumber uint64
	MaxScore  float64
	Duration  time.Duration
	Query     string
	Hits      []Hit
}

Directories

Path Synopsis
item
templ: version: v0.2.747
templ: version: v0.2.747
de

Jump to

Keyboard shortcuts

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