`

lucene的简单实例

阅读更多

说明一下,这一篇文章的用到的lucene,是用2.0版本的,主要在查询的时候2.0版本的lucene与以前的版本有了一些区别.
其实这一些代码都是早几个月写的,自己很懒,所以到今天才写到自己的博客上,高深的文章自己写不了,只能记录下一些简单的记录与点滴,其中的代码算是自娱自乐的,希望高手不要把重构之类的砸下来...

1、在windows系统下的的C盘,建一个名叫s的文件夹,在该文件夹里面随便建三个txt文件,随便起名啦,就叫"1.txt","2.txt"和"3.txt"啦
其中1.txt的内容如下:

Java代码 复制代码
  1. 中华人民共和国
  2. 全国人民
  3. 2006


而"2.txt"和"3.txt"的内容也可以随便写几写,这里懒写,就复制一个和1.txt文件的内容一样吧

2、下载lucene包,放在classpath路径中
建立索引:

Java代码 复制代码
  1. package lighter.javaeye.com;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.util.Date;
  8. import org.apache.lucene.analysis.Analyzer;
  9. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  10. import org.apache.lucene.document.Document;
  11. import org.apache.lucene.document.Field;
  12. import org.apache.lucene.index.IndexWriter;
  13. /**
  14. *authorlighterdate2006-8-7
  15. */
  16. public class TextFileIndexer{
  17. public static void main(String[]args) throws Exception{
  18. /*指明要索引文件夹的位置,这里是C盘的S文件夹下*/
  19. FilefileDir= new File( "c:\\s" );
  20. /*这里放索引文件的位置*/
  21. FileindexDir= new File( "c:\\index" );
  22. AnalyzerluceneAnalyzer= new StandardAnalyzer();
  23. IndexWriterindexWriter= new IndexWriter(indexDir,luceneAnalyzer,
  24. true );
  25. File[]textFiles=fileDir.listFiles();
  26. long startTime= new Date().getTime();
  27. //增加document到索引去
  28. for ( int i= 0 ;i<textFiles.length;i++){
  29. if (textFiles[i].isFile()
  30. &&textFiles[i].getName().endsWith( ".txt" )){
  31. System.out.println( "File" +textFiles[i].getCanonicalPath()
  32. + "正在被索引...." );
  33. Stringtemp=FileReaderAll(textFiles[i].getCanonicalPath(),
  34. "GBK" );
  35. System.out.println(temp);
  36. Documentdocument= new Document();
  37. FieldFieldPath= new Field( "path" ,textFiles[i].getPath(),
  38. Field.Store.YES,Field.Index.NO);
  39. FieldFieldBody= new Field( "body" ,temp,Field.Store.YES,
  40. Field.Index.ANALYZED,
  41. Field.TermVector.WITH_POSITIONS_OFFSETS);
  42. document.add(FieldPath);
  43. document.add(FieldBody);
  44. indexWriter.addDocument(document);
  45. }
  46. }
  47. //optimize()方法是对索引进行优化
  48. indexWriter.optimize();
  49. indexWriter.close();
  50. //测试一下索引的时间
  51. long endTime= new Date().getTime();
  52. System.out
  53. .println( "这花费了"
  54. +(endTime-startTime)
  55. + "毫秒来把文档增加到索引里面去!"
  56. +fileDir.getPath());
  57. }
  58. public static StringFileReaderAll(StringFileName,Stringcharset)
  59. throws IOException{
  60. BufferedReaderreader= new BufferedReader( new InputStreamReader(
  61. new FileInputStream(FileName),charset));
  62. Stringline= new String();
  63. Stringtemp= new String();
  64. while ((line=reader.readLine())!= null ){
  65. temp+=line;
  66. }
  67. reader.close();
  68. return temp;
  69. }
  70. }



索引的结果:

Java代码 复制代码
  1. FileC:\s\ 1 .txt正在被索引....
  2. 中华人民共和国全国人民 2006
  3. FileC:\s\ 2 .txt正在被索引....
  4. 中华人民共和国全国人民 2006
  5. FileC:\s\ 3 .txt正在被索引....
  6. 中华人民共和国全国人民 2006
  7. 这花费了 297 毫秒来把文档增加到索引里面去!c:\s



3、建立了索引之后,查询啦....

Java代码 复制代码
  1. package lighter.javaeye.com;
  2. import java.io.IOException;
  3. import org.apache.lucene.analysis.Analyzer;
  4. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  5. import org.apache.lucene.queryParser.ParseException;
  6. import org.apache.lucene.queryParser.QueryParser;
  7. import org.apache.lucene.search.Hits;
  8. import org.apache.lucene.search.IndexSearcher;
  9. import org.apache.lucene.search.Query;
  10. public class TestQuery{
  11. public static void main(String[]args) throws IOException,ParseException{
  12. Hitshits= null ;
  13. StringqueryString= "中华" ;
  14. Queryquery= null ;
  15. IndexSearchersearcher= new IndexSearcher( "c:\\index" );
  16. Analyzeranalyzer= new StandardAnalyzer();
  17. try {
  18. QueryParserqp= new QueryParser( "body" ,analyzer);
  19. query=qp.parse(queryString);
  20. } catch (ParseExceptione){
  21. }
  22. if (searcher!= null ){
  23. hits=searcher.search(query);
  24. if (hits.length()> 0 ){
  25. System.out.println( "找到:" +hits.length()+ "个结果!" );
  26. }
  27. }
  28. }
  29. }



其运行结果:

引用
找到:3 个结果!


具体的API的用法,这里就不说了,具体的做法参考lucene的官方文档吧...

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics