lucene3.6 mysql_lucene3.6.1 经典案例 入门教程

本文详细介绍了使用Apache Lucene进行文本索引和搜索的基本步骤,包括下载核心库、创建索引、索引文件操作及测试查询。通过实例演示了如何在C盘创建源文件夹、索引文件夹,并使用TextFileIndexer和TestQuery类实现索引和搜索功能。
摘要由CSDN通过智能技术生成

第一步:下载lucene的核心包

拷贝到项目的lib 文件夹里

第二步:

在C盘下建立source文件夹   (C:\source)

source文件夹存放待索引的文件,例如,建立两个文件,名称为 test1.txt  test2.txt  。

test1.txt文件内容为:欢迎来到绝对秋香的博客。

test2.txt文件内容为:绝对秋香引领你走向潮流。

在C盘下再建立index文件夹,存放索引文件 (C:\index)

第三步,建立索引类 TextFileIndexer ,并运行主函数

package com.newtouchone.lucene;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Date;

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;

import org.apache.lucene.index.IndexWriter;

import org.apache.lucene.index.IndexWriterConfig;

import org.apache.lucene.index.IndexWriterConfig.OpenMode;

import org.apache.lucene.store.Directory;

import org.apache.lucene.store.FSDirectory;

import org.apache.lucene.util.Version;

public class TextFileIndexer {

public static void main(String[] args) throws Exception {

/* 指明要索引文件夹的位置,这里是C盘的source文件夹下 */

File fileDir = new File("C:\\source");

/* 这里放索引文件的位置 */

File indexDir = new File("C:\\index");

Directory dir = FSDirectory.open(indexDir);

Analyzer luceneAnalyzer = new StandardAnalyzer(Version.LUCENE_36);

IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_36,luceneAnalyzer);

iwc.setOpenMode(OpenMode.CREATE);

IndexWriter indexWriter = new IndexWriter(dir,iwc);

File[] textFiles = fileDir.listFiles();

long startTime = new Date().getTime();

//增加document到索引去

for (int i = 0; i < textFiles.length; i++) {

if (textFiles[i].isFile()

&& textFiles[i].getName().endsWith(".txt")) {

System.out.println("File " + textFiles[i].getCanonicalPath()

+ "正在被索引....");

String temp = FileReaderAll(textFiles[i].getCanonicalPath(),

"GBK");

System.out.println(temp);

Document document = new Document();

Field FieldPath = new Field("path", textFiles[i].getPath(),

Field.Store.YES, Field.Index.NO);

Field FieldBody = new Field("body", temp, Field.Store.YES,

Field.Index.ANALYZED,

Field.TermVector.WITH_POSITIONS_OFFSETS);

document.add(FieldPath);

document.add(FieldBody);

indexWriter.addDocument(document);

}

}

indexWriter.close();

//测试一下索引的时间

long endTime = new Date().getTime();

System.out

.println("这花费了"

+ (endTime - startTime)

+ " 毫秒来把文档增加到索引里面去!"

+ fileDir.getPath());

}

public static String FileReaderAll(String FileName, String charset)

throws IOException {

BufferedReader reader = new BufferedReader(new InputStreamReader(

new FileInputStream(FileName), charset));

String line = new String();

String temp = new String();

while ((line = reader.readLine()) != null) {

temp += line;

}

reader.close();

return temp;

}

}

输出结果为:

File C:\source\test1.txt正在被索引....

欢迎来到绝对秋香的博客。

File C:\source\test2.txt正在被索引....

绝对秋香引领你走向潮流。

这花费了641 毫秒来把文档增加到索引里面去!C:\source

第四步,建立测试类TestQuery,并运行主函数,输出测试结果

package com.newtouchone.lucene;

import java.io.File;

import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;

import org.apache.lucene.analysis.standard.StandardAnalyzer;

import org.apache.lucene.index.IndexReader;

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.FSDirectory;

import org.apache.lucene.util.Version;

public class TestQuery {

public static void main(String[] args) throws IOException, ParseException {

String index = "C:\\index"; //搜索的索引路径

IndexReader reader = IndexReader.open(FSDirectory.open(new File(index)));

IndexSearcher searcher = new IndexSearcher(reader);

ScoreDoc[] hits = null;

String queryString = "绝对秋香"; //搜索的关键词

Query query = null;

Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);

try {

QueryParser qp = new QueryParser(Version.LUCENE_36,"body", analyzer);

query = qp.parse(queryString);

} catch (ParseException e) {

}

if (searcher != null) {

TopDocs results = searcher.search(query,10); //返回最多为10条记录

hits = results.scoreDocs;

if (hits.length > 0) {

System.out.println("找到:" + hits.length + " 个结果!");

}

searcher.close();

}

}

}

测试输出结果为:

找到:2 个结果!

附件homework.rar为项目文件,解压部署则可运行该lucene案例

12

2

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2012-09-12 10:47

浏览 17004

评论

12 楼

semimi1002

2015-01-06

我直接将项目导入然后运行,报错:Exception in thread "main" org.apache.lucene.index.IndexNotFoundException: no segments* file found in org.apache.lucene.store.MMapDirectory@C:\index lockFactory=org.apache.lucene.store.NativeFSLockFactory@d8a7efd: files: [],能告知一下么

11 楼

adanbaron

2014-10-30

38687d1a1ad71d37c86f287056834d1a.gif 很容易看懂,不像其他那里的,什么都说不清楚

10 楼

王鹏程

2014-10-02

好,非常好

9 楼

love521myself

2014-07-01

同意5楼的说法,楼主给解释一下哈

8 楼

Godlovequan

2014-01-06

感谢,不知道最新的4.6版本怎么弄!

7 楼

lvwenwen

2013-09-01

哥们不错,有可以跑的工程,有些就贴出代码,还要到处找包,麻烦,你这效率高

6 楼

guyanliang

2013-06-01

谢谢分享

50880c61e3199786f144db0328de5b6f.gif 

2fd2d9c1d175d11752585fa9b76aba29.gif

5 楼

asdfasdf

2013-05-30

索引好多,总是检索出来一个结果!一个hit不知道为啥?

4 楼

liqita

2012-09-14

过奖,

这是一个lucene简单的测试,以后还要整合到自己的项目去的

3 楼

mingsheng0310

2012-09-14

看了这么多乱七八糟的文档,就这个文档最直接了,非常感谢

2 楼

liqita

2012-09-13

3Q

75a3bf8974c9bd5a56863a745113d139.gif

1 楼

AlvinCross

2012-09-12

38687d1a1ad71d37c86f287056834d1a.gif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值