Luncene

来源:互联网 发布:解压缩软件密码破解 编辑:程序博客网 时间:2024/06/05 04:05

这是第一次在csdn上写技术博客,就从Lucene开始吧。
对于Lucene这个工具来说的话,最重要的就是采集数据,把数据封装到文档对象中去,导入jar包这些我就不多说了,我用到的版本是4.10.3的。要了解Lucene要了解他的原理图。
这里写图片描述
索引流程:采集数据—》文档处理存储到索引库中
搜索流程:输入查询条件—》通过lucene的查询器查询索引—》从索引库中取出结—》视图渲染

Lucene本身不能进行视图渲染。

以下是我的测试`代码

public class IndexManger {
@Test
public void createIndex() throws Exception {
// 采集数据
Bookdao bookdao = new BookDaoImpl();
List list = bookdao.queryBooks();
// 将采集的数据封装到Document中
List dolists = new ArrayList<>();
Document document;
for (Book book : list) {
// store:如果是yes的话,存储到文档域中
document = new Document();
// 图书
Field id = new TextField(“id”, book.getId().toString(), Store.YES);
// 图书的name
Field name = new TextField(“name”, book.getName(), Store.YES);
// 图书的价格
Field price = new TextField(“price”, book.getPrice().toString(), Store.YES);
// 图书的描述
Field description = new TextField(“description”, book.getDescription(), Store.YES);
// 图书的图片路径
Field pic = new TextField(“pic”, book.getPic(), Store.YES);

        // 将field域设置到document对象中去        document.add(id);        document.add(name);        document.add(price);        document.add(description);        document.add(pic);        dolists.add(document);    }    // 1.创建分词器,标准分词器    Analyzer analyzer = new StandardAnalyzer();    // 2.创建一个索引写的对象    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_10_3, analyzer);    Directory dirctory = FSDirectory.open(new File("G:\\lucen"));    IndexWriter indexWriter = new IndexWriter(dirctory, config);    // 3.用indexWriter写入到索引库中去    for (Document doc : dolists) {        indexWriter.addDocument(doc);    }    // 4.关闭indexwriter    indexWriter.close();}

}
`
所以总体来说的话,有这么几个步骤,首先需要的是从数据酷里面采集到数据。
第二步的话把数据封装到document的对象里去。
第三步的话是 将field域设置到document对象中去。
数据的封装到现在就结束了

然后是开始索引的操作了
这里写图片描述
第一步 创建一个分词器

我在代码里用的是一个标准的分词器,其实还有很多的分词器,后面还会给大家进行分享。

第二步 创建一个索引写的对象,是为了确定写入的位置

第三步 用索引写的对象把他写入本地,进行持久化

第四步 不要忘记关闭流!!!!

原创粉丝点击