2012-12-12 42 views
0

使用多個術語時可以控制操作的順序嗎?Lucene.net:如何控制多個術語的操作順序

例如,

Lucene.Net.Search.TermQuery tq1 = new Lucene.Net.Search.TermQuery(new Lucene.Net.Index.Term("location", "seattle")); 
Lucene.Net.Search.TermQuery tq2 = new Lucene.Net.Search.TermQuery(new Lucene.Net.Index.Term("location", "portland")); 
Lucene.Net.Search.TermQuery tq3 = new Lucene.Net.Search.TermQuery(new Lucene.Net.Index.Term("location", "spokane")); 
Lucene.Net.Search.TermQuery tq4 = new Lucene.Net.Search.TermQuery(new Lucene.Net.Index.Term("type", "city")); 

當我應用搜索,可以說,我想申請上述條款過濾我的搜索,讓搜索食物...所以我的查詢格式將類似於下面

食品和((TQ1 TQ2 OR OR TQ3)和TQ4)

目前我已經能夠建立它的格式如下

{+xml:food +(location:seattle location:portland location:spokane +location:city)} 

但是,我想獲得它的

{+xml:food +(+(location:seattle location:portland location:spokane) +location:city)} 

基本的格式,我做的是這樣的事情...

Lucene.Net.Search.BooleanQuery bq = new Lucene.Net.Search.BooleanQuery(); 
Lucene.Net.Search.BooleanQuery innerBQ = new Lucene.Net.Search.BooleanQuery(); 
bq.Add("xml:food", Lucene.Net.Search.BooleanClause.Occur.MUST); 
foreach (Lucene.Net.Index.Term term in listOfTerms) 
{ 
    Lucene.Net.Search.TermQuery tq = new Lucene.Net.Search.TermQuery(term)); 
    innerBQ.Add(tq, Lucene.Net.Search.BooleanClause.Occur.SHOULD); 
} 
innerBQ.Add("location:city", Lucene.Net.Search.BooleanClause.Occur.MUST); 
bq.Add(innerBQ, Lucene.Net.Search.BooleanClause.Occur.MUST); 

任何建議表示讚賞。

回答

0

您需要將location:city子句添加到bq代替innerBQ

+0

謝謝。只需要更多地注視它,並瞭解這些布爾查詢如何組裝。 – Kevin