星空网 > 软件开发 > Java

lucene入门

一、lucene简介

  Lucene是apache下的一个靠性能的、功能全面的用纯java开发的一个全文搜索引擎库。它几乎适合任何需要全文搜索应用程序,尤其是跨平台。lucene是开源的免费的工程。lucene使用简单但是提供的功能非常强大。相关特点如下:

  • 在硬件上的速度超过150GB/小时
  • 更小的内存需求,只需要1MB堆空间
  • 快速地增加索引、与批量索引
  • 索引的大小大于为被索引文本的20%-30%

  lucene下载地址为:http://lucene.apache.org/ 

  文本示例工程使用maven构建,lucene版本为5.2.1。相关依赖文件如下:

<project ="http://maven.apache.org/POM/4.0.0" ="http://www.w3.org/2001/  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>com.shh</groupId>  <artifactId>lucene</artifactId>  <packaging>war</packaging>  <version>0.0.1-SNAPSHOT</version>  <name>lucene Maven Webapp</name>  <url>http://maven.apache.org</url>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    <lucene.version>5.2.1</lucene.version>  </properties>  <dependencies>    <dependency>      <groupId>org.apache.lucene</groupId>      <artifactId>lucene-core</artifactId>      <version>${lucene.version}</version>    </dependency>    <dependency>      <groupId>org.apache.lucene</groupId>      <artifactId>lucene-queryparser</artifactId>      <version>${lucene.version}</version>    </dependency>    <dependency>      <groupId>org.apache.lucene</groupId>      <artifactId>lucene-analyzers-common</artifactId>      <version>${lucene.version}</version>    </dependency>    <!-- 分词器 -->    <dependency>      <groupId>org.apache.lucene</groupId>      <artifactId>lucene-analyzers-smartcn</artifactId>      <version>${lucene.version}</version>    </dependency>    <dependency>      <groupId>org.apache.lucene</groupId>      <artifactId>lucene-highlighter</artifactId>      <version>${lucene.version}</version>    </dependency>  </dependencies>  <build>    <finalName>lucene</finalName>   </build></project>

 二、示例

  1、索引的创建

  相关代码如下:

 1 package com.test.lucene; 2  3 import java.io.IOException;  4 import java.nio.file.Paths;  5  6 import org.apache.lucene.analysis.Analyzer; 7 import org.apache.lucene.analysis.standard.StandardAnalyzer; 8 import org.apache.lucene.document.Document; 9 import org.apache.lucene.document.Field.Store;10 import org.apache.lucene.document.IntField;11 import org.apache.lucene.document.StringField;12 import org.apache.lucene.document.TextField;13 import org.apache.lucene.index.IndexWriter;14 import org.apache.lucene.index.IndexWriterConfig;15 import org.apache.lucene.index.IndexWriterConfig.OpenMode;16 import org.apache.lucene.store.Directory;17 import org.apache.lucene.store.FSDirectory;18 19 /**20  * 创建索引21 */22 public class IndexCreate {23   24   public static void main(String[] args) {25     // 指定分词技术,这里使用的是标准分词26     Analyzer analyzer = new StandardAnalyzer();27 28     // indexWriter的配置信息29     IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);30 31     // 索引的打开方式:没有则创建,有则打开32     indexWriterConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);33 34     Directory directory = null;35     IndexWriter indexWriter = null;36     try {37       // 索引在硬盘上的存储路径38       directory = FSDirectory.open(Paths.get("D://index/test"));39       //indexWriter用来创建索引文件40       indexWriter = new IndexWriter(directory, indexWriterConfig);41     } catch (IOException e) {42       e.printStackTrace();43     }44     45     //创建文档一46     Document doc1 = new Document();47     doc1.add(new StringField("id", "abcde", Store.YES));48     doc1.add(new TextField("content", "中国广州", Store.YES));49     doc1.add(new IntField("num", 1, Store.YES));50 51     //创建文档二52     Document doc2 = new Document();53     doc2.add(new StringField("id", "asdff", Store.YES));54     doc2.add(new TextField("content", "中国上海", Store.YES));55     doc2.add(new IntField("num", 2, Store.YES));56 57     try {58       //添加需要索引的文档59       indexWriter.addDocument(doc1);60       indexWriter.addDocument(doc2);61 62       // 将indexWrite操作提交,如果不提交,之前的操作将不会保存到硬盘63       // 但是这一步很消耗系统资源,索引执行该操作需要有一定的策略64       indexWriter.commit();65     } catch (IOException e) {66       e.printStackTrace();67     } finally {68       // 关闭资源69       try { 70         indexWriter.close();71         directory.close(); 72       } catch (IOException e) {73         e.printStackTrace();74       }75     }76   }77 }

  2、搜索

  相关代码如下:

 1 package com.test.lucene; 2  3 import java.io.IOException; 4 import java.nio.file.Paths; 5  6 import org.apache.lucene.analysis.Analyzer; 7 import org.apache.lucene.analysis.standard.StandardAnalyzer; 8 import org.apache.lucene.document.Document; 9 import org.apache.lucene.index.DirectoryReader;10 import org.apache.lucene.queryparser.classic.ParseException;11 import org.apache.lucene.queryparser.classic.QueryParser;12 import org.apache.lucene.search.IndexSearcher;13 import org.apache.lucene.search.Query;14 import org.apache.lucene.search.TopDocs;15 import org.apache.lucene.store.Directory;16 import org.apache.lucene.store.FSDirectory;17 18 /**19  * 搜索20 */21 public class IndexSearch {22   23   public static void main(String[] args) {24     //索引存放的位置25     Directory directory = null;26     try {27       // 索引硬盘存储路径28       directory = FSDirectory.open(Paths.get("D://index/test"));29       // 读取索引30       DirectoryReader directoryReader = DirectoryReader.open(directory);31       // 创建索引检索对象32       IndexSearcher searcher = new IndexSearcher(directoryReader);33       // 分词技术34       Analyzer analyzer = new StandardAnalyzer();35       // 创建Query36       QueryParser parser = new QueryParser("content", analyzer);37       Query query = parser.parse("广州");// 查询content为广州的38       // 检索索引,获取符合条件的前10条记录39       TopDocs topDocs = searcher.search(query, 10);40       if (topDocs != null) {41         System.out.println("符合条件的记录为: " + topDocs.totalHits);42         for (int i = 0; i < topDocs.scoreDocs.length; i++) {43           Document doc = searcher.doc(topDocs.scoreDocs[i].doc);44           System.out.println("id = " + doc.get("id"));45           System.out.println("content = " + doc.get("content"));46           System.out.println("num = " + doc.get("num"));47         }48       }49       directory.close();50       directoryReader.close();51     } catch (IOException e) {52       e.printStackTrace();53     } catch (ParseException e) {54       e.printStackTrace();55     }56   }57 }

  运行结果如下:

lucene入门

三、lucene的工作原理

  lucene全文搜索分为两个步骤:

  索引创建:将数据(包括数据库数据、文件等)进行信息提取,并创建索引文件。

  搜索索引:根据用户的搜索请求,对创建的索引进行搜索,并将搜索的结果返回给用户。

   相关示意图如下:

lucene入门




原标题:lucene入门

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

VAT税务缴税指南简单而有效的方法:https://www.kjdsnews.com/a/1341214.html
VAT税务缴税指南简单易懂的操作步骤:https://www.kjdsnews.com/a/1341215.html
VAT税务计算方法及方法指南:https://www.kjdsnews.com/a/1341216.html
VAT税务自主申报指南解决您的税务问题:https://www.kjdsnews.com/a/1341217.html
VAT税务自主申报指南把握最新税收规则:https://www.kjdsnews.com/a/1341218.html
VAT税务自主申报指南如何轻松完成申报:https://www.kjdsnews.com/a/1341219.html
长治婚庆女司仪和主持人:https://www.vstour.cn/a/366176.html
北京丰台区水上乐园哪家好玩?:https://www.vstour.cn/a/366177.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流