• <tfoot id='qCQcH'></tfoot>

      <small id='qCQcH'></small><noframes id='qCQcH'>

        <bdo id='qCQcH'></bdo><ul id='qCQcH'></ul>
    1. <legend id='qCQcH'><style id='qCQcH'><dir id='qCQcH'><q id='qCQcH'></q></dir></style></legend>
      <i id='qCQcH'><tr id='qCQcH'><dt id='qCQcH'><q id='qCQcH'><span id='qCQcH'><b id='qCQcH'><form id='qCQcH'><ins id='qCQcH'></ins><ul id='qCQcH'></ul><sub id='qCQcH'></sub></form><legend id='qCQcH'></legend><bdo id='qCQcH'><pre id='qCQcH'><center id='qCQcH'></center></pre></bdo></b><th id='qCQcH'></th></span></q></dt></tr></i><div id='qCQcH'><tfoot id='qCQcH'></tfoot><dl id='qCQcH'><fieldset id='qCQcH'></fieldset></dl></div>

        Lucene.net 范围查询 + 突出显示

        时间:2023-09-16
            <tbody id='NVHy9'></tbody>

        • <i id='NVHy9'><tr id='NVHy9'><dt id='NVHy9'><q id='NVHy9'><span id='NVHy9'><b id='NVHy9'><form id='NVHy9'><ins id='NVHy9'></ins><ul id='NVHy9'></ul><sub id='NVHy9'></sub></form><legend id='NVHy9'></legend><bdo id='NVHy9'><pre id='NVHy9'><center id='NVHy9'></center></pre></bdo></b><th id='NVHy9'></th></span></q></dt></tr></i><div id='NVHy9'><tfoot id='NVHy9'></tfoot><dl id='NVHy9'><fieldset id='NVHy9'></fieldset></dl></div>

          <small id='NVHy9'></small><noframes id='NVHy9'>

            • <bdo id='NVHy9'></bdo><ul id='NVHy9'></ul>
              <legend id='NVHy9'><style id='NVHy9'><dir id='NVHy9'><q id='NVHy9'></q></dir></style></legend>

                  <tfoot id='NVHy9'></tfoot>
                  本文介绍了Lucene.net 范围查询 + 突出显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  另一个 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 范围查询 + 突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Lucene.Net 是否管理访问同一索引的多个线程,一个索引而另一个正在搜索? 下一篇:C# Lucene 获取所有索引

                  相关文章

                1. <small id='7Xa9y'></small><noframes id='7Xa9y'>

                    <tfoot id='7Xa9y'></tfoot>
                    • <bdo id='7Xa9y'></bdo><ul id='7Xa9y'></ul>

                      <legend id='7Xa9y'><style id='7Xa9y'><dir id='7Xa9y'><q id='7Xa9y'></q></dir></style></legend>
                      <i id='7Xa9y'><tr id='7Xa9y'><dt id='7Xa9y'><q id='7Xa9y'><span id='7Xa9y'><b id='7Xa9y'><form id='7Xa9y'><ins id='7Xa9y'></ins><ul id='7Xa9y'></ul><sub id='7Xa9y'></sub></form><legend id='7Xa9y'></legend><bdo id='7Xa9y'><pre id='7Xa9y'><center id='7Xa9y'></center></pre></bdo></b><th id='7Xa9y'></th></span></q></dt></tr></i><div id='7Xa9y'><tfoot id='7Xa9y'></tfoot><dl id='7Xa9y'><fieldset id='7Xa9y'></fieldset></dl></div>