views:

12

answers:

1

I'm having an issue with Lucene.Net and a BooleanQuery. This is my code:

 BooleanQuery query = new BooleanQuery();

 String[] types = searchTypes.Split(',');

 foreach (string t in types)
      query.Add(new TermQuery(new Term("document type", t.ToLower())), BooleanClause.Occur.SHOULD);

This should basically be an OR statement going through documents that have a certain type, which works on its own. However, I also have this query:

 Query documentTitleQuery = new WildcardQuery(new Term("title", "*" + documentTitle.ToLower() + "*"));
 query.Add(documentTitleQuery, BooleanClause.Occur.MUST);

Which searches for words in a title. Both of these queries work find on their own. When they are used together, it seems Lucene is treating the documentTitleQuery as an OR. So both queries together should return documents of a specific type AND contain specific words in the title, but it is returning all types that have specific words in the title.

+2  A: 

Use one more layer of Boolean query to group both:

BooleanQuery topQuery = new BooleanQuery();
...
BooleanQuery query1 = new BooleanQuery();
...
BooleanQuery query2 = new BooleanQuery();
...
topQuery.add(query1, BooleanClause.Occur.MUST);
topQuery.add(query2, BooleanClause.Occur.MUST);
Dewfy
Thanks, totally worked!