想要学好一门技术,首先就得从思想层次上全面了解这种技术的作用,适用范围,以及优缺点,对于这些理论,大家可以先在Google,或百度上深入了解一下,也可以到其官方网站上看文档或者WIKI,只有在思想层面上,大概了解这个技术的总体架构,那么学起来,就可以很快上手,举个例子,先“会当凌绝顶”一下,然后在爬山,肯定会容易一些,笔者想说的就是这个道理。
下面就开始进入正题,本篇的入门代码,相对简单,主要是先把lucene添加的Demo给搭建起来,后续的修改,删除,查询会在后面的文章中一一补上,笔者觉得学习这东西还是得脚踏实地一步一步来比较好,只要真真正正理解每一行代码的意思,我们就算有收获了,有时候学习步伐太快,反而会根基不牢,效果不好。
需要准备的Jar包
Java代码 复制代码 收藏代码
1.lucene-core-4.3.1.jar
Java代码 复制代码 收藏代码
1.lucene-analyzers-common-4.3.1.jar
Java代码 复制代码 收藏代码
1.package com.qin;
2.
3.import java.io.File;
4.
5.import org.apache.lucene.analysis.Analyzer;
6.import org.apache.lucene.analysis.standard.StandardAnalyzer;
7.import org.apache.lucene.document.Document;
8.import org.apache.lucene.document.StringField;
9.import org.apache.lucene.document.Field.Store;
10.import org.apache.lucene.index.IndexWriter;
11.import org.apache.lucene.index.IndexWriterConfig;
12.import org.apache.lucene.store.Directory;
13.import org.apache.lucene.store.FSDirectory;
14.import org.apache.lucene.util.Version;
15.
16./**
17. * Lucene的演示Demo类
18. *
19. * **/
20.public class CommonLuceneBasic {
21.
22. /**
23. * 抽象的父类文件夹
24. * */
25. public static Directory directory;
26. /**
27. * 返回IndexWriter
28. * */
29. public static IndexWriter getWriter() throws Exception{
30. Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_43);//设置标准分词器 ,默认是一元分词
31. IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_43, analyzer);//设置IndexWriterConfig
32. // iwc.setRAMBufferSizeMB(3);//设置缓冲区大小
33. return new IndexWriter(directory,iwc);
34. }
35. /**
36. * @indexPath 索引存放路径
37. * **/
38. public static void add(String indexWriterPath){
39. IndexWriter writer=null;
40. try{
41. directory=FSDirectory.open(new File(indexWriterPath));//打开存放索引的路径
42. writer=getWriter();
43. Document doc=new Document();
44. doc.add(new StringField("id", "1", Store.YES));//存储
45. doc.add(new StringField("name", "张飞", Store.YES));//存储
46. doc.add(new StringField("content", "也许放弃,才能靠近你!", Store.YES));//存储
47. writer.addDocument(doc);//添加进写入流里
48. writer.forceMerge(1);//优化压缩段,大规模添加数据的时候建议,少使用本方法,会影响性能
49. writer.commit();//提交数据
50. System.out.println("添加成功");
51. }catch(Exception e){
52.
53. e.printStackTrace();
54.
55. }finally{
56.
57. if(writer!=null){
58. try{
59. writer.close();//关闭流
60. }catch(Exception e){
61. e.printStackTrace();
62. }
63. }
64.
65.
66. }
67.
68.
69. }
70.
71. public static void main(String[] args) {
72. String path="E:\\临时索引";
73. add(path);//调用添加方法
74.
75. }
76.
77.
78.
79.
80.
81.}
添加成功之后,我们就可以通过Luke工具,进行索引查看,如果还有不知道Luke工具是什么,或者不知道怎么使用,可以参照我的上一篇日志如何使用luke
http://qindongliang1922.iteye.com/admin/blogs/1913232
下面就开始进入正题,本篇的入门代码,相对简单,主要是先把lucene添加的Demo给搭建起来,后续的修改,删除,查询会在后面的文章中一一补上,笔者觉得学习这东西还是得脚踏实地一步一步来比较好,只要真真正正理解每一行代码的意思,我们就算有收获了,有时候学习步伐太快,反而会根基不牢,效果不好。
需要准备的Jar包
Java代码 复制代码 收藏代码
1.lucene-core-4.3.1.jar
Java代码 复制代码 收藏代码
1.lucene-analyzers-common-4.3.1.jar
Java代码 复制代码 收藏代码
1.package com.qin;
2.
3.import java.io.File;
4.
5.import org.apache.lucene.analysis.Analyzer;
6.import org.apache.lucene.analysis.standard.StandardAnalyzer;
7.import org.apache.lucene.document.Document;
8.import org.apache.lucene.document.StringField;
9.import org.apache.lucene.document.Field.Store;
10.import org.apache.lucene.index.IndexWriter;
11.import org.apache.lucene.index.IndexWriterConfig;
12.import org.apache.lucene.store.Directory;
13.import org.apache.lucene.store.FSDirectory;
14.import org.apache.lucene.util.Version;
15.
16./**
17. * Lucene的演示Demo类
18. *
19. * **/
20.public class CommonLuceneBasic {
21.
22. /**
23. * 抽象的父类文件夹
24. * */
25. public static Directory directory;
26. /**
27. * 返回IndexWriter
28. * */
29. public static IndexWriter getWriter() throws Exception{
30. Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_43);//设置标准分词器 ,默认是一元分词
31. IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_43, analyzer);//设置IndexWriterConfig
32. // iwc.setRAMBufferSizeMB(3);//设置缓冲区大小
33. return new IndexWriter(directory,iwc);
34. }
35. /**
36. * @indexPath 索引存放路径
37. * **/
38. public static void add(String indexWriterPath){
39. IndexWriter writer=null;
40. try{
41. directory=FSDirectory.open(new File(indexWriterPath));//打开存放索引的路径
42. writer=getWriter();
43. Document doc=new Document();
44. doc.add(new StringField("id", "1", Store.YES));//存储
45. doc.add(new StringField("name", "张飞", Store.YES));//存储
46. doc.add(new StringField("content", "也许放弃,才能靠近你!", Store.YES));//存储
47. writer.addDocument(doc);//添加进写入流里
48. writer.forceMerge(1);//优化压缩段,大规模添加数据的时候建议,少使用本方法,会影响性能
49. writer.commit();//提交数据
50. System.out.println("添加成功");
51. }catch(Exception e){
52.
53. e.printStackTrace();
54.
55. }finally{
56.
57. if(writer!=null){
58. try{
59. writer.close();//关闭流
60. }catch(Exception e){
61. e.printStackTrace();
62. }
63. }
64.
65.
66. }
67.
68.
69. }
70.
71. public static void main(String[] args) {
72. String path="E:\\临时索引";
73. add(path);//调用添加方法
74.
75. }
76.
77.
78.
79.
80.
81.}
添加成功之后,我们就可以通过Luke工具,进行索引查看,如果还有不知道Luke工具是什么,或者不知道怎么使用,可以参照我的上一篇日志如何使用luke
http://qindongliang1922.iteye.com/admin/blogs/1913232