lucene

朴灿烈づ我的快乐病毒、 2022-06-04 10:38 248阅读 0赞

lucene 全文检索
流程
1创建索引库
2查询索引库

先建立索引,在对索引进行搜索就是全文检索
虽然创建索引的过程非常耗时,但索引一旦创建就可以多次使用,
全文检索主要处理是查询,所以耗时创建索引是值得的

lucene是apache下的一个开源代码的全文检索引擎工具包,提供了完整的查询和索引引擎
,部分文本分析引擎

索引文档和原始文档都会被放进索引库

爬虫
Nutch
Jsoup
heritrix

Luke工具查看索引库

lucene内置的分词器不能分中文,对中文分词会分为一个一个的字或者词语分析差。

IK-analyzer:IK分词器支持中文

有些词语并不能支持分解,例如高富帅,白富美,而有些单个字并不是我们想要的分词,我们可以配置自己的扩展字典和扩展停止字典,配置文件放在根路径下
配置文件如下
这里写图片描述

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
  3. <properties>
  4. <comment>IK Analyzer 扩展配置</comment>
  5. <!--用户可以在这里配置自己的扩展字典 -->
  6. <entry key="ext_dict">ext.dic;</entry>
  7. <!--用户可以在这里配置自己的扩展停止词字典-->
  8. <entry key="ext_stopwords">stopword.dic;</entry>
  9. </properties>

这里写图片描述

  1. // 创建索引
  2. @Test
  3. public void testIndex() throws Exception {
  4. // 第一步:创建一个java工程,并导入jar包。
  5. // 第二步:创建一个indexwriter对象。
  6. Directory directory = FSDirectory.open(new File("D:\\temp\\index"));
  7. // Directory directory = new RAMDirectory();//保存索引到内存中 (内存索引库)
  8. // Analyzer analyzer = new StandardAnalyzer();// 官方推荐
  9. Analyzer analyzer = new IKAnalyzer();// 官方推荐
  10. IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, analyzer);
  11. IndexWriter indexWriter = new IndexWriter(directory, config);
  12. // 1)指定索引库的存放位置Directory对象
  13. // 2)指定一个分析器,对文档内容进行分析。
  14. // 第三步:创建field对象,将field添加到document对象中。
  15. File f = new File("D:\\Lucene&solr\\searchsource");
  16. File[] listFiles = f.listFiles();
  17. for (File file : listFiles) {
  18. // 第三步:创建document对象。
  19. Document document = new Document();
  20. // 文件名称
  21. String file_name = file.getName();
  22. Field fileNameField = new TextField("fileName", file_name, Store.YES);
  23. // 文件大小
  24. long file_size = FileUtils.sizeOf(file);
  25. Field fileSizeField = new LongField("fileSize", file_size, Store.YES);
  26. // 文件路径
  27. String file_path = file.getPath();
  28. Field filePathField = new StoredField("filePath", file_path);
  29. // 文件内容
  30. String file_content = FileUtils.readFileToString(file);
  31. Field fileContentField = new TextField("fileContent", file_content, Store.NO);
  32. document.add(fileNameField);
  33. document.add(fileSizeField);
  34. document.add(filePathField);
  35. document.add(fileContentField);
  36. // 第四步:使用indexwriter对象将document对象写入索引库,此过程进行索引创建。并将索引和document对象写入索引库。
  37. indexWriter.addDocument(document);
  38. }
  39. // 第五步:关闭IndexWriter对象。
  40. indexWriter.close();
  41. }
  42. // 搜索索引
  43. @Test
  44. public void testSearch() throws Exception {
  45. // 第一步:创建一个Directory对象,也就是索引库存放的位置。
  46. Directory directory = FSDirectory.open(new File("D:\\temp\\index"));// 磁盘
  47. // 第二步:创建一个indexReader对象,需要指定Directory对象。
  48. IndexReader indexReader = DirectoryReader.open(directory);
  49. // 第三步:创建一个indexsearcher对象,需要指定IndexReader对象
  50. IndexSearcher indexSearcher = new IndexSearcher(indexReader);
  51. // 第四步:创建一个TermQuery对象,指定查询的域和查询的关键词。
  52. Query query = new TermQuery(new Term("fileName", "lucene"));
  53. // 第五步:执行查询。
  54. TopDocs topDocs = indexSearcher.search(query, 10);
  55. // 第六步:返回查询结果。遍历查询结果并输出。
  56. ScoreDoc[] scoreDocs = topDocs.scoreDocs;
  57. for (ScoreDoc scoreDoc : scoreDocs) {
  58. int doc = scoreDoc.doc;
  59. Document document = indexSearcher.doc(doc);
  60. // 文件名称
  61. String fileName = document.get("fileName");
  62. System.out.println(fileName);
  63. // 文件内容
  64. String fileContent = document.get("fileContent");
  65. System.out.println(fileContent);
  66. // 文件大小
  67. String fileSize = document.get("fileSize");
  68. System.out.println(fileSize);
  69. // 文件路径
  70. String filePath = document.get("filePath");
  71. System.out.println(filePath);
  72. System.out.println("------------");
  73. }
  74. // 第七步:关闭IndexReader对象
  75. indexReader.close();
  76. }
  77. // 查看标准分析器的分词效果
  78. @Test
  79. public void testTokenStream() throws Exception {
  80. // 创建一个标准分析器对象
  81. // Analyzer analyzer = new StandardAnalyzer();
  82. // Analyzer analyzer = new CJKAnalyzer();
  83. // Analyzer analyzer = new SmartChineseAnalyzer();
  84. Analyzer analyzer = new IKAnalyzer();
  85. // 获得tokenStream对象
  86. // 第一个参数:域名,可以随便给一个
  87. // 第二个参数:要分析的文本内容
  88. // TokenStream tokenStream = analyzer.tokenStream("test",
  89. // "The Spring Framework provides a comprehensive programming and configuration model.");
  90. TokenStream tokenStream = analyzer.tokenStream("test",
  91. "高富帅可以用二维表结构来逻辑表达实现的数据");
  92. // 添加一个引用,可以获得每个关键词
  93. CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
  94. // 添加一个偏移量的引用,记录了关键词的开始位置以及结束位置
  95. OffsetAttribute offsetAttribute = tokenStream.addAttribute(OffsetAttribute.class);
  96. // 将指针调整到列表的头部
  97. tokenStream.reset();
  98. // 遍历关键词列表,通过incrementToken方法判断列表是否结束
  99. while (tokenStream.incrementToken()) {
  100. // 关键词的起始位置
  101. System.out.println("start->" + offsetAttribute.startOffset());
  102. // 取关键词
  103. System.out.println(charTermAttribute);
  104. // 结束位置
  105. System.out.println("end->" + offsetAttribute.endOffset());
  106. }
  107. tokenStream.close();
  108. }

