转载http://liqita.iteye.com/blog/1676664

第一步:下载lucene的核心包

lucene-core-3.6.1-javadoc.jar (3.5 MB)

lucene-core-3.6.1.jar (1.5 MB)

拷贝到项目的lib 文件夹里

第二步:

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

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

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

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

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

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

  1. package com.newtouchone.lucene;
  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. import org.apache.lucene.index.IndexWriterConfig;
  14. import org.apache.lucene.index.IndexWriterConfig.OpenMode;
  15. import org.apache.lucene.store.Directory;
  16. import org.apache.lucene.store.FSDirectory;
  17. import org.apache.lucene.util.Version;
  18. public class TextFileIndexer {
  19. public static void main(String[] args) throws Exception {
  20. /* 指明要索引文件夹的位置,这里是C盘的source文件夹下 */
  21. File fileDir = new File("C:\\source");
  22. /* 这里放索引文件的位置 */
  23. File indexDir = new File("C:\\index");
  24. Directory dir = FSDirectory.open(indexDir);
  25. Analyzer luceneAnalyzer = new StandardAnalyzer(Version.LUCENE_36);
  26. IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_36,luceneAnalyzer);
  27. iwc.setOpenMode(OpenMode.CREATE);
  28. IndexWriter indexWriter = new IndexWriter(dir,iwc);
  29. File[] textFiles = fileDir.listFiles();
  30. long startTime = new Date().getTime();
  31. //增加document到索引去
  32. for (int i = 0; i < textFiles.length; i++) {
  33. if (textFiles[i].isFile()
  34. && textFiles[i].getName().endsWith(".txt")) {
  35. System.out.println("File " + textFiles[i].getCanonicalPath()
  36. + "正在被索引....");
  37. String temp = FileReaderAll(textFiles[i].getCanonicalPath(),
  38. "GBK");
  39. System.out.println(temp);
  40. Document document = new Document();
  41. Field FieldPath = new Field("path", textFiles[i].getPath(),
  42. Field.Store.YES, Field.Index.NO);
  43. Field FieldBody = new Field("body", temp, Field.Store.YES,
  44. Field.Index.ANALYZED,
  45. Field.TermVector.WITH_POSITIONS_OFFSETS);
  46. document.add(FieldPath);
  47. document.add(FieldBody);
  48. indexWriter.addDocument(document);
  49. }
  50. }
  51. indexWriter.close();
  52. //测试一下索引的时间
  53. long endTime = new Date().getTime();
  54. System.out
  55. .println("这花费了"
  56. + (endTime - startTime)
  57. + " 毫秒来把文档增加到索引里面去!"
  58. + fileDir.getPath());
  59. }
  60. public static String FileReaderAll(String FileName, String charset)
  61. throws IOException {
  62. BufferedReader reader = new BufferedReader(new InputStreamReader(
  63. new FileInputStream(FileName), charset));
  64. String line = new String();
  65. String temp = new String();
  66. while ((line = reader.readLine()) != null) {
  67. temp += line;
  68. }
  69. reader.close();
  70. return temp;
  71. }
  72. }

输出结果为:

  1. File C:\source\test1.txt正在被索引....
  2. 欢迎来到绝对秋香的博客。
  3. File C:\source\test2.txt正在被索引....
  4. 绝对秋香引领你走向潮流。
  5. 这花费了641 毫秒来把文档增加到索引里面去!C:\source

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

  1. package com.newtouchone.lucene;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import org.apache.lucene.analysis.Analyzer;
  5. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  6. import org.apache.lucene.index.IndexReader;
  7. import org.apache.lucene.queryParser.ParseException;
  8. import org.apache.lucene.queryParser.QueryParser;
  9. import org.apache.lucene.search.IndexSearcher;
  10. import org.apache.lucene.search.Query;
  11. import org.apache.lucene.search.ScoreDoc;
  12. import org.apache.lucene.search.TopDocs;
  13. import org.apache.lucene.store.FSDirectory;
  14. import org.apache.lucene.util.Version;
  15. public class TestQuery {
  16. public static void main(String[] args) throws IOException, ParseException {
  17. String index = "C:\\index";         //搜索的索引路径
  18. IndexReader reader = IndexReader.open(FSDirectory.open(new File(index)));
  19. IndexSearcher searcher = new IndexSearcher(reader);
  20. ScoreDoc[] hits = null;
  21. String queryString = "绝对秋香";   //搜索的关键词
  22. Query query = null;
  23. Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
  24. try {
  25. QueryParser qp = new QueryParser(Version.LUCENE_36,"body", analyzer);
  26. query = qp.parse(queryString);
  27. } catch (ParseException e) {
  28. }
  29. if (searcher != null) {
  30. TopDocs results = searcher.search(query,10);    //返回最多为10条记录
  31. hits = results.scoreDocs;
  32. if (hits.length > 0) {
  33. System.out.println("找到:" + hits.length + " 个结果!");
  34. }
  35. searcher.close();
  36. }
  37. }
  38. }

测试输出结果为:

  1. 找到:2 个结果!

附件homework.rar为项目文件,解压部署则可运行该lucene案例

最新文章

  1. Java 基础知识相关好文章
  2. Lab_6_SysOps_AutoScaling_Linux_v2.5
  3. 学习笔记:The Log(我所读过的最好的一篇分布式技术文章)
  4. Selenium - IWebDriver 控制scroll bar到底部
  5. 07_js走路小游戏
  6. Linux入门基础 #5:Linux文件系统挂载管理
  7. C# 代码 设置 前台 页面 JS提示
  8. AndroidMainifest标签说明2——&amp;lt;activity&amp;gt;
  9. (转)Unity3D 之插值计算
  10. Word 2007 封面、目录和正文页码单独设置
  11. MinerDB.java 数据库工具类
  12. Ubuntu下Gradle环境配置
  13. Spring Boot Log4j2 日志学习
  14. 运动目标检测中基于HSV空间的阴影去除算法
  15. Fabric密码保存
  16. 基于TLS证书手动部署kubernetes集群(下)
  17. BZOJ 1565 [NOI2009]植物大战僵尸 | 网络流
  18. 添加类似navigationController自带的返回按钮
  19. MAT(Memory Analyzer Tool)内存分析工具的使用
  20. OC 里面 webView与js

热门文章

  1. Java &quot;==&quot; 和 &quot;equals&quot; 和 &quot;&quot; 问题
  2. 【Linux】zookeeper构造伪集群
  3. redis数据类型:lists
  4. onbeforeunload与a标签在IE中的冲突bug(转载)
  5. 消除警告&quot;property access result unused - getters should not be used for side effects&quot;
  6. perl中的pack与unpack
  7. 修改jsonb的属性
  8. The first to Python
  9. XBOX360 硬盘玩游戏
  10. VBS脚本操作网页元素