Documentation
¶
Overview ¶
Package bleve is a pure-Go (CGO=0) full-text search library for the Ruby / rbgo ecosystem.
Not a port of an existing gem ¶
Unlike most go-ruby-* siblings, there is no single canonical Ruby "bleve" gem to mirror: bleve is a Go-native search engine. This package therefore exposes that Go capability through a clean, idiomatic Ruby-shaped search API -- the shape a hypothetical Bleve Ruby gem would have -- rather than reproducing a specific upstream Ruby library.
It is a thin, ergonomic wrapper over github.com/blevesearch/bleve/v2. All indexing and searching is delegated to bleve; this package only provides the Hash-shaped documents/results, snake_case-friendly method names, and query DSL a Ruby developer would expect. When bound into rbgo it surfaces as:
index = Bleve.new_mem_index(Bleve::Mapping.new)
index.index("doc-1", { "title" => "hello world", "views" => 42 })
res = index.search(Bleve::Query.match("hello"), size: 10, highlight: true)
res.hits.each { |h| puts "#{h.id} #{h.score}" }
Go surface ¶
Index construction:
NewMemIndex(mapping) // in-memory (Bleve.new_mem_index) New(path, mapping) // on-disk, fresh (Bleve.new) Open(path) // on-disk, existing (Bleve.open)
Index operations: Index, Batch, Delete, Document, Count, Close, Search.
Query builders (Bleve::Query.*): Match, MatchPhrase, Term, Prefix, Fuzzy, Wildcard, Regexp, NumericRange, DateRange, Bool, QueryString, MatchAll, MatchNone. Each returns a Query with fluent Field/Boost/Fuzziness setters.
Search options: Size, From, Fields, Highlight, HighlightStyle, SortBy, WithFacet. Results are a SearchResult exposing Hits, Total, MaxScore, Took and Facets.
Mapping (Bleve::Mapping) configures document and field mappings (text, keyword, numeric, datetime, boolean) and analyzers, or a sensible default.
Portability ¶
The default in-memory (gtreap) and on-disk (scorch) indexes build with CGO_ENABLED=0 on every supported 64-bit target -- amd64, arm64, riscv64, loong64, ppc64le and s390x (big-endian) -- so the library is fully pure-Go. bleve's optional faiss-backed vector search (which needs cgo) is gated behind a build tag and is not used here.
Index ¶
- Variables
- func F64(v float64) *float64
- type Batch
- type Error
- type Facet
- type FacetResult
- type Hit
- type Index
- func (ix *Index) Batch(fn func(*Batch) error) error
- func (ix *Index) Close() error
- func (ix *Index) Count() (uint64, error)
- func (ix *Index) Delete(id string) error
- func (ix *Index) Document(id string) (map[string]interface{}, error)
- func (ix *Index) Index(id string, doc map[string]interface{}) error
- func (ix *Index) Path() string
- func (ix *Index) Search(q Query, opts ...SearchOption) (*SearchResult, error)
- type Mapping
- func (m *Mapping) AddBooleanField(name string) *Mapping
- func (m *Mapping) AddDateTimeField(name string) *Mapping
- func (m *Mapping) AddKeywordField(name string) *Mapping
- func (m *Mapping) AddNumericField(name string) *Mapping
- func (m *Mapping) AddTextField(name string) *Mapping
- func (m *Mapping) AddTextFieldWithAnalyzer(name, analyzer string) *Mapping
- func (m *Mapping) SetDefaultAnalyzer(name string) *Mapping
- type Query
- func Bool(must, should, mustNot []Query) Query
- func DateRange(start, end time.Time) Query
- func Fuzzy(term string) Query
- func Match(text string) Query
- func MatchAll() Query
- func MatchNone() Query
- func MatchPhrase(text string) Query
- func NumericRange(min, max *float64) Query
- func Prefix(prefix string) Query
- func QueryString(s string) Query
- func Regexp(pattern string) Query
- func Term(term string) Query
- func Wildcard(pattern string) Query
- type RangeCount
- type SearchOption
- type SearchResult
- type TermCount
Constants ¶
This section is empty.
Variables ¶
var ( // ErrClosed is returned when an operation is attempted on a closed index. ErrClosed = errors.New("bleve: index is closed") // ErrNotFound is returned when a document id is not present in the index. ErrNotFound = errors.New("bleve: document not found") )
Sentinel errors at the root of the Bleve::Error tree. Callers may test for them with errors.Is; every wrapped Error unwraps to one of these (or to the underlying bleve error).
Functions ¶
Types ¶
type Batch ¶
type Batch struct {
// contains filtered or unexported fields
}
Batch is an accumulator for a group of index/delete operations applied atomically. Obtain one via Index.Batch.
type Error ¶
type Error struct {
// Op is the high-level operation that failed, e.g. "index" or "search".
Op string
// Err is the wrapped cause.
Err error
}
Error is the common error type returned by this package. It records the operation that failed and wraps the underlying cause, which may be a sentinel (ErrClosed, ErrNotFound) or an error from the underlying bleve engine.
type Facet ¶
type Facet struct {
// contains filtered or unexported fields
}
Facet describes an aggregation over the result set. Build a term facet with TermFacet, then optionally attach numeric or date buckets.
func (*Facet) AddDateRange ¶
AddDateRange adds a named date bucket [start, end) to the facet.
type FacetResult ¶
type FacetResult struct {
Field string
Total int
Missing int
Other int
Terms []TermCount
NumericRanges []RangeCount
DateRanges []RangeCount
}
FacetResult is the aggregated result for a single named facet.
type Hit ¶
type Hit struct {
ID string
Score float64
Fields map[string]interface{}
Fragments map[string][]string
}
Hit is a single search result: the document id, its relevance score, any requested stored fields (as a Hash) and, when highlighting is on, the highlighted fragments per field.
type Index ¶
type Index struct {
// contains filtered or unexported fields
}
Index is a full-text index. It mirrors the Ruby Bleve::Index surface: index Hash-shaped documents, search them, and (for on-disk indexes) persist across open/close. An Index is safe to use until Close is called.
func New ¶
New creates a fresh on-disk index at path (Bleve.new). It is an error if path already exists. Pass nil for the default dynamic mapping.
func NewMemIndex ¶
NewMemIndex creates an in-memory index (Bleve.new_mem_index). Pass nil to use the default dynamic mapping. Nothing is written to disk.
func (*Index) Batch ¶
Batch runs fn against a Batch and, if fn returns nil, applies all queued operations atomically (Bleve::Index#batch { |b| ... }).
func (*Index) Delete ¶
Delete removes the document with the given id. Deleting an absent id is not an error.
func (*Index) Document ¶
Document returns the stored fields of the document with the given id as a Hash. It returns an error wrapping ErrNotFound if the id is absent.
func (*Index) Index ¶
Index adds or replaces the document with the given id. The document is a Hash (map) of field name to value.
func (*Index) Search ¶
func (ix *Index) Search(q Query, opts ...SearchOption) (*SearchResult, error)
Search runs q against the index with the given options and returns a SearchResult (Bleve::Index#search).
type Mapping ¶
type Mapping struct {
// contains filtered or unexported fields
}
Mapping describes how documents and their fields are indexed. It mirrors the Ruby Bleve::Mapping surface: start from a sensible default and, optionally, declare typed fields (text, keyword, numeric, datetime, boolean) and analyzers. A zero-configuration Mapping dynamically indexes every field.
func NewMapping ¶
func NewMapping() *Mapping
NewMapping returns a Mapping seeded with bleve's default dynamic mapping.
func (*Mapping) AddBooleanField ¶
AddBooleanField declares a boolean field.
func (*Mapping) AddDateTimeField ¶
AddDateTimeField declares a datetime field.
func (*Mapping) AddKeywordField ¶
AddKeywordField declares a non-tokenized text field (exact-match keyword).
func (*Mapping) AddNumericField ¶
AddNumericField declares a numeric field (float64/int).
func (*Mapping) AddTextField ¶
AddTextField declares a tokenized, analyzed, stored text field.
func (*Mapping) AddTextFieldWithAnalyzer ¶
AddTextFieldWithAnalyzer declares a text field indexed with a named analyzer.
func (*Mapping) SetDefaultAnalyzer ¶
SetDefaultAnalyzer sets the analyzer used for fields that do not name their own. Returns the Mapping for chaining.
type Query ¶
type Query struct {
// contains filtered or unexported fields
}
Query wraps a bleve query. Build one with the package-level constructors (mirroring Bleve::Query.*) and, where applicable, refine it with the fluent Field, Boost and Fuzziness setters.
func Bool ¶
Bool builds a boolean query from must (AND), should (OR) and mustNot (NOT) sub-queries. Any slice may be nil.
func MatchPhrase ¶
MatchPhrase builds a phrase query preserving term order and adjacency.
func NumericRange ¶
NumericRange builds a numeric range query. A nil bound is unbounded; use F64 to take the address of a literal.
func QueryString ¶
QueryString builds a query from bleve's query-string mini-language.
type RangeCount ¶
RangeCount is one bucket of a numeric or date range facet.
type SearchOption ¶
type SearchOption func(*searchOpts)
SearchOption configures a Search call. Mirrors the Ruby keyword arguments size:, from:, fields:, highlight:, sort:, facets:.
func Fields ¶
func Fields(fields ...string) SearchOption
Fields requests that the given stored fields be loaded onto each hit. Use "*" to load every stored field.
func From ¶
func From(n int) SearchOption
From sets the offset of the first hit to return (for pagination).
func Highlight ¶
func Highlight() SearchOption
Highlight enables highlighting with the default fragmenter/formatter.
func HighlightStyle ¶
func HighlightStyle(style string) SearchOption
HighlightStyle enables highlighting with a named style (e.g. "html", "ansi").
func SortBy ¶
func SortBy(fields ...string) SearchOption
SortBy sets the sort order. Each entry is a field name; prefix with "-" for descending, and use "_score" / "_id" for the built-in sorts.
func WithFacet ¶
func WithFacet(name string, f *Facet) SearchOption
WithFacet adds a named facet (aggregation) to the search.
type SearchResult ¶
type SearchResult struct {
// contains filtered or unexported fields
}
SearchResult is the outcome of a Search (Bleve::SearchResult).
func (*SearchResult) Facets ¶
func (r *SearchResult) Facets() map[string]*FacetResult
Facets returns the computed facets keyed by the name given to WithFacet.
func (*SearchResult) MaxScore ¶
func (r *SearchResult) MaxScore() float64
MaxScore is the highest score among the matches.
func (*SearchResult) Took ¶
func (r *SearchResult) Took() time.Duration
Took is the wall-clock time the search took.
func (*SearchResult) Total ¶
func (r *SearchResult) Total() uint64
Total is the number of documents that matched (independent of paging).
