2011-02-16 110 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();