经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Java » 查看文章
Lucene的简单用法
来源:cnblogs  作者:DingYu  时间:2018/10/25 9:36:34  对本文有异议

1.创建索引

  

  1. package com.DingYu.Test;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.io.UnsupportedEncodingException;
  7. import java.nio.file.Paths;
  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.document.Field.Store;
  13. import org.apache.lucene.document.StoredField;
  14. import org.apache.lucene.index.IndexWriter;
  15. import org.apache.lucene.index.IndexWriterConfig;
  16. import org.apache.lucene.store.Directory;
  17. import org.apache.lucene.store.FSDirectory;
  18. import org.junit.Test;
  19. /**
  20. * 我们的目标是把索引和文档存入索引库中, 所以首先我们需要创建一个索引库 然后创建一个IndexWrite对象把索引,和文档对象写入,
  21. * 文档对象中需要自己设置域,索引是通过分词器对域进行分词产生的, 所以我们需要分词器
  22. *
  23. * @author 丁宇
  24. *
  25. */
  26. public class LuceneTest {
  27. /**
  28. * 创建索引
  29. * @throws IOException
  30. */
  31. @Test
  32. public void createIndex() throws IOException {
  33. // 标准分词器
  34. Analyzer analyzer = new StandardAnalyzer();
  35. // 创建一个索引
  36. Directory directory = FSDirectory.open(Paths.get("D:\\LuceneIndex"));
  37. // 创建一个IndexWriteConfig对象
  38. IndexWriterConfig config = new IndexWriterConfig(analyzer);
  39. // 创建一个IndexWrite对象
  40. IndexWriter write = new IndexWriter(directory, config);
  41. // 获得所有文件下的文件
  42. File[] files = new File("D:\\LuceneTest").listFiles();
  43. for (File file : files) {
  44. // 创建一个文档对象
  45. Document document = new Document();
  46. // 增加一个filepath域,不分析 不索引 但会存储在索引库里 把文件路径放到域中
  47. Field field1 = new StoredField("filepath", file.getPath());
  48. // 增加一个filename域,会分词,会索引,
  49. Field field2 = new org.apache.lucene.document.TextField("filename", file.getName(), Store.YES);
  50. // 增加一个fileContent域,会分词,会索引,只放文件内容的索引
  51. Field field3 = new org.apache.lucene.document.TextField("filecontent", fileContent(file), Store.NO);
  52. // 增加一个filesize域,不分析 不索引 但会存储在索引库里 把文件路径放到域中
  53. Field field4 = new StoredField("filesize", file.length());
  54. document.add(field1);
  55. document.add(field2);
  56. document.add(field3);
  57. document.add(field4);
  58. write.addDocument(document);
  59. }
  60. write.close();
  61. }
  62. /**
  63. * 获得文件内容
  64. * @param file
  65. * @return
  66. */
  67. public String fileContent(File file) {
  68. byte[] fileContent = new byte[(int) file.length()];
  69. FileInputStream in = null;
  70. try {
  71. in = new FileInputStream(file);
  72. } catch (FileNotFoundException e2) {
  73. e2.printStackTrace();
  74. }
  75. try {
  76. in.read(fileContent);
  77. } catch (IOException e1) {
  78. e1.printStackTrace();
  79. }
  80. try {
  81. in.close();
  82. } catch (IOException e) {
  83. e.printStackTrace();
  84. }
  85. try {
  86. return new String(fileContent, "UTF-8");
  87. } catch (UnsupportedEncodingException e) {
  88. e.printStackTrace();
  89. }
  90. return null;
  91. }
  92. }

