本文转自:http://blog.csdn.net/jyf211314/article/details/17503997           点击打开链接

lucene4.6小例子,Lucene全文检索步骤大体上有两方面:索引过程和搜索过程,具体如下:

1.索引过程

<1>创建IndexWriter
,它的作用是用来写索引文件,

可以将IndexWriter看做是一个特定类型的数据库,用来存放各种表,可以将Document看做是一张张的表。

IndexWriter iw=new IndexWriter(Directory dire, IndexWriterConfig iwc);

可见,该方法有两个参数,第一个参数为索引存放位置,参数类型为Directory,第二个参数为 IndexWriter的配置类,

@1.Directory dire=FSDirectory.open(new File(D:\\lucenefiles));

通过Directory的创建指定索引存放位置(D:\lucenefiles文件夹)

@2.Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_46);

通过Analyzer 的创建指定索引语言词汇的分析器。当前版本Lucene-core-4.6.0

@3.IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_46, analyzer);

iwc.setOpenMode(OpenMode.CREATE); //CREATE:清空重建(推荐);

IndexWriter indexWriter=new IndexWriter(dire, iwc);

通过IndexWriterConfig的创建指定索引版本和语言词汇分析器

<2>创建Document,代表要索引的文档

可以将Document看做是数据库中的一张张的表,而每个field都是表中的一个colum用来存放各种类型的信息,如标题、作者、时间等等

@1.File fileDir= new File("D:\\lucenefiles");
File[] files = fileDir.listFiles();

读取文件

@2.Document doc=new Document();

通过创建Document指定要索引的文档

@3.doc.add(new TextField("name", String name=files[i].getName(), Store.YES));

doc.add(new TextField("content", getFileContent(files[i]), Store.YES));

向Document文档中添加Field信息,不同类型的信息用不同类型的Field来表示;

getFileContent(File file)用于读取文件内容。

<3>将Document添加到IndexWriter中并且提交

表(Document)创建好之后,当然要添加到数据库(IndexWriter)中,同时commit

@1.indexWriter.addDocument(doc); 通过该方法添加

indexWriter.commit();

2.搜索过程

<!>首先指定搜索目录

Directory dire=FSDirectory.open(new File("D:\\luceneindex"));

<2>创建语言词汇解析器,前后版本要一致

Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_46);

<3>创建IndexReader将搜索目录读取到内存,

IndexReader ireader=DirectoryReader.open(dire);

<4>创建IndexSearcher准备搜索

IndexSearcher isearcher=new IndexSearcher(ireader);

<5>创建QueryParser对查询语句进行语法分析,确定搜索的内容

QueryParser parser = new QueryParser(Version.LUCENE_46, "path", analyzer);

<6>创建Query生成查询语法树,

QueryParser parser = new QueryParser(Version.LUCENE_46, "content", analyzer);

通过调用QueryParser 的parse("");方法来生成Query以及查询语法树

Query query = parser.parse("程序员");

<7>获取搜索结果

TopDocs td=is.search(query, 1000);ScoreDoc[] sds =td.scoreDocs;

<8>对搜索结果遍历操作

