Lucene.Net 進行排序

摘要:Lucene.Net 進行排序

接下來我們將使用到 Sort  class 來針對搜尋的結果做出排序 

 

Lucene.Net 對於 Sort 的描述如下 : 

The fields used to determine sort order must be carefully chosen. Documents must contain a 
single term in such a field, and the value of the term should indicate the document's 
relative position in a given sort order. The field must be indexed, but should not be 
tokenized, and does not need to be stored (unless you happen to want it back with the 
rest of your document data). In other words

 

Step 1 : Build RAMDirectory :

 


public static RAMDirectory dir = new RAMDirectory();

 

 

Step 2 : Build Index : 

 

在這裡建立了十項產品 , 其產品 ID 為 1 ~ 10

 


private void BuildIndex()
    {
        IndexWriter iw = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_30), true, IndexWriter.MaxFieldLength.UNLIMITED);

        Document doc = new Document();
        doc.Add(new Field("PROD_ID", "", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));
        doc.Add(new Field("PROD_Name", "", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));

        for (int i = 1; i <= 1000; i++)
        {

            doc.GetField("PROD_ID").SetValue(i.ToString());
            doc.GetField("PROD_Name").SetValue("Lucene.Net" + i.ToString());
            iw.AddDocument(doc);
        }
        iw.Optimize();
        iw.Commit();
        iw.Close();
    }

 

Step 3 : Search : 

 

在這裡我們將對搜尋的結果做 DESC 的排序

 


SearchIndex("Lucene.Net*");


private void SearchIndex(string KeyWord)
    {

        IndexSearcher search = new IndexSearcher(dir, true);

        QueryParser qp = new QueryParser(Version.LUCENE_30, "PROD_Name", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30));

        Query query = qp.Parse(KeyWord);

        Sort sort = new Sort(new SortField("PROD_ID", 4,true));

        var hits = search.Search(query, null, search.MaxDoc,sort).ScoreDocs;

        foreach (var res in hits)
        {
            Response.Write(string.Format("PROD_ID:{0} / PROD_Name{1}", search.Doc(res.Doc).Get("PROD_ID").ToString()
                                        , search.Doc(res.Doc).Get("PROD_Name").ToString() + "
"));
        }
    }

 

 

Result : 

 


PROD_ID:10 / PROD_NameLucene.Net10
PROD_ID:9 / PROD_NameLucene.Net9
PROD_ID:8 / PROD_NameLucene.Net8
PROD_ID:7 / PROD_NameLucene.Net7
PROD_ID:6 / PROD_NameLucene.Net6
PROD_ID:5 / PROD_NameLucene.Net5
PROD_ID:4 / PROD_NameLucene.Net4
PROD_ID:3 / PROD_NameLucene.Net3
PROD_ID:2 / PROD_NameLucene.Net2
PROD_ID:1 / PROD_NameLucene.Net1

 

當我們在上面建構 SortField 時 , 我們加入了一個參數 4 , 其參數值範圍如下列所示 : 

 

const int  SCORE = 0
  Sort by document score (relevancy). Sort values are Float and higher values are at the front. 
 
const int  DOC = 1
  Sort by document number (index order). Sort values are Integer and lower values are at the front. 
 
const int  STRING = 3
  Sort using term values as Strings. Sort values are String and lower values are at the front. 
 
const int  INT = 4
  Sort using term values as encoded Integers. Sort values are Integer and lower values are at the front. 
 
const int  FLOAT = 5
  Sort using term values as encoded Floats. Sort values are Float and lower values are at the front. 
 
const int  LONG = 6
  Sort using term values as encoded Longs. Sort values are Long and lower values are at the front. 
 
const int  DOUBLE = 7
  Sort using term values as encoded Doubles. Sort values are Double and lower values are at the front. 
 
const int  SHORT = 8
  Sort using term values as encoded Shorts. Sort values are Short and lower values are at the front. 
 
const int  CUSTOM = 9
  Sort using a custom Comparator. Sort values are any Comparable and sorting is done according to natural order. 
 
const int  BYTE = 10
  Sort using term values as encoded Bytes. Sort values are Byte and lower values are at the front. 
 
const int  STRING_VAL = 11
  Sort using term values as Strings, but comparing by value (using String.compareTo) for all comparisons. This is typically slower than STRING, which uses ordinals to do the sorting. 

 

資料來源 : 

Lucene.Net  SortField  API

Lucene.Net Sort API