I have a Lucene.net index with a search that applies multiple queries:
BooleanQuery filterQuery = new BooleanQuery();
if (searchDto.SubCategoryId != Guid.Empty)
{
TermQuery tq = new TermQuery(new Term("SubCategoryId", searchDto.SubCategoryId.ToString()));
filterQuery.Add(tq, Occur.MUST);
}
if (!string.IsNullOrEmpty(searchDto.SearchPhrase))
{
var parser = new MultiFieldQueryParser
(Version.LUCENE_30, new[] { "Title", "Description", "SubCategoryName", "LongDescription" }, analyzer);
var query = parseQuery(searchDto.SearchPhrase, parser);
filterQuery.Add(query, Occur.MUST);
}
topDocs = searcher.Search(filterQuery, null, hits_limit);
This works great. For instance, it would only match a subcategory if I pass it a subcategory Id. However, how do I formulate a query that would match not based on whether I send it a filter value, but based on whether an index record has a value for one of it's fields.
For instance, one of the lucene index record fields is IsBundle. Now, if IsBundle is true, I want the record if RelationshipId matches the searchDto.RelationshipId I send it. If IsBundle is false, I don't care about RelationshipId. So my end result would be some combination of records where the IsBundle is true and the RelationshipId matches my searchDto.RelationshipId and where IsBundle is false