惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threatpost
aimingoo的专栏
aimingoo的专栏
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
Tailwind CSS Blog
J
Java Code Geeks
博客园_首页
Google Online Security Blog
Google Online Security Blog
Hugging Face - Blog
Hugging Face - Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
I
Intezer
P
Palo Alto Networks Blog
V
Vulnerabilities – Threatpost
雷峰网
雷峰网
O
OpenAI News
SecWiki News
SecWiki News
小众软件
小众软件
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
N
News | PayPal Newsroom
Project Zero
Project Zero
Forbes - Security
Forbes - Security
IT之家
IT之家
A
Arctic Wolf
WordPress大学
WordPress大学
Jina AI
Jina AI
T
Tor Project blog
博客园 - 三生石上(FineUI控件)
S
Secure Thoughts
Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
博客园 - 聂微东
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Privacy International News Feed
Cloudbric
Cloudbric
G
GRAHAM CLULEY
博客园 - 叶小钗
H
Hacker News: Front Page
腾讯CDC
量子位
Help Net Security
Help Net Security
人人都是产品经理
人人都是产品经理
C
Cyber Attacks, Cyber Crime and Cyber Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
宝玉的分享
宝玉的分享
爱范儿
爱范儿
L
Lohrmann on Cybersecurity
Hacker News - Newest:
Hacker News - Newest: "LLM"
Recorded Future
Recorded Future
C
CERT Recently Published Vulnerability Notes

博客园 - kwklover

给同为.NET开发者普及一点Oracle数据库经验 使用mencoder转换flv为ipad/iphone下能播放的mp4格式 Lucene.net常见功能实现知识汇总 Lucene 1.9 多目录搜索的的一个bug 众里寻他千百度,蓦然回首,那人却在灯火阑珊处 问题总结:判断MS SQLSERVER临时表是否存在 小技巧:处理ASP提交的参数是经过GB2312 URL编码的 模版引擎AderTemplate源代码分析笔记 T-SQL复习总结--用T-SQL创建,修改,管理,删除数据库 面向搜索的中文分词设计 需要整理研究的搜索引擎技术点(目录,无实际价值) 试用了一下Sqlite,总结和整理一下参考资料 数据结构与算法学习记录:快速排序 小总结:DotLucene如何才能快速生成索引? 小总结:如何表达用户是否禁止的概念 ? Web Spider提取编码方法总结 WebSpider的编码问题(乱码)浅析 VS2005 Winform程序不能启动调试,别忘了启动Terminal Services服务[记录] 系统问题解决记录:IIS 500内部错误之解决办法
Lucene.net实现自定义排序笔记
kwklover · 2007-07-28 · via 博客园 - kwklover

在Lucene.net实现自定义排序,需要实现两个Lucene.Net.Search的两个接口:
public interface SortComparatorSource
{
   ScoreDocComparator NewComparator(IndexReader reader , System.String fieldname) ;
}

public interface ScoreDocComparator
{
   int Compare(ScoreDoc i , ScoreDoc j) ;
   System.IComparable SortValue(ScoreDoc i) ;
   int SortType() ;
}

涉及到的一个类:
public class ScoreDoc
{
   public float score ;
   public int doc ;
   public ScoreDoc(int doc , float score)
   {
      this.doc = doc ;
      this.score = score ;
   }
}

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接口的实现类:
    public class DateDocComparator : Lucene.Net.Search.ScoreDocComparator
    {
        private string fieldname = null;
        private System.IComparable[] cachedValues ;

        public DateDocComparator(System.IComparable[] cachedValues, string fieldname)
        {
            this.cachedValues = cachedValues;
            this.fieldname = string.Intern(fieldname) ;
        }

        public int Compare(ScoreDoc i, ScoreDoc j)
        {
            return this.cachedValues[i.doc].CompareTo(this.cachedValues[j.doc]) ;
        }

        public System.IComparable SortValue(ScoreDoc i)
        {
            return this.cachedValues[i.doc] ;
        }

        public int SortType()
        {
            return Lucene.Net.Search.SortField.CUSTOM ;
        }
    }

Lucene.Net.Search.SortComparatorSource接口的实现类:
    public class DateSortComparatorSource : Lucene.Net.Search.SortComparatorSource
    {
        public ScoreDocComparator NewComparator(Lucene.Net.Index.IndexReader reader, System.String field)
        {
            return new DateDocComparator(GetCustom(reader, field), field);
        }

        protected virtual System.IComparable[] GetCustom(Lucene.Net.Index.IndexReader reader, System.String field)
        {
                System.IComparable[] retArray = new System.IComparable[reader.MaxDoc()];
                Lucene.Net.Index.TermDocs termDocs = reader.TermDocs();
                Lucene.Net.Index.TermEnum termEnum = reader.Terms(new Lucene.Net.Index.Term(field, ""));
                try
                {
                    do
                    {
                        Lucene.Net.Index.Term term = termEnum.Term();
                        if (term == null || term.Field() != field)
                            break;
                        System.IComparable termval = Lucene.Net.Documents.DateTools.StringToDate(term.Text()) ;
                        termDocs.Seek(termEnum);
                        while (termDocs.Next())
                        {
                            retArray[termDocs.Doc()] = termval;
                        }
                    }
                    while (termEnum.Next());
                }
                finally
                {
                    termDocs.Close();
                    termEnum.Close();
                }
                return retArray;
        }
    }

使用:
Sort sort = new Sort(new SortField("datecreated",new DateSortComparatorSource(),true)) ;