2.查询索引

  1. package com.DingYu.Test;
  2. import java.io.IOException;
  3. import java.nio.file.Paths;
  4. import org.apache.lucene.document.Document;
  5. import org.apache.lucene.index.DirectoryReader;
  6. import org.apache.lucene.index.IndexReader;
  7. import org.apache.lucene.index.Term;
  8. import org.apache.lucene.search.IndexSearcher;
  9. import org.apache.lucene.search.Query;
  10. import org.apache.lucene.search.ScoreDoc;
  11. import org.apache.lucene.search.TermQuery;
  12. import org.apache.lucene.search.TopDocs;
  13. import org.apache.lucene.store.Directory;
  14. import org.apache.lucene.store.FSDirectory;
  15. import org.apache.lucene.store.IOContext;
  16. import org.apache.lucene.store.IndexInput;
  17. import org.junit.Test;
  18. /**
  19. * 查询索引
  20. *
  21. * @author 丁宇
  22. *
  23. */
  24. public class LuceneTest1 {
  25. @Test
  26. public void selectIndex() throws IOException {
  27. // 指定索引库
  28. Directory directory = FSDirectory.open(Paths.get("D:\\LuceneIndex"));
  29. // 打开索引库
  30. IndexReader reader = DirectoryReader.open(directory);
  31. // 创建查询的对象
  32. IndexSearcher searcher = new IndexSearcher(reader);
  33. // 选择合适的查询方法,这里用最简单的,具体的看下图
  34. Query query = new TermQuery(new Term("filename", "txt"));
  35. // 执行查询
  36. TopDocs docs = searcher.search(query, 2);
  37. // 获得在索引库中存着的文档的id,利用id去寻找文档
  38. ScoreDoc[] scoreDocs = docs.scoreDocs;
  39. for (ScoreDoc scoreDoc : scoreDocs) {
  40. // 获得id
  41. int doc = scoreDoc.doc;
  42. // 获得文档
  43. Document document = searcher.doc(doc);
  44. // 获得这个文档的域
  45. System.out.println(document.get("filename"));
  46. System.out.println(document.get("filecontent"));
  47. System.out.println(document.get("filepath"));
  48. System.out.println(document.get("filesize"));
  49. System.out.println("------------------------");
  50. }
  51. // 关闭索引库
  52. reader.close();
  53. }
  54. }

 

 

3.删除索引

 

  1. package com.DingYu.Test;
  2. import java.io.IOException;
  3. import java.nio.file.Paths;
  4. import org.apache.lucene.analysis.Analyzer;
  5. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  6. import org.apache.lucene.index.IndexWriter;
  7. import org.apache.lucene.index.IndexWriterConfig;
  8. import org.apache.lucene.index.Term;
  9. import org.apache.lucene.search.Query;
  10. import org.apache.lucene.search.TermQuery;
  11. import org.apache.lucene.store.Directory;
  12. import org.apache.lucene.store.FSDirectory;
  13. import org.junit.Test;
  14. /**
  15. * 删除索引 一般增删改都是同一个操作对象 这里使用IndexWriter对象
  16. *
  17. * @author 丁宇
  18. *
  19. */
  20. public class LuceneTest3 {
  21. /**
  22. * 获得IndexWrite对象
  23. * @return
  24. * @throws IOException
  25. */
  26. public IndexWriter getIndexWrite() throws IOException {
  27. Analyzer analyzer = new StandardAnalyzer();
  28. Directory directory = FSDirectory.open(Paths.get("D:\\LuceneIndex"));
  29. IndexWriterConfig config = new IndexWriterConfig(analyzer);
  30. return new IndexWriter(directory, config);
  31. }
  32. /**
  33. * 删除所有的索引
  34. *
  35. * @throws IOException
  36. */
  37. @Test
  38. public void deleteAllIndex() throws IOException {
  39. IndexWriter indexWrite = getIndexWrite();
  40. indexWrite.deleteAll();
  41. indexWrite.close();
  42. }
  43. /**
  44. * 根据条件删除索引,同时删除文档
  45. * @throws IOException
  46. */
  47. @Test
  48. public void deleteSomeIndex() throws IOException {
  49. IndexWriter indexWrite = getIndexWrite();
  50. Query query = new TermQuery(new Term("filename","txt"));
  51. indexWrite.deleteDocuments(query);
  52. indexWrite.close();
  53. }
  54. }

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号