Lucene入门案例

1. 新建maven工程导入lucene相关依赖

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- mysql版本 -->
        <mysql.version>5.1.10</mysql.version>
        <!-- lucene版本 -->
        <lucene.version>4.10.3</lucene.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.6</version>
            <scope>provided</scope>
        </dependency>
        <!-- mysql数据库依赖 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <!--lucene依赖包 -->
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-core</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-queryparser</artifactId>
            <version>${lucene.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!-- ik分词器 -->
        <dependency>
            <groupId>com.janeluo</groupId>
            <artifactId>ikanalyzer</artifactId>
            <version>2012_u6</version>
        </dependency>
    </dependencies>

 2.准备索引数据

 

package com.pomelo.dao.impl;

import com.pomelo.dao.BookDao;
import com.pomelo.domain.Book;
import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

//准备文档数据
public class BookDaoImpl implements BookDao {

    @Override
    public List<Book> findAll() {

        List<Book> bookList = new ArrayList<>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            //1.加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2. 创建连接
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ssm", "root", "root");
            //3. 创建预编译对象
            statement = connection.prepareStatement("SELECT * from book");
            //4. 执行查询,得到结果集
            resultSet = statement.executeQuery();

            //5. 循环遍历,放入list容器
            while (resultSet.next()) {
                Book book = new Book();
                book.setId(resultSet.getInt("id"));
                book.setBookName(resultSet.getString("bookname"));
                book.setPrice(resultSet.getFloat("price"));
                book.setPic(resultSet.getString("pic"));
                book.setBookDesc(resultSet.getString("bookdesc"));
                bookList.add(book);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //6. 释放资源
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (statement != null) {
                    statement.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        return bookList;
    }

    //测试
    @Test
    public void findAllTest() {
        List<Book> bookList = findAll();
        System.out.println(bookList);
    }

}

 

3.创建索引库

package com.pomelo.search;

import com.pomelo.dao.BookDao;
import com.pomelo.dao.impl.BookDaoImpl;
import com.pomelo.domain.Book;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
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.apache.lucene.util.Version;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

//2. 创建索引
public class IndexManagerDemo {

    @Test
    public void createIndex() throws IOException {

        //1. 采集文档数据
        BookDao bookDao = new BookDaoImpl();
        List<Book> bookList = bookDao.findAll();

        //2. 创建文档对象
        List<Document> documentList = new ArrayList<>();
        for (Book book : bookList) {
            Document document = new Document();
            /**
             * IntField整型类型域,TextField文本类型域,FloatField浮点型类型域
             * 参数1:域名--对应数据库中字段名
             * 参数2:域值
             * 参数3:是否存储--是否需要将该域对应的值存储到文档中
             */
            document.add(new TextField("bookId", book.getId() + "", Store.YES));
            document.add(new TextField("bookName", book.getBookName(), Store.YES));
            document.add(new TextField("bookPrice", book.getPrice() + "", Store.YES));
            document.add(new TextField("bookPic", book.getPic(), Store.YES));
            document.add(new TextField("bookDesc", book.getBookDesc(), Store.YES));

            documentList.add(document);
        }

        //3. 创建分词器
        Analyzer analyzer = new IKAnalyzer();

        //4、创建文档索引配置对象
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_4_10_3, analyzer);

        //5、创建存放索引目录Directory,指定索引存放路径
        File file = new File("C:\\tmp\\lucene\\book1");
        Directory directory = FSDirectory.open(file);

        //6、创建索引编写器
        IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);

        //7、利用索引编写器写入文档到索引目录
        for (Document document : documentList) {
            //把文档对象写入到索引库中
            indexWriter.addDocument(document);
        }

        //8. 释放资源
        indexWriter.close();

    }

}

 

 

4.检索索引

package com.pomelo.search;

import org.apache.lucene.analysis.Analyzer;
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.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;

//3. 检索索引
public class SearchManagerDemo {

    @Test
    public void searchIndex() throws Exception {

        //1. 创建分词器
        Analyzer analyzer = new IKAnalyzer();

        //2、创建查询对象Query
        //2.1、创建查询分析器;参数1:默认查询的域,参数2:分词器
        QueryParser queryParser = new QueryParser("bookName", analyzer);
        //2.2、创建Query对象
        Query query = queryParser.parse("java");

        //3、创建存放索引目录Directory,指定索引存放路径
        Directory directory = FSDirectory.open(new File("C:\\tmp\\lucene\\book1"));

        //4、创建索引读对象IndexReader
        IndexReader indexReader = DirectoryReader.open(directory);

        //5、创建索引搜索对象IndexSearcher,执行搜索,返回结果
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        /**
         * 参数1:查询对象
         * 参数2:查询前n个文档
         * 返回结果:得分文档(包含文档数组,总的命中数)
         */
        TopDocs topDocs = indexSearcher.search(query, 10);
        System.out.println("符合本次查询的总命中文档数为:" + topDocs.totalHits);

        //6、处理搜索结果
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        for (ScoreDoc scoreDoc : scoreDocs) {
            System.out.println("文档在Lucene中的id为:" + scoreDoc.doc + ";文档分值为:" + scoreDoc.score);
            //根据lucene中的文档id查询到文档
            Document document = indexSearcher.doc(scoreDoc.doc);

            System.out.println("文档id为:" + document.get("bookId"));
            System.out.println("名称为:" + document.get("bookName"));
            System.out.println("价格为:" + document.get("bookPrice"));
            System.out.println("图片为:" + document.get("bookPic"));
            System.out.println("描述为:" + document.get("bookDesc"));
            System.out.println("---------------------------------------");
        }
        //7、释放资源
        indexReader.close();
    }

}

 

posted @ 2019-08-28 17:08  闲云陌路  阅读(255)  评论(0编辑  收藏  举报