索引中的每个项目都有一个名为TYPE
的字段。这是"Document", "Blog" or "Forum"
用户可以选择要搜索的TYPE,但是如果搜索“文档”,我需要添加范围查询,以便仅在"Document"
字段属于STARTDATE
字段时才返回 using (var searcher = new IndexSearcher(_directory, false))
{
var analyzer = new StandardAnalyzer(Version.LUCENE_29);
BooleanQuery booleanQuery = new BooleanQuery();
BooleanQuery innerTypeFilterQuery = new BooleanQuery();
long today = Convert.ToInt64(LuceneDate.Today().Value);
foreach (string filterItem in filterItems)
{
TermQuery filterItemQuery = new TermQuery(new Term("Type", filterItem));
innerTypeFilterQuery.Add(filterItemQuery, Occur.SHOULD);
if (filterItem == "Document")
{
innerTypeFilterQuery.Add(NumericRangeQuery.NewLongRange("StartDate", null, today, true, true), Occur.MUST);
}
}
if (innerTypeFilterQuery.Clauses.Count > 0)
{
booleanQuery.Add(innerTypeFilterQuery, Occur.MUST);
}
Sort sort = new Sort(new SortField(LuceneGeneric.IndexFields.StartDate, Lucene.Net.Search.SortField.LONG, true));
var hits = searcher.Search(booleanQuery, null, LuceneContentWidgetResult.HITS_LIMIT, sort).ScoreDocs;
var results = _mapLuceneContentWidgetSearchResultsToDataList(hits, searcher);
analyzer.Close();
searcher.Dispose();
return results;
}
类型有效范围
{{1}}
我的查询如下:
{+(Type:Document + StartDate:[* TO 20141022000000]类型:BlogPost类型:ForumQuestion类型:ForumIdea类型:ForumIdeaList类型:ForumDiscussion)}
答案 0 :(得分:1)
这取决于你想要达到的目标。
我们假设您有一份文件:
title=foo
date=100 // using numeric value for simplicity
这是两种查询类型的真值表(全部以BooleanQuery
形式完成)
query | doc match result
-------------------------------------------------------------------------------------
title:foo +date:[80 TO 150] | true (both matched)
title:foo +date:[10 TO 20] | false (date hasn't matched even though title matched)
title:boo +date:[80 TO 150] | true (date matched but title hasn't)
title:boo +date:[10 TO 20] | false (none matched)
-------------------------------------------------------------------------------------
title:foo date:[80 TO 150] | true (both matched)
title:foo date:[10 TO 20] | true (date hasn't matched but title matched)
title:boo date:[80 TO 150] | true (date matched but title hasn't)
title:boo date:[10 TO 20] | false (none matched)
如您所见,前4个表行的日期为MUST
,后4行的日期为SHOULD
。
Q>我不确定在哪里添加,我是否需要向innerTypeFilterQuery
添加另一个布尔查询
A>再次,请解释您想要的匹配结果类型,但如果上述真值表的任何结果满足您的要求,您可以将其添加到同一查询中。
编辑:我认为您的查询应如下所示:
+((Type:Document +StartDate:[* TO 20141022000000]) Type:BlogPost Type:ForumQuestion Type:ForumIdea Type:ForumIdeaList Type:ForumDiscussion)
所以是的,你需要一个单独的布尔查询来包装Document约束。