//初始化搜索引擎 func initsearchengine(settings map[string]string) *search.Engine { dict, _ := settings["DICT"] stop, _ := settings["STOP"] //初始化分词器 seg := segmenter.InitChinaCut(dict) //生成一个搜索引擎 searcher := search.NewSearchEngine() searcher.Init(search.EngineInitOptions{ //分词器采用引擎自带的分词器 Segmenter: seg, StopTokenFile: stop, UsePersistentStorage: false, IndexerInitOptions: &search.IndexerInitOptions{ IndexType: search.LocationsIndex, BM25Parameters: &search.BM25Parameters{ K1: 2.0, B: 0.75, }, }, //索引器接口实现,采用自带的wukong索引器 CreateIndexer: func() search.SearchIndexer { return indexer.NewWuKongIndexer() }, //排序器生成方法 CreateRanker: func() search.SearchRanker { return ranker.NewWuKongRanker() }, }) return searcher }
func init() { runtime.GOMAXPROCS(runtime.NumCPU()) //加载配置文件 loadConf() debug, e := kernel.GetSetting(settings, "DEBUG") checkError(e) //挂载数据库 mountDB() //挂载redis mountRedis() dict, e1 := kernel.GetSetting(settings, "DICT") checkError(e1) stop, e2 := kernel.GetSetting(settings, "STOP") checkError(e2) //初始化分词, 采用引擎自带分词器 seg := segmenter.InitChinaCut(dict) //初始化搜索引擎 var searcher search.Engine //获取mongodb集合数 indextmp, e3 := kernel.GetSetting(settings, "INDEXSTORENUM") checkError(e3) indexstorenum, err := strconv.Atoi(indextmp) checkError(err) //获取mongodb库名 mongodbname, e4 := kernel.GetSetting(settings, "MONGODBNAME") checkError(e4) //获取mongodb连接url mongodburl, e5 := kernel.GetSetting(settings, "MONGODBURL") checkError(e5) //获取mongodb集合前缀 collectionprefix, e5 := kernel.GetSetting(settings, "COLLECTIONPREFIX") checkError(e5) searcher.Init(search.EngineInitOptions{ //分词器采用引擎自带的分词器 Segmenter: seg, StopTokenFile: stop, UsePersistentStorage: func() bool { if debug == "True" { return false } else { return true } }(), IndexerInitOptions: &search.IndexerInitOptions{ IndexType: search.LocationsIndex, BM25Parameters: &search.BM25Parameters{ K1: 2.0, B: 0.75, }, }, //pipline采用引擎自带的mongo pipline SearchPipline: pipeline.InitMongo( mongodbname, indexstorenum, mongodburl, collectionprefix), //索引器接口实现,采用自带的wukong索引器 CreateIndexer: func() search.SearchIndexer { return indexer.NewWuKongIndexer() }, //排序器生成方法 CreateRanker: func() search.SearchRanker { return ranker.NewWuKongRanker() }, }) g = kernel.G{ //可以处理的http方法字典 Ml: map[string]string{ "GET": "Get", "POST": "Post", "OPTIONS": "Options", "HEAD": "Head", "PUT": "Put", "DELETE": "Delete", "CONNECT": "Connect", }, Init: initHandler, DefaultHandler: defaultHandler, End: endHandler, DB: db, CC: cc, Settings: settings, DIY: map[string]interface{}{ "seg": seg, "searcher": searcher, }, } }