Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

lucene4.5.0之filter使用

$
0
0

filter过滤操作,对query出来的文档进行再次过滤。

 

public class SearcherUtil {
	
	private  Directory directory=null;
	private IndexWriter writer=null;
	private IndexReader reader=null;
	private IndexSearcher searcher=null;
	
	public void createMyIndex()
	{
		try {
			directory=FSDirectory.open(new File("D:/lucene/index03"));
			writer=new IndexWriter
				(directory, new IndexWriterConfig(Version.LUCENE_45,new StandardAnalyzer(Version.LUCENE_45)));
			File[] files=new File("D:/src").listFiles();
			for(int i=0;i<files.length;i++)
			{
				File file=files[i];
				Document doc=new Document();
				doc.add(new StringField("filename", file.getName(), Field.Store.YES));
				doc.add(new StringField("path", file.getAbsolutePath(), Field.Store.YES));
				FileInputStream fis=new FileInputStream(file);
				doc.add(new TextField("contents", new BufferedReader(new InputStreamReader(fis, "UTF-8"))));
				doc.add(new LongField("size", file.length(),Field.Store.YES));
				System.out.print(file.length()+" : ");
				System.out.println("lucene crate index"+i+"from"+file.getAbsolutePath() );
				writer.addDocument(doc);
				writer.commit();
				fis.close();
				
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			if(writer!=null)
			{
				try {
					writer.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				writer=null;
			}
			
			
		}
	}
	public void searchByFilter(String string,Filter filter)
	{
		try {
			directory=FSDirectory.open(new File("D:/lucene/index03"));
			reader= DirectoryReader.open(directory);
			searcher=new IndexSearcher(reader);
			
			QueryParser queryParser=new QueryParser(Version.LUCENE_45,"contents",new StandardAnalyzer(Version.LUCENE_45));
			Query query=queryParser.parse(string);
			ScoreDoc[] sds = searcher.search(query,filter,100).scoreDocs;
		//	ScoreDoc[] sds = searcher.search(query,100).scoreDocs;
			System.out.println("找到了"+sds.length+"篇文章");
			for(ScoreDoc doc:sds){
				Document document=searcher.doc(doc.doc);
				System.out.println(document.get("size")+" : "+document.get("filename"));
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally
		{
			if(reader!=null)
				try {
					reader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
	}
	
}


测试:

public void searchByFilterTest(){
		SearcherUtil searcherUtil=new SearcherUtil();
	  // Filter filter=TermRangeFilter.newStringRange("filename", "cpp.txt","lucene.txt", true, true);
	//	Filter filter=NumericRangeFilter.newLongRange("size", (long)100, (long)500, true, true);
		Query query=new TermQuery(new Term("contents", "java"));
		Filter filter=new QueryWrapperFilter(query);
		searcherUtil.searchByFilter("lucene",filter);
	}


 


 

作者:keep_moving_cqu 发表于2013-12-29 1:40:09 原文链接
阅读:114 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>