• (转)Lucene.net实现自定义排序


    本文转自:http://www.cnblogs.com/peida/archive/2008/11/27/1341920.html

    在Lucene.net实现自定义排序,需要实现两个Lucene.Net.Search的两个接口:

     1 public interface SortComparatorSource
     2 {
     3    ScoreDocComparator NewComparator(IndexReader reader , System.String fieldname) ;
     4 }
     5 
     6 public interface ScoreDocComparator
     7 {
     8    int Compare(ScoreDoc i , ScoreDoc j) ;
     9    System.IComparable SortValue(ScoreDoc i) ;
    10    int SortType() ;
    11 }

    涉及到的一个类:

     1 public class ScoreDoc
     2 {
     3    public float score ;
     4    public int doc ;
     5    public ScoreDoc(int doc , float score)
     6    {
     7       this.doc = doc ;
     8       this.score = score ;
     9    }
    10 }

    Lucene.net 2.0包含的SortType有:
    在Lucene.Net.Search.SortField里定义的:



    public class SortField
    {
       public const int SCORE = 0 ;  //相关度
       public const int DOC = 1 ;    //文挡号
       public const int AUTO = 2 ;   //自动识别
       public const int STRING = 3 ; //字符型
       public const int INT = 4 ;    //int
       public const int FLOAT = 5 ;  //float
       public const int CUSTOM = 9 ; //自定义
       ..
    }

    少了DateTime,那就实现DateTime类型的自定义排序来测试下:
    Lucene.Net.Search.ScoreDocComparator接口的实现类:

     1     public class DateDocComparator : Lucene.Net.Search.ScoreDocComparator 
     2     {
     3         private string fieldname = null;
     4         private System.IComparable[] cachedValues ;
     5 
     6         public DateDocComparator(System.IComparable[] cachedValues, string fieldname)
     7         {
     8             this.cachedValues = cachedValues;
     9             this.fieldname = string.Intern(fieldname) ;
    10         }
    11 
    12         public int Compare(ScoreDoc i, ScoreDoc j)
    13         {
    14             return this.cachedValues[i.doc].CompareTo(this.cachedValues[j.doc]) ;
    15         }
    16 
    17         public System.IComparable SortValue(ScoreDoc i)
    18         {
    19             return this.cachedValues[i.doc] ;
    20         }
    21 
    22         public int SortType()
    23         {
    24             return Lucene.Net.Search.SortField.CUSTOM ;
    25         }
    26     }

    Lucene.Net.Search.SortComparatorSource接口的实现类:

     1 public class DateSortComparatorSource : Lucene.Net.Search.SortComparatorSource
     2     {
     3         public ScoreDocComparator NewComparator(Lucene.Net.Index.IndexReader reader, System.String field)
     4         {
     5             return new DateDocComparator(GetCustom(reader, field), field);
     6         }
     7 
     8         protected virtual System.IComparable[] GetCustom(Lucene.Net.Index.IndexReader reader, System.String field)
     9         {
    10                 System.IComparable[] retArray = new System.IComparable[reader.MaxDoc()];
    11                 Lucene.Net.Index.TermDocs termDocs = reader.TermDocs();
    12                 Lucene.Net.Index.TermEnum termEnum = reader.Terms(new Lucene.Net.Index.Term(field, ""));
    13                 try
    14                 {
    15                     do
    16                     {
    17                         Lucene.Net.Index.Term term = termEnum.Term();
    18                         if (term == null || term.Field() != field)
    19                             break;
    20                         System.IComparable termval = Lucene.Net.Documents.DateTools.StringToDate(term.Text()) ;
    21                         termDocs.Seek(termEnum);
    22                         while (termDocs.Next())
    23                         {
    24                             retArray[termDocs.Doc()] = termval;
    25                         }
    26                     }
    27                     while (termEnum.Next());
    28                 }
    29                 finally
    30                 {
    31                     termDocs.Close();
    32                     termEnum.Close();
    33                 }
    34                 return retArray;
    35         }
    36     }

    使用:

    1 Sort sort = new Sort(new SortField("datecreated",new DateSortComparatorSource(),true)) ;
  • 相关阅读:
    Druid连接池的简单使用
    JDBC工具包commons-dbutils的基本介绍
    Java IO: ByteArrayOutputStream使用
    开源数据库连接池之Tomcat内置连接池
    jquery里面的$(this)和this的区别
    css自定义属性(css变量)
    CSS选择器
    IntelliJ IDEA 学习笔记
    JSP基本的语法、3个编译指令、7个动作指令、9个内置对象
    CSS3 box-shadow
  • 原文地址:https://www.cnblogs.com/servant/p/3026418.html
Copyright © 2020-2023  润新知