3.以下是个人测试例子

  1. public class Constants {
  2. public final static String INDEX_FILE_PATH = "F:\\lucene\\test";
  3. //索引的文件的存放路径 测试时可以在本目录下自行建一些文档,内容自行编辑即可
  4. public final static String INDEX_STORE_PATH = "F:\\lucene\\index";
  5. //索引的存放位置
  6. }
  1. package com.lucene.jl;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.InputStreamReader;
  7.  
  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.Store;
  12. import org.apache.lucene.document.TextField;
  13. import org.apache.lucene.index.DirectoryReader;
  14. import org.apache.lucene.index.IndexReader;
  15. import org.apache.lucene.index.IndexWriter;
  16. import org.apache.lucene.index.IndexWriterConfig;
  17. import org.apache.lucene.queryparser.classic.QueryParser;
  18. import org.apache.lucene.search.IndexSearcher;
  19. import org.apache.lucene.search.Query;
  20. import org.apache.lucene.search.ScoreDoc;
  21. import org.apache.lucene.search.TopDocs;
  22. import org.apache.lucene.store.Directory;
  23. import org.apache.lucene.store.FSDirectory;
  24. import org.apache.lucene.util.Version;
  25.  
  26. public class LuceneIndex {
  27.  
  28. /**
  29. * 创建索引
  30. * @param analyzer
  31. * @throws Exception
  32. */
  33. public static void createIndex(Analyzer analyzer) throws Exception{
  34. Directory dire=FSDirectory.open(new File(Constants.INDEX_STORE_PATH));
  35. IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_46, analyzer);
  36. IndexWriter iw=new IndexWriter(dire, iwc);
  37. LuceneIndex.addDoc(iw);
  38. iw.close();
  39. }
  40.  
  41. /**
  42. * 动态添加Document
  43. * @param iw
  44. * @throws Exception
  45. */
  46. public static void addDoc(IndexWriter iw) throws Exception{
  47. File[] files=new File(Constants.INDEX_FILE_PATH).listFiles();
  48. for (File file : files) {
  49. Document doc=new Document();
  50. String content=LuceneIndex.getContent(file);
  51. String name=file.getName();
  52. String path=file.getAbsolutePath();
  53. doc.add(new TextField("content", content, Store.YES));
  54. doc.add(new TextField("name", name, Store.YES));
  55. doc.add(new TextField("path", path,Store.YES));
  56. System.out.println(name+"==="+content+"==="+path);
  57. iw.addDocument(doc);
  58. iw.commit();
  59. }
  60. }
  61.  
  62. /**
  63. * 获取文本内容
  64. * @param file
  65. * @return
  66. * @throws Exception
  67. */
  68. @SuppressWarnings("resource")
  69. public static String getContent(File file) throws Exception{
  70. FileInputStream fis=new FileInputStream(file);
  71. InputStreamReader isr=new InputStreamReader(fis);
  72. BufferedReader br=new BufferedReader(isr);
  73. StringBuffer sb=new StringBuffer();
  74. String line=br.readLine();
  75. while(line!=null){
  76. sb.append(line+"\n");
  77. line=null;
  78. }
  79. return sb.toString();
  80. }
  81.  
  82. /**
  83. * 搜索
  84. * @param query
  85. * @throws Exception
  86. */
  87. private static void search(Query query) throws Exception {
  88. Directory dire=FSDirectory.open(new File(Constants.INDEX_STORE_PATH));
  89. IndexReader ir=DirectoryReader.open(dire);
  90. IndexSearcher is=new IndexSearcher(ir);
  91. TopDocs td=is.search(query, 1000);
  92. System.out.println("共为您查找到"+td.totalHits+"条结果");
  93. ScoreDoc[] sds =td.scoreDocs;
  94. for (ScoreDoc sd : sds) {
  95. Document d = is.doc(sd.doc);
  96. System.out.println(d.get("path") + ":["+d.get("path")+"]");
  97. }
  98. }
  99.  
  100. public static void main(String[] args) throws Exception, Exception {
  101. Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_46);
  102. LuceneIndex.createIndex(analyzer);
  103. QueryParser parser = new QueryParser(Version.LUCENE_46, "content", analyzer);
  104. Query query = parser.parse("人");
  105. LuceneIndex.search(query);
  106. }
  107. }

4.磁盘文件检索如上述,对于数据库的检索页应该是差不多,应该也是先从数据库查出数据,然后对查出的数据做索引然后搜索,大致一样吧

