bleve package - github.com/blevesearch/bleve/v2 - Go Packages
Package bleve is a library for indexing and searching text.
Example Opening New Index, Indexing Data
message := struct{
Id: "example"
From: "xyz@couchbase.com",
Body: "bleve indexing is easy",
}
mapping := bleve.NewIndexMapping()
index, _ := bleve.New("example.bleve", mapping)
index.Index(message.Id, message)
Example Opening Existing Index, Searching Data
index, _ := bleve.Open("example.bleve")
query := bleve.NewQueryStringQuery("bleve")
searchRequest := bleve.NewSearchRequest(query)
searchResult, _ := index.Search(searchRequest)
- Constants
- Variables
- func DeletedFields(ori, upd *mapping.IndexMappingImpl) (map[string]*index.UpdateFieldInfo, error)
- func IsScoreFusionRequested(req *SearchRequest) bool
- func LoadAndHighlightFields(hit *search.DocumentMatch, req *SearchRequest, indexName string, ...) (error, uint64)
- func MemoryNeededForSearchResult(req *SearchRequest) uint64
- func NewBoolFieldQuery(val bool) *query.BoolFieldQuery
- func NewBooleanFieldMapping() *mapping.FieldMapping
- func NewBooleanQuery() *query.BooleanQuery
- func NewConjunctionQuery(conjuncts ...query.Query) *query.ConjunctionQuery
- func NewDateRangeInclusiveQuery(start, end time.Time, startInclusive, endInclusive *bool) *query.DateRangeQuery
- func NewDateRangeInclusiveStringQuery(start, end string, startInclusive, endInclusive *bool) *query.DateRangeStringQuery
- func NewDateRangeQuery(start, end time.Time) *query.DateRangeQuery
- func NewDateRangeStringQuery(start, end string) *query.DateRangeStringQuery
- func NewDateTimeFieldMapping() *mapping.FieldMapping
- func NewDisjunctionQuery(disjuncts ...query.Query) *query.DisjunctionQuery
- func NewDocIDQuery(ids []string) *query.DocIDQuery
- func NewDocumentDisabledMapping() *mapping.DocumentMapping
- func NewDocumentMapping() *mapping.DocumentMapping
- func NewDocumentStaticMapping() *mapping.DocumentMapping
- func NewFuzzyQuery(term string) *query.FuzzyQuery
- func NewGeoBoundingBoxQuery(topLeftLon, topLeftLat, bottomRightLon, bottomRightLat float64) *query.GeoBoundingBoxQuery
- func NewGeoDistanceQuery(lon, lat float64, distance string) *query.GeoDistanceQuery
- func NewGeoPointFieldMapping() *mapping.FieldMapping
- func NewGeoShapeCircleQuery(coordinates []float64, radius, relation string) (*query.GeoShapeQuery, error)
- func NewGeoShapeFieldMapping() *mapping.FieldMapping
- func NewGeoShapeQuery(coordinates [][][][]float64, typ, relation string) (*query.GeoShapeQuery, error)
- func NewGeometryCollectionQuery(coordinates [][][][][]float64, types []string, relation string) (*query.GeoShapeQuery, error)
- func NewIPFieldMapping() *mapping.FieldMapping
- func NewIPRangeQuery(cidr string) *query.IPRangeQuery
- func NewIndexAlias(indexes ...Index) *indexAliasImpl
- func NewIndexMapping() *mapping.IndexMappingImpl
- func NewKeywordFieldMapping() *mapping.FieldMapping
- func NewMatchAllQuery() *query.MatchAllQuery
- func NewMatchNoneQuery() *query.MatchNoneQuery
- func NewMatchPhraseQuery(matchPhrase string) *query.MatchPhraseQuery
- func NewMatchQuery(match string) *query.MatchQuery
- func NewNumericFieldMapping() *mapping.FieldMapping
- func NewNumericRangeInclusiveQuery(min, max *float64, minInclusive, maxInclusive *bool) *query.NumericRangeQuery
- func NewNumericRangeQuery(min, max *float64) *query.NumericRangeQuery
- func NewPhraseQuery(terms []string, field string) *query.PhraseQuery
- func NewPrefixQuery(prefix string) *query.PrefixQuery
- func NewQueryStringQuery(q string) *query.QueryStringQuery
- func NewRegexpQuery(regexp string) *query.RegexpQuery
- func NewTermQuery(term string) *query.TermQuery
- func NewTermRangeInclusiveQuery(min, max string, minInclusive, maxInclusive *bool) *query.TermRangeQuery
- func NewTermRangeQuery(min, max string) *query.TermRangeQuery
- func NewTextFieldMapping() *mapping.FieldMapping
- func NewWildcardQuery(wildcard string) *query.WildcardQuery
- func SetLog(l *log.Logger)
- type Batch
- func (b *Batch) Delete(id string)
- func (b *Batch) DeleteInternal(key []byte)
- func (b *Batch) Index(id string, data interface{}) error
- func (b *Batch) IndexAdvanced(doc *document.Document) (err error)
- func (b *Batch) IndexSynonym(id string, collection string, definition *SynonymDefinition) error
- func (b *Batch) LastDocSize() uint64
- func (b *Batch) Merge(o *Batch)
- func (b *Batch) PersistedCallback() index.BatchCallback
- func (b *Batch) Reset()
- func (b *Batch) SetInternal(key, val []byte)
- func (b *Batch) SetPersistedCallback(f index.BatchCallback)
- func (b *Batch) Size() int
- func (b *Batch) String() string
- func (b *Batch) TotalDocsSize() uint64
- type Builder
- type Error
- type FacetRequest
- func (fr *FacetRequest) AddDateTimeRange(name string, start, end time.Time)
- func (fr *FacetRequest) AddDateTimeRangeString(name string, start, end *string)
- func (fr *FacetRequest) AddDateTimeRangeStringWithParser(name string, start, end *string, parser string)
- func (fr *FacetRequest) AddNumericRange(name string, min, max *float64)
- func (fr *FacetRequest) SetPrefixFilter(prefix string)
- func (fr *FacetRequest) SetRegexFilter(pattern string)
- func (fr *FacetRequest) Validate() error
- type FacetsRequest
- type FileSystemDirectory
- type HighlightRequest
- type Index
- func New(path string, mapping mapping.IndexMapping) (Index, error)
- func NewMemOnly(mapping mapping.IndexMapping) (Index, error)
- func NewUsing(path string, mapping mapping.IndexMapping, indexType string, kvstore string, ...) (Index, error)
- func Open(path string) (Index, error)
- func OpenUsing(path string, runtimeConfig map[string]interface{}) (Index, error)
- type IndexAlias
- type IndexCopyable
- type IndexErrMap
- type IndexStat
- type IndexStats
- type InsightsIndex
- type RequestParams
- type SearchQueryEndCallbackFn
- type SearchQueryStartCallbackFn
- type SearchRequest
- func (r *SearchRequest) AddFacet(facetName string, f *FacetRequest)
- func (r *SearchRequest) AddParams(params RequestParams)
- func (r *SearchRequest) SetSearchAfter(after []string)
- func (r *SearchRequest) SetSearchBefore(before []string)
- func (r *SearchRequest) SetSortFunc(s func(sort.Interface))
- func (r *SearchRequest) SortBy(order []string)
- func (r *SearchRequest) SortByCustom(order search.SortOrder)
- func (r *SearchRequest) SortFunc() func(data sort.Interface)
- func (r *SearchRequest) UnmarshalJSON(input []byte) error
- func (r *SearchRequest) Validate() error
- type SearchResult
- type SearchStatus
- type SynonymDefinition
- type SynonymIndex
- FacetRequest.AddDateTimeRange
- FacetRequest.AddNumericRange
- Index (Indexing)
- New
- NewBooleanQuery
- NewConjunctionQuery
- NewDisjunctionQuery
- NewFacetRequest
- NewHighlight
- NewHighlightWithStyle
- NewMatchAllQuery
- NewMatchNoneQuery
- NewMatchPhraseQuery
- NewMatchQuery
- NewNumericRangeInclusiveQuery
- NewNumericRangeQuery
- NewPhraseQuery
- NewPrefixQuery
- NewQueryStringQuery
- NewSearchRequest
- NewTermQuery
- SearchRequest.AddFacet
- SearchRequest.SortBy
- SearchRequest.SortByCustom
const ( ScoreDefault = "" ScoreNone = "none" ScoreRRF = "rrf" ScoreRSF = "rsf" )
Config contains library level configuration
Compare two index mappings to identify all of the updatable changes
func IsScoreFusionRequested(req *SearchRequest) bool
Checks if the request is hybrid search. Currently supports: RRF, RSF.
func MemoryNeededForSearchResult(req *SearchRequest) uint64
MemoryNeededForSearchResult is an exported helper function to determine the RAM needed to accommodate the results for a given search request.
NewBoolFieldQuery creates a new Query for boolean fields
NewBooleanFieldMapping returns a default field mapping for booleans
NewBooleanQuery creates a compound Query composed of several other Query objects. These other query objects are added using the AddMust() AddShould() and AddMustNot() methods. Result documents must satisfy ALL of the must Queries. Result documents must satisfy NONE of the must not Queries. Result documents that ALSO satisfy any of the should Queries will score higher.
must := NewMatchQuery("one")
mustNot := NewMatchQuery("great")
query := NewBooleanQuery()
query.AddMust(must)
query.AddMustNot(mustNot)
searchRequest := NewSearchRequest(query)
searchResults, err := exampleIndex.Search(searchRequest)
if err != nil {
panic(err)
}
fmt.Println(searchResults.Hits[0].ID)
Output: document id 1
NewConjunctionQuery creates a new compound Query. Result documents must satisfy all of the queries.
conjunct1 := NewMatchQuery("great")
conjunct2 := NewMatchQuery("one")
query := NewConjunctionQuery(conjunct1, conjunct2)
searchRequest := NewSearchRequest(query)
searchResults, err := exampleIndex.Search(searchRequest)
if err != nil {
panic(err)
}
fmt.Println(searchResults.Hits[0].ID)
Output: document id 2
NewDateRangeInclusiveQuery creates a new Query for ranges of date values. Date strings are parsed using the DateTimeParser configured in the
top-level config.QueryDateTimeParser
Either, but not both endpoints can be nil. startInclusive and endInclusive control inclusion of the endpoints.
NewDateRangeInclusiveStringQuery creates a new Query for ranges of date values. Date strings are parsed using the DateTimeParser set using
the DateRangeStringQuery.SetDateTimeParser() method.
this DateTimeParser is a custom date time parser defined in the index mapping, using AddCustomDateTimeParser() method. If no DateTimeParser is set, then the
top-level config.QueryDateTimeParser
is used. Either, but not both endpoints can be nil. startInclusive and endInclusive control inclusion of the endpoints.
NewDateRangeQuery creates a new Query for ranges of date values. Date strings are parsed using the DateTimeParser configured in the
top-level config.QueryDateTimeParser
Either, but not both endpoints can be nil.
NewDateRangeStringQuery creates a new Query for ranges of date values. Date strings are parsed using the DateTimeParser set using
the DateRangeStringQuery.SetDateTimeParser() method.
If no DateTimeParser is set, then the
top-level config.QueryDateTimeParser
is used.
NewDateTimeFieldMapping returns a default field mapping for dates
NewDisjunctionQuery creates a new compound Query. Result documents satisfy at least one Query.
disjunct1 := NewMatchQuery("great")
disjunct2 := NewMatchQuery("named")
query := NewDisjunctionQuery(disjunct1, disjunct2)
searchRequest := NewSearchRequest(query)
searchResults, err := exampleIndex.Search(searchRequest)
if err != nil {
panic(err)
}
fmt.Println(len(searchResults.Hits))
Output: 2
NewDocIDQuery creates a new Query object returning indexed documents among the specified set. Combine it with ConjunctionQuery to restrict the scope of other queries output.
NewDocumentDisabledMapping returns a new document mapping that will not perform any indexing.
NewDocumentMapping returns a new document mapping with all the default values.
NewDocumentStaticMapping returns a new document mapping that will not automatically index parts of a document without an explicit mapping.
NewFuzzyQuery creates a new Query which finds documents containing terms within a specific fuzziness of the specified term. The default fuzziness is 1.
The current implementation uses Levenshtein edit distance as the fuzziness metric.
NewGeoBoundingBoxQuery creates a new Query for performing geo bounding box searches. The arguments describe the position of the box and documents which have an indexed geo point inside the box will be returned.
NewGeoDistanceQuery creates a new Query for performing geo distance searches. The arguments describe a position and a distance. Documents which have an indexed geo point which is less than or equal to the provided distance from the given position will be returned.
NewGeoShapeCircleQuery creates a new query for a geoshape that is a circle given center point and the radius. Radius formats supported: "5in" "5inch" "7yd" "7yards" "9ft" "9feet" "11km" "11kilometers" "3nm" "3nauticalmiles" "13mm" "13millimeters" "15cm" "15centimeters" "17mi" "17miles" "19m" "19meters" If the unit cannot be determined, the entire string is parsed and the unit of meters is assumed.
NewGeoShapeQuery creates a new Query for matching the given geo shape. This method can be used for creating geoshape queries for shape types like: point, linestring, polygon, multipoint, multilinestring, multipolygon and envelope.
NewGeometryCollectionQuery creates a new query for the provided geometrycollection coordinates and types, which could contain multiple geo shapes.
NewIPRangeQuery creates a new Query for matching IP addresses. If the argument is in CIDR format, then the query will match all IP addresses in the network specified. If the argument is an IP address, then the query will return documents which contain that IP. Both ipv4 and ipv6 are supported.
func NewIndexAlias(indexes ...Index) *indexAliasImpl
NewIndexAlias creates a new IndexAlias over the provided Index objects.
NewIndexMapping creates a new IndexMapping that will use all the default indexing rules
NewKeywordFieldMapping returns a field mapping for text using the keyword analyzer, which essentially doesn't apply any specific text analysis.
NewMatchAllQuery creates a Query which will match all documents in the index.
// finds all documents in the index
query := NewMatchAllQuery()
searchRequest := NewSearchRequest(query)
searchResults, err := exampleIndex.Search(searchRequest)
if err != nil {
panic(err)
}
fmt.Println(len(searchResults.Hits))
Output: 2
NewMatchNoneQuery creates a Query which will not match any documents in the index.
// matches no documents in the index
query := NewMatchNoneQuery()
searchRequest := NewSearchRequest(query)
searchResults, err := exampleIndex.Search(searchRequest)
if err != nil {
panic(err)
}
fmt.Println(len(searchResults.Hits))
Output: 0
NewMatchPhraseQuery creates a new Query object for matching phrases in the index. An Analyzer is chosen based on the field. Input text is analyzed using this analyzer. Token terms resulting from this analysis are used to build a search phrase. Result documents must match this phrase. Queried field must have been indexed with IncludeTermVectors set to true.
// finds all documents with the given phrase in the index
query := NewMatchPhraseQuery("nameless one")
searchRequest := NewSearchRequest(query)
searchResults, err := exampleIndex.Search(searchRequest)
if err != nil {
panic(err)
}
fmt.Println(searchResults.Hits[0].ID)
Output: document id 2
NewMatchQuery creates a Query for matching text. An Analyzer is chosen based on the field. Input text is analyzed using this analyzer. Token terms resulting from this analysis are used to perform term searches. Result documents must satisfy at least one of these term searches.
// finds documents with fields fully matching the given query text
query := NewMatchQuery("named one")
searchRequest := NewSearchRequest(query)
searchResults, err := exampleIndex.Search(searchRequest)
if err != nil {
panic(err)
}
fmt.Println(searchResults.Hits[0].ID)
Output: document id 1
NewNumericFieldMapping returns a default field mapping for numbers
NewNumericRangeInclusiveQuery creates a new Query for ranges of numeric values. Either, but not both endpoints can be nil. Control endpoint inclusion with inclusiveMin, inclusiveMax.
value1 := float64(10)
value2 := float64(100)
v1incl := false
v2incl := false
query := NewNumericRangeInclusiveQuery(&value1, &value2, &v1incl, &v2incl)
searchRequest := NewSearchRequest(query)
searchResults, err := exampleIndex.Search(searchRequest)
if err != nil {
panic(err)
}
fmt.Println(searchResults.Hits[0].ID)
Output: document id 3
NewNumericRangeQuery creates a new Query for ranges of numeric values. Either, but not both endpoints can be nil. The minimum value is inclusive. The maximum value is exclusive.
value1 := float64(11)
value2 := float64(100)
data := struct{ Priority float64 }{Priority: float64(15)}
data2 := struct{ Priority float64 }{Priority: float64(10)}
err = exampleIndex.Index("document id 3", data)
if err != nil {
panic(err)
}
err = exampleIndex.Index("document id 4", data2)
if err != nil {
panic(err)
}
query := NewNumericRangeQuery(&value1, &value2)
searchRequest := NewSearchRequest(query)
searchResults, err := exampleIndex.Search(searchRequest)
if err != nil {
panic(err)
}
fmt.Println(searchResults.Hits[0].ID)
Output: document id 3
NewPhraseQuery creates a new Query for finding exact term phrases in the index. The provided terms must exist in the correct order, at the correct index offsets, in the specified field. Queried field must have been indexed with IncludeTermVectors set to true.
// finds all documents with the given phrases in the given field in the index
query := NewPhraseQuery([]string{"nameless", "one"}, "Name")
searchRequest := NewSearchRequest(query)
searchResults, err := exampleIndex.Search(searchRequest)
if err != nil {
panic(err)
}
fmt.Println(searchResults.Hits[0].ID)
Output: document id 2
NewPrefixQuery creates a new Query which finds documents containing terms that start with the specified prefix.
// finds all documents with terms having the given prefix in the index
query := NewPrefixQuery("name")
searchRequest := NewSearchRequest(query)
searchResults, err := exampleIndex.Search(searchRequest)
if err != nil {
panic(err)
}
fmt.Println(len(searchResults.Hits))
Output: 2
NewQueryStringQuery creates a new Query used for finding documents that satisfy a query string. The query string is a small query language for humans.
query := NewQueryStringQuery("+one -great")
searchRequest := NewSearchRequest(query)
searchResults, err := exampleIndex.Search(searchRequest)
if err != nil {
panic(err)
}
fmt.Println(searchResults.Hits[0].ID)
Output: document id 1
NewRegexpQuery creates a new Query which finds documents containing terms that match the specified regular expression.
NewTermQuery creates a new Query for finding an exact term match in the index.
query := NewTermQuery("great")
searchRequest := NewSearchRequest(query)
searchResults, err := exampleIndex.Search(searchRequest)
if err != nil {
panic(err)
}
fmt.Println(searchResults.Hits[0].ID)
Output: document id 2
NewTermRangeInclusiveQuery creates a new Query for ranges of text terms. Either, but not both endpoints can be "". Control endpoint inclusion with inclusiveMin, inclusiveMax.
NewTermRangeQuery creates a new Query for ranges of text terms. Either, but not both endpoints can be "". The minimum value is inclusive. The maximum value is exclusive.
NewTextFieldMapping returns a default field mapping for text
NewWildcardQuery creates a new Query which finds documents containing terms that match the specified wildcard. In the wildcard pattern '*' will match any sequence of 0 or more characters, and '?' will match any single character.
SetLog sets the logger used for logging by default log messages are sent to io.Discard
A Batch groups together multiple Index and Delete operations you would like performed at the same time. The Batch structure is NOT thread-safe. You should only perform operations on a batch from a single thread at a time. Once batch execution has started, you may not modify it.
Delete adds the specified delete operation to the batch. NOTE: the bleve Index is not updated until the batch is executed.
DeleteInternal adds the specified delete internal operation to the batch. NOTE: the bleve Index is not updated until the batch is executed.
Index adds the specified index operation to the batch. NOTE: the bleve Index is not updated until the batch is executed.
IndexAdvanced adds the specified index operation to the batch which skips the mapping. NOTE: the bleve Index is not updated until the batch is executed.
Reset returns a Batch to the empty state so that it can be reused in the future.
SetInternal adds the specified set internal operation to the batch. NOTE: the bleve Index is not updated until the batch is executed.
Size returns the total number of operations inside the batch including normal index operations and internal operations.
String prints a user friendly string representation of what is inside this batch.
Builder is a limited interface, used to build indexes in an offline mode. Items cannot be updated or deleted, and the caller MUST ensure a document is indexed only once.
NewBuilder creates a builder, which will build an index at the specified path, using the specified mapping and options.
Error represents a more strongly typed bleve error for detecting and handling specific types of errors.
const ( ErrorIndexPathExists Error = iota ErrorIndexPathDoesNotExist ErrorIndexMetaMissing ErrorIndexMetaCorrupt ErrorIndexClosed ErrorAliasMulti ErrorAliasEmpty ErrorUnknownIndexType ErrorEmptyID ErrorIndexReadInconsistency ErrorTwoPhaseSearchInconsistency ErrorSynonymSearchNotSupported )
Constant Error values which can be compared to determine the type of error
type FacetRequest struct {
Size int `json:"size"`
Field string `json:"field"`
TermPrefix string `json:"term_prefix,omitempty"`
TermPattern string `json:"term_pattern,omitempty"`
NumericRanges []*numericRange `json:"numeric_ranges,omitempty"`
DateTimeRanges []*dateTimeRange `json:"date_ranges,omitempty"`
}
A FacetRequest describes a facet or aggregation of the result document set you would like to be built.
NewFacetRequest creates a facet on the specified field that limits the number of entries to the specified size.
facet := NewFacetRequest("Name", 1)
query := NewMatchAllQuery()
searchRequest := NewSearchRequest(query)
searchRequest.AddFacet("facet name", facet)
searchResults, err := exampleIndex.Search(searchRequest)
if err != nil {
panic(err)
}
// total number of terms
fmt.Println(searchResults.Facets["facet name"].Total)
// number of docs with no value for this field
fmt.Println(searchResults.Facets["facet name"].Missing)
// term with highest occurrences in field name
fmt.Println(searchResults.Facets["facet name"].Terms.Terms()[0].Term)
Output: 5 2 one
AddDateTimeRange adds a bucket to a field containing date values. Documents with a date value falling into this range are tabulated as part of this bucket/range.
facet := NewFacetRequest("Created", 1)
facet.AddDateTimeRange("range name", time.Unix(0, 0), time.Now())
query := NewMatchAllQuery()
searchRequest := NewSearchRequest(query)
searchRequest.AddFacet("facet name", facet)
searchResults, err := exampleIndex.Search(searchRequest)
if err != nil {
panic(err)
}
// dates in field Created since starting of unix time till now
fmt.Println(searchResults.Facets["facet name"].DateRanges[0].Count)
Output: 2
func (fr *FacetRequest) AddDateTimeRangeString(name string, start, end *string)
AddDateTimeRangeString adds a bucket to a field containing date values. Uses defaultDateTimeParser to parse the date strings.
AddDateTimeRangeString adds a bucket to a field containing date values. Uses the specified parser to parse the date strings. provided the parser is registered in the index mapping.
AddNumericRange adds a bucket to a field containing numeric values. Documents with a numeric value falling into this range are tabulated as part of this bucket/range.
value1 := float64(11)
facet := NewFacetRequest("Priority", 1)
facet.AddNumericRange("range name", &value1, nil)
query := NewMatchAllQuery()
searchRequest := NewSearchRequest(query)
searchRequest.AddFacet("facet name", facet)
searchResults, err := exampleIndex.Search(searchRequest)
if err != nil {
panic(err)
}
// number documents with field Priority in the given range
fmt.Println(searchResults.Facets["facet name"].NumericRanges[0].Count)
Output: 1
func (fr *FacetRequest) SetPrefixFilter(prefix string)
SetPrefixFilter sets the prefix filter for term facets.
func (fr *FacetRequest) SetRegexFilter(pattern string)
SetRegexFilter sets the regex pattern filter for term facets.
FacetsRequest groups together all the FacetRequest objects for a single query.
type FileSystemDirectory string
FileSystemDirectory is the default implementation for the index.Directory interface.
HighlightRequest describes how field matches should be highlighted.
func NewHighlight() *HighlightRequest
NewHighlight creates a default HighlightRequest.
query := NewMatchQuery("nameless")
searchRequest := NewSearchRequest(query)
searchRequest.Highlight = NewHighlight()
searchResults, err := exampleIndex.Search(searchRequest)
if err != nil {
panic(err)
}
fmt.Println(searchResults.Hits[0].Fragments["Name"][0])
Output: great <mark>nameless</mark> one
func NewHighlightWithStyle(style string) *HighlightRequest
NewHighlightWithStyle creates a HighlightRequest with an alternate style.
query := NewMatchQuery("nameless")
searchRequest := NewSearchRequest(query)
searchRequest.Highlight = NewHighlightWithStyle(ansi.Name)
searchResults, err := exampleIndex.Search(searchRequest)
if err != nil {
panic(err)
}
fmt.Println(searchResults.Hits[0].Fragments["Name"][0])
Output: great �[43mnameless�[0m one
type Index interface {
Index(id string, data interface{}) error
Delete(id string) error
NewBatch() *Batch
Batch(b *Batch) error
Document(id string) (index.Document, error)
DocCount() (uint64, error)
Search(req *SearchRequest) (*SearchResult, error)
SearchInContext(ctx context.Context, req *SearchRequest) (*SearchResult, error)
Fields() ([]string, error)
FieldDict(field string) (index.FieldDict, error)
FieldDictRange(field string, startTerm []byte, endTerm []byte) (index.FieldDict, error)
FieldDictPrefix(field string, termPrefix []byte) (index.FieldDict, error)
Close() error
Mapping() mapping.IndexMapping
Stats() *IndexStat
StatsMap() map[string]interface{}
GetInternal(key []byte) ([]byte, error)
SetInternal(key, val []byte) error
DeleteInternal(key []byte) error
Name() string
SetName(string)
Advanced() (index.Index, error)
}
An Index implements all the indexing and searching capabilities of bleve. An Index can be created using the New() and Open() methods.
Index() takes an input value, deduces a DocumentMapping for its type, assigns string paths to its fields or values then applies field mappings on them.
The DocumentMapping used to index a value is deduced by the following rules:
- If value implements mapping.bleveClassifier interface, resolve the mapping from BleveType().
- If value implements mapping.Classifier interface, resolve the mapping from Type().
- If value has a string field or value at IndexMapping.TypeField.
(defaulting to "_type"), use it to resolve the mapping. Fields addressing is described below. 4) If IndexMapping.DefaultType is registered, return it. 5) Return IndexMapping.DefaultMapping.
Each field or nested field of the value is identified by a string path, then mapped to one or several FieldMappings which extract the result for analysis.
Struct values fields are identified by their "json:" tag, or by their name. Nested fields are identified by prefixing with their parent identifier, separated by a dot.
Map values entries are identified by their string key. Entries not indexed by strings are ignored. Entry values are identified recursively like struct fields.
Slice and array values are identified by their field name. Their elements are processed sequentially with the same FieldMapping.
String, float64 and time.Time values are identified by their field name. Other types are ignored.
Each value identifier is decomposed in its parts and recursively address SubDocumentMappings in the tree starting at the root DocumentMapping. If a mapping is found, all its FieldMappings are applied to the value. If no mapping is found and the root DocumentMapping is dynamic, default mappings are used based on value type and IndexMapping default configurations.
Finally, mapped values are analyzed, indexed or stored. See FieldMapping.Analyzer to know how an analyzer is resolved for a given field.
Examples:
type Date struct {
Day string `json:"day"`
Month string
Year string
}
type Person struct {
FirstName string `json:"first_name"`
LastName string
BirthDate Date `json:"birth_date"`
}
A Person value FirstName is mapped by the SubDocumentMapping at "first_name". Its LastName is mapped by the one at "LastName". The day of BirthDate is mapped to the SubDocumentMapping "day" of the root SubDocumentMapping "birth_date". It will appear as the "birth_date.day" field in the index. The month is mapped to "birth_date.Month".
data := struct {
Name string
Created time.Time
Age int
}{Name: "named one", Created: time.Now(), Age: 50}
data2 := struct {
Name string
Created time.Time
Age int
}{Name: "great nameless one", Created: time.Now(), Age: 25}
// index some data
err = exampleIndex.Index("document id 1", data)
if err != nil {
panic(err)
}
err = exampleIndex.Index("document id 2", data2)
if err != nil {
panic(err)
}
// 2 documents have been indexed
count, err := exampleIndex.DocCount()
if err != nil {
panic(err)
}
fmt.Println(count)
Output: 2
New index at the specified path, must not exist. The provided mapping will be used for all Index/Search operations.
indexMapping = NewIndexMapping()
exampleIndex, err = New("path_to_index", indexMapping)
if err != nil {
panic(err)
}
count, err := exampleIndex.DocCount()
if err != nil {
panic(err)
}
fmt.Println(count)
Output: 0
NewMemOnly creates a memory-only index. The contents of the index is NOT persisted, and will be lost once closed. The provided mapping will be used for all Index/Search operations.
NewUsing creates index at the specified path, which must not already exist. The provided mapping will be used for all Index/Search operations. The specified index type will be used. The specified kvstore implementation will be used and the provided kvconfig will be passed to its constructor. Note that currently the values of kvconfig must be able to be marshaled and unmarshaled using the encoding/json library (used when reading/writing the index metadata file).
Open index at the specified path, must exist. The mapping used when it was created will be used for all Index/Search operations.
OpenUsing opens index at the specified path, must exist. The mapping used when it was created will be used for all Index/Search operations. The provided runtimeConfig can override settings persisted when the kvstore was created. If runtimeConfig has updated mapping, then an index update is attempted Throws an error without any changes to the index if an unupdatable mapping is provided
An IndexAlias is a wrapper around one or more Index objects. It has two distinct modes of operation. 1. When it points to a single index, ALL index operations are valid and will be passed through to the underlying index. 2. When it points to more than one index, the only valid operation is Search. In this case the search will be performed across all the underlying indexes and the results merged. Calls to Add/Remove/Swap the underlying indexes are atomic, so you can safely change the underlying Index objects while other components are performing operations.
IndexCopyable is an index which supports an online copy operation of the index.
IndexErrMap tracks errors with the name of the index where it occurred
MarshalJSON serializes the error into a string for JSON consumption
type IndexStat struct {
}
type IndexStats struct {
}
func NewIndexStats() *IndexStats
func (i *IndexStats) Register(index Index)
func (i *IndexStats) UnRegister(index Index)
type RequestParams struct {
ScoreRankConstant int `json:"score_rank_constant,omitempty"`
ScoreWindowSize int `json:"score_window_size,omitempty"`
}
Additional parameters in the search request. Currently only being used for score fusion parameters.
func NewDefaultParams(from, size int) *RequestParams
type SearchRequest struct {
ClientContextID string `json:"client_context_id,omitempty"`
Query query.Query `json:"query"`
Size int `json:"size"`
From int `json:"from"`
Highlight *HighlightRequest `json:"highlight"`
Fields []string `json:"fields"`
Facets FacetsRequest `json:"facets"`
Explain bool `json:"explain"`
Sort search.SortOrder `json:"sort"`
IncludeLocations bool `json:"includeLocations"`
Score string `json:"score,omitempty"`
SearchAfter []string `json:"search_after"`
SearchBefore []string `json:"search_before"`
PreSearchData map[string]interface{} `json:"pre_search_data,omitempty"`
Params *RequestParams `json:"params,omitempty"`
}
A SearchRequest describes all the parameters needed to search the index. Query is required. Size/From describe how much and which part of the result set to return. Highlight describes optional search result highlighting. Fields describes a list of field values which should be retrieved for result documents, provided they were stored while indexing. Facets describe the set of facets to be computed. Explain triggers inclusion of additional search result score explanations. Sort describes the desired order for the results to be returned. Score controls the kind of scoring performed SearchAfter supports deep paging by providing a minimum sort key SearchBefore supports deep paging by providing a maximum sort key sortFunc specifies the sort implementation to use for sorting results.
A special field named "*" can be used to return all fields.
NewSearchRequest creates a new SearchRequest for the Query, using default values for all other search parameters.
// finds documents with fields fully matching the given query text
query := NewMatchQuery("named one")
searchRequest := NewSearchRequest(query)
searchResults, err := exampleIndex.Search(searchRequest)
if err != nil {
panic(err)
}
fmt.Println(searchResults.Hits[0].ID)
Output: document id 1
NewSearchRequestOptions creates a new SearchRequest for the Query, with the requested size, from and explanation search parameters. By default results are ordered by score, descending.
AddFacet adds a FacetRequest to this SearchRequest
facet := NewFacetRequest("Name", 1)
query := NewMatchAllQuery()
searchRequest := NewSearchRequest(query)
searchRequest.AddFacet("facet name", facet)
searchResults, err := exampleIndex.Search(searchRequest)
if err != nil {
panic(err)
}
// total number of terms
fmt.Println(searchResults.Facets["facet name"].Total)
// number of docs with no value for this field
fmt.Println(searchResults.Facets["facet name"].Missing)
// term with highest occurrences in field name
fmt.Println(searchResults.Facets["facet name"].Terms.Terms()[0].Term)
Output: 5 2 one
func (r *SearchRequest) AddParams(params RequestParams)
AddParams adds a RequestParams field to the search request
func (r *SearchRequest) SetSearchAfter(after []string)
SetSearchAfter sets the request to skip over hits with a sort value less than the provided sort after key
func (r *SearchRequest) SetSearchBefore(before []string)
SetSearchBefore sets the request to skip over hits with a sort value greater than the provided sort before key
SetSortFunc sets the sort implementation to use when sorting hits.
SearchRequests can specify a custom sort implementation to meet their needs. For instance, by specifying a parallel sort that uses all available cores.
SortBy changes the request to use the requested sort order this form uses the simplified syntax with an array of strings each string can either be a field name or the magic value _id and _score which refer to the doc id and search score any of these values can optionally be prefixed with - to reverse the order
// find docs containing "one", order by Age instead of score
query := NewMatchQuery("one")
searchRequest := NewSearchRequest(query)
searchRequest.SortBy([]string{"Age"})
searchResults, err := exampleIndex.Search(searchRequest)
if err != nil {
panic(err)
}
fmt.Println(searchResults.Hits[0].ID)
fmt.Println(searchResults.Hits[1].ID)
Output: document id 2 document id 1
SortByCustom changes the request to use the requested sort order
// find all docs, order by Age, with docs missing Age field first
query := NewMatchAllQuery()
searchRequest := NewSearchRequest(query)
searchRequest.SortByCustom(search.SortOrder{
&search.SortField{
Field: "Age",
Missing: search.SortFieldMissingFirst,
},
&search.SortDocID{},
})
searchResults, err := exampleIndex.Search(searchRequest)
if err != nil {
panic(err)
}
fmt.Println(searchResults.Hits[0].ID)
fmt.Println(searchResults.Hits[1].ID)
fmt.Println(searchResults.Hits[2].ID)
fmt.Println(searchResults.Hits[3].ID)
Output: document id 3 document id 4 document id 2 document id 1
SortFunc returns the sort implementation to use when sorting hits. Defaults to sort.Sort.
UnmarshalJSON deserializes a JSON representation of a SearchRequest
A SearchResult describes the results of executing a SearchRequest.
Status - Whether the search was executed on the underlying indexes successfully or failed, and the corresponding errors. Request - The SearchRequest that was executed. Hits - The list of documents that matched the query and their corresponding scores, score explanation, location info and so on. Total - The total number of documents that matched the query. Cost - indicates how expensive was the query with respect to bytes read from the mapped index files. MaxScore - The maximum score seen across all document hits seen for this query. Took - The time taken to execute the search. Facets - The facet results for the search.
MultiSearch executes a SearchRequest across multiple Index objects, then merges the results. The indexes must honor any ctx deadline.
func (sr *SearchResult) Merge(other *SearchResult)
Merge will merge together multiple SearchResults during a MultiSearch
func (sr *SearchResult) Size() int
type SearchStatus struct {
Total int `json:"total"`
Failed int `json:"failed"`
Successful int `json:"successful"`
Errors IndexErrMap `json:"errors,omitempty"`
}
SearchStatus is a section in the SearchResult reporting how many underlying indexes were queried, how many were successful/failed and a map of any errors that were encountered
func (ss *SearchStatus) Merge(other *SearchStatus)
Merge will merge together multiple SearchStatuses during a MultiSearch
type SynonymDefinition struct {
Input []string `json:"input,omitempty"`
Synonyms []string `json:"synonyms"`
}
SynonymDefinition represents a synonym mapping in Bleve. Each instance associates one or more input terms with a list of synonyms, defining how terms are treated as equivalent in searches.
SynonymIndex supports indexing synonym definitions alongside regular documents. Synonyms, grouped by collection name, define term relationships for query expansion in searches.