• bleve搜索引擎源码分析之索引——mapping和lucene一样,也有_all


    例子:

    package main
    
    import (
        "fmt"
        "github.com/blevesearch/bleve"
    )
    
    func main() {
        // open a new index
        mapping := bleve.NewIndexMapping()
        index, err := bleve.New("example.bleve", mapping)
        if err != nil {
            fmt.Println(err)
            return
        }
    
        data := struct {
            Name string
            Des  string
        }{
            Name: "hello world this is bone",
            Des:  "this is a good time",
        }
    
        // index some data
        index.Index("id", data)
    
        // search for some text
        query := bleve.NewMatchQuery("this is bone")
        search := bleve.NewSearchRequest(query)
        searchResults, err := index.Search(search)
        if err != nil {
            fmt.Println(err)
            return
        }
        fmt.Println(searchResults)
    }

    mapping这里:

    // NewIndexMapping creates a new IndexMapping that will use all the default indexing rules
    func NewIndexMapping() *mapping.IndexMappingImpl {
        return mapping.NewIndexMapping()
    }

    难道是使用和lucene一样的???

    // NewIndexMapping creates a new IndexMapping that will use all the default indexing rules
    func NewIndexMapping() *IndexMappingImpl {
        return &IndexMappingImpl{
            TypeMapping:           make(map[string]*DocumentMapping),
            DefaultMapping:        NewDocumentMapping(),
            TypeField:             defaultTypeField,
            DefaultType:           defaultType,
            DefaultAnalyzer:       defaultAnalyzer,
            DefaultDateTimeParser: defaultDateTimeParser,
            DefaultField:          defaultField,
            IndexDynamic:          IndexDynamic,
            StoreDynamic:          StoreDynamic,
            CustomAnalysis:        newCustomAnalysis(),
            cache:                 registry.NewCache(),
        }
    }

    New就是设置索引目录和mapping。

    // New index at the specified path, must not exist.
    // The provided mapping will be used for all
    // Index/Search operations.
    func New(path string, mapping mapping.IndexMapping) (Index, error) {
        return newIndexUsing(path, mapping, Config.DefaultIndexType, Config.DefaultKVStore, nil)
    }

    index文档实现:

    // Index adds the specified index operation to the
    // batch.  NOTE: the bleve Index is not updated
    // until the batch is executed.
    func (b *Batch) Index(id string, data interface{}) error {
        if id == "" {
            return ErrorEmptyID
        }
        doc := document.NewDocument(id)
        err := b.index.Mapping().MapDocument(doc, data)
        if err != nil {
            return err
        }
        b.internal.Update(doc)
        return nil
    }

    其中,NewDocument实现:

    type Document struct {
        ID              string  `json:"id"`
        Fields          []Field `json:"fields"`
        CompositeFields []*CompositeField
        Number          uint64 `json:"-"`
    }
    
    func NewDocument(id string) *Document {
        return &Document{
            ID:              id,
            Fields:          make([]Field, 0),
            CompositeFields: make([]*CompositeField, 0),
        }
    }

    MappingDocument实现:

    func (im *IndexMappingImpl) MapDocument(doc *document.Document, data interface{}) error {
        docType := im.determineType(data)
        docMapping := im.mappingForType(docType)
        walkContext := im.newWalkContext(doc, docMapping)
        if docMapping.Enabled {
            docMapping.walkDocument(data, []string{}, []uint64{}, walkContext)
    
            // see if the _all field was disabled
            allMapping := docMapping.documentMappingForPath("_all")
            if allMapping == nil || (allMapping.Enabled != false) {
                field := document.NewCompositeFieldWithIndexingOptions("_all", true, []string{}, walkContext.excludedFromAll, document.IndexField|document.IncludeTermVectors)
                doc.AddField(field)
            }
        }
    
        return nil
    }

    我晕,看来bleve真的是和lucene设计一样!也有_all属性。

    难道后面倒排列表也会使用skip list???

  • 相关阅读:
    JavaScript 获取CSS媒体查询信息
    拖拉事件
    JavaScript 中的正常任务与微任务
    IOS 采用https 协议访问接口
    将类数组 转化为数组
    合并两个数组的方法
    base64转码
    Promise 异步执行的同步操作
    proxy set 拦截
    VIm 一些常用的设置
  • 原文地址:https://www.cnblogs.com/bonelee/p/6592188.html
Copyright © 2020-2023  润新知