在sql语句中,有升序和降序排列。在Lucene中,同样也有。

Sort里的属性 SortField里的属性 含义
Sort.INDEXORDER SortField.FIELD_DOC 按照索引的顺序进行排序
Sort.RELEVANCE SortField.FIELD_SCORE 按照关联性评分进行排序
=========SortField类============
//field是排序字段type是排序类型
public SortField(String field, Type type);
//field是排序字段type是排序类型reverse是指定升序还是降序
//reverse 为true是降序  false为升序
public SortField(String field, Type type, boolean reverse)

=========Sort类============
public Sort();//Sort对象构造方法默认是按文档评分排序
public Sort(SortField field);//排序的一个SortField
public Sort(SortField... fields)//排序的多个SortField可以传入一个数组

 

前提,创建索引:

 1 import java.io.File;
 2 import java.io.FileReader;
 3 import java.io.IOException;
 4 import java.nio.file.Paths;
 5 import java.util.Random;
 6 
 7 import org.apache.lucene.analysis.standard.StandardAnalyzer;
 8 import org.apache.lucene.document.Document;
 9 import org.apache.lucene.document.Field;
10 import org.apache.lucene.document.IntField;
11 import org.apache.lucene.document.LongField;
12 import org.apache.lucene.document.NumericDocValuesField;
13 import org.apache.lucene.index.IndexWriter;
14 import org.apache.lucene.index.IndexWriterConfig;
15 import org.apache.lucene.store.Directory;
16 import org.apache.lucene.store.FSDirectory;
17 
18 public class FileIndexUtils {
19     private static Directory directory = null;
20     static{
21         try {
22             directory = FSDirectory.open(Paths.get("D://lucene//document"));
23         } catch (Exception e) {
24             // TODO: handle exception
25             e.printStackTrace();
26         }
27     }
28     public static Directory getDirectory(){
29         return directory;
30     }
31     /**
32      * 创建索引
33      * @param hasNew
34      */
35     @SuppressWarnings("deprecation")
36     public static void createIndex(boolean hasNew){
37         IndexWriter writer = null;
38         try {
39             writer = new IndexWriter(directory, new IndexWriterConfig(new StandardAnalyzer()));
40             if(hasNew){
41                 writer.deleteAll();
42             }
43         
44             Document document = null;
45             int index = 0;
46             //随机数,为排序数字
47             Random random = new Random();
48             //为源文件创建索引
49             File file = new File("D://text");
50             for(File f:file.listFiles()){
51                 int score = random.nextInt();
52                 document = new Document();
53                 document.add(new Field("id",String.valueOf(index++), Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
54                 document.add(new Field("content",new FileReader(f)));
55                 document.add(new Field("fileName",f.getName(),Field.Store.YES,Field.Index.NOT_ANALYZED));
56                 document.add(new Field("path",f.getPath(),Field.Store.YES,Field.Index.NOT_ANALYZED));
57                 document.add(new IntField("score",score,Field.Store.YES));
58                 document.add(new NumericDocValuesField("size",(long)f.length()));
59                 document.add(new LongField("size", f.length(), Field.Store.YES));
60                 document.add(new IntField("date",(int) f.lastModified(),Field.Store.YES));
61                 writer.addDocument(document);
62             }        
63         } catch (Exception e) {
64             // TODO: handle exception
65             e.printStackTrace();
66         }finally{
67             if(writer!=null){
68                 try {
69                     writer.close();
70                 } catch (IOException e) {
71                     // TODO Auto-generated catch block
72                     e.printStackTrace();
73                 }
74             }
75         }
76     }
77 }
View Code

相关文章:

  • 2021-10-18
  • 2022-12-23
  • 2022-12-23
  • 2022-02-28
  • 2022-03-06
  • 2022-01-10
猜你喜欢
  • 2022-12-23
  • 2022-01-29
  • 2022-12-23
  • 2021-04-11
  • 2021-09-27
  • 2022-01-15
相关资源
相似解决方案