bleve源码解析 - index mapping

2024-04-07  本文已影响0人  SecondRocker

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

我们重点看下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
上一篇 下一篇

猜你喜欢

热点阅读