根据Lucene学习笔记之三:全文搜索详解,搭建lucene入门实例。主要内容包括建立索引和搜索,分词贯穿其中。
lucene建立索引
信息源
要采集,必须有信息源,在这里我们就以读取硬盘中一个文件夹下所有的文件(File)充当信息源。
File f= newFile("E:/lucene/example");
加工
要把采集的信息,以lucene规定的形式存放到索引库中,所以要创建相应的文档(Document)对象。在这个文档中,我们要存放哪些信息才能达到完整且辟免垃圾信息,例如网页,我们可以要存储的是他的标题、内容、URL等,那些广告是不用存储的。在这里我们用到Field来存储各项目内容。
注:Document包含一系列的(域名)Field:
List<Fieldable>fields = newArrayList<Fieldable>();
分词
对于加工好的了文档,对其进行分词。用什么分词器呢?对英文和中文使用的分词器有可能不一样吧,这个得看后续分解了。在这里我们就用lucene提供的标准的分词器(StandardAnalyzer)
索引库
要把文档写入到索引库,并且根据分词器进行分词、建立索引,这得建索引库吧,在lucene中对应的是Directory,它可以建立在内存中,也可以建立在硬盘中。
//Directory directory=newRAMDirectory();//建立在内存中
Directorydirectory=FSDirectory.open(newFile("E:/lucene/index01"));
建立索引的完整代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
public void index(){
IndexWriter writer= null ;
try {
Directory directory=FSDirectory.open(
new File( "E:/lucene/index01" ));
IndexWriterConfig iwc= new IndexWriterConfig(
Version.LUCENE_36,
new StandardAnalyzer(Version.LUCENE_36));
writer= new IndexWriter(directory, iwc);
Document doc= null ;
File f= new File( "E:/lucene/example" );
for (File file:f.listFiles()){
doc= new Document();
doc.add( new Field( "content" , new FileReader(file)));
doc.add( new Field( "fileName" ,file.getName(),
Field.Store.YES,Field.Index.NOT_ANALYZED));
doc.add( new Field( "path" ,file.getAbsolutePath(),
Field.Store.YES,Field.Index.NOT_ANALYZED));
writer.addDocument(doc);
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer!= null )
try {
writer.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
|
根据创建好的索引进行搜索
创建好了索引之后,接下来就是搜索了。按照搜索关键字(下例的关键字是“java”),在指定域(content)与分词器(StandardAnalyzer),通过IndexReader输入流读取索引库中的文档进行搜索。然后遍历搜索到的文档,把他的文件名和路径输出到控制台。
搜索的完整代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
public void searcher(){
try {
Directory directory=FSDirectory.open(
new File( "E:/lucene/index01" ));
IndexReader reader=IndexReader.open(directory);
IndexSearcher searcher= new IndexSearcher(reader);
QueryParser parser= new QueryParser(Version.LUCENE_36,
"content" ,
new StandardAnalyzer(Version.LUCENE_36));
Query query=parser.parse( "java" );
TopDocs tds=searcher.search(query, 2 );
System.out.println( "总共有【" + tds.totalHits + "】条匹配结果" );
ScoreDoc[] sds=tds.scoreDocs;
for (ScoreDoc sd:sds){
Document d = searcher.doc(sd.doc);
System.out.println(d.get( "fileName" )+
"[" +d.get( "path" )+ "]" );
}
reader.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
|
本文链接:Lucene学习笔记之四:lucene入门实例,本文由huangyineng原创,转载请注明出处
分享到:
相关推荐
Lucene学习总结之四:Lucene索引过程分析(1) Lucene学习总结之四:Lucene索引过程分析(2) Lucene学习总结之四:Lucene索引过程分析(3) Lucene学习总结之四:Lucene索引过程分析(4) www.chinaandroid.com
24 Lucene学习总结之八:Lucene的查询语法,JavaCC及QueryParser(1)
Lucene学习总结之一:全文检索的基本原理[归纳].pdf
lucene实例lucene实例lucene实例lucene实例lucene实例lucene实例lucene实例lucene实例lucene实例
本系列文章将详细描述几乎最新版本的Lucene的基本...Lucene学习总结之四:Lucene索引过程分析(1) http://www.cnblogs.com/forfuture1978/archive/2010/02/02/1661439.html Lucene学习总结之四:Lucene索引过程分析(2) ...
1.6 Lucene学习总结之四:Lucene索引过程分析(1) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 1.7 Lucene学习总结之四:Lucene索引过程分析(2) . . . . . . . . . . . ....
NULL 博文链接:https://kylinsoong.iteye.com/blog/719415
Maven坐标:org.apache.lucene:lucene-core:7.7.0; 标签:apache、lucene、core、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档...
lucene实例lucene实例lucene实例lucene实例lucene实例
lucene学习lucene学习lucene学习lucene学习lucene学习lucene学习lucene学习lucene学习lucene学习lucene学习lucene学习lucene学习lucene学习lucene学习lucene学习lucene学习lucene学习lucene学习lucene学习lucene学习...
lucene学习笔记,lucene入门必备材料
lucene学习笔记 1 .txt lucene学习笔记 2.txt lucene学习笔记 3 .txt lucene入门实战.txt Lucene 的学习 .txt Lucene-2.0学习文档 .txt Lucene入门与使用 .txt lucene性能.txt 大富翁全文索引和查询的例子...
Maven坐标:org.apache.lucene:lucene-sandbox:6.6.0; 标签:apache、lucene、sandbox、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译...
本人的Lucene2.9学习笔记 本人的Lucene2.9学习笔记 本人的Lucene2.9学习笔记 本人的Lucene2.9学习笔记本人的Lucene2.9学习笔记本人的Lucene2.9学习笔记 本人的Lucene2.9学习笔记
Lucene索引器实例Lucene索引器实例Lucene索引器实例Lucene索引器实例
Lucene 简单入门程序实例
通过lucene创建索引,然后进行各种搜索
很好的Lucene学习入门资料。lucene是纯java开发的,支持索引的建立和搜索
官网的lucene全文检索引擎工具包,下载后直接解压缩即可使用