search

package
v0.0.2-rc-ci Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2024 License: AGPL-3.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TermsAggregation     = "terms"
	ObjectsAggregation   = "objects"
	DateRangeAggregation = "daterange"
)
View Source
const DateLayout = "2006-01-02T15:04:05Z07:00"

DateLayout is the date layout used internally for date aggregations.

Variables

This section is empty.

Functions

func Router

func Router(service Service) func(chi.Router)

Router returns an http router for the search service.

Types

type Aggregation

type Aggregation struct {
	Type    AggregationType `json:"type"`
	Field   string          `json:"field"`
	Filters []Filter        `json:"filters"`
}

func (Aggregation) CacheKey

func (a Aggregation) CacheKey() string

CacheKey returns the same string for every identical aggregations. All aggregations resulting in the same bucket (given the same index) should return an equal cache key.

type AggregationType

type AggregationType string

type BoolFilter

type BoolFilter struct {
	Field string `json:"field"`
	Value bool   `json:"value"`
}

func (*BoolFilter) CacheKey

func (b *BoolFilter) CacheKey() string

type Bucket

type Bucket interface {
	BucketType() AggregationType
}

type DateRangeBucket

type DateRangeBucket struct {
	Min time.Time `json:"min"`
	Max time.Time `json:"max"`
}

func (DateRangeBucket) BucketType

func (d DateRangeBucket) BucketType() AggregationType

type DateRangeFilter

type DateRangeFilter struct {
	Field string    `json:"field"`
	Min   time.Time `json:"min"`
	Max   time.Time `json:"max"`
}

func (*DateRangeFilter) CacheKey

func (d *DateRangeFilter) CacheKey() string

CacheKey returns the same string for all identical filters.

type Filter

type Filter interface {
	CacheKey() string
	// contains filtered or unexported methods
}

type Hit

type Hit struct {
	// ID is the unique identifier of the stored entity. It might not match the
	// id of the entity in case it was indexed multiple times.
	ID string `json:"id"`
	// Type is the type of the document.
	Type string `json:"type"`
	// Raw contains the raw data of the document in the form of a json string.
	Raw string `json:"raw"`
}

Hit is a single search hit.

func (*Hit) Unmarshal

func (h *Hit) Unmarshal(v interface{}) error

Unmarshal unmarshals the raw data into v using json.Unmarshal.

type Indexable

type Indexable interface {
	// Identifier returns an id which should uniquely identify the object for
	// its type.
	Identifier() string

	// Type returns the type of the object.
	Type() string

	// QueryText returns the string to index for a text search.
	QueryText() string

	// SearchFields returns a map of additional fields to be indexed. Those
	// fields can be used for filtering or aggregations.
	SearchFields() map[string]interface{}
}

Indexable defines a document, that can be indexed. Any model, that implements this interface can be indexed and therefore is searchable. An indexable document can be uniquely identifyed by combining its identifier and type.

type NotFilter

type NotFilter struct {
	Filter Filter
}

func (*NotFilter) CacheKey

func (n *NotFilter) CacheKey() string

type NumericRangeFilter

type NumericRangeFilter struct {
	Field string   `json:"field"`
	Min   *float64 `json:"min"`
	Max   *float64 `json:"max"`
}

func (*NumericRangeFilter) CacheKey

func (n *NumericRangeFilter) CacheKey() string

CacheKey returns the same string for all identical filters.

type Options

type Options struct {
	// Query is the search query used for a text search.
	Query string `json:"query"`

	// Sort is the field, that should be sorted by.
	// When left empty, the default sorting is used.
	Sort string `json:"sort"`

	// SortDescending defines the sort order.
	SortDescending bool `json:"sortAscending"`

	// Page is current page.
	Page int `json:"page"`

	// PageSize defines the number of hits returned per page.
	//
	// PageSize is infinite when set to 0.
	PageSize int `json:"pageSize"`

	// Filters is a list of filters, that reduce the search result. All filters
	// are combined with AND logic in addition with the search query.
	Filters []Filter `json:"filter"`

	// Aggregations is a map of aggregations, to perform aggregations on fields.
	// The provided map key can be used to identify the corresponding bucket in
	// the result.
	Aggregations map[string]Aggregation `json:"aggregations"`
}

Options defines a set of optional search options.

func (*Options) CacheKey

func (o *Options) CacheKey() string

CacheKey returns a string uniquely identifying the Options object.

type Result

type Result struct {
	// Hits are the search hits for the current pagination.
	Hits []Hit `json:"hits"`

	// Total is the total number of search hits.
	// It is independet of the current pagination.
	Total uint64 `json:"total"`

	// Buckets is a set of aggregation buckets.
	// The map key corresponds to aggregation name.
	Buckets map[string]Bucket `json:"buckets"`
}

Result contains a search result.

type Service

type Service interface {
	// Index takes one or many indexable document and adds them to the search index.
	Index(docs ...Indexable) error

	// Delete deletes the document with the given type and id.
	// Note: both type and id have to be provided in order to uniquely identify
	// the document.
	Delete(docType, id string) error

	// Search performes a full text search on all indexed documents.
	// Performes a wildcard match for empty queries.
	// In addition to the search query a set of additional search options can be
	// provided.
	Search(ctx context.Context, opts *Options) (*Result, error)

	// LastModified returns the last time, the store was updated. This can be
	// used to invalidate a client side cache.
	LastModified() time.Time

	// Stops the search service.
	Stop()
}

Service defines a search service.

-go:generate go run github.com/petergtz/pegomock/pegomock generate eintopf.info/service/search Service --output=../../internal/mock/search_service.go --package=mock --mock-name=SearchService

func NewService

func NewService(indexPath string, searchTimeout time.Duration, resultCacheSize int, bucketCacheSize int, tz *time.Location) (Service, error)

NewService returns a new search service. Takes the path to the index directory. If the index already exits, the existing index is used. Otherwise a new one will be created.

type Term

type Term struct {
	Term  string `json:"term"`
	Count int    `json:"count"`
}

type TermsBucket

type TermsBucket []Term

func (TermsBucket) BucketType

func (t TermsBucket) BucketType() AggregationType

type TermsFilter

type TermsFilter struct {
	Field string   `json:"field"`
	Terms []string `json:"terms"`
}

TermsFilter can be used to filter documents containing one of the provided terms in the field. If the field contains mutiple terms, only one must match.

func (*TermsFilter) CacheKey

func (t *TermsFilter) CacheKey() string

CacheKey returns the same string for all identical filters.

Jump to

Keyboard shortcuts

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