lucene4.6 索引创建和搜索例子的更多相关文章

  1. Lucene7.1.0版本的索引创建与查询以及维护,包括新版本的一些新特性探索!

    一 吐槽 lucene版本更新实在太快了,往往旧版本都还没学会,新的就出来,而且每个版本改动都特别大,尤其是4.7,6,6,7.1.......ε=(´ο`*)))唉,但不可否认,新版本确实要比旧版本 ...

  2. lucene4入门(2)搜索

    欢迎转载http://www.cnblogs.com/shizhongtao/p/3440479.html 接着上一篇,这里继续搜索,对于搜索和创建一样,首先你要确定搜索位置,然后用规定的类来读取.还 ...

  3. 关于lucene的IndexSearcher单实例,对于索引的实时搜索

    Lucene版本:3.0 一般情况下,lucene的IndexSearcher都要写成单实例,因为每次创建IndexSearcher对象的时候,它都需要把索引文件加载进来,如果访问量比较大,而索引也比 ...

  4. mysql 优化实例之索引创建

    mysql 优化实例之索引创建 优化前: pt-query-degist分析结果: # Query 23: 0.00 QPS, 0.00x concurrency, ID 0x78761E301CC7 ...

  5. MongoDB索引创建(5)

    索引创建 1:索引提高查询速度,降低写入速度,权衡常用的查询字段,不必在太多列上建索引 2. 在mongodb中,索引可以按字段升序/降序来创建,便于排序 3. 默认是用btree来组织索引文件,2. ...

  6. 数据结构二叉树的java实现,包括二叉树的创建、搜索、删除和遍历

    根据自己的学习体会并参考了一些网上的资料,以java写出了二叉树的创建.搜索.删除和遍历等操作,尚未实现的功能有:根据先序和中序遍历,得到后序遍历以及根据后序和中序遍历,得到先序遍历,以及获取栈的深度 ...

  7. 索引-mysql索引创建、查看、删除及使用示例

    mysql索引创建.查看.删除及使用示例 1.创建索引: ALTER TABLE用来创建普通索引.UNIQUE索引或PRIMARY KEY索引. ALTER TABLE table_name ADD ...

  8. solr索引创建流程

    solr索引创建流程: 分词组件Tokenizer 分词组件(Tokenizer)会做以下几件事情(这个过程称为:Tokenize),处理得到的结果是词汇单元(Token). 1.将文档分成一个一个单 ...

  9. ArcEngine创建IElement简单例子

    转自IT-GIS终结者原文ArcEngine创建IElement简单例子 代码下载地址:http://files.cnblogs.com/ogis/MapControlApplication2.rar ...

随机推荐

  1. Grails教程

    1简介- Reference DocumentationVersion:null Table of Contents 1.1Grails 2.0有那些新特性?1.1.1面向开发的特性1.1.2核心特性 ...

  2. bat文件调用shutdown命令不生效问题原因

    背景: 本人使用云桌面办公,但是用于登陆云桌面的终端运行卡顿,每次开机要20min才能登陆云桌面,所以: 1)在BIOS设置了定时开关,让终端提前开机 2)在系统上层,开机启动项增加一个bat文件(s ...

  3. referer报头

    依据客户的到达方式定制页面 referer报头保存了用户是从哪个网站找到到这里来的信息的.如果用户直接输入了页面的地址,浏览器就不会发送referer信息. 5.6.1 创建一个可以根据链接过来的网站 ...

  4. 新浪微博登陆,获取token

    用WeiboAuthListener获取code 用下面的代码获取token..半成品的sdk真让人捉急. String code = values.getString("code" ...

  5. Eclipse颜色主题插件-Eclipse Color Theme

    Eclipse颜色主题插件-Eclipse Color Theme 由于看烦了eclipse自带的的配色方案,自己动手配置又太麻烦,无意间在 http://eclipsecolorthemes.org ...

  6. AgileEAS.NET SOA中间件平台/敏捷软件开发平台 and SQL详解

    AgileEAS.NET SOA中间件平台/敏捷软件开发平台 http://www.smarteas.net/ SQL详解: http://www.w3school.com.cn/sql/func_d ...

  7. web笔记

    application: 在tomcat启动过程,会将所有的应用加载进来,会为每一个应用创建一个application对象.这个对象是唯一.但是所有的web应用是互不影响的. like模糊查询 重定向 ...

  8. ArrayList 练习

    ArrayList list = new ArrayList(); Random rd = new Random(); ; i <; i++) { , ); //是否包含当前数字 if (!li ...

  9. GitHub 相关内容

    1. Git是分布式版本控制系统 集中式版本控制系统:版本库是集中存放在中央服务器的,而干活的时候,用的都是自己的电脑,所以要先从中央服务器取得最新的版本,然后开始干活,干完活了,再把自己的活推送给中 ...

  10. checkbox的问题整理

    之前做过一些checkbox的小功能,由于水平有限总是在这一部分花费一些时间,趁现在时间充裕些,整理出来. checkbox是否被选中的判断 第一种: $(this).is(":checke ...