Skip to content

Take Marty Schoch´s bluge on a ride aka an easy integration for in process use of github.com/blugelabs/bluge

License

Notifications You must be signed in to change notification settings

foomo/bluge-sled

Repository files navigation

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:https://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
}

search

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.NewAnalyzerConfig(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.BulkInsert(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)

About

Take Marty Schoch´s bluge on a ride aka an easy integration for in process use of github.com/blugelabs/bluge

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published