xim

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2021 License: MIT Imports: 8 Imported by: 4

README

xim

xim is means The extra indexer for map. inspired by xian.
Package github.com/go-utils/xim generates Indexes and Filters to search NoSQL Firestore.

It's designed especially for Google Cloud Firestore and firestore-repo. (firestore-repo is firestore-model generator)

However, it doesn't depend on any specific Firestore APIs so that you may be able to use for some other NoSQL databases which support list-property and merge-join.

日本語ドキュメント

Features

  • prefix/suffix/partial match search
  • IN search
  • reduce composite indexes(esp. for Cloud Firestore)

Note

  • Search latency can increase depending on its result-set size and filter condition.
  • Index storage size can be bigger especially with long text prefix/suffix/partial match.

Usage

Code example is for Cloud Firestore.

Installation

$ go get -u github.com/go-utils/xim

Configuration

var bookIndexesConfig = xim.MustValidateConfig(&xim.Config{
	IgnoreCase:         true, // search case-insensitive
	SaveNoFiltersIndex: true, // always save 'NoFilters' index
})

// configure IN-filter
var statusInBuilder *xim.InBuilder = xim.NewInBuilder()

var (
    BookStatusUnpublished  = statusInBuilder.NewBit()
    BookStatusPublished    = statusInBuilder.NewBit()
    BookStatusDiscontinued = statusInBuilder.NewBit()
)

This configuration should be used to initialize both Indexes and Filters.

Label Constants

Define common labels for both Indexes and Filters.
Constants are not necessary but recommended.
Short label names would make index size smaller.

const (
	BookQueryLabelTitlePartial = "ti"
	BookQueryLabelTitlePrefix  = "tp"
	BookQueryLabelTitleSuffix  = "ts"
	BookQueryLabelIsHobby      = "h"
	BookQueryLabelStatusIN     = "s"
	BookQueryLabelPriceRange   = "pr"
)

Save indexes

idxs := xim.NewIndexes(bookIndexesConfig)

idxs.AddBigrams(BookQueryLabelTitlePartial, book.Title)
idxs.AddBiunigrams(BookQueryLabelTitlePartial, book.Title)
idxs.AddPrefixes(BookQueryLabelTitlePrefix, book.Title)
idxs.AddSuffixes(BookQueryLabelTitleSuffix, book.Title)
idxs.AddSomething(BookQueryLabelIsHobby, book.Category == "sports" || book.Category == "cooking")
idxs.Add(BookQueryLabelStatusIN, statusInBuilder.Indexes(BookStatusUnpublished)...)

switch {
case book.Price < 3000:
	idxs.Add(BookQueryLabelPriceRange, "p<3000")
case book.Price < 5000:
	idxs.Add(BookQueryLabelPriceRange, "3000<=p<5000")
case book.Price < 10000:
	idxs.Add(BookQueryLabelPriceRange, "5000<=p<10000")
default:
	idxs.Add(BookQueryLabelPriceRange, "10000<=p")
}

// build and set indexes to the book's property
var err error
book.Indexes, err = idxs.Build()
if err != nil {
	// error handling
}

// save book

Search (example of Cloud Firestore)

q := firestore.Query{}

filters := NewFilters(bookIndexesConfig).
    AddSomething(BookQueryLabelIsHolly, true).
    Add(BookQueryLabelStatusIN, statusInBuilder.Filter(BookStatusUnpublished, BookStatusPublished)).
    Add(BookQueryLabelPriceRange, "5000<=p<10000").
    AddBigrams(BookQueryLabelTitlePartial, title).
    AddBiunigrams(BookQueryLabelTitlePartial, title).
    AddSuffix(BookQueryLabelTitleSuffix, title)

built, err := filters.Build()
if err != nil {
    // error handling
}

for idx := range built {
    q = q.WherePath(firestore.FieldPath{"Indexes", idx}, "==", true)
}

// query books

Documentation

Index

Constants

View Source
const (
	IndexNoFilters          = "_NF_" // index to be used for no-filters.
	MaxIndexesSize          = 512    // maximum size of indexes.
	MaxCompositeIndexLabels = 8      // maximum number of labels for composite index.
)

Variables

View Source
var DefaultConfig = new(Config)

DefaultConfig - default configuration.

Functions

func Bigrams

func Bigrams(s string) []string

Bigrams returns bigram tokens from s.

func Biunigrams

func Biunigrams(s string) []string

