bleve是golang實現的一個全文索引庫,類似lucene
官方使用示例:
import "github.com/blevesearch/bleve/v2"
func main() {
// 創建index
mapping := bleve.NewIndexMapping()
index, err := bleve.New("example.bleve", mapping)
// 索引數據
err = index.Index(identifier, your_data)
// 全文搜索
query := bleve.NewMatchQuery("text")
search := bleve.NewSearchRequest(query)
searchResults, err := index.Search(search)
}
我們先來看看他的類型
type IndexMappingImpl struct {
TypeMapping map[string]*DocumentMapping `json:"types,omitempty"`
DefaultMapping *DocumentMapping `json:"default_mapping"`
TypeField string `json:"type_field"`
DefaultType string `json:"default_type"`
DefaultAnalyzer string `json:"default_analyzer"`
DefaultDateTimeParser string `json:"default_datetime_parser"`
DefaultField string `json:"default_field"`
StoreDynamic bool `json:"store_dynamic"`
IndexDynamic bool `json:"index_dynamic"`
DocValuesDynamic bool `json:"docvalues_dynamic"`
CustomAnalysis *customAnalysis `json:"analysis,omitempty"`
cache *registry.Cache
}
image.png
- TypeMapping:它是一個字符串到DocumentMapping指針的map。它的key是字符串,代表Document的類型。它的value是DocumentMapping的指針,用來定制該類型文檔的索引方式。
- DefaultMapping:它是DocumentMapping的指針。當TypeMapping沒有配置某類型文檔的DocumentMapping時,則該類文檔使用該默認的DocumentMapping。
- DefaultAnalyzer:默認的根節點Analyzer。由于每個DocumentMapping、每個DocumentField都可以配置自己的Analyzer,當處理某個Field時,優先使用最個性化的設置。找不到個性化的才使用默認的,一級一級往上找,直到這里。
- DefaultType:當一篇文檔未提供分類時,就會使用DefaultType設置的類型名稱,默認是“_default”,也可以修改。需要注意的是,一個文檔時DefaultType不等于它就要使用DefaultMapping,可以在TypeMapping中為DefaultType設置個性化的索引方式。
我們重點看下documentMapping
type DocumentMapping struct {
Enabled bool `json:"enabled"`
Dynamic bool `json:"dynamic"`
Properties map[string]*DocumentMapping `json:"properties,omitempty"`
Fields []*FieldMapping `json:"fields,omitempty"`
DefaultAnalyzer string `json:"default_analyzer,omitempty"`
// StructTagKey overrides "json" when looking for field names in struct tags
StructTagKey string `json:"struct_tag_key,omitempty"`
}
Fields (FieldMapping)就是具體字段定義
type FieldMapping struct {
Name string `json:"name,omitempty"` //名稱
Type string `json:"type,omitempty"` //類型
Analyzer string `json:"analyzer,omitempty"` // 分詞器
// Store indicates whether to store field values in the index. Stored
// values can be retrieved from search results using SearchRequest.Fields.
Store bool `json:"store,omitempty"` //保留原始值
Index bool `json:"index,omitempty"` // 索引
// IncludeTermVectors, if true, makes terms occurrences to be recorded for
// this field. It includes the term position within the terms sequence and
// the term offsets in the source document field. Term vectors are required
// to perform phrase queries or terms highlighting in source documents.
IncludeTermVectors bool `json:"include_term_vectors,omitempty"`
IncludeInAll bool `json:"include_in_all,omitempty"`
DateFormat string `json:"date_format,omitempty"`
// DocValues, if true makes the index uninverting possible for this field
// It is useful for faceting and sorting queries.
DocValues bool `json:"docvalues,omitempty"`
// SkipFreqNorm, if true, avoids the indexing of frequency and norm values
// of the tokens for this field. This option would be useful for saving
// the processing of freq/norm details when the default score based relevancy
// isn't needed.
SkipFreqNorm bool `json:"skip_freq_norm,omitempty"`
// Dimensionality of the vector
Dims int `json:"dims,omitempty"`
// Similarity is the similarity algorithm used for scoring
// vector fields.
// See: index.DefaultSimilarityMetric & index.SupportedSimilarityMetrics
Similarity string `json:"similarity,omitempty"`
// Applicable to vector fields only - optimization string
VectorIndexOptimizedFor string `json:"vector_index_optimized_for,omitempty"`
}
索引數據時,遍歷數據層級,組織doc文檔。
image.png
image.png