lucene学习笔记
1.maven依赖
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>top.simba1949</groupId><artifactId>lucene</artifactId><version>1.0-SNAPSHOT</version><dependencies><!--核心包--><dependency><groupId>org.apache.lucene</groupId><artifactId>lucene-core</artifactId><version>5.3.1</version></dependency><!--一般分词器,适用于英文分词--><dependency><groupId>org.apache.lucene</groupId><artifactId>lucene-analyzers-common</artifactId><version>5.3.1</version></dependency><!--中文分词器--><dependency><groupId>org.apache.lucene</groupId><artifactId>lucene-analyzers-smartcn</artifactId><version>5.3.1</version></dependency><!--对分词索引查询解析--><dependency><groupId>org.apache.lucene</groupId><artifactId>lucene-queryparser</artifactId><version>5.3.1</version></dependency><!--检索关键字高亮显示--><dependency><groupId>org.apache.lucene</groupId><artifactId>lucene-highlighter</artifactId><version>5.3.1</version></dependency><!--MySQL--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.40</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency><groupId>org.wltea.ik-analyzer</groupId><artifactId>ik-analyzer</artifactId><version>5.3.1.RELEASE</version></dependency><dependency><groupId>org.wltea.ik-analyzer</groupId><artifactId>ik-analyzer-extra</artifactId><version>5.3.1.RELEASE</version></dependency></dependencies><build><plugins><!--编译插件--><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.5.1</version><configuration><!-- 配置使用的 jdk 版本 --><target>1.8</target><source>1.8</source></configuration></plugin><!-- 资源文件拷贝插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><version>2.7</version><configuration><encoding>UTF-8</encoding></configuration></plugin><!-- 配置Tomcat插件 --><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><configuration><!-- http://localhost:{port}/{path}--><port>80</port><path>/</path><!-- 解决GET请求编码 --><uriEncoding>UTF-8</uriEncoding></configuration></plugin></plugins><!--IDEA是不会编译src的java目录的xml文件,如果需要读取,则需要手动指定哪些配置文件需要读取 --><resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes></resource><resource><directory>src/main/resources</directory><includes><include>**/*.xml</include><include>**/*.properties</include></includes></resource></resources></build></project>
2.创建索引库
package top.simba1949.lucene;import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.document.TextField;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.IndexWriterConfig;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;import org.junit.Before;import org.junit.Test;import top.simba1949.dao.BookDao;import top.simba1949.dao.impl.BookDaoImpl;import top.simba1949.model.Book;import java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.List;public class CreateIndex { private BookDao bookDao; @Before public void init(){ bookDao = new BookDaoImpl();}/*** * 1.采集数据 * 2.创建Document文档对象 * 3.创建分析器(分词器) * 4.创建IndexWriterConfig配置信息类 * 5.创建Directory对象,声明索引库存储位置 * 6.创建IndexWriter写入对象 * 7.把Document写入到索引库中 * 8.释放资源 */ @Test public void createIndex() throws IOException {//创建Documnent对象 List<Document> documents = new ArrayList<Document>();//采集数据,从数据库获取数据 List<Book> books = bookDao.queryBookList(); for (Book book : books){ Document doc = new Document(); doc.add(new TextField("id",book.getId()+"", Field.Store.YES)); doc.add(new TextField("name",book.getName(), Field.Store.YES)); doc.add(new TextField("price",book.getPrice()+"", Field.Store.YES)); doc.add(new TextField("pic",book.getPic(), Field.Store.YES)); doc.add(new TextField("desc",book.getDesc(), Field.Store.YES)); documents.add(doc);}//创建一个分词器 Analyzer analyzer = new StandardAnalyzer();//创建IndexWriterConfig配置信息类 IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);//创建Directory,生命索引库存储位置 Directory directory = FSDirectory.open(new File("D:/index").toPath());//创建indexWriter写入对象 IndexWriter indexWriter = new IndexWriter(directory,indexWriterConfig);//把document写入到索引库 indexWriter.addDocuments(documents);//提交操作 indexWriter.commit();//关闭资源 indexWriter.close();}}
3.实现搜索流程
package top.simba1949.lucene;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.index.DirectoryReader;import org.apache.lucene.index.IndexReader;import org.apache.lucene.queryparser.classic.QueryParser;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.Query;import org.apache.lucene.search.ScoreDoc;import org.apache.lucene.search.TopDocs;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;import org.junit.Before;import org.junit.Test;import top.simba1949.dao.BookDao;import top.simba1949.dao.impl.BookDaoImpl;import java.io.File;public class SearchIndex {/*** * 1. 创建Query搜索对象 * 2. 创建Directory流对象,声明索引库位置 * 3. 创建索引读取对象IndexReader * 4. 创建索引搜索对象IndexSearcher * 5. 使用索引搜索对象,执行搜索,返回结果集TopDocs * 6. 解析结果集 * 7. 释放资源 */ @Test public void Search() throws Exception{//创建分词器 StandardAnalyzer analyzer = new StandardAnalyzer();//构建搜索条件 QueryParser queryParser = new QueryParser("desc", analyzer); Query query = queryParser.parse("desc:java");//创建Directory流对象,声明索引库位置 Directory directory = FSDirectory.open(new File("D:/index").toPath());//创建索引引导对象indexReader IndexReader indexReader = DirectoryReader.open(directory);//创建索引搜索对象Indexearcher IndexSearcher indexSearcher = new IndexSearcher(indexReader);//使用索引索引对象执行搜索,返回结果接TopDocs TopDocs docs = indexSearcher.search(query,2);//获得查询的文档数据 ScoreDoc[] scoreDocs = docs.scoreDocs; for (ScoreDoc scoreDoc : scoreDocs){ Document doc = indexSearcher.doc(scoreDoc.doc); System.out.println("----------------------------"); String name = doc.get("name"); System.out.println("bookId:"+doc.get("id")); System.out.println("name"+doc.get("name")); System.out.println("price"+doc.get("price")); System.out.println("pic"+doc.get("pid"));}//关闭资源 indexReader.close();}}