另一个 Lucene.net 问题由一个极端新手提出.
Yet another Lucene.net question by an extreme newbie to it.
这一次,我发现了一个有趣的问题,即使用包含范围的查询并使用突出显示.
This time, I have found an interesting issue with using a query that contains a range and using highlighting.
我是凭记忆写的,所以请原谅任何语法错误.
I am writing this from memory, so please forgive any syntax errors.
我有一个假设的 Lucene 索引:
I have a hypothetical Lucene index of this:
---------------------------------------------------------
| date | text |
---------------------------------------------------------
| 1317809124 | a crazy block of text |
---------------------------------------------------------
| 1317809284 | programmers are crazy |
---------------------------------------------------------
** date is a unix timestamp
...它们已通过以下方式添加到索引中:
... and they have been added to the index via this:
Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document();
doc.Add(new Lucene.Net.Documents.Field("text", "some block of text", Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));
doc.Add(new Lucene.Net.Documents.Field("date", "some unix timestamp", Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.NOT_ANALYZED));
这就是我查询 Lucene 的方式:
This is how I am querying Lucene:
Lucene.Net.Analysis.Standard.StandardAnalyzer analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);
Lucene.Net.Search.IndexSearcher searcher = new Lucene.Net.Search.IndexSearcher(Lucene.Net.Store.FSDirectory.Open(_headlinesDirectory), true);
Lucene.Net.QueryParsers.QueryParser parser = new Lucene.Net.QueryParsers.QueryParser(Lucene.Net.Util.Version.LUCENE_29, "text", analyzer);
Lucene.Net.Search.Query query = parser.Parse(queryPhrase);
Lucene.Net.Search.Hits hits = searcher.Search(query);
// code highlighting
Lucene.Net.Highlight.Formatter formatter = new Lucene.Net.Highlight.SimpleHTMLFormatter("<span style="background:yellow;">","</span>");
Lucene.Net.Highlight.SimpleFragmenter fragmenter = new Lucene.Net.Highlight.SimpleFragmenter(50);
Lucene.Net.Highlight.QueryScorer scorer = new Lucene.Net.Highlight.QueryScorer(query);
Lucene.Net.Highlight.Highlighter highlighter = new Lucene.Net.Highlight.Highlighter(formatter, scorer);
highlighter.SetTextFragmenter(fragmenter);
for (int i = 0; i < hits.Length(); i++)
{
Lucene.Net.Documents.Document doc = hits.Doc(i);
Lucene.Net.Analysis.TokenStream stream = analyzer.TokenStream("", new StringReader(doc.Get("text")));
string highlightedText = highlighter.GetBestFragments(stream, doc.Get("text"), 1, "...");
Console.WriteLine("--> " + highlightedText);
}
这是我的查询示例:
crazy AND date:[1286273266 TO 32503680000]
查询时,它会找到疯狂"的所有结果,但不输出任何突出显示的文本.
When this is queried, it finds all the results for "crazy" but does not output any highlighted text.
当日期范围被删除并且您只需查询该术语时:
When the date range is removed and you simply query the term:
crazy
...这次突出显示正常工作.
... this time highlighting works properly.
在我的实现中我做错了什么,我应该寻找一个新的实现,或者这是一个可能会解决的已知问题.
Is there something I am doing wrong in my implementation, should I be looking at a new implementation, or is this a known issue with potentially a work around.
提前感谢stackeroverflow'ers :)
Thank you in advance stackeroverflow'ers :)
-- 编辑--
我已经实施了 LB 的建议(令人惊叹的顺便说一句!).我仍然不知道为什么会这样,因为我认为 Lucene 完全是巫术或编程巫术,但确实如此,我很高兴 :).
I have implemented the suggestions from LB (amazing btw!). I still have no idea why this works as I think Lucene is complete voodoo or programming witchcraft, but it does and I am happy :).
为了完整起见,这里是修改后的代码:
For completeness, here is the modified code:
Lucene.Net.Analysis.Standard.StandardAnalyzer analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);
Lucene.Net.Search.IndexSearcher searcher = new Lucene.Net.Search.IndexSearcher(Lucene.Net.Store.FSDirectory.Open(_headlinesDirectory), true);
Lucene.Net.QueryParsers.QueryParser parser = new Lucene.Net.QueryParsers.QueryParser(Lucene.Net.Util.Version.LUCENE_29, "text", analyzer);
// new line here
parser.SetMultiTermRewriteMethod(Lucene.Net.Search.MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
Lucene.Net.Search.Query query = parser.Parse(queryPhrase);
// new line here
Lucene.Net.Search.Query query2 = query.Rewrite(searcher.GetIndexReader());
Lucene.Net.Search.Hits hits = searcher.Search(query);
// code highlighting
Lucene.Net.Highlight.Formatter formatter = new Lucene.Net.Highlight.SimpleHTMLFormatter("<span style="background:yellow;">","</span>");
Lucene.Net.Highlight.SimpleFragmenter fragmenter = new Lucene.Net.Highlight.SimpleFragmenter(50);
// changed to use query2
Lucene.Net.Highlight.QueryScorer scorer = new Lucene.Net.Highlight.QueryScorer(query2);
Lucene.Net.Highlight.Highlighter highlighter = new Lucene.Net.Highlight.Highlighter(formatter, scorer);
highlighter.SetTextFragmenter(fragmenter);
for (int i = 0; i < hits.Length(); i++)
{
Lucene.Net.Documents.Document doc = hits.Doc(i);
Lucene.Net.Analysis.TokenStream stream = analyzer.TokenStream("", new StringReader(doc.Get("text")));
string highlightedText = highlighter.GetBestFragments(stream, doc.Get("text"), 1, "...");
Console.WriteLine("--> " + highlightedText);
}
如果可以,请告诉我我是否准确地实施了这些建议.
If you could, let me know if I have implemented the suggestions accurately.
首先调用QueryParser的
First invoke QueryParser's
SetMultiTermRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE)
方法,然后创建一个新查询为
method, then create a new query as
Query newQuery = query.Rewrite(indexReader);
现在您可以使用newQuery"进行搜索.
Now you can use "newQuery" to make your searches.
这篇关于Lucene.net 范围查询 + 突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!