Lucene自定义排序

 1 package com.lucene.search;
 2 
 3 import java.io.File;
 4 import java.io.FileNotFoundException;
 5 import java.io.FileReader;
 6 import java.io.IOException;
 7 
 8 import org.apache.lucene.document.Document;
 9 import org.apache.lucene.document.Field;
10 import org.apache.lucene.document.NumericField;
11 import org.apache.lucene.index.CorruptIndexException;
12 import org.apache.lucene.index.IndexWriter;
13 import org.apache.lucene.index.IndexWriterConfig;
14 import org.apache.lucene.store.Directory;
15 import org.apache.lucene.store.FSDirectory;
16 import org.apache.lucene.util.Version;
17 
18 import com.chenlb.mmseg4j.Dictionary;
19 import com.chenlb.mmseg4j.analysis.ComplexAnalyzer;
20 
21 public class IndexUtils {
22 
23     private static Directory directory=null;
24     
25     static{
26         try {
27             directory=FSDirectory.open(new File("E:/lucene/files"));
28         } catch (IOException e) {
29             e.printStackTrace();
30         }
31     }
32     
33     public static Directory getDirectory() {
34         return directory;
35     }
36 
37     /**
38      * 创建索引
39      */
40     public static void createIndex(){
41         IndexWriter writer=null;
42         File file=new File("E:/lucene/resource");
43         Document document=null;
44         try {
45             //创建IndexWriter   使用中文分词器
46             Dictionary dic=Dictionary.getInstance(new File("F:/官方包/lucene-3.5.0/mmseg4j-1.8.5/data"));
47             writer=new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35,new ComplexAnalyzer(dic)));
48             for(File f:file.listFiles()){
49                 document=new Document();
50                 document.add(new Field("filename", f.getName(), Field.Store.YES, Field.Index.NOT_ANALYZED));
51                 document.add(new Field("content",new FileReader(f)));
52                 document.add(new NumericField("date", Field.Store.YES, true).setLongValue(f.lastModified()));
53                 document.add(new NumericField("size", Field.Store.YES, false).setLongValue(f.length()/1000));
54                 writer.addDocument(document);
55             }
56         } catch (FileNotFoundException e) {
57             e.printStackTrace();
58         } catch (CorruptIndexException e) {
59             e.printStackTrace();
60         } catch (IOException e) {
61             e.printStackTrace();
62         }finally{
63             try {
64                 writer.close();
65             } catch (CorruptIndexException e) {
66                 e.printStackTrace();
67             } catch (IOException e) {
68                 e.printStackTrace();
69             }
70         }
71     }
72     
73 }
 1 package com.lucene.search;
 2 
 3 import java.io.IOException;
 4 import java.sql.Date;
 5 import java.text.SimpleDateFormat;
 6 
 7 import org.apache.lucene.analysis.standard.StandardAnalyzer;
 8 import org.apache.lucene.document.Document;
 9 import org.apache.lucene.index.CorruptIndexException;
10 import org.apache.lucene.index.IndexReader;
11 import org.apache.lucene.queryParser.ParseException;
12 import org.apache.lucene.queryParser.QueryParser;
13 import org.apache.lucene.search.IndexSearcher;
14 import org.apache.lucene.search.Query;
15 import org.apache.lucene.search.ScoreDoc;
16 import org.apache.lucene.search.Sort;
17 import org.apache.lucene.search.TopDocs;
18 import org.apache.lucene.util.Version;
19 
20 public class SearchUtils {
21 
22     // 定义IndexReader,并使用静态块加载IndexReader
23     private static IndexReader reader = null;
24     static {
25         try {
26             reader = IndexReader.open(IndexUtils.getDirectory());
27         } catch (CorruptIndexException e) {
28             e.printStackTrace();
29         } catch (IOException e) {
30             e.printStackTrace();
31         }
32     }
33 
34     // 获取IndexSearcher
35     private IndexSearcher getSearcher() {
36         try {
37             if (reader == null) {
38                 reader = IndexReader.open(IndexUtils.getDirectory());
39             } else {
40                 IndexReader ir = IndexReader.openIfChanged(reader);
41                 if (ir != null) {
42                     reader.close();
43                     reader = ir;
44                 }
45             }
46             return new IndexSearcher(reader);
47         } catch (CorruptIndexException e) {
48             e.printStackTrace();
49         } catch (IOException e) {
50             e.printStackTrace();
51         }
52         return null;
53     }
54 
55     /**
56      * 排序查询
57      * 
58      * @param querystr
59      *            查找匹配的字符串
60      * @param domain
61      *            查找内容的域
62      * @param sort
63      *            排序方式
64      */
65     public void SearchBySort(String querystr, String domain, Sort sort) {
66         TopDocs docs = null;
67         IndexSearcher searcher = this.getSearcher();
68         try {
69             QueryParser parser = new QueryParser(Version.LUCENE_35, domain,
70                     new StandardAnalyzer(Version.LUCENE_35));
71             Query query = parser.parse(querystr);
72             if (sort == null) {
73                 docs = searcher.search(query, 150);
74             } else {
75                 docs = searcher.search(query, 150, sort);
76             }
77 
78             // 输出信息
79             ScoreDoc[] sds = docs.scoreDocs;
80             Document d = null;
81             SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
82             for (ScoreDoc s : sds) {
83                 d = searcher.doc(s.doc);
84                 System.out.println(s.doc+"->"
85                                     +s.score+"->"
86                                     +d.get("filename")+"->"
87                                     +d.get("size")+"->"
88                                     +sdf.format(new Date(Long.valueOf(d.get("date")))));
89             }
90 
91         } catch (ParseException e) {
92             e.printStackTrace();
93         } catch (IOException e) {
94             e.printStackTrace();
95         }
96     }
97 }
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)));
        
    }
}

 

posted @ 2012-04-22 13:38  Paul.Lau  阅读(7883)  评论(0编辑  收藏  举报