【问题标题】:Sort lucene over score在分数上排序 lucene
【发布时间】:2012-08-13 12:41:55
【问题描述】:

我正在测试 lucene 中的排序功能,但没有运气。我是新手。 我尝试使用 TopFieldCollector 或 TopFieldDocs,但似乎没有应用排序。 下面是测试代码。它出什么问题了?

private void testNumericSorting(){
    // 1. index some data
    StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);
    Directory index = new RAMDirectory();

    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_35, analyzer);
    IndexWriter w = new IndexWriter(index, config);
    addDoc(w, "orange", 1);
    addDoc(w, "lemon orange", 10);
    w.close();

    // 2. query
    String querystr = "orange";
    Query q = new QueryParser(Version.LUCENE_35, "title", analyzer).parse(querystr);

    int hitsPerPage = 10;
    IndexSearcher searcher = new IndexSearcher(index, true);
    // Normal score, with no sorting
    //TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
    //searcher.search(q, collector);
    //ScoreDoc[] hits = collector.topDocs().scoreDocs;

    // Score with TopFieldCollector
    Sort sort = new Sort(new SortField[] {
                                SortField.FIELD_SCORE,
                                new SortField("num", SortField.INT) });  
    TopFieldCollector topField = TopFieldCollector.create(sort, hitsPerPage, true, true, true, false);
    searcher.search(q, topField);   
    ScoreDoc[] sortedHits = topField.topDocs().scoreDocs; 

    // Score with TopFieldDocs
    // TopFieldDocs topFields =  searcher.search(q, null, hitsPerPage, sort);
    // ScoreDoc[] sortedHits = topFields.scoreDocs; 

    System.out.println("Found " + sortedHits.length + " hits.");
    for(int i=0;i<sortedHits.length;++i) {
       int docId = sortedHits[i].doc;
       float score = sortedHits[i].score;
       Document d = searcher.doc(docId);
       System.out.println((i + 1) + ". " + d.get("title")+ " score:"+score);
    }

         searcher.close();
    }
     private static void addDoc(IndexWriter w, String value, Integer num) throws IOException {
        Document doc = new Document();
        doc.add(new Field("title", value, Field.Store.YES, Field.Index.ANALYZED));
        //doc.add(new NumericField("num", Field.Store.NO, false).setIntValue(num));
        doc.add(new Field ("num", Integer.toString(num), Field.Store.NO, Field.Index.NOT_ANALYZED));
        w.addDocument(doc);
    }

如果打印结果有和没有排序,我会得到以下输出(基本上没有变化):

Without sorting, found 2 hits.
1. orange score:0.5945348
2. lemon orange score:0.37158427

With sorting, found 2 hits.
1. orange score:0.5945348
2. lemon orange score:0.37158427

【问题讨论】:

    标签: sorting lucene numeric


    【解决方案1】:

    问题是您将“num”字段添加为字符串,然后尝试将其排序为整数。您应该将其添加为整数(使用 NumericField)或排序为字符串(但请注意,它将根据字典顺序进行排序)。

    【讨论】:

    • 我也尝试过作为 NumericField (您是否看到 addDoc 函数中有注释行)并将排序为整数,但它仍然不起作用。当我使用 NumericField 时,构造函数不允许我使用 Field.Index.NOT_ANALYZED (它只接受“false”)。可能是这个问题吗?
    • 您应该使用“true”作为最后一个 NumericField 构造函数参数。这将导致字段被索引,这是排序所必需的。
    猜你喜欢
    • 2017-07-09
    • 1970-01-01
    • 1970-01-01
    • 2011-01-20
    • 2010-09-05
    • 1970-01-01
    • 2013-12-30
    • 2016-10-22
    • 1970-01-01
    相关资源
    最近更新 更多