[英]Lucene Nested Boolean equivalent to OR
Each item in my index has a field called TYPE
. 我索引中的每个项目都有一个名为
TYPE
的字段。 This is either "Document", "Blog" or "Forum"
这是
"Document", "Blog" or "Forum"
Users can can choose which TYPE's they want to search for, but if searching for "Document" I need to add a range query so that "Document"
types are only returned if its STARTDATE
field falls into an a valid range 用户可以选择要搜索的TYPE,但是如果要搜索“文档”,我需要添加一个范围查询,以便仅在其“
STARTDATE
字段处于有效范围内时才返回"Document"
类型。
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;
}
My query looks like this: 我的查询如下所示:
{+(Type:Document +StartDate:[* TO 20141022000000] Type:BlogPost Type:ForumQuestion Type:ForumIdea Type:ForumIdeaList Type:ForumDiscussion)} {+(Type:Document + StartDate:[* TO 20141022000000] Type:BlogPost Type:ForumQuestion Type:ForumIdea Type:ForumIdeaList Type:ForumDiscussion)}
It depends what you want to achieve. 这取决于您要实现的目标。
Let's say you have a document: 假设您有一个文档:
title=foo
date=100 // using numeric value for simplicity
Here's your truth table for both types of queries (all done as a single BooleanQuery
) 这是这两种查询的真值表(全部作为一个
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)
As you can see, date is MUST
for the first 4 table rows and SHOULD
for the bottom 4 rows. 如您所见,表的前4行
MUST
是日期,后4行SHOULD
是日期。
Q> Im unsure where to add, do i need to add another boolean query to innerTypeFilterQuery
Q>不确定在哪里添加,是否需要向
innerTypeFilterQuery
添加另一个布尔查询
A> Again, please explain what kind of matching result you want but if any result from the above truth table satisfies your requirements, you can add it to the same query. A>请再次说明您想要哪种匹配结果,但是如果上述真值表中的任何结果满足您的要求,则可以将其添加到同一查询中。
Edit: I think your query should look like as this: 编辑:我认为您的查询应如下所示:
+((Type:Document +StartDate:[* TO 20141022000000]) Type:BlogPost Type:ForumQuestion Type:ForumIdea Type:ForumIdeaList Type:ForumDiscussion)
So yes, you will need a separate boolean query to wrap both Document constraints. 因此,是的,您将需要一个单独的布尔查询来包装两个Document约束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.