lucene学习笔记

lucene学习笔记
1.maven依赖

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">
4.0.0

top.simba1949
lucene
1.0-SNAPSHOT




org.apache.lucene
lucene-core
5.3.1



org.apache.lucene
lucene-analyzers-common
5.3.1



org.apache.lucene
lucene-analyzers-smartcn
5.3.1



org.apache.lucene
lucene-queryparser
5.3.1



org.apache.lucene
lucene-highlighter
5.3.1



mysql
mysql-connector-java
5.1.40


junit
junit
4.12
test


org.wltea.ik-analyzer
ik-analyzer
5.3.1.RELEASE


org.wltea.ik-analyzer
ik-analyzer-extra
5.3.1.RELEASE





org.apache.maven.plugins
maven-compiler-plugin
3.5.1


1.8
1.8




org.apache.maven.plugins
maven-resources-plugin
2.7

UTF-8




org.apache.tomcat.maven
tomcat7-maven-plugin


80
/

UTF-8






src/main/java

**/*.xml



src/main/resources

**/*.xml
**/*.properties





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 documents = new ArrayList();
//采集数据,从数据库获取数据
List 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();
}
}

你可能感兴趣的:(lucene学习笔记)