Bleve代码阅读(一)——新建索引

 

引言

Bleve是Golang实现的一个全文检索库,类似Lucene之于Java。在这里通过阅读其代码,来学习如何使用及定制检索功能。也是为了通过阅读代码,学习在具体环境下Golang的一些使用方式。代码的路径在github上https://github.com/blevesearch/bleve

1 新建索引

下面的代码摘自Bleve的"Hello World"示例。

// open a new index mapping := bleve.NewIndexMapping() index, err := bleve.New("example.bleve", mapping) if err != nil {     fmt.Println(err)     return }

1.1和1.2两节是对上面逻辑的展开介绍,1.3节是在阅读代码中遇到的一些Golang特性的介绍,1.4节是当我们在使用bleve新建索引时可能会怎么做。

1.1 新建一个IndexMapping

下面这段代码是Bleve的"Hello World"示例的第一条语句,表示打开一个新索引。

// open a new index mapping := bleve.NewIndexMapping()

这个函数的定义位于bleve目录下的mapping.go文件

func NewIndexMapping() *mapping.IndexMappingImpl {     return mapping.NewIndexMapping() }

可以看出它是一个封装函数,调用了mapping package的NewIndexMapping函数。该函数的定义位于bleve/mapping/index.go文件内,属于github.com/blevesearch/bleve/mapping包。

// 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,         DocValuesDynamic:      DocValuesDynamic,         CustomAnalysis:        newCustomAnalysis(),         cache:                 registry.NewCache(),     } }

建立了一个IndexMappingImpl结构并返回其指针,该IndexMappingImpl的所有成员均以默认方式初始化。接下来看一下IndexMappingImpl结构的定义,该结构同样属于github.com/blevesearch/bleve/mapping包,并位于bleve/mapping/index.go文件内。

// An IndexMappingImpl controls how objects are placed // into an index. // First the type of the object is determined. // Once the type is know, the appropriate // DocumentMapping is selected by the type. // If no mapping was determined for that type, // a DefaultMapping will be used. 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,omitempty"`     CustomAnalysis        *customAnalysis             `json:"analysis,omitempty"`     cache                 *registry.Cache }

从注释可以看出,IndexMappingImpl结构是用来控制每一个对象应该被如何放入Index中,其各字段也是围绕这个目标展开的。

TypeMapping :一个map类型,Key是string类型,表示文档的类型。Value是DocumentMapping类型的指针,表示该类型文档对应的DocumentMapping。
DefaultMapping:一个DocumentMapping类型的指针。当文档的类型未知时,使用的默认DocumentMapping。

函数

关键字:
50000+
5万行代码练就真实本领
17年
创办于2008年老牌培训机构
1000+
合作企业
98%
就业率

联系我们

电话咨询

0532-85025005

扫码添加微信