Biunigrams - returns bigram and unigram tokens from s.

func Prefixes

func Prefixes(s string) []string

Prefixes - returns prefix tokens from s.

func Suffixes

func Suffixes(s string) []string

Suffixes - returns suffix tokens from s.

Types

type Bit

type Bit uint16

Bit - describes In-Filter mask bit

type Config

type Config struct {
	CompositeIdxLabels []string // label list which defines composite indexes to improve the search performance
	IgnoreCase         bool     // defines whether to ignore case on search
	SaveNoFiltersIndex bool     // defines whether to save IndexNoFilters index.
}

Config - describe extra indexes configuration.

func MustValidateConfig

func MustValidateConfig(conf *Config) *Config

MustValidateConfig - validates fields and panics if it's invalid.

func ValidateConfig

func ValidateConfig(conf *Config) (*Config, error)

ValidateConfig - validates Config fields.

type Filters

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

Filters - filters builder for extra indexes.

func NewFilters

func NewFilters(conf *Config) *Filters

NewFilters - creates and initializes a new Filters.

func (*Filters) Add

func (filters *Filters) Add(label string, indexes ...string) *Filters

Add - adds new filters with a label.

func (*Filters) AddBigrams

func (filters *Filters) AddBigrams(label string, s string) *Filters

AddBigrams - adds new bigram filters with a label.

func (*Filters) AddBiunigrams

func (filters *Filters) AddBiunigrams(label string, s string) *Filters

AddBiunigrams - adds new biunigram filters with a label.

func (*Filters) AddPrefix

func (filters *Filters) AddPrefix(label string, s string) *Filters

AddPrefix - adds a new prefix filters with a label.

func (*Filters) AddSomething

func (filters *Filters) AddSomething(label string, indexes interface{}) *Filters

AddSomething - adds new filter with a label. The indexes can be a slice or a string convertible value.

func (*Filters) AddSuffix

func (filters *Filters) AddSuffix(label string, s string) *Filters

AddSuffix - adds a new suffix filters with a label.

func (*Filters) Build

func (filters *Filters) Build() (map[string]bool, error)

Build - builds filters to save.

func (Filters) MustBuild

func (filters Filters) MustBuild() map[string]bool

MustBuild - builds filters to save and panics with error.

type InBuilder

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

InBuilder - creates Bit for In-Filter

func NewInBuilder

func NewInBuilder() *InBuilder

NewInBuilder - creates InBuilder

func (*InBuilder) Filter

func (f *InBuilder) Filter(bits ...Bit) string

Indexes - creates indexes for In-Filter

func (*InBuilder) Indexes

func (f *InBuilder) Indexes(bits ...Bit) []string

Indexes - creates indexes for In-Filter with multi-bits

func (*InBuilder) NewBit

func (f *InBuilder) NewBit() Bit

NewBit - returns a new bit shifted.

type Indexes

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

Indexes - extra indexes for firestore query.

func NewIndexes

func NewIndexes(conf *Config) *Indexes

NewIndexes - creates and initializes a new Indexes.

func (*Indexes) Add

func (idxs *Indexes) Add(label string, indexes ...string) *Indexes

Add - adds new indexes with a label.

func (*Indexes) AddBigrams

func (idxs *Indexes) AddBigrams(label string, s string) *Indexes

AddBigrams - adds new bigram indexes with a label.

func (*Indexes) AddBiunigrams

func (idxs *Indexes) AddBiunigrams(label string, s string) *Indexes

AddBiunigrams - adds new biunigram indexes with a label.

func (*Indexes) AddPrefixes

func (idxs *Indexes) AddPrefixes(label string, s string) *Indexes

AddPrefixes - adds new prefix indexes with a label.

func (*Indexes) AddSomething

func (idxs *Indexes) AddSomething(label string, indexes interface{}) *Indexes

AddSomething - adds new indexes with a label. The indexes can be a slice or a string convertible value.

func (*Indexes) AddSuffixes

func (idxs *Indexes) AddSuffixes(label string, s string) *Indexes

AddSuffixes - adds new prefix indexes with a label.

func (Indexes) Build

func (idxs Indexes) Build() (map[string]bool, error)

Build - builds indexes to save.

func (Indexes) MustBuild

func (idxs Indexes) MustBuild() map[string]bool

MustBuild - builds indexes to save and panics with error.

Jump to

Keyboard shortcuts

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