lucene笔记

 

2.创建索引库

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();
}
}

posted @ 2019-12-09 14:32  威兰达  阅读(169)  评论(0编辑  收藏  举报