Lucene 3.2 具体实现

package lucene;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.DateTools;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
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.store.LockObtainFailedException;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;

public class T2 {

@SuppressWarnings("deprecation")
public void index(String indexDir, String dataDir)
throws CorruptIndexException, LockObtainFailedException,
IOException, ParseException {
// 设置分词版本
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_32);
// 存储在内存
// Directory directory = new RAMDirectory();

// 存储在硬盘
Directory directory = new SimpleFSDirectory(new File(indexDir));

/* 这种方式是最新的,但是不能删除以前的索引 */
// IndexWriter writer = new IndexWriter(directory, new
// IndexWriterConfig(
// Version.LUCENE_32, analyzer));
IndexWriter writer = new IndexWriter(directory, analyzer, true,
IndexWriter.MaxFieldLength.UNLIMITED);

// document.add(new Field("fieldname", text, Field.Store.YES,
// Field.Index.ANALYZED));

File sourceFile = new File(dataDir);
File[] files = sourceFile.listFiles();
for (File file : files) {
Document document = new Document();
// document.add(new Field("contents", new FileReader(file)));
document.add(new Field("filename", file.getName(), Field.Store.YES,
Field.Index.ANALYZED));
document.add(new Field("indexDate", DateTools.dateToString(
new Date(), DateTools.Resolution.DAY), Field.Store.YES,
Field.Index.ANALYZED_NO_NORMS));
document.add(new Field("fileSize", (file.length() / 1024) + "k",
Field.Store.YES, Field.Index.ANALYZED_NO_NORMS));
document.add(new Field("filePath", file.getAbsolutePath(),
Field.Store.YES, Field.Index.ANALYZED_NO_NORMS));
document.add(new Field("contents", getComtent(file),
Field.Store.YES, Field.Index.ANALYZED_NO_NORMS));
writer.addDocument(document);
}
System.out.println("索引数目: " + writer.numDocs());
writer.close();
directory.close();
}

public String getComtent(File file) throws IOException {
InputStreamReader reader = new InputStreamReader(new FileInputStream(
file));
BufferedReader br = new BufferedReader(reader);
StringBuilder sb = new StringBuilder();
String tem = null;
while ((tem = br.readLine()) != null) {
sb.append(tem + "\n");
}
return sb.toString();
}

/**
*
* @param indexDir
* 索引目录
* @param type
* 类型,1,内容,2.文件名称
* @param vlue
* 要查询的内容
* @throws IOException
* @throws ParseException
*/
public void search(String indexDir, int type, String vlue)
throws IOException, ParseException {
Directory dir = new SimpleFSDirectory(new File(indexDir));
// 创建 IndexSearcher对象,相比IndexWriter对象,这个参数就要提供一个索引的目录就行了
IndexSearcher indexSearch = new IndexSearcher(dir);
// 创建QueryParser对象,第一个参数表示Lucene的版本,第二个表示搜索Field的字段,第三个表示搜索使用分词器
QueryParser queryParser = null;
if (type == 1) {
queryParser = new QueryParser(Version.LUCENE_30, "contents",
new StandardAnalyzer(Version.LUCENE_30));
} else {
queryParser = new QueryParser(Version.LUCENE_30, "filename",
new StandardAnalyzer(Version.LUCENE_30));
}
// 生成Query对象
Query query = queryParser.parse(vlue);// 设置查询语句
// 搜索结果 TopDocs里面有scoreDocs[]数组,里面保存着索引值
TopDocs hits = indexSearch.search(query, 10);
// hits.totalHits表示一共搜到多少个
System.out.println("找到了" + hits.totalHits + "个");
// 循环hits.scoreDocs数据,并使用indexSearch.doc方法把Document还原,再拿出对应的字段的值
for (int i = 0; i < hits.scoreDocs.length; i++) {
ScoreDoc sdoc = hits.scoreDocs[i];
Document doc = indexSearch.doc(sdoc.doc);
// System.out.println(doc.get("filename"));
// System.out.println(doc.get("fileSize"));
System.out.println(doc.get("filePath"));
// System.out.println(doc.get("contents"));
}
indexSearch.close();

}

public static void main(String[] args) throws CorruptIndexException,
LockObtainFailedException, IOException, ParseException {
T2 t2 = new T2();
String indexDir = "D:\\lucene\\index";
String dataDir = "D:\\lucene\\data";
// t2.index(indexDir, dataDir);
t2.search(indexDir, 1, "真正的主人");

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值