/// <summary> /// 获取索引存放的分目录 /// </summary> /// <param name="type"></param> /// <param name="parameter"></param> /// <returns></returns> public static string GetLuceneTextIndexDirectoryPath(LuceneTextIndexType type, string parameter) { string d = string.Concat(type.ToString(), "\\"); if (!string.IsNullOrEmpty(parameter)) { d = string.Concat(d, parameter, "\\"); } string p = Path.Combine(Vars.LUCENE_TEXT_INDEX_FILE_DIRECTORY, d); return p; }
/// <summary> /// 获取索引存放的分目录 /// </summary> /// <param name="type"></param> /// <param name="parameter"></param> /// <returns></returns> public static string GetLuceneTextIndexDirectoryPath(LuceneTextIndexType type, string parameter) { string d = string.Concat(type.ToString(), "\\"); if (!string.IsNullOrEmpty(parameter)) { d = string.Concat(d, parameter, "\\"); } string p = Path.Combine(Vars.LUCENE_TEXT_INDEX_FILE_DIRECTORY, d); return(p); }
/// <summary> /// 搜索LUCENE数据 /// </summary> /// <param name="indexType"></param> /// <param name="query"></param> /// <param name="sort"></param> /// <param name="pagerInfo"></param> /// <param name="callback"></param> /// <returns></returns> public static List<Document> SearchLuceneData(LuceneTextIndexType indexType, Query query, Sort sort, PagerInfo pagerInfo, SearchLuceneDataLoopItemHandler callback) { List<Document> list = new List<Document>(); string textIndexDir = Utilities.GetLuceneTextIndexDirectoryPath(indexType, null); FSDirectory directory = FSDirectory.Open(new System.IO.DirectoryInfo(textIndexDir), new NoLockFactory()); IndexReader indexReader = IndexReader.Open(directory, true); IndexSearcher indexSearcher = new IndexSearcher(indexReader); ScoreDoc[] docs; int totalCount; int startOffset; int endOffset; if (sort != null) { TopFieldDocs resultFieldDocs = indexSearcher.Search(query, null, indexSearcher.MaxDoc(), sort); totalCount = resultFieldDocs.totalHits; pagerInfo.RecordCount = totalCount; startOffset = (pagerInfo.PageIndex - 1) * pagerInfo.PageSize; endOffset = pagerInfo.PageIndex * pagerInfo.PageSize; if (endOffset >= totalCount) { endOffset = totalCount; } docs = resultFieldDocs.scoreDocs; } else { TopDocs resultFieldDocs = indexSearcher.Search(query, null, indexSearcher.MaxDoc()); totalCount = resultFieldDocs.totalHits; pagerInfo.RecordCount = totalCount; startOffset = (pagerInfo.PageIndex - 1) * pagerInfo.PageSize; endOffset = pagerInfo.PageIndex * pagerInfo.PageSize; if (endOffset >= totalCount) { endOffset = totalCount; } docs = resultFieldDocs.scoreDocs; } if (totalCount > 0) { for (int i = startOffset; i < endOffset; i++) { ScoreDoc hit = docs[i]; Document doc = indexSearcher.Doc(hit.doc); list.Add(doc); if (callback != null) { callback(doc); } } } indexSearcher.Close(); directory.Close(); return list; }
/// <summary> /// 搜索LUCENE数据 /// </summary> /// <param name="indexType"></param> /// <param name="query"></param> /// <param name="sort"></param> /// <param name="pagerInfo"></param> /// <param name="callback"></param> /// <returns></returns> public static List <Document> SearchLuceneData(LuceneTextIndexType indexType, Query query, Sort sort, PagerInfo pagerInfo, SearchLuceneDataLoopItemHandler callback) { List <Document> list = new List <Document>(); string textIndexDir = Utilities.GetLuceneTextIndexDirectoryPath(indexType, null); FSDirectory directory = FSDirectory.Open(new System.IO.DirectoryInfo(textIndexDir), new NoLockFactory()); IndexReader indexReader = IndexReader.Open(directory, true); IndexSearcher indexSearcher = new IndexSearcher(indexReader); ScoreDoc[] docs; int totalCount; int startOffset; int endOffset; if (sort != null) { TopFieldDocs resultFieldDocs = indexSearcher.Search(query, null, indexSearcher.MaxDoc(), sort); totalCount = resultFieldDocs.totalHits; pagerInfo.RecordCount = totalCount; startOffset = (pagerInfo.PageIndex - 1) * pagerInfo.PageSize; endOffset = pagerInfo.PageIndex * pagerInfo.PageSize; if (endOffset >= totalCount) { endOffset = totalCount; } docs = resultFieldDocs.scoreDocs; } else { TopDocs resultFieldDocs = indexSearcher.Search(query, null, indexSearcher.MaxDoc()); totalCount = resultFieldDocs.totalHits; pagerInfo.RecordCount = totalCount; startOffset = (pagerInfo.PageIndex - 1) * pagerInfo.PageSize; endOffset = pagerInfo.PageIndex * pagerInfo.PageSize; if (endOffset >= totalCount) { endOffset = totalCount; } docs = resultFieldDocs.scoreDocs; } if (totalCount > 0) { for (int i = startOffset; i < endOffset; i++) { ScoreDoc hit = docs[i]; Document doc = indexSearcher.Doc(hit.doc); list.Add(doc); if (callback != null) { callback(doc); } } } indexSearcher.Close(); directory.Close(); return(list); }