`

lucene 学习笔记-排序

阅读更多
1.排序

Lucene有内置的排序用IndexSearcher.search(query,sort)但是功能并不理想。我们需要自己实现自定义的排序。

这样的话得实现两个接口: ScoreDocComparator, SortComparatorSource

用IndexSearcher.search(query,new Sort(new SortField(String Field,SortComparatorSource)));

就看个例子吧:

这是一个建立索引的例子:
public void IndexSort() throws IOException { 

        IndexWriter writer = new IndexWriter("C:\\\\indexStore",new StandardAnalyzer(),true); 

        Document doc = new Document(); 

        doc.add(new Field("sort","1",Field.Store.YES,Field.Index.TOKENIZED)); 

        writer.addDocument(doc); 

        doc = new Document(); 

        doc.add(new Field("sort","4",Field.Store.YES,Field.Index.TOKENIZED)); 

        writer.addDocument(doc); 

        doc = new Document(); 

        doc.add(new Field("sort","3",Field.Store.YES,Field.Index.TOKENIZED)); 

        writer.addDocument(doc); 

        doc = new Document(); 

        doc.add(new Field("sort","5",Field.Store.YES,Field.Index.TOKENIZED)); 

        writer.addDocument(doc); 

        doc = new Document();

        doc.add(new Field("sort","9",Field.Store.YES,Field.Index.TOKENIZED)); 

        writer.addDocument(doc); 

        doc = new Document(); 

        doc.add(new Field("sort","6",Field.Store.YES,Field.Index.TOKENIZED)); 

        writer.addDocument(doc); 

        doc = new Document(); 

        doc.add(new Field("sort","7",Field.Store.YES,Field.Index.TOKENIZED)); 

        writer.addDocument(doc); 

        writer.close(); 
} 

//下面是搜索的例子: 
public void SearchSort1() throws IOException, ParseException { 

        IndexSearcher indexSearcher = new IndexSearcher("C:\\\\indexStore"); 

        QueryParser queryParser = new QueryParser("sort",new StandardAnalyzer()); 

        Query query = queryParser.parse("4"); 

        Hits hits = indexSearcher.search(query); 

        System.out.println("有"+hits.length()+"个结果"); 

        Document doc = hits.doc(0); 

        System.out.println(doc.get("sort")); 
} 


public void SearchSort2() throws IOException, ParseException{ 

        IndexSearcher indexSearcher = new IndexSearcher("C:\\\\indexStore"); 

        //这个地方前面没有提到,它是用于范围的Query可以看一下帮助文档. 
	Query query = new RangeQuery(new Term("sort","1"),new Term("sort","9"),true);

        Hits hits = indexSearcher.search(query,new Sort(new SortField("sort",new MySortComparatorSource()))); 

        System.out.println("有"+hits.length()+"个结果"); 

        for(int i=0;i<hits.length();i++) { 

            Document doc = hits.doc(i); 

            System.out.println(doc.get("sort")); 

        } 

} 


public class MyScoreDocComparator implements ScoreDocComparator{ 

    private Integer[] sort; 

    public MyScoreDocComparator(String s,IndexReader reader, String fieldname) throws IOException{ 

        sort = new Integer[reader.maxDoc()]; 

        for(int i = 0;i<reader.maxDoc();i++){

            Document doc =reader.document(i); 

            sort[i]=new Integer(doc.get("sort")); 

        } 

    } 

    public int compare(ScoreDoc i, ScoreDoc j){ 

        if(sort[i.doc]>sort[j.doc])

            return 1; 

        if(sort[i.doc]<sort[j.doc]) 

            return -1; 

        return 0; 

    } 

    public int sortType(){ 

     return SortField.INT; 

    } 







    public Comparable sortValue(ScoreDoc i){ 

        return new Integer(sort[i.doc]); 
    } 

} 




public class MySortComparatorSource implements SortComparatorSource { 

    private static final long serialVersionUID = -9189690812167655465L; 

    public ScoreDocComparator newComparator(IndexReader reader, String fieldname)throws IOExceptio{ 

        if(fieldname.equals("sort")) 

            return new MyScoreDocComparator("sort",reader,fieldname); 

        return null; 

    } 

} 

//SearchSort1()输出的结果没有排序,SearchSort2()就排序了。
分享到:
评论

相关推荐

    Lucene 学习笔记 1

    **Lucene 学习笔记 1** Lucene 是一个全文搜索引擎库,由 Apache 软件基金会开发。它提供了一个可扩展的、高性能的搜索框架,使得开发者能够在其应用程序中集成高级的搜索功能。本篇学习笔记将深入探讨 Lucene 的...

    【分享:lucene学习资料】---<下载不扣分,回帖加1分,欢迎下载,童叟无欺>

    1&gt; lucene学习笔记 2&gt; 全文检索的实现机制 【1】lucene学习笔记的目录如下 1. 概述 3 2. lucene 的包结构 3 3. 索引文件格式 3 4. lucene中主要的类 4 4.1. Document文档类 4 4.1.1. 常用方法 4 4.1.2. 示例 4 4.2...

    lucene学习笔记

    **Lucene学习笔记** Lucene是一个高性能、全文检索库,由Apache软件基金会开发并维护,是Java编程语言中广泛使用的搜索引擎库。它提供了强大的文本分析、索引和搜索功能,适用于构建复杂的全文检索系统。本笔记将...

    lucene基础学习笔记&源码

    **Lucene 基础学习笔记与源码分析** **一、Lucene 概述** Lucene 是一个高性能、全文本搜索库,由 Apache 软件基金会开发并维护。它是一个 Java 开发的开源项目,被广泛应用于各种搜索引擎的构建,支持多种编程...

    lucene 3.5学习笔记

    《Lucene 3.5 学习笔记》 在信息技术高速发展的今天,搜索引擎技术成为了信息检索的核心工具。Apache Lucene,作为一个开源全文检索库,为开发者提供了强大的文本搜索功能。本文将深入探讨Lucene 3.5版本的相关知识...

    lucene3.0学习笔记(三)与paoding整合

    《Lucene 3.0 学习笔记(三)与Paoding整合》 在深入了解Lucene 3.0的过程中,我们经常会遇到如何将其与第三方工具进行整合的问题,以提升搜索性能和用户体验。这篇学习笔记主要关注的是将Lucene 3.0与Paoding搜索...

    Lucene 课堂笔记

    ### Lucene 课堂笔记知识点详解 #### 一、信息检索概览 **1.1 信息检索的概念** 信息检索指的是从海量信息集中筛选出与用户需求相关联的信息。本课程主要探讨文本信息的检索,虽然实际应用中还可能涉及图像、音频...

    lucene笔记

    **Lucene笔记** Lucene是Apache软件基金会的一个开放源代码项目,它是一个全文搜索引擎库,提供了文本检索和分析的核心工具。作为一个高性能、可扩展的信息检索库,Lucene被广泛应用于各种搜索应用,如网站搜索、...

    lucene笔记共38页.pdf.zip

    《Lucene笔记共38页.pdf》压缩包包含了一份详尽的Lucene学习资料,这份笔记深入浅出地探讨了Apache Lucene这个全文搜索引擎库。Lucene是Java开发的开源库,广泛应用于信息检索和大数据分析领域,为开发者提供了强大...

    Lucene开发指南

    Lucene学习笔记(二)可能涉及索引构建过程,讲解了如何使用Document对象存储文档内容,Field对象定义字段属性,以及如何使用IndexWriter进行索引更新和优化。 笔记(三)和(四)可能深入到查询解析和执行。查询解析器...

    Lucene.net学习书记整理

    **Lucene.net学习笔记整理** 在信息技术领域,搜索引擎技术一直占据着重要的地位,尤其是在大数据时代,高效、精准的检索能力显得尤为重要。Lucene是Apache软件基金会的一个开源项目,它为Java开发者提供了一个高...

    lucene学习资料

    另一份资料`Lucene学习笔记.doc`则可能包含了作者在学习Lucene过程中积累的笔记,可能包括了对Lucene核心类的理解,如IndexWriter、Directory、Document、Field等,以及对高级特性的探索,如近实时搜索、多字段排序...

    全文检索学习笔记

    在本文中,我们将探讨全文检索的核心概念、索引的构建以及搜索过程,同时也会提及两个流行的全文检索框架——Lucene和Solr。 首先,全文检索的主要目的是解决非结构化数据的快速查找问题。与结构化数据(如数据库中...

    Lucene技术文档doc

    在深入学习Lucene的过程中,理解其基本原理和使用方法至关重要。通过对不同版本文档的阅读和比较,可以更好地掌握Lucene的发展历程和技术改进,从而在实际项目中灵活运用。对于想要进一步提升搜索技术能力的开发者,...

    [HeyJava][传智播客]全文检索Lucene源码

    1. **全文检索Lucene.pdf**:这可能是一个详细的教程或者课程笔记,涵盖了Lucene的基本概念、架构、核心类的解析,以及如何利用Lucene进行全文检索的实例。你可能会在这里找到关于如何创建索引、查询执行、结果排序...

Global site tag (gtag.js) - Google Analytics