lucene4入门(2)搜索
欢迎转载http://www.cnblogs.com/shizhongtao/p/3440479.html
接着上一篇,这里继续搜索,对于搜索和创建一样,首先你要确定搜索位置,然后用规定的类来读取。还要注意一点,确定分词器,因为不同的分词器所创建的分词规则不同。上篇我使用的是默认的分词器,这里我也先不管分词器。为了方便阅读,代码就全部粘上。
package com.bing.test; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException; import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.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.util.Version; /**
* @author bingyulei
*
*/
public class HelloLucene
{ Directory directory = null;
Document doc;
IndexWriter writer = null; /**
*
* @param indexWriterPath
* 索引创建路径
* @param filePath
* 读取文件路径
*/
public void createIndex(String indexWriterPath, String filePath)
{ // 创建indexwriter
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_45);// 设置标准分词器
// ,默认是一元分词
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_45,
analyzer);// 设置IndexWriterConfig try
{
// 创建directory
// directory=RAMDirectory();//创建在内存中
// 创建在硬盘上
directory = FSDirectory.open(new File(indexWriterPath));// 打开存放索引的路径
writer = new IndexWriter(directory, iwc); // 为document添加field
addFile(writer, filePath); System.out.println("添加成功");
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} } private void addFile(IndexWriter writer, String filePath)
{
File f = new File(filePath);
FieldType ft = new FieldType();
ft.setIndexed(true);// 索引
ft.setStored(true);// 存储,数据量比较大,一般都是不鼓励存储,放在索引文件中会把索引文件撑大
ft.setTokenized(true);
for (File file : f.listFiles())
{
try
{
// 创建Document对象
doc = new Document();
// doc.add(new Field("content", new FileReader(file), ft));
doc.add(new TextField("content", new FileReader(file)));
doc.add(new TextField("filename", file.getName(), Store.YES));
doc.add(new StringField("path", file.getPath(), Store.YES));
// 添加文档
writer.addDocument(doc);
writer.commit();// 提交数据
} catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} }
} /**
* 搜索
*
* @param path
* 搜索路径
* @param indexReaderPath
* 索引存放路径
*/
public void seacher(String indexReaderPath, String searthText)
{
IndexReader reader=null;
try
{
directory = FSDirectory.open(new File(indexReaderPath));
// 创建读取索引的reader
reader = DirectoryReader.open(directory);
// 根据reader创建search
IndexSearcher searcher = new IndexSearcher(reader);
// 创建查询,第二个参数表示查询的字段名,第三个是分词器
QueryParser parser = new QueryParser(Version.LUCENE_45, "content",
new StandardAnalyzer(Version.LUCENE_45));
// 搜索包含searthText的内容
Query query = parser.parse(searthText);
// 搜索返回10条记录
TopDocs tds = searcher.search(query, 10); //获取scoredoc对象组,
ScoreDoc[] sds=tds.scoreDocs;
for(ScoreDoc sd:sds){
//获取具体的doc
Document doc=searcher.doc(sd.doc);
System.out.println(doc.get("filename")+":"+doc.get("path"));
}
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}// 打开存放索引的路径
catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (reader!=null)
{
try
{
reader.close();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
说明,"D:\\lucene\\file"是我复制lucene官方文档上的两段话,不过当你创建完索引之后,然后再修改文件内容,新加的内容并不能搜索出来。这个应该很好理解。
然后进行测试:searchTest,就可以得到那个文本文件中有"Changing Similarity"这段字符
package com.bing.test; import org.junit.Test; public class HelloLuceneTest
{
@Test
public void writertest(){
HelloLucene test=new HelloLucene();
test.createIndex("D:\\lucene\\index","D:\\lucene\\file");
}
@Test
public void searchTest(){
HelloLucene test=new HelloLucene();
test.seacher("D:\\lucene\\index", "Changing Similarity");
}
}
lucene4入门(2)搜索的更多相关文章
- lucene4入门(1)
欢迎转载http://www.cnblogs.com/shizhongtao/p/3440325.html lucene你可以理解为一种数据库,他是全文搜索的一种引擎. 1.首先去官网download ...
- lucene4入门(3)琐记
欢迎转载http://www.cnblogs.com/shizhongtao/p/3440486.html <--这个是lucene4.6的api下载地址,格式是chm的.需要的人可以下载htt ...
- solr入门之搜索建议的几种实现方式和最终选取实现思路
上篇博客中我简单的讲了下solr自身的suggest模块来实现搜索建议.但是今天研究了下在solr自身的suggest中添加进去拼音来智能推荐时不时很方便.在次从网上搜集和整理思考了下该问题的解决. ...
- angular入门--filter搜索
首先,列表绑定忽略 先上代码 <html ng-app="app1"> <head> <meta charset='utf-8' /> < ...
- [算法入门]——深度优先搜索(DFS)
深度优先搜索(DFS) 深度优先搜索叫DFS(Depth First Search).OK,那么什么是深度优先搜索呢?_? 样例: 举个例子,你在一个方格网络中,可以简单理解为我们的地图,要从A点到B ...
- linux 入门教程
linux入门教程 搜索 Linux入门教程 前言 第一章 关于Linux的历史 第二章 图形界面还是命令窗口 第三章 Linux操作系统的安装 第四章 初步进入linux世界 第五章 Linux系统 ...
- Linux入门基础篇
Linux入门基础篇 Linux诞生 Linux发行版本说明 Linux官方网站 Linux内核官方网站 比较有名的Linux发行版 虚拟机(Virtual Machine),一个虚拟的系统,安装在系 ...
- Egret入门学习日记 --- 第二篇 (书籍的选择 && 书籍目录 && 书中 3.3 节 内容)
第二篇 (书籍的选择 && 书籍目录 && 书中 3.3 节 内容) 既然选好了Egret,那我就要想想怎么学了. 开始第一步,先加个Q群先,这不,拿到了一本<E ...
- java课程设计团队博客《基于学院的搜索引擎》
JAVA课程设计 基于学院网站的搜索引擎 对学院网站用爬虫进行抓取.建索(需要中文分词).排序(可选).搜索.数据摘要高亮.分页显示.Web界面. 一.团队介绍 学号 班级 姓名 简介 2016211 ...
随机推荐
- GLSL实现简单硬件Anisotrop Lighting 【转】
http://blog.csdn.net/a3070173/archive/2008/11/13/3294660.aspx 各向异性光照往往用于处理一些具有各向异性表面的物体,如:光盘的盘面.为避免在 ...
- OSG中的示例程序简介(转)
OSG中的示例程序简介 1.example_osganimate一)演示了路径动画的使用 (AnimationPath.AnimationPathCallback),路径动画回调可以作用在Camera ...
- oc-04-类的声明和实现
//main.m //10-[掌握]类的声明和实现 //.h为类的声明,.m为类的实现,+表示类方法静态方法,-表示对象方法..h文件中的方法都是public不能更改的.变量3中访问域:public, ...
- SQL用法总结
1.创建数据库语句 create table persons( 'id' INT NOT NULL AUTO_INCREMENT, ) NOT NULL, ) NOT NULL, PRIMARY KE ...
- IE7下position:relative的问题
如果在IE7中使用position:relative属性,需要在该元素的容器上,同时给予position:relative属性.
- 网卡及MAC和PHY的区别
转载:http://blog.sina.com.cn/s/blog_53d7350f0100mudb.html 一块以太网网卡包括OSI(开方系统互联)模型的两个层.物理层和数据链路层.物理层定义了数 ...
- Spket在Eclipse/MyEclipse下的安装和配置支持Ext(图文教程)
一.安装Spket 第一种方法:网上更新方式 1.插件首页:http://www.spket.com2.插件名称:Spket IDE3.更新连接(Update Site):http://www.spk ...
- Ubuntu配置ssh server
SSH-Server配置指南 一.SSH简介 SSH (Secure Shell)是一个应用程序中提供安全通信的协议,通过SSH协议可以安全地访问服务器,因为SSH 具有成熟的公钥加密体系,在数据进行 ...
- Google Code Jam 2009, Round 1C C. Bribe the Prisoners (记忆化dp)
Problem In a kingdom there are prison cells (numbered 1 to P) built to form a straight line segment. ...
- 使用cocos2d-x制作 Texture unpacker
使用cocos2d-x制作 Texture unpacker 没错,就是unpacker. 在大多数游戏包里面,可以找到很多纹理图集,他们基本上是用texture packer制作的,有plist文件 ...