说明一下,这一篇文章的用到的lucene,是用2.0版本的,主要在查询的时候2.0版本的lucene与以前的版本有了一些区别.
其实这一些代码都是早几个月写的,自己很懒,所以到今天才写到自己的博客上,高深的文章自己写不了,只能记录下一些简单的记录与点滴,其中的代码算是自娱自乐的,希望高手不要把重构之类的砸下来...
1、在windows系统下的的C盘,建一个名叫s的文件夹,在该文件夹里面随便建三个txt文件,随便起名啦,就叫"1.txt","2.txt"和"3.txt"啦
其中1.txt的内容如下:
中华人民共和国
全国人民
2006年
而"2.txt"和"3.txt"的内容也可以随便写几写,这里懒写,就复制一个和1.txt文件的内容一样吧
2、下载lucene包,放在classpath路径中
建立索引:
-
package
lighter.javaeye.com;
-
-
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;
-
-
-
-
-
public
class
TextFileIndexer{
-
public
static
void
main(String[]args)
throws
Exception{
-
-
FilefileDir=
new
File(
"c:\\s"
);
-
-
-
FileindexDir=
new
File(
"c:\\index"
);
-
AnalyzerluceneAnalyzer=
new
StandardAnalyzer();
-
IndexWriterindexWriter=
new
IndexWriter(indexDir,luceneAnalyzer,
-
true
);
-
File[]textFiles=fileDir.listFiles();
-
long
startTime=
new
Date().getTime();
-
-
-
for
(
int
i=
0
;i<textFiles.length;i++){
-
if
(textFiles[i].isFile()
-
&&textFiles[i].getName().endsWith(
".txt"
)){
-
System.out.println(
"File"
+textFiles[i].getCanonicalPath()
-
+
"正在被索引...."
);
-
Stringtemp=FileReaderAll(textFiles[i].getCanonicalPath(),
-
"GBK"
);
-
System.out.println(temp);
-
Documentdocument=
new
Document();
-
FieldFieldPath=
new
Field(
"path"
,textFiles[i].getPath(),
-
Field.Store.YES,Field.Index.NO);
-
FieldFieldBody=
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.optimize();
-
indexWriter.close();
-
-
-
long
endTime=
new
Date().getTime();
-
System.out
-
.println(
"这花费了"
-
+(endTime-startTime)
-
+
"毫秒来把文档增加到索引里面去!"
-
+fileDir.getPath());
-
}
-
-
public
static
StringFileReaderAll(StringFileName,Stringcharset)
-
throws
IOException{
-
BufferedReaderreader=
new
BufferedReader(
new
InputStreamReader(
-
new
FileInputStream(FileName),charset));
-
Stringline=
new
String();
-
Stringtemp=
new
String();
-
-
while
((line=reader.readLine())!=
null
){
-
temp+=line;
-
}
-
reader.close();
-
return
temp;
-
}
-
}
package lighter.javaeye.com;
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;
/**
* author lighter date 2006-8-7
*/
public class TextFileIndexer {
public static void main(String[] args) throws Exception {
/* 指明要索引文件夹的位置,这里是C盘的S文件夹下 */
File fileDir = new File("c:\\s");
/* 这里放索引文件的位置 */
File indexDir = new File("c:\\index");
Analyzer luceneAnalyzer = new StandardAnalyzer();
IndexWriter indexWriter = new IndexWriter(indexDir, luceneAnalyzer,
true);
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.TOKENIZED,
Field.TermVector.WITH_POSITIONS_OFFSETS);
document.add(FieldPath);
document.add(FieldBody);
indexWriter.addDocument(document);
}
}
//optimize()方法是对索引进行优化
indexWriter.optimize();
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;
}
}
索引的结果:
-
FileC:\s\
1
.txt正在被索引....
-
中华人民共和国全国人民
2006
年
-
FileC:\s\
2
.txt正在被索引....
-
中华人民共和国全国人民
2006
年
-
FileC:\s\
3
.txt正在被索引....
-
中华人民共和国全国人民
2006
年
-
这花费了
297
毫秒来把文档增加到索引里面去!c:\s
File C:\s\1.txt正在被索引....
中华人民共和国全国人民2006年
File C:\s\2.txt正在被索引....
中华人民共和国全国人民2006年
File C:\s\3.txt正在被索引....
中华人民共和国全国人民2006年
这花费了297 毫秒来把文档增加到索引里面去!c:\s
3、建立了索引之后,查询啦....
-
package
lighter.javaeye.com;
-
-
import
java.io.IOException;
-
-
import
org.apache.lucene.analysis.Analyzer;
-
import
org.apache.lucene.analysis.standard.StandardAnalyzer;
-
import
org.apache.lucene.queryParser.ParseException;
-
import
org.apache.lucene.queryParser.QueryParser;
-
import
org.apache.lucene.search.Hits;
-
import
org.apache.lucene.search.IndexSearcher;
-
import
org.apache.lucene.search.Query;
-
-
public
class
TestQuery{
-
public
static
void
main(String[]args)
throws
IOException,ParseException{
-
Hitshits=
null
;
-
StringqueryString=
"中华"
;
-
Queryquery=
null
;
-
IndexSearchersearcher=
new
IndexSearcher(
"c:\\index"
);
-
-
Analyzeranalyzer=
new
StandardAnalyzer();
-
try
{
-
QueryParserqp=
new
QueryParser(
"body"
,analyzer);
-
query=qp.parse(queryString);
-
}
catch
(ParseExceptione){
-
}
-
if
(searcher!=
null
){
-
hits=searcher.search(query);
-
if
(hits.length()>
0
){
-
System.out.println(
"找到:"
+hits.length()+
"个结果!"
);
-
}
-
}
-
}
-
-
}
package lighter.javaeye.com;
import java.io.IOException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
public class TestQuery {
public static void main(String[] args) throws IOException, ParseException {
Hits hits = null;
String queryString = "中华";
Query query = null;
IndexSearcher searcher = new IndexSearcher("c:\\index");
Analyzer analyzer = new StandardAnalyzer();
try {
QueryParser qp = new QueryParser("body", analyzer);
query = qp.parse(queryString);
} catch (ParseException e) {
}
if (searcher != null) {
hits = searcher.search(query);
if (hits.length() > 0) {
System.out.println("找到:" + hits.length() + " 个结果!");
}
}
}
}
其运行结果:
引用
找到:3 个结果!
具体的API的用法,这里就不说了,具体的做法参考lucene的官方文档吧...
分享到:
相关推荐
Lucene简单实例记录 简单Luncene的示例应用
lucene 简单例子 , 直接运行, `
lucene简单实例 分页存在BUG 此实例只是项目中一小部分代码 下载需谨慎
使用lucene对数据进行简单的存储,和搜索
Lucene 简单入门程序实例
一个用lucene编写的搜索引擎小项目.适合初学者学习
这是自己做的一个简单例子,以前在网上搜的那些都不好用,所以自己做了一个觉得不错,就和大家一起分享
lucene简单的代码例子 lucene简单的代码例子 参考
我自己写的一个lucene搜索引擎的简单入门例子源代码 对照lucene,相当的易懂。api我这也有,含JE分词器。
基于java的Lucene全文搜索引擎资源简单实例下载
基于lucene 2.4简单的一个索引和搜索实例
lucene与sqlserver数据库实现索引的简单实例lucene与sqlserver数据库实现索引的简单实例
lucene4.7开发实例,包括索引创建、修改、删除、排序、分页、优化、高亮显示、常见几种分词器等。实例加全jar包
做了一个简单的Lucene的例子 只要导入Eclipse中就可以运行了!!
自己修改的基于lucene.net 2.9和highlighter插件的简单例子
程序展现了lucene包强大的建索引和搜索功能!
利用paoding进行中文分词,可进行自定义词典。利用lucene对文本内容进行索引和搜索并进行高亮处理。
luceneTest 博文链接:https://wolftou.iteye.com/blog/148701