How to use multifieldquery and filters in Lucene.net

I want to do a multi-user search on the lucene.net index, but filter the results based on one of the fields. Here is what I am doing now:

To index fields, the following definitions:

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));

When I do a search, I do the following:

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);

However, the result is always 0 beats.

What am I doing wrong?

+5
source share
1 answer

I think now I have a solution. I gave up using QueryFilter and use a boolean query to limit the results to MultiFieldQuery. Thus, the code will look something like this:

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); 
+6
source

All Articles