我正在使用基于 Lucene.net 的搜索引擎开发 EPiServer 网站。
我有一个查询,只查找具有特定 pageTypeId 的页面。现在我想做相反的事情,我只想找到不是某个 pageTypeId 的页面。那可能吗?
这是创建查询以仅搜索 pageTypeId 为 1、2 或 3 的页面的代码:
public BooleanClause GetClause()
{
var booleanQuery = new BooleanQuery();
var typeIds = new List<string>();
typeIds.Add("1");
typeIds.Add("2");
typeIds.Add("3");
foreach (var id in this.typeIds)
{
var termQuery = new TermQuery(
new Term(IndexFieldNames.PageTypeId, id));
var clause = new BooleanClause(termQuery,
BooleanClause.Occur.SHOULD);
booleanQuery.Add(clause);
}
return new BooleanClause(booleanQuery,
BooleanClause.Occur.MUST);
}
相反,我想创建一个查询,在其中搜索 pageTypeId 不是“4”的页面。
我尝试用“MUST_NOT”简单地替换“应该”和“必须”,但这没有用。
感谢@goalie7960 如此迅速地回复。这是我修改后的代码,用于搜索除某些选定页面类型之外的任何内容。此搜索包括除 pageTypeId 为“1”、“2”或“3”的文档之外的所有文档:
public BooleanClause GetClause()
{
var booleanQuery = new BooleanQuery();
booleanQuery.Add(new MatchAllDocsQuery(),
BooleanClause.Occur.MUST);
var typeIds = new List<string>();
typeIds.Add("1");
typeIds.Add("2");
typeIds.Add("3");
foreach (var typeId in this.typeIds)
{
booleanQuery.Add(new TermQuery(
new Term(IndexFieldNames.PageTypeId, typeId)),
BooleanClause.Occur.MUST_NOT);
}
return new BooleanClause(booleanQuery,
BooleanClause.Occur.MUST);
}