转载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案例

lucene3.6.1 经典案例 入门教程 (包含从文件中读取content)的更多相关文章

  1. lucene3.6.0 经典案例 入门教程

    第一步:下载并导入lucene的核心包(注意版本问题):  例如Lucene3.6版本:将lucene-core-3.6.0.jar拷贝到项目的libs 文件夹里.  例如Lucene4.6版本:将l ...

  2. Entity Framework入门教程(3)---EF中的上下文简介

    1.DbContext(上下文类) 在DbFirst模式中,我们添加一个EDM(Entity Data Model)后会自动生成一个.edmx文件,这个文件中包含一个继承DbContext类的上下文实 ...

  3. DotNetBrowser入门教程(更新完善中)

    DotNetBrowser 希望实现的目标:桌面软件可以完美运行Html5,内置支持MVC与WebSocket的微型服务器. 基于.Net 4.0开发.开发环境:VS2017,运行环境支持Window ...

  4. linux入门教程(六) Linux文件与目录管理

    在linux中什么是一个文件的路径呢,说白了就是这个文件存在的地方,例如在上一章提到的/root/.ssh/authorized_keys 这就是一个文件的路径.如果你告诉系统这个文件的路径,那么系统 ...

  5. flask的模板引擎jinja入门教程 包含一个通过网络实时传输Video视频流的示例

    本文首发于个人博客https://kezunlin.me/post/1e37a6/,欢迎阅读最新内容! tutorial to use python flask jinja templates and ...

  6. Entity Framework入门教程(4)---EF中的实体关系

    这一节将总结EF是怎么管理实体之间的关系.EF与数据库一样支持三种关系类型:①一对一 ,②一对多,③多对多. 下边是一个SchoolDB数据库的实体数据模型,图中包含所有的实体和各个实体间的关系.通过 ...

  7. JavaScript 入门教程二 在HTML中使用 JavaScript

    一.使用 <script> 元素的方式有两种:直接在页面中嵌入 JavaScript 代码和引用外部 JavaScript 文件. 二.使用内嵌方式,一般写法为: <script t ...

  8. Entity Framework入门教程(5)---EF中的持久化场景

    EF中的持久性场景 使用EF实现实体持久化(保存)到数据库有两种情况:在线场景和离线场景. 1.在线场景 在线场景中,context是同一个上下文实例(从DbContext派生),检索和保存实体都通过 ...

  9. Entity Framework入门教程(7)--- EF中的查询方法

    这里主要介绍两种查询方法 Linq to entity(L2E)和Sql 1.L2E查询 L2E查询时可以使用linq query语法,或者lambda表达式,默认返回的类型是IQueryable,( ...

随机推荐

  1. poj 1687 Buggy Sat 简单计算几何

    暑期集训出的第一道一血 感觉自己萌萌哒…… 这道题本身并没有坑点 仅仅是翻译巨坑…… 解大腿在做B 安学长在做E 我闲着也没事 就一个词一个词翻译F…… 最后感觉…… 题干大多数都看不懂…… 也都没啥 ...

  2. java 单元测试

    ---恢复内容开始--- 1/引入包:junit-4.11.jar|hamcrest-core-1.3(alilib里有) 2/与src同级建立一个文件夹,名为test,右击,Mark Directo ...

  3. git提交失败

    git push "提示:更新被拒绝,因为您当前分支的最新提交落后于其对应的远程分支."的解决办法 本地已存在项目,需要先获取远端更新并与本地合并,再git push具体操作如下: ...

  4. mybatis 一点整理

    mapper指定对应的接口 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper ...

  5. mongodb type it for more

    当使用MongoChef Core 链接mongodb的时候 ,需要查看更多的数据时候,系统提示  type it for more 可以设置系统参数 DBQuery.shellBatchSize = ...

  6. ASP.NET应用程序的生命周期

    对基于.Net平台的Web应用程序开发者来说,编写适合的应用程序生命周期的代码是非常重要的.所以必须要了解应用程序生命周期才能在适当的生命周期阶段编写合适代码,达到预期的效果.ASP.NET应用程序的 ...

  7. android 按钮Button单击背景切换

    res/drawable/btn_selected.xml <?xml version="1.0" encoding="utf-8"?> <s ...

  8. CSU 1004并查集

    试题链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1004 题目分析: 讲述的主要是是否可以通过公交直接到达自己的目的地,如果最后将问题转换为 ...

  9. Python使用os.listdir()函数来获得目录中的内容

    摘自:http://it.100xuexi.com/view/otdetail/20130423/057606dc-7ad1-47e4-8ea6-0cf75f514837.html   1.在Pyth ...

  10. Java良葛格 学习笔记

    学习一个新的事物时,如果遇到一些概念无法很快理解,这可能是因为要理解概念会需要其它概念先建立起来,所以先暂时放下这个疑问也是一个学习方法,称之为“存疑” ,在以后的学习过程中待必要的概念学会后,目前的 ...