2012-08-13 39 views
2

我正在測試排序功能在lucene中沒有運氣。我對它很陌生。 我試過使用TopFieldCollector或TopFieldDocs,但似乎沒有應用排序。 下面是測試代碼。它出什麼問題了?對lucene進行排序得分

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 

回答

1

的問題是,您要添加的「編號」字段作爲字符串,然後嘗試將其排序爲整數。您應該將它作爲整數(使用NumericField)添加或按字符串排序(但要注意它將按字典順序排序)。

+0

我也嘗試作爲NumericField(你看到在addDoc函數中有一個註釋行)和排序爲整數,但它仍然無法正常工作。當我使用NumericField時,構造函數不允許使用Field.Index.NOT_ANALYZED(它只接受「false」)。這可能是問題嗎? – Daniele 2012-08-14 18:11:26

+0

您應該使用「true」作爲最後的NumericField構造函數參數。這將導致字段被索引,這是需要排序的。 – jpountz 2012-08-18 09:19:45

相關問題