1、将Index的相关独立出来
public class FileIndexUtils { private static Directory directory = null; static { try { directory = FSDirectory.open(new File("d:/lucene/files")); } catch (IOException e) { e.printStackTrace(); } } public static Directory getDirectory() { return directory; } public static void index(boolean hasNew) { IndexWriter writer = null; try { writer = new IndexWriter(directory,new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35))); if(hasNew) writer.deleteAll(); File file = new File("d:/lucene/example"); Document doc = null; for(File f:file.listFiles()) { doc = new Document(); doc.add(new Field("content",new FileReader(f))); doc.add(new Field("filename",f.getName(),Field.Store.YES,Field.Index.NOT_ANALYZED)); doc.add(new Field("path",f.getAbsolutePath(),Field.Store.YES,Field.Index.NOT_ANALYZED)); doc.add(new NumericField("date",Field.Store.YES,true).setLongValue(f.lastModified())); doc.add(new NumericField("size",Field.Store.YES,true).setIntValue((int)f.length()/1024)); writer.addDocument(doc); } } catch (CorruptIndexException e) { e.printStackTrace(); } catch (LockObtainFailedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if(writer!=null) writer.close(); } catch (CorruptIndexException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } }2、分页的实现
//所有数据分页的方式 public void searchPage(String query,int pageIndex,int pageSize) { try { Directory dir = FileIndexUtils.getDirectory(); IndexSearcher searcher = getSearcher(dir); QueryParser parser = new QueryParser(Version.LUCENE_35,"content",new StandardAnalyzer(Version.LUCENE_35)); Query q = parser.parse(query); TopDocs tds = searcher.search(q, 500); ScoreDoc[] sds = tds.scoreDocs; int start = (pageIndex-1)*pageSize; int end = pageIndex*pageSize; for(int i=start;i<end;i++) { Document doc = searcher.doc(sds[i].doc); System.out.println(sds[i].doc+":"+doc.get("path")+"--->"+doc.get("filename")); } searcher.close(); } catch (org.apache.lucene.queryParser.ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } //使用searcherAfter形式 public void searchPageByAfter(String query,int pageIndex,int pageSize) { try { Directory dir = FileIndexUtils.getDirectory(); IndexSearcher searcher = getSearcher(dir); QueryParser parser = new QueryParser(Version.LUCENE_35,"content",new StandardAnalyzer(Version.LUCENE_35)); Query q = parser.parse(query); ScoreDoc lastSd = getLastScoreDoc(pageIndex, pageSize, q, searcher); TopDocs tds = searcher.searchAfter(lastSd, q,pageSize); for(ScoreDoc sd:tds.scoreDocs) { Document doc = searcher.doc(sd.doc); System.out.println(sd.doc+":"+doc.get("path")+"--->"+doc.get("filename")); } searcher.close(); } catch (org.apache.lucene.queryParser.ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } //返回下一页开始的上一个数据 private ScoreDoc getLastScoreDoc(int pageIndex,int pageSize,Query query,IndexSearcher searcher) throws IOException { if(pageIndex==1) return null; int num = pageSize*(pageIndex-1);//对于lucene只能一次性至少取出页数所有的记录才可以 TopDocs tds = searcher.search(query, num); return tds.scoreDocs[num-1]; }
@Test public void testSearchePage01() { su.searchPage("log", 1, 20); } @Test public void testSearchePage02() { su.searchPageByAfter("log", 1, 20); }