For reference, if anyone else encounters this problem, here is my solution:
IList<string> ALL_TYPES = new[] { "article", "blog", "forum" };
string q = ...;
IList<string> includeTypes = ...;
Query searchQuery = parser.Parse(q);
Query parentQuery = new BooleanQuery();
parentQuery.Add(searchQuery, BooleanClause.Occur.SHOULD);
foreach (var type in ALL_TYPES.Except(includeTypes))
{
query.Add(
new TermQuery(new Term("type", type)),
BooleanClause.Occur.MUST_NOT
);
}
searchQuery = parentQuery;
I inverted the logic (i.e., excluded types that the user did not select), because if you do not order the results, it is lost. I don’t know why, though ...! This is a shame as it makes the code less comprehensible / maintainable, but at least it works!
source
share