今天看视频学了Lucene的分页操作,写了写这方面的程序。
Lucene提供了两种方法进行分页操作,一种需要把所有内容都找出来,再来提取所需要显示的内容;另一种是需要哪里找哪里(比如你要查询第三页的内容,每页显示10条结果,会找到前29条)。显然第二种方法效率更高一些。以下是代码,两种都写了,可以对比一下。
//包含索引的建立和两种搜索方法
package PageShow;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
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.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
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.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;
public class FileIndexUtil
{
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 index() throws LockObtainFailedException
{
IndexWriter writer=null;
try
{
writer=new IndexWriter(directory,new IndexWriterConfig(Version.LUCENE_35,new StandardAnalyzer(Version.LUCENE_35)));
File file=new File("e:/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();
}
}
}
public static void searchPage(String query,int pageIndex,int pageSize)
{
try
{
Directory dir=directory;
IndexReader reader=IndexReader.open(directory);
IndexSearcher searcher=new IndexSearcher(reader);
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"));
}
System.out.println("---------------------------------------------->");
searcher.close();
}
catch(ParseException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
//获取上一页的最后一个ScoreDoc
private static ScoreDoc getLastScoreDoc(int pageIndex,int pageSize,Query query,IndexSearcher searcher)throws IOException
{
//如果是第一页就返回空
if(pageIndex==1)return null;
//获取上一页的数量
int num=(pageIndex-1)*pageSize;
TopDocs tds=searcher.search(query, num);
return tds.scoreDocs[num-1];
}
public static void searchPageByAfter(String query,int pageIndex,int pageSize )
{
try
{
Directory dir=directory;
IndexReader reader=IndexReader.open(directory);
IndexSearcher searcher=new IndexSearcher(reader);
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);
//通过最后一个元素搜索下一页的pageSize个元素
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"));
}
System.out.println("---------------------------------------------->");
searcher.close();
}
catch(ParseException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
//测试类
package PageShow;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.store.LockObtainFailedException;
public class PageShow
{
public static void main(String[] args) throws LockObtainFailedException
{
FileIndexUtil.index();
FileIndexUtil.searchPage("java", 2, 10);
FileIndexUtil.searchPageByAfter("java", 2, 10);
}
}
class Copy
{
public void TestCopyFile()throws IOException
{
File file=new File("e:Lucene实例/example/");
for(File f:file.listFiles())
{
String destFileName=FilenameUtils.getFullPath(f.getAbsolutePath())+
FilenameUtils.getBaseName(f.getName())+".wang";
FileUtils.copyFile(f,new File(destFileName));
}
}
}