lucene的简单使用

1、下载地址:http://archive.apache.org/dist/lucene/java/

2、往项目中导入相关包。

3、构建第一个lucene项目,该项目的功能是把文档进行索引,索引之后进行搜索。

4、代码:

       HelloWord.java

                 

public class HelloWord {
	String filePath = "磁盘文件路径";
	String indexPath = "索引结果的存放路径";
//	Analyzer analyzer = new StandardAnalyzer();// 创建一个分词器
	Analyzer analyzer = new MMAnalyzer();// 创建一个分词器
	
    @Test
	public void createIndex() throws Exception {//创建索引
		Document doc = File2DocumentUtils.file2Document(filePath);;// 将磁盘文件转成Document对象
		IndexWriter indexWriter = new IndexWriter(indexPath, analyzer, true,
				MaxFieldLength.LIMITED);
	    indexWriter.addDocument(doc);
	    indexWriter.close();
	}
    @Test
	public void search() throws Exception{//搜索功能
    	  String queryString="room";
    	
    	// 1,把要搜索的文本解析为 Query
    	   String[] fields={"name","content"};
    	   QueryParser queryParse=new MultiFieldQueryParser(fields,analyzer);//解析器
    	   Query query=queryParse.parse(queryString);//解析搜索内容
    	// 2,进行查询
    	   IndexSearcher indexSearcher=new IndexSearcher(indexPath);//搜索器
    	   Filter filter=null;
    	   TopDocs topDocs= indexSearcher.search(query, filter, 10000);
    	  	// 3,打印结果
    	   System.out.println("总共有【" + topDocs.totalHits + "】条匹配结果");
    	   for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
    		   int docSn = scoreDoc.doc; // 文档内部编号
    		    Document doc = indexSearcher.doc(docSn); // 根据编号取出相应的文档
    		    File2DocumentUtils.printDocumentInfo(doc); // 打印出文档信息  
    	   }
	}
}

  File2DocumentUtils.java

 

public class File2DocumentUtils {

	public static Document file2Document(String filePath) {
	     File file=new File(filePath);
	     Document doc=new Document();
	     doc.add(new Field("name",file.getName(),Store.YES,Index.ANALYZED));
	     doc.add(new Field("content",readFileContent(file),Store.YES,Index.ANALYZED));
	     doc.add(new Field("size",NumberTools.longToString(file.length()),Store.YES,Index.ANALYZED));
	     doc.add(new Field("path",file.getAbsolutePath(),Store.YES,Index.ANALYZED));
	     return doc;
	}

	private static String readFileContent(File file){
       try {
		BufferedReader bufer=new BufferedReader(new InputStreamReader(new FileInputStream(file))); 
		   StringBuffer buf=new StringBuffer();
		   String str="";
		
		 while( (str= bufer.readLine())!=null)
		 {
			 buf.append(str).append("\n");
		 }
	     return buf.toString();
	} catch (Exception e) {
		throw new RuntimeException(e);
	} 
		
	}

	public static void printDocumentInfo(Document doc) {
		System.out.println("------------------------------");
		System.out.println("name     = " + doc.get("name"));
		System.out.println("content  = " + doc.get("content"));
		System.out.println("size     = " + NumberTools.stringToLong(doc.get("size")));
		System.out.println("path     = " + doc.get("path"));		
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值