Reputation: 746
I'm using the latest (beta) version of Lucene.NET 4.8.
I have a pretty tricky access system, and I need my search engine to respect it. Hence, I need to filter my search queries by documents having or not a field. The field in question is a "tag" field (aka a document can have multiple such fields), and we consider a document "private" if it have no "tag" fields assigned. So I don't know in advance what content that field will have.
The only way I found so far is to use:
var filterQuery = new WildcardQuery(new Term("tag", "?*"));
Although using a wildcard query matching at least one symbol seems to me kind of overkill - even if it actually seems to be working just fine.
Is there a better solution without reindexing and introducing a new field?
Upvotes: 3
Views: 412
Reputation: 33917
I think you want to be able to filter based on whether a document has a field or not. I don’t believe this feature exists in Lucene. I’ve seen this question come up before on StackOverflow.
In Lucene a field points to a list of terms and the terms point to a list of docs. So finding docs that are not in any of the term lists for a field isn’t something Lucene is really designed for.
A better way is to add another field that all docs have and set it to true or false based on whether the field in question exists or not. Then query on that field, potentially in combination with other fields. For the later you can use a BooleanQuery
.
Anyway, that's my take. I hope it helps.
Upvotes: 3