2011-02-16 111 views
2

中提升字段或文档没有效果我试图让工作得到提升,所以我可以提高文档和/或字段以使搜索结果符合我的喜好。在Lucene.Net

但是,我无法使助推文档或字段在得分上完全没有任何影响。

Lucene.Net boosting不起作用(不太可能)或者我误解了某些东西(很可能)。

这是我从外表到最基本展示代码:

using System; 
using System.Collections.Generic; 
using Lucene.Net.Analysis; 
using Lucene.Net.Documents; 
using Lucene.Net.Index; 
using Lucene.Net.QueryParsers; 
using Lucene.Net.Search; 

namespace SO_LuceneTest 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 

     const string INDEXNAME = "TextIndex"; 

     var writer = new IndexWriter(INDEXNAME, new SimpleAnalyzer(), true); 
     writer.DeleteAll(); 

     var persons = new Dictionary<string, string> 
          { 
          { "Smithers", "Jansen" }, 
          { "Jan", "Smith" } 
          }; 

     foreach (var p in persons) 
     { 
      var doc = new Document(); 
      var firstnameField = new Field("Firstname", p.Key, Field.Store.YES, Field.Index.ANALYZED); 
      var lastnameField = new Field("Lastname", p.Value, Field.Store.YES, Field.Index.ANALYZED); 
      //firstnameField.SetBoost(2.0f); 
      doc.Add(firstnameField); 
      doc.Add(lastnameField); 
      writer.AddDocument(doc); 
     } 

     writer.Commit(); 
     writer.Close(); 

     var term = "jan*"; 
     var queryFields = new string[] { "Firstname", "Lastname" }; 

     var boosts = new Dictionary<string, float>(); 
     //boosts.Add("Firstname", 10); 

     QueryParser mqp = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_24, queryFields, new SimpleAnalyzer(), boosts); 

     var query = mqp.Parse(term); 

     IndexSearcher searcher = new IndexSearcher(INDEXNAME); 

     Hits hits = searcher.Search(query); 

     int results = hits.Length(); 
     Console.WriteLine("Found {0} results", results); 
     for (int i = 0; i < results; i++) 
     { 
      Document doc = hits.Doc(i); 
      Console.WriteLine("{0} {1}\t\t{2}", doc.Get("Firstname"), doc.Get("Lastname"), hits.Score(i)); 
     } 

     searcher.Close(); 

     Console.WriteLine("..."); 
     Console.Read(); 

    } 
} 
} 

我注释掉促进的两个实例。当包括在内时,得分与没有助推的情况仍然完全相同。

我在这里错过了什么?

我正在使用Lucene.Net v2.9.2.2,截至目前的最新版本。

+0

你还想知道吗? – Spikolynn 2012-07-16 13:20:52

回答

1

请尝试,如果这将工作,它对我来说,但你必须修改它,因为我有很多其他的代码,我不会在这篇文章中包括,除非必要。主要区别是使用topfieldcollector得到结果

 var dir = SimpleFSDirectory.Open(new DirectoryInfo(IndexPath)); 
     var ixSearcher = new IndexSearcher(dir, false); 
     var qp = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, f_Text, analyzer); 
     query = CleanQuery(query); 
     Query q = qp.Parse(query); 
     TopFieldCollector collector = TopFieldCollector.Create(
       new Sort(new SortField(null, SortField.SCORE, false), new SortField(f_Date, SortField.LONG, true)), 
       MAX_RESULTS, 
       false,   // fillFields - not needed, we want score and doc only 
       true,   // trackDocScores - need doc and score fields 
       true,   // trackMaxScore - related to trackDocScores 
       false); // should docs be in docId order? 
     ixSearcher.Search(q, collector); 
     TopDocs topDocs = collector.TopDocs(); 
     ScoreDoc[] hits = topDocs.ScoreDocs; 

     uint pageCount = (uint)Math.Ceiling((double)hits.Length/pageSize); 

     for (uint i = pageIndex * pageSize; i < (pageIndex + 1) * pageSize; i++) { 
      if (i >= hits.Length) { 
       break; 
      } 
      int doc = hits[i].Doc; 

      Content c = new Content { 
       Title = ixSearcher.Doc(doc).GetField(f_Title).StringValue(), 
       Text = FragmentOnOrgText(ixSearcher.Doc(doc).GetField(f_TextOrg).StringValue(), highligter.GetBestFragments(analyzer, ixSearcher.Doc(doc).GetField(f_Text).StringValue(), maxNumberOfFragments)), 
       Date = DateTools.StringToDate(ixSearcher.Doc(doc).GetField(f_Date).StringValue()), 
       Score = hits[i].Score 
      }; 

      rv.Add(c); 
     } 

     ixSearcher.Close();