/// <summary> /// 搜索(返回所有) /// </summary> /// <param name="indexPath">返回结果数据类型</param> /// <param name="query">Query</param> /// <param name="sort">排序</param> /// <returns></returns> public static List <T> Search <T>(string indexPath, Query query, Sort sort) where T : BaseIndexModel { if (string.IsNullOrWhiteSpace(indexPath)) { return(null); } if (null == query) { return(null); } FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NoLockFactory()); if (null == directory) { return(null); } IndexReader reader = IndexReader.Open(directory, true); IndexSearcher searcher = new IndexSearcher(reader); ScoreDoc[] docs = null; if (null == sort) { docs = searcher.Search(query, 0).ScoreDocs; } else { docs = searcher.Search(query, null, 0, sort).ScoreDocs; } List <T> list = new List <T>(); foreach (var scoreDoc in docs) { Document doc = searcher.Doc(scoreDoc.Doc); var data = new IndexFactory(doc).Result as T; if (null != data) { list.Add(data); } } return(list); }
/// <summary> /// 多索引文件联合查询搜索(带分页) /// </summary> /// <typeparam name="T">返回结果数据类型</typeparam> /// <param name="indexPaths">索引文件路径集合</param> /// <param name="query">Query</param> /// <param name="sort">排序</param> /// <param name="pageIndex">当前搜索页</param> /// <param name="pageSize">每页显示数</param> /// <param name="count">总搜索结果数</param> /// <returns></returns> public static List <T> Search <T>(string[] indexPaths, Query query, Sort sort, int pageIndex, int pageSize, out int count) where T : BaseIndexModel { count = 0; if (null == query) { return(null); } if (pageIndex < 1) { pageIndex = 1; } if (pageSize < 1) { pageSize = 1; } //起始搜索位置 int start = (pageIndex - 1) * pageSize; if (null == indexPaths || indexPaths.Length < 1) { return(null); } List <IndexSearcher> searchers = new List <IndexSearcher>(); foreach (var indexPath in indexPaths) { if (string.IsNullOrWhiteSpace(indexPath)) { continue; } //打开索引文件 FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NoLockFactory()); if (null == directory) { continue; } //检测索引文件是否存在 bool isExist = IndexReader.IndexExists(directory); if (!isExist) { continue; } //创建一个只读的索引文件读取实例 IndexReader reader = IndexReader.Open(directory, true); //实例化IndexSearcher搜索器 IndexSearcher searcher = new IndexSearcher(reader); searchers.Add(searcher); } if (searchers.Count < 1) { return(null); } MultiSearcher multiSearcher = new MultiSearcher(searchers.ToArray()); //Collector TopFieldCollector results = TopFieldCollector.Create(sort, start + pageSize, false, false, false, false); //搜索 multiSearcher.Search(query, results); //总命中率(精算) count = results.TotalHits; //获取当前页文档 var docs = results.TopDocs(start, pageSize).ScoreDocs; //定义一个结果返回变量 List <T> list = new List <T>(); //遍历当前页文档,并转换为需要的结果类型 foreach (var scoreDoc in docs) { Document doc = multiSearcher.Doc(scoreDoc.Doc); var data = new IndexFactory(doc).Result as T; if (null != data) { list.Add(data); } } return(list); }