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/ ...
随机推荐
- Swift 数组,字典,结构体,枚举
1.数组 let types = ["none","warning","error"]//省略类型的数组声明 var menbers = [ ...
- MySQL之避免插入重复数据
INSERT ignore INTO `$table_name`($field_name) VALUES(),(),(),()"; //若重复数据可以添加,请在对应的数据表字段中添加数据库索 ...
- win10+PHP 安装redis
1.给php环境安装redis扩展 2.给电脑安装redis环境 一.为php安装redis服务 使用 phpinfo() 函数查看php对应的版本 二.去下面的两个网站下载对应版本的压缩包并解压(注 ...
- 关于`babel-loader`和`babel-core`版本兼容性问题
1. 安装babel-loader和babel-core出现问题 1.1 安装babel的转换工具包: npm i babel-core babel-loader babel-plugin-trans ...
- Qt中csv文件的导入与导出
转自:http://blog.csdn.net/mingxia_sui/article/details/7683030 CSV 1.简介: 全称:Comma Separated Values. 是“逗 ...
- stringstream转换
在这之前,在杭电刷题的时候,并没有注意到这个好东西. 使用stringstream对象简化类型转换C++标准库中的<sstream>提供了比ANSI C的<stdio.h>更高 ...
- Codeforces Round #541 (Div. 2) A.Sea Battle
链接:https://codeforces.com/contest/1131/problem/A 题意: 给两个矩形,一个再上一个在下,求两个矩形合并的周围一圈的面积. 思路: 因为存在下面矩形宽度大 ...
- 洛谷1005(dp)
1.不要贪,缩小区间去dp就好. 2.预处理指数. 3.__int128可还行. #include <cstdio> #include <cctype> #include &l ...
- 洛谷1072(gcd的运用)
已知正整数a0,a1,b0,b1,设某未知正整数x满足: 1. x 和 a0 的最大公约数是 a1: 2. x 和 b0 的最小公倍数是b1. Hankson 的“逆问题”就是求出满足条件的正整数 ...
- pip 的简单安装与基本使用
pip 是 Python 著名的包管理工具,在 Python 开发中必不可少.本文只介绍各平台最新可用并且最简单的 pip 安装方式,以方便初学者和不会敲代码只需通过 pip 安装特定工具的小伙伴们. ...