• (转)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)) ;
  • 相关阅读:
    ES7/ES8 语法学习
    JavaScript中this对象原理简洁说明
    浅谈http协议
    各个浏览器之间常见的兼容性问题
    npm -v 报错:cannot find module 'core-util-is'
    对象遍历的几种方法
    Vue项目如何关闭Eslint检测
    axios 基本运用
    如何去掉vue路由中的#
    vue 父子组件、兄弟组件传值
  • 原文地址:https://www.cnblogs.com/servant/p/3026418.html
Copyright © 2020-2023  润新知