package org.itat.test;

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.store.RAMDirectory;
import org.apache.lucene.util.Version; public class HelloLucene
{
/**
* 建立索引
* @throws IOException
* @throws CorruptIndexException
*/
public void index() throws CorruptIndexException, IOException
{
//1. 创建Directory
Directory directory = new RAMDirectory();//建立在内存中的
//Directory directory = FSDirectory.open(new File("d:/lucene1/index01")); //2. 创建indexWriter
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35));
IndexWriter writer = null; try
{
writer = new IndexWriter(directory, iwc);
//3. 创建document对象
Document doc = null; //4. 为document添加Field
File f = new File("D:/lucene"); 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 e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch (LockObtainFailedException e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch (IOException e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
}
finally
{
if(writer != null)
{
writer.close();
}
}
} @SuppressWarnings("resource")
public void search() throws ParseException
{ //1. 创建Directory
Directory directory;
IndexReader reader;
try
{
directory = FSDirectory.open(new File("d:/lucene1/index01"));
//2. 创建indexReader
reader = IndexReader.open(directory);
//3. 根据indexReader 创建IndexSearcher
//4. 创建搜索的Query
QueryParser parser = new QueryParser(Version.LUCENE_35, "content", new StandardAnalyzer(Version.LUCENE_35));
//创建parser来确定要搜索文件的内容,第二个参数 表示搜索的域
//创建query,表示搜索域为content中包含java的文档
Query query = parser.parse("com");
//5. 根据sea人撤人搜索并且返回TopDocs
TopDocs tds = new IndexSearcher(reader).search(query, 100);
//6. 根据TopDocs 获取ScoreDOc对象
ScoreDoc[] sds = tds.scoreDocs;
for(ScoreDoc sd : sds)
{
//7. 根据searcher和scoreDoc对象获取具体的Document对象
Document d = new IndexSearcher(reader).doc(sd.doc);
//8. 根据Document对象获取需要的值
System.out.println(d.get("filename") + "[" + d.get("path") + "]");
} //9. 关闭reader
reader.close(); }
catch (IOException e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}

测试类:

package org.itat.test;

import java.io.IOException;

import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.queryParser.ParseException;
import org.junit.Test; public class TestLucene
{
@Test
public void testIndex() throws CorruptIndexException, IOException, ParseException
{ HelloLucene hl = new HelloLucene();
hl.index();
} @Test
public void testSearcher() throws ParseException
{
HelloLucene hl = new HelloLucene();
// hl.index();
hl.search();
}
}

Lucene小例子的更多相关文章

  1. lucene.net 3.0.3、结合盘古分词进行搜索的小例子(转)

    lucene.net 3.0.3.结合盘古分词进行搜索的小例子(分页功能)   添加:2013-12-25 更新:2013-12-26 新增分页功能. 更新:2013-12-27 新增按分类查询功能, ...

  2. 2、Lucene 最简单的使用(小例子)

    在了解了Lucene以后,我打算亲手来做一个Lucene的小例子,这个例子只是Lucene最简单的应用:使用Lucene实现标准的英文搜索: 1.下载Lucene 下载Lucene,到Lucene的官 ...

  3. springmvc入门的第一个小例子

    今天我们探讨一下springmvc,由于是初学,所以简单的了解一下 springmvc的流程,后续会持续更新... 由一个小例子来简单的了解一下 springmvc springmvc是spring框 ...

  4. java即时通信小例子

    学习java一段时间了,今天写来一个即时通信的小例子练手在其过程中也学到了一些知识拿出来和大家分享,请路过的各位大神多多赐教... 好了下面讲一下基本的思路: 首先,编写服务器端的程序,简单点说吧就是 ...

  5. Runtime的几个小例子(含Demo)

    一.什么是runtime(也就是所谓的“运行时”,因为是在运行时实现的.)           1.runtime是一套底层的c语言API(包括很多强大实用的c语言类型,c语言函数);  [runti ...

  6. bootstrap 模态 modal 小例子

    bootstrap 模态 modal  小例子 <html> <head> <meta charset="utf-8" /> <title ...

  7. INI配置文件分析小例子

    随手写个解析INI配置字符串的小例子 带测试 #include <iostream> #include <map> #include <string> #inclu ...

  8. JavaScript小例子:复选框全选

    JavaScript小例子:复选框全选 这只是一个小例子,很简单,但是这个功能还是很常用的: 实现后效果如图: JavaScript代码: <script type="text/jav ...

  9. 【zTree】 zTree使用的 小例子

    使用zTree树不是第一次了  但是 还是翻阅着之前做的 对照着 使用起来比较方便  这里就把小例子列出来   总结一下使用步骤 这样方便下次使用起来方便一点 使用zTree树的步骤: 1.首先  在 ...

随机推荐

  1. IntelliJ IDEA 常用快捷键总结

    个人使用频率的高低排序: Alt+enter               代码提示 Alt+7                查看类的方法 Alt+insert        生成get和set方法 ...

  2. GRPC协议的相关原理

    GRPC的Client与Server,均通过Netty Channel作为数据通信,序列化.反序列化则使用Protobuf,每个请求都将被封装成HTTP2的Stream,在整个生命周期中,客户端Cha ...

  3. js文件/图片从电脑里面拖拽到浏览器上传文件/图片

    1.效果展示 2.html 代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <!DOCTYPE html> <html lang=& ...

  4. Attempt to invoke virtual method 'void android.app.ActionBar.setTitle的解决方法

    在安卓4.4.2的关于蓝牙开发的一个sample BluetoothChat中,调试时,老是出错:Attempt to invoke virtual method 'void android.app. ...

  5. GO -- socket读取内容

    func handleRead(conn net.Conn, done chan string) { for { buf := make([]) reqLen, err := conn.Read(bu ...

  6. 基于GPU加速的三维空间分析【转】

    基于GPU加速的三维空间分析 标签:supermap地理信息系统gisit 文:李凯 随着三维GIS 的快速发展和应用普及,三维空间分析技术以其应用中的实用性成为当前GIS技术研究的热点领域.面对日益 ...

  7. pandas常见函数详细使用

    groupby函数 pandas提供了一个灵活高效的groupby功能,它使你能以一种自然的方式对数据集进行切片.切块.摘要等操作,根据一个或多个键(可以是函数.数组.Series或DataFrame ...

  8. PPAPI插件的动态创建、改动、删除

    一旦你完毕了PPAPI插件的开发,实际使用时可能会有下列需求: 动态创建PPAPI插件 删除PPAPI插件 改变PPAPI插件的尺寸 实现起来非常easy,从JS里直接訪问DOM(BOM)就可以.以下 ...

  9. Odoo webinar

    分享些 odoo9 webinar 视频     https://pan.baidu.com/s/1pLF5njt

  10. Food hub

    Work center List Tillage 耕作 Hand harvest 手工采收 Planting 种植 Cultivating 培养 Mulching 覆盖 Dig harvest 挖地采 ...