java面试题

当前位置: 面试问题网 > java面试题 > 介绍下Lucene建立索引的过程

介绍下Lucene建立索引的过程

代码如下:
  
   1. package utils;
   2.
   3. import java.io.File;
   4. import java.io.FileReader;
   5. import java.io.IOException;
   6. import java.io.Reader;
   7.
   8. import org.apache.lucene.analysis.standard.StandardAnalyzer;
   9. import org.apache.lucene.document.Document;
   10. import org.apache.lucene.document.Field;
   11. import org.apache.lucene.index.IndexWriter;
   12.
   13. public class Indexer {
   14.
   15. public int index(String indexDir, String dataDir) throws IOException
   16. {
   17. File indexDirFile = new File(indexDir);
   18. File dataDirFile = new File(dataDir);
   19. int numIndexed = index(indexDirFile, dataDirFile);
   20. return 0;
   21. }
   22.
   23. private int index(File indexDirFile, File dataDirFile) throws IOException {
   24. if(!dataDirFile.exists() || !dataDirFile.isDirectory())
   25. {
   26. throw new IOException(dataDirFile + ” does not exist or is not a directory”);
   27. }
   28. IndexWriter writer = new IndexWriter(indexDirFile, new StandardAnalyzer(), true);
   29. writer.setUseCompoundFile(false);
   30. indexDirectory(writer, dataDirFile);
   31.
   32. int numIndexed = writer.docCount();
   33. writer.optimize();
   34. writer.close();
   35. return numIndexed;
   36. }
   37.
   38. private void indexDirectory(IndexWriter writer, File dataDirFile) throws IOException {
   39. File[] files = dataDirFile.listFiles();
   40. for(int i = 0; i
   41. {
   42. File f = files[i];
   43. if(f.isDirectory())
   44. {
   45. indexDirectory(writer, f);
   46. }else if(f.getName().endsWith(”.java”) || f.getName().endsWith(”.txt”))//需要索引的文件类型
   47. {
   48. indexFile(writer, f);
   49. }
   50.
   51. }
   52.
   53. }
   54.
   55. private void indexFile(IndexWriter writer, File f) throws IOException {
   56. if(f.isHidden() || !f.exists() || !f.canRead())
   57. {
   58. return;
   59. }
   60. System.out.println(”Indexing” + f.getCanonicalPath());
   61. Document doc = new Document();
   62. Reader txtReader = new FileReader(f);
   63. doc.add(new Field(”path”,f.getCanonicalPath(),Field.Store.YES,Field.Index.UN_TOKENIZED));
   64. doc.add(new Field(”contents”,txtReader));
   65. doc.add(new Field(”name”,f.getName(),Field.Store.YES,Field.Index.UN_TOKENIZED));
   66. writer.addDocument(doc);
   67. }
   68.
   69. }
   70.
   71.
   调用的代码如下:
   1. String filesRepoDir = “C:/workspace-2.0″;//需要被索引的目录
   2. String indexDir = “C:/apache-tomcat-6.0.18/webapps/index”;//存放索引的目录
   3. Indexer indexer= new Indexer();
   4. indexer.index(indexDir, filesRepoDir);

【介绍下Lucene建立索引的过程】相关文章

1. 介绍下Lucene建立索引的过程

2. 简述索引存取方法的作用和建立索引的原则

3. 当我正在为表建立索引的时候,SQL Server 会禁止对表的访问吗

4. 如何用Lucene索引数据库

5. Lucene推荐的分页方式是什么?

6. 简述DNS进行域名解析的过程

7. TCP协议通讯的过程和步骤是什么

8. 简述安装Slackware Linux系统的过程

9. Can a struct inherit from another struct? (结构体能继承结构体吗)

10. 声明struct x1 { . . . }; 和typedef struct { . . . }x2;有什么不同

本文来源:https://www.mianshiwenti.com/a12510.html

点击展开全部

《介绍下Lucene建立索引的过程》

将本文的Word文档下载到电脑,方便收藏和打印

推荐程度:

进入下载页面

﹝介绍下Lucene建立索引的过程﹞相关内容

「介绍下Lucene建立索引的过程」相关专题

其它栏目

也许您还喜欢