导读
本文主要是对代码的一些介绍及需要一些准备工作
项目结构: 效果图: 需要建立索引的文件(我们需要从中查找出关键字的文档) 建立好的所有文件 搜索关键字"lucene"信息 大家是不是也想亲自动手尝试一下呢... ========================================================= 代码部分 ========================================================= 准备工作: 下载完后,解压缩,可以得到: lucene-core-3.5.0.jar junit-4.7.jar 把这两个jar包加入到项目构建路径下面...看看----->项目结构 /lucene_0100_helloworld/src/com/b510/lucene/LuceneIndex.java
- /**
- *
- */
- package com.b510.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.index.CorruptIndexException;
- import org.apache.lucene.index.IndexReader;
- import org.apache.lucene.index.IndexWriter;
- import org.apache.lucene.index.IndexWriterConfig;
- 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.FSDirectory;
- import org.apache.lucene.store.LockObtainFailedException;
- import org.apache.lucene.util.Version;
-
- /**
- * Lucene create Index and search key word
- *
- * @author Hongten (hongtenzone@foxmail.com) <br />
- * @date 2012-11-28
- */
- public class LuceneIndex {
-
- /**
- * 创建索引
- */
- public void index() {
- IndexWriter writer = null;
- try {
- // 1.创建Directory
- // 这种方式是建立在内存中
- // Directory directory = new RAMDirectory();
- // 这种方式是存放在硬盘中
- Directory directory = FSDirectory.open(new File(
- "D:/WordPlace/lucene/lucene_0100_helloworld/lucene/index"));
- // 2.创建IndexWriter
- IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35,
- new StandardAnalyzer(Version.LUCENE_35));
-
- writer = new IndexWriter(directory, iwc);
- // 3.创建Document对象
- Document doc = null;
- // 4.为Document添加Field
- File f = new File(
- "D:/WordPlace/lucene/lucene_0100_helloworld/lucene/example");
- for (File file : f.listFiles()) {
- doc = new Document();
- doc.add(new Field("content", new FileReader(file)));
- doc.add(new Field("filename", file.getName(), Field.Store.YES,
- Field.Index.NOT_ANALYZED));
- doc.add(new Field("path", file.getAbsolutePath(),
- Field.Store.YES, Field.Index.NOT_ANALYZED));
- // 5.通过IndexWriter添加文档到索引中
- writer.addDocument(doc);
- }
- } catch (CorruptIndexException ce) {
- ce.printStackTrace();
- } catch (LockObtainFailedException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (writer != null) {
- writer.close();
- }
- } catch (CorruptIndexException e2) {
- e2.printStackTrace();
- } catch (IOException ioe) {
- ioe.printStackTrace();
- }
- }
- }
-
- /**
- * 搜索关键字为key的n条记录
- *
- * @param key
- * 关键字
- * @param n
- * 搜索的记录数
- */
- public void search(String key, int n) {
- try {
- // 1.创建Directory
- Directory directory = FSDirectory.open(new File(
- "D:/WordPlace/lucene/lucene_0100_helloworld/lucene/index"));
- // 2.创建IndexReader
- IndexReader reader = IndexReader.open(directory);
- // 3.根据IndexReader创建IndexSearcher
- IndexSearcher searcher = new IndexSearcher(reader);
- // 4.创建搜索的Query
- // 创建parser来确定要搜索的文件的内容,第二个参数表示搜索的域
- QueryParser parser = new QueryParser(Version.LUCENE_35, "content",
- new StandardAnalyzer(Version.LUCENE_35));
- // 创建query,表示搜索域为content中包含key的文档
- Query query = parser.parse(key);
- // 5.根据searcher搜索并返回TopDocs
- TopDocs tds = searcher.search(query, n);
- // 6.根据TopDocs获取ScoreDoc对象
- ScoreDoc[] sds = tds.scoreDocs;
- for (ScoreDoc sd : sds) {
- // 7.根据searcher和ScordDoc对象获取具体的Document对象
- Document document = searcher.doc(sd.doc);
- // 8.根据Document对象获取需要的值
- System.out.println("文件名称:[" + document.get("filename")
- + "] 文件路径:[" + document.get("path") + "]");
- }
-
- // 9.关闭reader
- reader.close();
- } catch (CorruptIndexException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (ParseException e) {
- e.printStackTrace();
- }
- }
-
- }
复制代码
/lucene_0100_helloworld/src/com/b510/lucene/LuceneIndexTest.java
- /**
- *
- */
- package com.b510.lucene;
-
- import org.junit.Test;
-
- /**
- * @author Hongten (hongtenzone@foxmail.com) <br />
- * @date 2012-11-28
- */
- public class LuceneIndexTest {
-
- @Test
- public final void testIndex() {
- LuceneIndex luceneIndex= new LuceneIndex();
- luceneIndex.index();
- }
-
- @Test
- public final void testSearch(){
- LuceneIndex luceneIndex= new LuceneIndex();
- luceneIndex.search("lucene", 10);
- }
-
- }
复制代码
链接:http://pan.baidu.com/s/1pJyo9Zl 密码:ko2k
|