首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Lucene2.4第一个容易实例

2012-11-01 
Lucene2.4第一个简单实例?首先来认识下全文检索的工作流程:???java? Project 目录结构如图:??? package co

Lucene2.4第一个简单实例



?首先来认识下全文检索的工作流程:

?

?
Lucene2.4第一个容易实例
?

java? Project 目录结构如图:


Lucene2.4第一个容易实例

?

?


?

 package com.lebuqi.lucene.utils; import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.InputStreamReader;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.document.Field.Index;import org.apache.lucene.document.Field.Store;/**   * @Title: File2Document.java  * @Package com.lebuqi.lucene.utils  * @Description: TODO(添加描述)  * @author longzhun * @date 2011-8-6 下午07:46:51  * @version V1.0  */public class File2Document {public static Document file2Document(String path){File file = new File(path);//文件.name,content.size,pathDocument 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", String.valueOf(file.length()),Store.YES,Index.NOT_ANALYZED));doc.add(new Field("path", file.getAbsolutePath(),Store.YES,Index.NO ));return doc;}private static String readFileContent(File file) {try {BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));StringBuffer content = new StringBuffer();for(String line = null;(line = br.readLine())!= null;){content.append(line).append("\n");}return content.toString();} catch (Exception e) {throw new RuntimeException(e);}}}

?

热点排行