Lucene的步驟

來源:互聯網
上載者:User

標籤:返回   family   dex   writer   表示   sea   field   名稱   .com   

// 1. 採集資料

BookDao bookDao = new BookDaoImpl();

List<Book> bookList = bookDao.queryBookList();

 

// 2. 建立Document文檔對象

List<Document> documents = new ArrayList<>();

for (Book book : bookList) {

Document document = new Document();

 

// Document文檔中添加Field域

// 圖書Id

// Store.YES:表示儲存到文檔域中

document.add(new TextField("id", book.getId().toString(), Store.YES));

// 圖書名稱

document.add(new TextField("name", book.getName().toString(), Store.YES));

// 圖書價格

document.add(new TextField("price", book.getPrice().toString(), Store.YES));

// 圖書圖片地址

document.add(new TextField("pic", book.getPic().toString(), Store.YES));

// 圖書描述

document.add(new TextField("desc", book.getDesc().toString(), Store.YES));

 

// 把Document放到list中

documents.add(document);

}

 

// 3. 建立Analyzer分詞器,分析文檔,對文檔進行分詞

Analyzer analyzer = new StandardAnalyzer();

 

// 4. 建立Directory對象,聲明索引庫的位置

Directory directory = FSDirectory.open(new File("C:/itcast/lucene/index"));

 

// 5. 建立IndexWriteConfig對象,寫入索引需要的配置

IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_10_3, analyzer);

 

// 6.建立IndexWriter寫入對象

IndexWriter indexWriter = new IndexWriter(directory, config);

 

// 7.寫入到索引庫,通過IndexWriter添加文檔對象document

for (Document doc : documents) {

indexWriter.addDocument(doc);

}

 

// 8.釋放資源

indexWriter.close();

 

 

 

 

實現搜尋

 1. 建立Query搜尋對象

 2. 建立Directory流對象,聲明索引庫位置

 3. 建立索引讀取對象IndexReader

 4. 建立索引搜尋對象IndexSearcher

 5. 使用索引搜尋對象,執行搜尋,返回結果集TopDocs

 6. 解析結果集

 7. 釋放資源

 

 分詞器的底層:

 

 語彙單元的形成過程

 

Lucene的步驟

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.