Lucene小练十二(分页搜索)

package Java.se.lucene;
//创建索引
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 PageIndex {

	private static Directory directory=null;
	static{
		try {
			directory=FSDirectory.open(new File("f:/lucene/Index05"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static Directory getDirectory()
	{
		return directory;
	}
	public void Index(boolean hasNew)
	{
		IndexWriter writer=null;
		try {
			  writer=new IndexWriter(directory,new IndexWriterConfig
					(Version.LUCENE_36, new StandardAnalyzer(Version.LUCENE_36)));
			if(hasNew)
			{
				writer.deleteAll();
			}
			File file=new File("F:/lucene/lucenes");
			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));//添加Name
				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
		{
			if(writer!=null)
			{
				try {
					writer.close();
				} catch (CorruptIndexException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
}
package Java.se.lucene;
//创建搜索
import java.io.IOException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
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.util.Version;

public class PageSearch {

	 private static IndexReader reader=null;
	 public static IndexSearcher getSearcher(Directory directory)
	    {
	    	try {
				reader=IndexReader.open(directory);
			} catch (CorruptIndexException e) {
				e.printStackTrace();
			} catch (IOException e){
				e.printStackTrace();
			}

	    	return(new IndexSearcher(reader));
	     }
     //分页查询
	 public void searchPage(String query,int pageIndex,int pageSize)
	    {
	       try {
			   Directory dir=PageIndex.getDirectory();
			   IndexSearcher searcher=getSearcher(dir);
			   //默认搜索域为content
			   QueryParser parser=new QueryParser(Version.LUCENE_36,"content",
					   new StandardAnalyzer(Version.LUCENE_36));
			   //创建 Query
			   Query q=parser.parse(query);
			   TopDocs tds=searcher.search(q,200);
			   ScoreDoc[] sd=tds.scoreDocs;
			   //分页显示,start为每页第一个索引,end是最后一个索引,pagesize是每页显示条数,pageindex是第几页
			   int start=(pageIndex-1)*pageSize;
			   int end=pageIndex*pageSize;
			   for(int i=start;i<end;i++)
			   {
				   Document document=searcher.doc(sd[i].doc);
				   System.out.println(sd[i].doc+"-->"+document.get("path")+"-->"
						   +document.get("filename"));
				}
			   searcher.close();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	  }
	 //不分页查询
	 public void searchNoPage(String query)
	    {
	       try {
			   Directory dir=PageIndex.getDirectory();
			   IndexSearcher searcher=getSearcher(dir);
			   QueryParser parser=new QueryParser(Version.LUCENE_36,"content",
					   new StandardAnalyzer(Version.LUCENE_36));
			   Query q=parser.parse(query);
			   TopDocs tds=searcher.search(q, 200);
			   ScoreDoc[] sd=tds.scoreDocs;
			   for(int i=0;i<sd.length;i++)
			   {
				   Document document=searcher.doc(sd[i].doc);
				   System.out.println(sd[i].doc+"-->"+document.get("path")+"-->"
						   +document.get("filename"));
				}
			   searcher.close();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	  }
	 //After查询
	 public void searchPageByAfter(String query,int pageIndex,int pageSize)
	    {
	       try {
			   Directory dir=PageIndex.getDirectory();
			   IndexSearcher searcher=getSearcher(dir);
			   QueryParser parser=new QueryParser(Version.LUCENE_36,"content",
					   new StandardAnalyzer(Version.LUCENE_36));
			   Query q=parser.parse(query);
			   //获取最后一次搜索的最后一个元素
			   ScoreDoc lastSdDoc=getLastScoreDoc(q, pageIndex, pageSize, searcher);
			   //将所有结果储存在内存中
               TopDocs tds=searcher.searchAfter(lastSdDoc,q,pageSize);
//			   //将所有结果储存在内存中
//			   TopDocs tds=searcher.search(q, 200);
//			   ScoreDoc[] sd=tds.scoreDocs;
//			   //从上次搜索的最后一个开始,索引从零开始
//			   int last=(pageIndex-1)*pageSize-1;
//			   tds=searcher.searchAfter(sd[last],q, 5);
			   for(ScoreDoc sds:tds.scoreDocs)
			   {
				   Document document=searcher.doc(sds.doc);
				   System.out.println(sds.doc+"-->"+document.get("path")+"-->"
						   +document.get("filename"));
				}
			   searcher.close();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	  }
     //获取最后一次搜索的最后一个元素
	 public ScoreDoc getLastScoreDoc(Query query,int pageIndex,int pageSize,IndexSearcher searcher)
	 {
		 if(pageIndex==1)
		 {
			 return null;
		 }
		 int num=(pageIndex-1)*pageSize;
		 TopDocs tds = null;
		try {
			tds = searcher.search(query, num);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		return tds.scoreDocs[num-1]; 
	 }
}
package Java.se.lucene;
//测试类
import org.junit.Before;
import org.junit.Test;

public class Test_Page {
	private static PageIndex pi=null;
	private static PageSearch ps=null;
	@Before
	public void init()
	{
		 pi=new PageIndex();
	     ps=new PageSearch();
	}
	@Test
	public void test_pageindex()
	{
		pi.Index(true);
	}
	@Test
	public void test_pagesearch01()
	{
		ps.searchPage("java",3,5);
		System.out.println("---------------------------");
		//ps.searchNoPage("java");
		ps.searchPageByAfter("java",3,5);
	}
	@Test
	public void test_pagesearch02()
	{
		ps.searchPageByAfter("java",3, 5);
	}
	


}
 


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值