当前位置: 代码迷 >> Web前端 >> lucene创设索引
  详细解决方案

lucene创设索引

热度:384   发布时间:2012-11-22 00:16:41.0
lucene创建索引

lucene创建索引:

  1. import?java.io.File;
  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.document.Document;
  6. import?org.apache.lucene.document.Field;
  7. import?org.apache.lucene.index.CorruptIndexException;
  8. import?org.apache.lucene.index.IndexWriter;
  9. import?org.apache.lucene.index.IndexWriterConfig;
  10. import?org.apache.lucene.index.IndexWriterConfig.OpenMode;
  11. import?org.apache.lucene.store.Directory;
  12. import?org.apache.lucene.store.FSDirectory;
  13. import?org.apache.lucene.store.LockObtainFailedException;
  14. import?org.apache.lucene.util.Version;
  15. public?class?LuceneTest?{
  16. ????String?docsPath?=?null;???//文件位置
  17. ????String?indexPath?=null;???//索引位置
  18. ????public?static?void?main(String[]?args)?{
  19. ????????LuceneTest?test?=?new?LuceneTest();
  20. ????????try?{
  21. ????????????test.index();
  22. ????????}?catch?(CorruptIndexException?e)?{
  23. ????????????//?TODO?Auto-generated?catch?block
  24. ????????????e.printStackTrace();
  25. ????????}?catch?(LockObtainFailedException?e)?{
  26. ????????????//?TODO?Auto-generated?catch?block
  27. ????????????e.printStackTrace();
  28. ????????}?catch?(IOException?e)?{
  29. ????????????//?TODO?Auto-generated?catch?block
  30. ????????????e.printStackTrace();
  31. ????????}
  32. ????}
  33. ????/**
  34. ?????*?先创立索引
  35. ?????*?@throws?IOException
  36. ?????*?@throws?LockObtainFailedException
  37. ?????*?@throws?CorruptIndexException
  38. ?????*/
  39. ????public?void?index()?throws?CorruptIndexException,?LockObtainFailedException,?IOException
  40. ????{
  41. ????????docsPath=”F:\\Search?engine\\搜索引擎\\lucene-3.1.0-src\\lucene-3.1.0\\contrib\\analyzers\\common\\readm.txt”;
  42. ????????indexPath=”D:\\mywork\\LuceneTest\\lucenedic”;
  43. ????????if(docsPath==null)
  44. ????????{
  45. ????????????System.err.println(“docsPath为空”);
  46. ????????????System.exit(1);
  47. ????????}
  48. ????????File?docDir?=?new?File(docsPath);
  49. ????????Analyzer?analyzer?=?new?StandardAnalyzer(Version.LUCENE_31);
  50. ????????Directory?dir?=?FSDirectory.open(new?File(indexPath));
  51. ????????IndexWriterConfig?iwc?=?new?IndexWriterConfig(Version.LUCENE_31,?analyzer);
  52. ????????/*IndexWriter?indexWriter1?=?new?IndexWriter(dir,new?IndexWriterConfig(Version.LUCENE_31,
  53. ????????????????????????????????????new?WhitespaceAnalyzer(Version.LUCENE_31)));*/
  54. ????????iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
  55. ????????IndexWriter?indexWriter?=?new?IndexWriter(dir,iwc);
  56. ????????Document?doc?=?new?Document();
  57. ????????Field?pathField?=?new?Field(“path”,docDir.getPath(),Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS);
  58. ????????pathField.setOmitTermFreqAndPositions(true);
  59. ????????doc.add(pathField);
  60. ????????indexWriter.addDocument(doc);
  61. ????????indexWriter.close();
  62. ????}
  63. }
  相关解决方案