lucene3.6 mysql

阅读: 评论:0

lucene3.6 mysql

lucene3.6 mysql

第一步:下载lucene的核心包

拷贝到项目的lib 文件夹里

第二步:

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

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

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

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

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

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

wtouchone.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)

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

+ Path());

}

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 = adLine()) != null) {

temp += line;

}

reader.close();

return temp;

}

}

输出结果为:

File C:正在被索引....

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

File C:正在被索引....

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

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

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

wtouchone.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

分享到:

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

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

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

谢谢分享

 

5 楼

asdfasdf

2013-05-30

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

4 楼

liqita

2012-09-14

过奖,

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

3 楼

mingsheng0310

2012-09-14

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

2 楼

liqita

2012-09-13

3Q

1 楼

AlvinCross

2012-09-12

本文发布于:2024-02-03 05:16:18,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170690857948897.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

下一篇:JavaSE IO流File
标签:mysql
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23