博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Lucene自定义排序
阅读量:4031 次
发布时间:2019-05-24

本文共 5071 字,大约阅读时间需要 16 分钟。

package com.lucene.search;
 
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.IOException;
 
 import org.apache.lucene.document.Document;
 import org.apache.lucene.document.Field;
 import org.apache.lucene.document.NumericField;
 import org.apache.lucene.index.CorruptIndexException;
 import org.apache.lucene.index.IndexWriter;
 import org.apache.lucene.index.IndexWriterConfig;
 import org.apache.lucene.store.Directory;
 import org.apache.lucene.store.FSDirectory;
 import org.apache.lucene.util.Version;
 
 import com.chenlb.mmseg4j.Dictionary;
 import com.chenlb.mmseg4j.analysis.ComplexAnalyzer;
 
 public class IndexUtils {
 
     private static Directory directory=null;
     
     static{
         try {
             directory=FSDirectory.open(new File("E:/lucene/files"));
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
     
     public static Directory getDirectory() {
         return directory;
     }
 
     /**
      * 创建索引
      */
     public static void createIndex(){
         IndexWriter writer=null;
         File file=new File("E:/lucene/resource");
         Document document=null;
         try {
             //创建IndexWriter   使用中文分词器
             Dictionary dic=Dictionary.getInstance(new File("F:/官方包/lucene-3.5.0/mmseg4j-1.8.5/data"));
             writer=new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35,new ComplexAnalyzer(dic)));
             for(File f:file.listFiles()){
                 document=new Document();
                 document.add(new Field("filename", f.getName(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                 document.add(new Field("content",new FileReader(f)));
                 document.add(new NumericField("date", Field.Store.YES, true).setLongValue(f.lastModified()));
                 document.add(new NumericField("size", Field.Store.YES, false).setLongValue(f.length()/1000));
                 writer.addDocument(document);
             }
         } catch (FileNotFoundException e) {
             e.printStackTrace();
         } catch (CorruptIndexException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         }finally{
             try {
                 writer.close();
             } catch (CorruptIndexException e) {
                 e.printStackTrace();
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
     }
     

 }

######################

package com.lucene.search;

 
 import java.io.IOException;
 import java.sql.Date;
 import java.text.SimpleDateFormat;
 
 import org.apache.lucene.analysis.standard.StandardAnalyzer;
 import org.apache.lucene.document.Document;
 import org.apache.lucene.index.CorruptIndexException;
 import org.apache.lucene.index.IndexReader;
 import org.apache.lucene.queryParser.ParseException;
 import org.apache.lucene.queryParser.QueryParser;
 import org.apache.lucene.search.IndexSearcher;
 import org.apache.lucene.search.Query;
 import org.apache.lucene.search.ScoreDoc;
 import org.apache.lucene.search.Sort;
 import org.apache.lucene.search.TopDocs;
 import org.apache.lucene.util.Version;
 
 public class SearchUtils {
 
     // 定义IndexReader,并使用静态块加载IndexReader
     private static IndexReader reader = null;
     static {
         try {
             reader = IndexReader.open(IndexUtils.getDirectory());
         } catch (CorruptIndexException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
 
     // 获取IndexSearcher
     private IndexSearcher getSearcher() {
         try {
             if (reader == null) {
                 reader = IndexReader.open(IndexUtils.getDirectory());
             } else {
                 IndexReader ir = IndexReader.openIfChanged(reader);
                 if (ir != null) {
                     reader.close();
                     reader = ir;
                 }
             }
             return new IndexSearcher(reader);
         } catch (CorruptIndexException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         }
         return null;
     }
 
     /**
      * 排序查询
      *
      * @param querystr
      *            查找匹配的字符串
      * @param domain
      *            查找内容的域
      * @param sort
      *            排序方式
      */
     public void SearchBySort(String querystr, String domain, Sort sort) {
         TopDocs docs = null;
         IndexSearcher searcher = this.getSearcher();
         try {
             QueryParser parser = new QueryParser(Version.LUCENE_35, domain,
                     new StandardAnalyzer(Version.LUCENE_35));
             Query query = parser.parse(querystr);
             if (sort == null) {
                 docs = searcher.search(query, 150);
             } else {
                 docs = searcher.search(query, 150, sort);
             }
 
             // 输出信息
             ScoreDoc[] sds = docs.scoreDocs;
             Document d = null;
             SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
             for (ScoreDoc s : sds) {
                 d = searcher.doc(s.doc);
                 System.out.println(s.doc+"->"
                                     +s.score+"->"
                                     +d.get("filename")+"->"
                                     +d.get("size")+"->"
                                     +sdf.format(new Date(Long.valueOf(d.get("date")))));
             }
 
         } catch (ParseException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
 }

############################

package com.lucene.test;

import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.junit.Before;
import org.junit.Test;
import com.lucene.search.SearchUtils;
public class TestSortSearch {
    private SearchUtils su=null;
    
    @Before
    public void init(){
        su=new SearchUtils();
    }
    
    @Test
    public void testSortSearch(){
        //无排序搜索,默认根据评分降序排序
        //su.SearchBySort("中国", "content", null);
        //通过doc的id进行排序
        //su.SearchBySort("中国", "content", Sort.INDEXORDER);
        //通过评分进行排序
        //su.SearchBySort("中国", "content", Sort.RELEVANCE);
        //根据SortField设置属性对filename进行升序排序
        //su.SearchBySort("中国", "content", new Sort(new SortField("filename", SortField.STRING)));
        //通过根据SortField设置最后一个属性进行降序排序
        su.SearchBySort("中国", "content", new Sort(new SortField("filename", SortField.STRING,true)));
        
    }
}

转载地址:http://enebi.baihongyu.com/

你可能感兴趣的文章
深入理解Apache Flink核心技术
查看>>
SpringCloud 各组件原理图,面试必备
查看>>
面试题总结:可能是全网最好的MySQL重要知识点
查看>>
MySQL面试之数据库索引
查看>>
完整的项目管理流程,看清PMP42个过程的执行顺序
查看>>
设计模式,面试速记手册1
查看>>
设计模式,面试速记手册2
查看>>
备受面试官青睐的 Java NIO,到底和传统 IO 有啥不一样
查看>>
各大公司Java面试题超详细总结
查看>>
搞定MySQL之面经(一)
查看>>
排序算法,看这一篇就够了,含动图+Java实现
查看>>
性能指标:QPS、TPS、系统吞吐量理解
查看>>
搞清 适配器模式、代理模式和装饰者模式的不同
查看>>
一次完整的HTTP请求过程
查看>>
HTTP 与 HTTPS 的区别
查看>>
SVN E200030: There are unfinished transactions detected
查看>>
搞定Nginx高并发原理:多进程单线程和多路IO复用模型
查看>>
深入NGINX:nginx高性能的实现原理
查看>>
搞定分布式锁,主流的三种解决方案
查看>>
搞定JVM系列(一):JVM原理初识
查看>>