lucene 布尔查询与 TermQuery?
我得到了以下代码:
Lucene.Net.Analysis.Analyzer analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);
var mainQuery = new Lucene.Net.Search.BooleanQuery();
foreach (var str in fields)
{
var parser = new Lucene.Net.QueryParsers.QueryParser(Lucene.Net.Util.Version.LUCENE_29, str, analyzer);
mainQuery.Add(parser.Parse(search +"*"), Lucene.Net.Search.BooleanClause.Occur.SHOULD);
}
Lucene.Net.Search.TopScoreDocCollector collector = Lucene.Net.Search.TopScoreDocCollector.create(21, true);
searcher.Search(mainQuery, collector);
hits = collector.TopDocs().scoreDocs;
生活很美好。 直到我注意到,我得到的结果也是 NotActive
。 所以我对自己说,好吧,没问题,让我们在 for 循环之后添加另一个查询到 mainQuery 就像这样:
var parser2 = new Lucene.Net.QueryParsers.QueryParser(Lucene.Net.Util.Version.LUCENE_29, "StateProperties.IsActive", analyzer);
mainQuery.Add(parser2.Parse("True"), Lucene.Net.Search.BooleanClause.Occur.MUST);
不幸的是,这不起作用。 任何人都可以指出我这样做的正确方向吗? 我读过有关过滤器的内容,甚至设法应用了一个过滤器,但后来我失去了分数(好吧,并不是真正失去了,但它们与原始查询相差甚远)。 我还阅读了有关 TermQueryWrapper 的内容,但找不到如何实现它(所以我不知道这是否是我所需要的,即使它似乎是正确的方向)。
编辑:我忘了提及,fields
中的所有字段都已分析, StateProperties.IsActive
为 NOT_ANALYZED
。
i got the following code:
Lucene.Net.Analysis.Analyzer analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);
var mainQuery = new Lucene.Net.Search.BooleanQuery();
foreach (var str in fields)
{
var parser = new Lucene.Net.QueryParsers.QueryParser(Lucene.Net.Util.Version.LUCENE_29, str, analyzer);
mainQuery.Add(parser.Parse(search +"*"), Lucene.Net.Search.BooleanClause.Occur.SHOULD);
}
Lucene.Net.Search.TopScoreDocCollector collector = Lucene.Net.Search.TopScoreDocCollector.create(21, true);
searcher.Search(mainQuery, collector);
hits = collector.TopDocs().scoreDocs;
and life was good.
until i noticed, that i get results that are also NotActive
.
so i said to myself, ok , no problem, lets add another query to the mainQuery just after the for loop
like so:
var parser2 = new Lucene.Net.QueryParsers.QueryParser(Lucene.Net.Util.Version.LUCENE_29, "StateProperties.IsActive", analyzer);
mainQuery.Add(parser2.Parse("True"), Lucene.Net.Search.BooleanClause.Occur.MUST);
unfortunally, this does not work.
anyone can point me to the right direction of doing this ?
i have read about filters, and even managed to apply one, but then i lose the scores ( well, not really lose, but they are way off the original query ).
i also read about TermQueryWrapper
, but could not find how to implement that ( so i dont know if thats what i need, even due it seems to be the right direction ).
EDIT: i forgot to mention, all the fields in fields
are ANALYZED,
the StateProperties.IsActive
is NOT_ANALYZED
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论