如何在Lucene.net中使用多字段查询和过滤器

时间:2010-02-02 17:31:08

标签: filter lucene.net

我想在lucene.net索引上执行多字段搜索,但是根据其中一个字段过滤结果。这就是我目前正在做的事情:

索引定义为的字段:

doc.Add(new Field("id", id.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
doc.Add(new Field("title", title, Field.Store.NO, Field.Index.TOKENIZED));
doc.Add(new Field("summary", summary, Field.Store.NO, Field.Index.TOKENIZED, Field.TermVector.YES));
doc.Add(new Field("description", description, Field.Store.NO, Field.Index.TOKENIZED, Field.TermVector.YES));
doc.Add(new Field("distribution", distribution, Field.Store.NO, Field.Index.UN_TOKENIZED));

当我执行搜索时,我会执行以下操作:

MultiFieldQueryParser parser = new MultiFieldQueryParser(new string[]{"title", "summary", "description"}, analyzer);
parser.SetDefaultOperator(QueryParser.Operator.AND);
Query query = parser.Parse(text);

BooleanQuery bq = new BooleanQuery();
TermQuery tq = new TermQuery(new Term("distribution", distribution));
bq.Add(tq, BooleanClause.Occur.MUST);
Filter filter = new QueryFilter(bq);

Hits hits = searcher.Search(query, filter);

然而,结果总是0次点击。

我做错了什么?

1 个答案:

答案 0 :(得分:6)

我想我现在有了解决方案。我已经放弃使用QueryFilter并使用布尔查询来限制MultiFieldQuery之前的结果。所以代码看起来像这样:

MultiFieldQueryParser parser = new MultiFieldQueryParser(new string[]{"title", "summary", "description"}, analyzer); 
parser.SetDefaultOperator(QueryParser.Operator.AND); 
Query query = parser.Parse(text); 

BooleanQuery bq = new BooleanQuery(); 
TermQuery tq = new TermQuery(new Term("distribution", distribution)); 
bq.Add(tq, BooleanClause.Occur.MUST); 
bq.Add(query, BooleanClause.Occur.MUST)

Hits hits = searcher.Search(bq);