lucene的基础入门

一 创建maven项目 lucene_1

引入依赖:

<dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</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>
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-analyzers-common</artifactId>
            <version>4.10.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-core -->
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-core</artifactId>
            <version>4.10.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-queryparser -->
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-queryparser</artifactId>
            <version>4.10.3</version>
        </dependency>
    </dependencies>

二 准备数据

1.创建表book

CREATE TABLE `book` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(192) DEFAULT NULL,
  `price` float DEFAULT NULL,
  `pic` varchar(96) DEFAULT NULL,
  `description` text
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2.编写Book实体类

package domain;

public class Book {
    private Integer id;
    private String name;
    private Float price;
    private String pic;
    private String description;

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", pic='" + pic + '\'' +
                ", description='" + description + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    public String getPic() {
        return pic;
    }

    public void setPic(String pic) {
        this.pic = pic;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

3 编写Dao,查询所有图书信息

public class BookDao {
    public List<Book> findAll(){
        List<Book> bookList = new ArrayList<>();
        Connection conn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        String url = "jdbc:mysql:///mybatis1";
        String user = "root";
        String root = "root";
        //加载驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        try {
            //获得链接
            conn = DriverManager.getConnection(url, user, root);
            //获得执行者对象
            pst = conn.prepareStatement("SELECT  * FROM  Book");
            //获得结果集
            rs = pst.executeQuery();
            while (rs.next()) {
                //封装结果集
                Book book = new Book();
                book.setId(rs.getInt("id"));
                book.setName(rs.getString("name"));
                book.setPic(rs.getString("pic"));
                book.setPrice(rs.getFloat("price"));
                book.setDescription(rs.getString("description"));

                bookList.add(book);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (pst != null) {
                try {
                    pst.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return bookList;
    }
}

三 创建索引

public class TestCreateIndex {
    @Test
    public void test() throws IOException {
       // 创建分词器
        Analyzer analyzer = new StandardAnalyzer();
        //指定索引的位置:创建索引库的位置对象
        FSDirectory fsDirectory = FSDirectory.open(new File("f:/dic"));
        //创建索引输出流配置对象: 版本号,分词器对象
        IndexWriterConfig writerConfig = new IndexWriterConfig(Version.LUCENE_4_10_3,analyzer);
        //索引输出流: 索引库位置, 索引输出流配置对象
        IndexWriter indexWriter = new IndexWriter(fsDirectory,writerConfig);

        //获取所有数据
        BookDao bookDao = new BookDao();
        List<Book> bookList = bookDao.findAll();
        //创建doc 文档对象列表
        List<Document> docList = new ArrayList<>();
        //一条数据对应一个文档
        for (Book book : bookList) {
            //一个列对应一个域对象
            Document doc = new Document();
            
            TextField idField = new TextField("id",String.valueOf(book.getId()), Field.Store.YES);
            TextField nameField = new TextField("name",book.getName(), Field.Store.YES);
            TextField picField = new TextField("pic",book.getPic(), Field.Store.YES);
            TextField priceField = new TextField("price",String.valueOf(book.getPrice()), Field.Store.YES);
            TextField descriptionField = new TextField("description",book.getDescription(), Field.Store.YES);
            //将域对象添加到文档中
            doc.add(idField);
            doc.add(nameField);
            doc.add(picField);
            doc.add(priceField);
            doc.add(descriptionField);

            docList.add(doc);
        }
        //通过索引写到索引库
        for (Document document : docList) {
            indexWriter.addDocument(document);
        }
        //提交
        indexWriter.commit();
        //关闭liu
        indexWriter.close();
    }
}

 四 查看测试索引

准备:lukeall-4.10.3.jar(本人直接放在f盘根目录)

在创建索引时的位置为f:dic

 

运行jar

填写索引库的位置:查看索引库

 

 

 

 

猜你喜欢

转载自www.cnblogs.com/Cyan-W/p/9938510.html