Lucene.net:如何用多个术语控制操作顺序

时间:2012-12-12 01:51:00

标签: 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或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);

任何建议都表示赞赏。

1 个答案:

答案 0 :(得分:0)

您需要将location:city条款添加到bq而不是innerBQ