/// <summary> /// 执行搜索并将结果限制为特定类型,在返回之前,搜索结果将转换为相关类型,但不返回任何评分信息 /// </summary> /// <typeparam name ="T">要搜索的实体类型 - 注意:必须实现ILuceneIndexable </typeparam> /// <param name ="options">搜索选项</param> /// <returns></returns> public IScoredSearchResultCollection <T> ScoredSearch <T>(SearchOptions options) { // 确保类型匹配 if (typeof(T) != typeof(ILuceneIndexable)) { options.Type = typeof(T); } var indexResults = LuceneIndexSearcher.ScoredSearch(options); IScoredSearchResultCollection <T> results = new ScoredSearchResultCollection <T>(); results.TotalHits = indexResults.TotalHits; var sw = Stopwatch.StartNew(); foreach (var indexResult in indexResults.Results) { IScoredSearchResult <T> result = new ScoredSearchResult <T>(); result.Score = indexResult.Score; result.Entity = (T)GetConcreteFromDocument(indexResult.Document); results.Results.Add(result); } sw.Stop(); results.Elapsed = indexResults.Elapsed + sw.ElapsedMilliseconds; return(results); }
/// <summary> /// 执行搜索并将结果限制为特定类型,在返回之前,搜索结果将转换为相关类型,但不返回任何评分信息 /// </summary> /// <typeparam name ="T">要搜索的实体类型 - 注意:必须实现ILuceneIndexable </typeparam> /// <param name ="options">搜索选项</param> /// <returns></returns> public ISearchResultCollection <T> Search <T>(SearchOptions options) { options.Type = typeof(T); var indexResults = LuceneIndexSearcher.ScoredSearch(options); ISearchResultCollection <T> resultSet = new SearchResultCollection <T> { TotalHits = indexResults.TotalHits }; var sw = Stopwatch.StartNew(); foreach (var indexResult in indexResults.Results) { var entity = (T)GetConcreteFromDocument(indexResult.Document); resultSet.Results.Add(entity); } sw.Stop(); resultSet.Elapsed = indexResults.Elapsed + sw.ElapsedMilliseconds; return(resultSet); }
/// <summary> /// 搜索引擎 /// </summary> /// <param name="directory"></param> /// <param name="analyzer"></param> /// <param name="memoryCache"></param> public SearchEngineImp(Directory directory, Analyzer analyzer, ICache cache) { this.analyzer = analyzer; LuceneIndexer = new LuceneIndexer(directory, analyzer); LuceneIndexSearcher = new LuceneIndexSearcher(directory, analyzer, cache); }
/// <summary> /// 搜索一条匹配度最高的记录 /// </summary> /// <param name ="options">搜索选项</param> /// <returns></returns> public T SearchOne <T>(SearchOptions options) where T : class { return(GetConcreteFromDocument(LuceneIndexSearcher.ScoredSearchSingle(options)) as T); }
/// <summary> /// 搜索一条匹配度最高的记录 /// </summary> /// <param name ="options">搜索选项</param> /// <returns></returns> public object SearchOne(SearchOptions options) { return(GetConcreteFromDocument(LuceneIndexSearcher.ScoredSearchSingle(options))); }