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实例详解的更多相关文章

  1. linux基础-磁盘阵列(RAID)实例详解

    磁盘阵列(RAID)实例详解 raid技术分类 软raid技术 硬raid技术 Raid和lvm的区别 为什么选择用raid RAID详解 RAID-0 RAID-1 RAID-5 Raid-10 R ...

  2. Cocos2d-x 3.X手游开发实例详解

    Cocos2d-x 3.X手游开发实例详解(最新最简Cocos2d-x手机游戏开发学习方法,以热门游戏2048.卡牌为例,完整再现手游的开发过程,实例丰富,代码完备,Cocos2d-x作者之一林顺和泰 ...

  3. JavaScript学习笔记-实例详解-类(二)

    实例详解-类(二)   //===给Object.prototype添加只读\不可枚举\不可配置的属性objectId(function(){ Object.defineProperty(Object ...

  4. JavaScript学习笔记-实例详解-类(一)

    实例详解-类(一): //每个javascript函数(除了bind())都自动拥有一个prototype对象// 在未添加属性或重写prototype对象之前,它只包含唯一一个不可枚举属性const ...

  5. Entity Framework实例详解

    Entity Framework Code First的默认行为是使用一系列约定将POCO类映射到表.然而,有时候,不能也不想遵循这些约定,那就需要重写它们.重写默认约定有两种方式:Data Anno ...

  6. 免费的HTML5连载来了《HTML5网页开发实例详解》连载(二)

    最近新浪.百度.腾讯.京东.大众点评.淘宝等流行的网站都加大了招聘HTML5的力度,HTML5开发人员成了抢手货,本次连载的是由大众点评前端工程师和一淘网前端工程师基情奉献的<HTML5网页开发 ...

  7. Linux下rz命令使用的实例详解

    Linux中rz命令和sz命令都可用于文件传输,而rz命令主要用于文件的上传,下面将通过几个实例来给大家详细介绍下Linux下rz命令的用法,一起来学习下吧. rz命令可以批量上传文件,当然也可上传单 ...

  8. 实例详解 DB2 排序监控和调优

    实例详解 DB2 排序监控和调优http://automationqa.com/forum.php?mod=viewthread&tid=2882&fromuid=2

  9. 转:【工欲善其事必先利其器】—Entity Framework实例详解

    开始本篇文章之前,先说一下Entity Framework 6 Alpha1在NuGet中已可用,原文链接http://blogs.msdn.com/b/adonet/archive/2012/10/ ...

随机推荐

  1. Sping中使用Junit进行测试

    分析: 1.应用程序的入口 main方法2.junit单元测试中,没有main方法也能执行 junit集成了一个main方法 该方法就会判断当前测试类中哪些方法有 @Test注解 junit就让有Te ...

  2. 51nod 1297

    思路: 搞个栈模拟一下,也才5w; 直接wa1了..然后想到井口如果都进不去那就...一定GG了. 所以维护一下从井口到井底是非递增的就好了: #include <cstdio> #inc ...

  3. JAG Practice Contest for ACM-ICPC Asia Regional 2016B题【BFS】

    题意: 就是公主要逃跑,士兵要抓公主,问你能不能逃跑哇: 思路: 就是终点搞成起点,然后BFS一下就好了,最后枚举一下出口到公主的距离是不是<所有的到士兵的距离: #include <bi ...

  4. 笔记-JavaWeb学习之旅8

    Window对象-定时器方法 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  5. 图片美化增强AI接口调用手册

    在调合合AI平台提供的图片美化增强API接口,API平台链接:https://ai.ccint.com/doc/api/crop_enhance_image, 因为有遇到一些问题,写篇博客记录一下 A ...

  6. P3809【模板】后缀排序

    传送门 深入理解了一波后缀数组,这东西真的很妙诶,自己推感觉完全不现实,看来只能靠背代码了 这段时间就多敲敲,把板子记熟吧 代码: #include<cstdio> #include< ...

  7. kuangbin大佬的高斯消元模板

    dalao解释的博客 #include <bits/stdc++.h> using namespace std; ; int a[MAXN][MAXN];//增广矩阵 int x[MAXN ...

  8. .Net Core应用框架Util介绍(二) 转

    Util的开源地址 https://github.com/dotnetcore/util Util的开源协议 Util以MIT协议开源,这是目前最宽松的开源协议,你不仅可以用于商业项目,还能把Util ...

  9. 深度学习环境搭建(Ubuntu16.04+GTX1080Ti+CUDA8.0+Cudnn6.0+TensorFlow+Caffe2(Pytorch))

    OS System:Ubuntu16.04 GPU Device:GTX1080Ti Softwares:CUDA8.0.Cudnn6.0.TensorFlow(1.4.0).Caffe2(1.0.0 ...

  10. C/C++程序员应聘常见面试题深入剖析(2)

    摘自:http://blog.csdn.net/zhoudengqing 3.内功题 试题1:分别给出BOOL,int,float,指针变量 与“零值”比较的 if 语句(假设变量名为var) 解答: ...