美文网首页
bleve源码解析 - index mapping

bleve源码解析 - index mapping

作者: SecondRocker | 来源:发表于2024-04-07 09:15 被阅读0次

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

相关文章

网友评论

      本文标题:bleve源码解析 - index mapping

      本文链接:https://www.haomeiwen.com/subject/cexvtjtx.html