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???