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);
}
- Lucene八(搜索分页)
- Lucene小练十(八种搜索)
- lucene学习笔记(八)lucene实时搜索
- Lucene小练十二(分页搜索)
- lucene搜索结果分页
- 03-Lucene中的搜索再搜索分页
- Lucene高级搜素(Query、QueryParser和分页搜索)
- Lucene——八种搜索
- Lucene实现京东搜索(八)
- Lucene搜索结果分页 query+cache 输出
- Lucene(四)搜索
- Lucene七(搜索)
- Lucene(2)搜索
- Lucene(二)搜索
- 搜索学习--Lucene中搜索的排序、范围区间搜索、分页搜索、多条件搜索
- lucene.net 3.0.3、结合盘古分词进行搜索的小例子(分页功能)
- lucene.net 3.0.3、结合盘古分词进行搜索的小例子(分页功能)
- lucene搜索结果分页显示 google、baidu式的分页 我在项目中的应用(二)
- [LeetCode]240.Search a 2D Matrix II
- Problem F
- Redis 数据持久化
- 高效Linux之Terminal下实用快捷键
- 07 JAVA IO流
- Lucene八(搜索分页)
- 数据结构 线性表的顺序存储
- 数据库设计--实体关系图(ERD)
- ExpandableListView组件学习笔记
- Android开发----音乐播放器(界面设计)
- 杭电4883 TIANKENG’s restaurant(小板凳的问题)
- win7旗舰版升级win10
- HDU 4987/BC 7C Little Pony and Dice
- hdu2037今年暑假不AC【贪心】