提升字段或文档对Lucene.Net没有影响

时间:2011-02-16 19:22:13

标签: c# lucene.net

我正在努力提高工作效率,因此我可以提升文档和/或字段来制作我喜欢的搜索结果。

但是,我无法提升文档或字段对得分有任何影响。

Lucene.Net提升不起作用(不太可能)或我误解了某些事情(非常可能)。

以下是我的简要展示代码:

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,这是目前的最新版本。

1 个答案:

答案 0 :(得分: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();