Lucene.net多字段多索引目錄搜索

作者: 周建軍  發布時間: 2009-12-08 15:18  閱讀: 2708 次  推薦: 1   原文鏈接   [收藏]  

Lucene.net是目前在.net環境中被普遍使用的全文索引的開源項目,這次在項目的開發中也使用它進行全文索引。
在開發過程中碰到一些小問題就是對多字段和多索引目錄進行搜索。

1、多字段搜索就是同時要一個以上的字段中的內容進行比較搜索,類似概念在SQL中就是select * from Table where a like '%query%' or b like '%query%'。

Lucene.net中的單個字段查詢大家都比較熟悉,這里對字段content進行搜索

 
Query query = QueryParser.Parse(querystr,"content",new ChineseAnalyzer());
Hits hits
= searcher.Search(query);

 

對多個字段查詢用到一個MultiFieldQueryParser對象,該對象繼承自Query,我們要對字段title,content進行搜索。

 
string[] fields = {"content","title"};
Query multiquery
= MultiFieldQueryParser.Parse(querystr,fields,new ChineseAnalyzer());
Hits hits
= searcher.Search(multiquery);

 

2、多索引目錄就是要在多個索引目錄的中進行比較搜索,類似概念在SQL中就是select * from TableA union select * from TableB。

 
IndexSearcher[] searchers = new IndexSearcher[2];
searchers[
0] = new IndexSearcher(IndexPath0);
searchers[
1] = new IndexSearcher(IndexPath1);

MultiSearcher multisearcher
= new MultiSearcher(searchers);
TopDocs multitopdocs
= multisearcher.Search(query, null, 1000);

 

這個搜索的結果可能有相同的信息,比如你有一條相同的信息在多個目錄中索引,搜索的結果就會出現多次相同的信息。

還有一種搜索方式是用到ParallelMultiSearcher這個對象,它是從MulitSearcher繼承而來。

 
ParallelMultiSearcher parallelmultisearcher = new ParallelMultiSearcher(searchers);
TopDocs paralleltopdocs
= parallelmultisearcher.Search(query, null, 1000);

 

這個搜索是對搜索后的結果進行合并,剔除重復的信息。

1
0
 
 
 
 

文章列表

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()