1. 程式人生 > >lucene的基礎入門

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

填寫索引庫的位置:檢視索引庫