Lucene八(搜索分页)

来源:互联网 发布:mac邮件设置qq邮箱 编辑:程序博客网 时间:2024/05/01 03:37

Lucene搜索分页有两种方式,即“再查询”和基于searchAfter的方式,这里为了演示分页,我在F:\stady\JAVA\other\Lucene\test\sourceToFenye下添加了很多个文件,用于测试分页,然后为这些文件创建索引,代码及测试如下:

package cn.liuys.lucene.index;


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.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
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 FileIndexUtils {
private static Directory directory = null;
static{
try {
directory = FSDirectory.open(new File("F:/stady/JAVA/other/Lucene/test/index03/"));
} 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("F:/stady/JAVA/other/Lucene/test/sourceToFenye");
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();
}
}
}
}


//为文件添加索引用于分页
@Test
public void testIndexFile(){
FileIndexUtils.index(true);
}


两种分页方式的代码及测试如下:

public IndexSearcher getSearcher(Directory directory){
try {
if(reader == null){
reader = IndexReader.open(directory);
}else{
IndexReader tr = IndexReader.openIfChanged(reader);
if(tr != null){
reader.close();
reader = tr;
}
}
return new IndexSearcher(reader);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}


/**
 * 分页搜索
 */
public void searchByPage1(String query, int pageIndex, int pageSize){
try {
Directory directory = FileIndexUtils.getDirectory();
IndexSearcher searcher = getSearcher(directory);
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"));
}
} catch (org.apache.lucene.queryParser.ParseException | IOException e) {
e.printStackTrace();
}
}

/**
 * 分页搜索,基于searchAfter的方式
 */
public void searchPageByAfter(String query, int pageIndex, int pageSize){
try {
Directory directory = FileIndexUtils.getDirectory();
IndexSearcher searcher = getSearcher(directory);
QueryParser parser = new QueryParser(Version.LUCENE_35, "content", new StandardAnalyzer(Version.LUCENE_35));
Query q = parser.parse(query);
TopDocs tds = searcher.search(q, pageIndex * pageSize);
ScoreDoc[] sds = tds.scoreDocs;
int start = (pageIndex - 1) * pageSize - 1;
if(pageIndex == 1){
start = (pageIndex - 1) * pageSize;
}
tds = searcher.searchAfter(sds[start], q, 20);
for (ScoreDoc sd : tds.scoreDocs) {
Document doc = searcher.doc(sd.doc);
System.out.println(sd.doc+":"+doc.get("path")+"--->"+doc.get("filename"));
}
} catch (org.apache.lucene.queryParser.ParseException | IOException e) {
e.printStackTrace();
}
}


测试:

@Test
public void testSearchByPage1(){
su.searchByPage1("java", 1, 20);
}

@Test
public void testSearchPageByAfter(){
su.searchByPage1("java", 1, 20);
}

0 0