func NewSearchIndex(repo content.Repo, config Config, logger webfw.Logger) (SearchIndex, error) { var err error var index bleve.Index si := SearchIndex{} _, err = os.Stat(config.SearchIndex.BlevePath) if err == nil { logger.Infoln("Opening search index " + config.SearchIndex.BlevePath) index, err = bleve.Open(config.SearchIndex.BlevePath) if err != nil { return EmptySearchIndex, errors.New(fmt.Sprintf("Error opening search index: %v\n", err)) } } else if os.IsNotExist(err) { mapping := bleve.NewIndexMapping() docMapping := bleve.NewDocumentMapping() idfieldmapping := bleve.NewTextFieldMapping() idfieldmapping.IncludeInAll = false docMapping.AddFieldMappingsAt("FeedId", idfieldmapping) docMapping.AddFieldMappingsAt("ArticleId", idfieldmapping) mapping.AddDocumentMapping(mapping.DefaultType, docMapping) logger.Infoln("Creating search index " + config.SearchIndex.BlevePath) index, err = bleve.New(config.SearchIndex.BlevePath, mapping) if err != nil { return EmptySearchIndex, errors.New(fmt.Sprintf("Error creating search index: %v\n", err)) } si.newIndex = true } else { return EmptySearchIndex, errors.New( fmt.Sprintf("Error getting stat of '%s': %v\n", config.SearchIndex.BlevePath, err)) } si.logger = logger si.repo = repo si.Index = index si.batchSize = config.SearchIndex.BatchSize return si, nil }
func NewBleve(path string, size int64, logger webfw.Logger) (content.SearchProvider, error) { var err error var exists bool var index bleve.Index _, err = os.Stat(path) if err == nil { logger.Infoln("Opening search index " + path) index, err = bleve.Open(path) if err != nil { return nil, errors.New(fmt.Sprintf("Error opening search index: %v\n", err)) } exists = true } else if os.IsNotExist(err) { mapping := bleve.NewIndexMapping() docMapping := bleve.NewDocumentMapping() idfieldmapping := bleve.NewTextFieldMapping() idfieldmapping.IncludeInAll = false docMapping.AddFieldMappingsAt("FeedId", idfieldmapping) docMapping.AddFieldMappingsAt("ArticleId", idfieldmapping) mapping.AddDocumentMapping(mapping.DefaultType, docMapping) logger.Infoln("Creating search index " + path) index, err = bleve.NewUsing(path, mapping, upside_down.Name, goleveldb.Name, nil) if err != nil { return nil, errors.New(fmt.Sprintf("Error creating search index: %v\n", err)) } } else { return nil, errors.New( fmt.Sprintf("Error getting stat of '%s': %v\n", path, err)) } return &Bleve{logger: logger, index: index, batchSize: size, newIndex: !exists}, nil }