lucene4.7实例详解
java.lang.UnsupportedClassVersionError: org/apache/lucene/index/IndexableField : Unsupported major.minor version 51.0
Apache Lucene 4.8.0发布:不再支持Java 6,因为Lucene4.9要求Java版本最低为Java7,本人用的是4.7.2因为我安装的是jdk6
一。lucene4.7版本的实例1
public class IndexFile {
protected String[] ids={"1", "2"};
protected String[] content={"Amsterdam has lost of add cancals", "i love add this girl"};
protected String[] city={"Amsterdam", "Venice"};
private Directory dir;
/**
* 初始添加文档
* @throws Exception
*/
@Before
public void init() throws Exception {
String pathFile="E:/indexPath";//创建索引存放位置
//1.4 通过Analyzer 的创建指定索引语言词汇的分析器
Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_47);
//1.3 通过IndexWriterConfig的创建指定索引版本和语言词汇分析器
IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_47, analyzer);
//1.2 通过Directory的创建指定索引存放位置
dir=FSDirectory.open(new File(pathFile));
//1.1 创建IndexWriter,它的作用是用来写索引文件,
//可以将IndexWriter看做是一个特定类型的数据库,用来存放各种表,可以将Document看做是一张张的表
//该方法有两个参数,第一个dir参数为索引存放位置,参数类型为Directory,第二个参数conf为 IndexWriter的配置类
IndexWriter writer = new IndexWriter(dir, iwc);
for(int i=0; i < ids.length; i++) {
<span style="white-space:pre"> </span>//2.1 创建Document指定要索引的文档
<span style="white-space:pre"> </span>//可以将Document看做是数据库中的一张张的表,而每个field都是表中的一个colum用来存放各种类型的信息,如标题、作者、时间等等
Document doc=new Document();
doc.add(new StringField("id", ids[i], Store.YES));
doc.add(new TextField("content", content[i], Store.YES));
doc.add(new StringField("city", city[i], Store.YES));
writer.addDocument(doc);
}
//2.2表(Document)创建好之后,当然要添加到数据库(IndexWriter)中,同时commit
writer.commit();
writer.close();
}
/**
* 查询
* @throws Exception
*/
@Test
public void search() throws Exception {
String filePath="E:/indexPath";
//1.3 指定搜索目录
Directory dir=FSDirectory.open(new File(filePath));
//1.2创建IndexReader将搜索目录读取到内存
IndexReader reader=DirectoryReader.open(dir);
//1.1创建IndexSearcher准备搜索
IndexSearcher searcher=new IndexSearcher(reader);
//2.3 在content索引区搜索关键字“add”
Term term=new Term("content", "add");
//2.2 创建Query生成查询语法树
TermQuery query=new TermQuery(term);
//2.1 获取搜索结果,搜索相似度最高的5条记录
TopDocs topdocs=searcher.search(query, 5);
ScoreDoc[] scoreDocs=topdocs.scoreDocs;
System.out.println("查询结果总数: " + topdocs.totalHits+" 最大的评分:"+topdocs.getMaxScore());
for(int i=0; i < scoreDocs.length; i++) {
int doc = scoreDocs[i].doc;
Document document = searcher.doc(doc);//命中的文件
System.out.println("content===="+document.get("content"));
System.out.println("id:" + scoreDocs[i].doc + " score:" + scoreDocs[i].score+" index:"+scoreDocs[i].shardIndex);
}
reader.close();
}
}
二、lucene4.6实例2
public class Constants {
public final static String INDEX_FILE_PATH = "F:\\lucene\\test"; //索引的文件的存放路径 测试时可以在本目录下自行建一些文档,内容自行编辑即可
public final static String INDEX_STORE_PATH = "F:\\lucene\\index"; //索引的存放位置
}
public class LuceneIndex {
/**
* 创建索引
* @param analyzer
* @throws Exception
*/
public static void createIndex(Analyzer analyzer) throws Exception{
Directory dire=FSDirectory.open(new File(Constants.INDEX_STORE_PATH));
IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_46, analyzer);
IndexWriter iw=new IndexWriter(dire, iwc);
this.addDoc(iw);
iw.close();
}
/**
* 动态添加Document
* @param iw
* @throws Exception
*/
public static void addDoc(IndexWriter iw) throws Exception{
File[] files=new File(Constants.INDEX_FILE_PATH).listFiles();
for (File file : files) {
Document doc=new Document();
String content=LuceneIndex.getContent(file);
String name=file.getName();
String path=file.getAbsolutePath();
doc.add(new TextField("content", content, Store.YES));
doc.add(new TextField("name", name, Store.YES));
doc.add(new TextField("path", path,Store.YES));
System.out.println(name+"==="+content+"==="+path);
iw.addDocument(doc);
iw.commit();
}
}
/**
* 获取文本内容
* @param file
* @return
* @throws Exception
*/
@SuppressWarnings("resource")
public static String getContent(File file) throws Exception{
FileInputStream fis=new FileInputStream(file);
InputStreamReader isr=new InputStreamReader(fis);
BufferedReader br=new BufferedReader(isr);
StringBuffer sb=new StringBuffer();
String line=br.readLine();
while(line!=null){
sb.append(line+"\n");
line=null;
}
return sb.toString();
}
/**
* 搜索
* @param query
* @throws Exception
*/
private static void search(Query query) throws Exception {
Directory dire=FSDirectory.open(new File(Constants.INDEX_STORE_PATH));
IndexReader ir=DirectoryReader.open(dire);
IndexSearcher is=new IndexSearcher(ir);
TopDocs td=is.search(query, 1000);
System.out.println("共为您查找到"+td.totalHits+"条结果");
ScoreDoc[] sds =td.scoreDocs;
for (ScoreDoc sd : sds) {
Document d = is.doc(sd.doc);
System.out.println(d.get("path") + ":["+d.get("path")+"]");
}
}
public static void main(String[] args) throws Exception, Exception {
Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_46);
LuceneIndex.createIndex(analyzer);
QueryParser parser = new QueryParser(Version.LUCENE_46, "content", analyzer);
Query query = parser.parse("人");
LuceneIndex.search(query);
}
}
lucene4.7实例详解的更多相关文章
- linux基础-磁盘阵列(RAID)实例详解
磁盘阵列(RAID)实例详解 raid技术分类 软raid技术 硬raid技术 Raid和lvm的区别 为什么选择用raid RAID详解 RAID-0 RAID-1 RAID-5 Raid-10 R ...
- Cocos2d-x 3.X手游开发实例详解
Cocos2d-x 3.X手游开发实例详解(最新最简Cocos2d-x手机游戏开发学习方法,以热门游戏2048.卡牌为例,完整再现手游的开发过程,实例丰富,代码完备,Cocos2d-x作者之一林顺和泰 ...
- JavaScript学习笔记-实例详解-类(二)
实例详解-类(二) //===给Object.prototype添加只读\不可枚举\不可配置的属性objectId(function(){ Object.defineProperty(Object ...
- JavaScript学习笔记-实例详解-类(一)
实例详解-类(一): //每个javascript函数(除了bind())都自动拥有一个prototype对象// 在未添加属性或重写prototype对象之前,它只包含唯一一个不可枚举属性const ...
- Entity Framework实例详解
Entity Framework Code First的默认行为是使用一系列约定将POCO类映射到表.然而,有时候,不能也不想遵循这些约定,那就需要重写它们.重写默认约定有两种方式:Data Anno ...
- 免费的HTML5连载来了《HTML5网页开发实例详解》连载(二)
最近新浪.百度.腾讯.京东.大众点评.淘宝等流行的网站都加大了招聘HTML5的力度,HTML5开发人员成了抢手货,本次连载的是由大众点评前端工程师和一淘网前端工程师基情奉献的<HTML5网页开发 ...
- Linux下rz命令使用的实例详解
Linux中rz命令和sz命令都可用于文件传输,而rz命令主要用于文件的上传,下面将通过几个实例来给大家详细介绍下Linux下rz命令的用法,一起来学习下吧. rz命令可以批量上传文件,当然也可上传单 ...
- 实例详解 DB2 排序监控和调优
实例详解 DB2 排序监控和调优http://automationqa.com/forum.php?mod=viewthread&tid=2882&fromuid=2
- 转:【工欲善其事必先利其器】—Entity Framework实例详解
开始本篇文章之前,先说一下Entity Framework 6 Alpha1在NuGet中已可用,原文链接http://blogs.msdn.com/b/adonet/archive/2012/10/ ...
随机推荐
- MYSQL5.7版本解决sql_mode=only_full_group_by问题
在安装有些二开框架时会遇到下面的问题,在填写完数据库密码之后他会提示你请在mysql配置文件中修改ql-mode去掉ONLY_FULL_GROUP_BY,但是我们去mysql的配置文件中查找此配置,有 ...
- CSS小技巧收藏
居中对齐 很多时候我们需要把一个元素在其父级容器里水平.垂直居中对齐.以下我列出了常用的几种方法: 1.在知道子元素宽度与高度的情况下进行居中,采用位置定位:absolute + margin .pa ...
- gSoap学习笔记
http://www.cnblogs.com/xiangism/archive/2012/11/14/2770242.html http://www.cnblogs.com/lvkun/archive ...
- LightOJ 1022 【读题】
求阴影面积: 犯了两个错误,漏看了两个条件. 第一个wa:题面中PI说要取pi = 2 * acos (0.0) 第二个wa: For example, add 10-9 to your result ...
- 解决MySql报错:1130 - Host 'xxx' is not allowed to connect to this MySQL server的方法
发现问题 使用Navicat连接MySql数据库时,未能成功,提示信息如下图: 这个错误提示已经很明确了,"不允许主机'desktop-teat9ob'连接到此mysql服务器", ...
- Unity 5.4 公开测试版发布:增强的视觉效果,更佳的性能表现
为用户提供可靠稳定的产品是我们的一贯使命,现在我们将发布Unity 5.4 beta版本,提供所有的用户公开测试,这包含了Unity Personal Edition版本用户.我们非常希望大家下载并尝 ...
- Unity3D之如何将包大小减少到极致
http://www.luzexi.com Unity3D之如何将包大小减少到极致,图片是游戏app里最最占空间的资源,所以请各位还没有理解u3d对图片文件存储方式理解的请看<unity3d-t ...
- Unity3D研究院之IOS Android支持中文与本地文件的读取写
前几天有个朋友问我为什么在IOS平台中可以正常的读写文件可是在Android平台中就无法正常的读写.当时因为在上班所以我没时间来帮他解决,晚上回家后我就拿起安卓手机真机调试很快就定位问题所在,原 ...
- bzoj 3876: [Ahoi2014&Jsoi2014]支线剧情【有上下界有源汇最小费用最大流】
每条边流量有下界有费用,很显然是有上下界有源汇最小费用最大流 连边(s,1,(0,inf),0),(i,t,(0,inf),0),表示从1出发inf次从每个点结束inf次 连边(i,j,(1,inf) ...
- Node.js 使用Stream的pipe(管道)方法实现文件复制
Stream模块有一个pipe方法,可以将两个流串起来,实现所有的数据自动从Readable流进入Writable流 "use strict"; const fs = requir ...