发表评论

表情:
评论列表 (有 0 条评论,248人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Lucene

    什么是Lucene Lucene是一套用于全文检索和搜寻的开源程序库,由Apache软件基金会支持和提供 Lucene提供了一个简单却强大的应用程序接口(AP

    相关 lucene

    Lucene是apache组织的一个用java实现全文搜索引擎的开源项目。其功能非常的强大,但api其实很简单的,它最主要就是做两件事:建立索引和进行搜索。 1. 建立索引

    相关 lucene

    一、           Introduction Lucene是apache的一个项目,是一个开源的全文检索引擎工具包,不是完整的全文检索引擎,而是一个全文检索引擎的架构,

    相关 lucene

    lucene 全文检索 流程 1创建索引库 2查询索引库 先建立索引,在对索引进行搜索就是全文检索 虽然创建索引的过程非常耗时,但索引一旦创建就可以多次使用,

    相关 lucene

    lucene适用于全文检索,文件内容检索,目前少数情境下会使用lucene,而不用sql 一. 原始记录表+索引表 1.lucene索引库在硬盘中,原始库接受的是D

    相关 Lucene的使用,Lucene入门

    本文主要介绍几个方面,为什么使用Lucene使用场景,解决的问题,Lucene的入门使用,以及Lucene一些语法(增删改查)。 一简述Lucene概念:磁盘上的一些邮件,文

    相关 lucene

      Lucene是一款高性能的、可扩展的信息检索(IR)工具库。信息检索是指文档搜索、文档内信息搜索或者文档相关的元数据搜索等操作。 lucene实现全文检索的流程为: