Lucene 6.5.0 入門Demo


Lucene 6.5.0 要求jdk 1.8

1.目錄結構;

2.數據庫環境;

private int id;
private String name;
private float price;
private String pic;
private String description

3.

LuceneApache的一個全文檢索引擎工具包,它不能獨立運行不能單獨對外提供服務

 

 

/**
 * Created by  on 2017/4/25.
 */
public class IndexManager {
    @Test
    public void createIndex() throws Exception {
        // 采集數據
        BookDao dao = new BookDaoImpl();
        List<Book> list = dao.queryBooks();

        // 將采集到的數據封裝到Document對象中
        List<Document> docList = new ArrayList<Document>();
        Document document;
        for (Book book : list) {
            document = new Document();
            // store:如果是yes,則說明存儲到文檔域中
            // 圖書ID
            // Field id = new TextField("id", book.getId().toString(), Store.YES);

            Field id = new TextField("id", Integer.toString(book.getId()), Field.Store.YES);
            // 圖書名稱
            Field name = new TextField("name", book.getName(), Field.Store.YES);
            // 圖書價格
            Field price = new TextField("price", Float.toString(book.getPrice()), Field.Store.YES);
            // 圖書圖片地址
            Field pic = new TextField("pic", book.getPic(), Field.Store.YES);
            // 圖書描述
            Field description = new TextField("description", book.getDescription(), Field.Store.YES);

            // 將field域設置到Document對象中
            document.add(id);
            document.add(name);
            document.add(price);
            document.add(pic);
            document.add(description);

            docList.add(document);
        }
        //JDK 1.7以后 open只能接收Path/////////////////////////////////////////////////////

        // 創建分詞器,標准分詞器
        Analyzer analyzer = new StandardAnalyzer();

        // 創建IndexWriter
        // IndexWriterConfig cfg = new IndexWriterConfig(Version.LUCENE_6_5_0,analyzer);
        IndexWriterConfig cfg = new IndexWriterConfig(analyzer);

        // 指定索引庫的地址
//         File indexFile = new File("D:\\L\a\Eclipse\\lecencedemo\\");
//         Directory directory = FSDirectory.open(indexFile);
        Directory directory = FSDirectory.open(FileSystems.getDefault().getPath("D:\\Lpj\\JetBrains\\lucenceIndex1\\"));

        IndexWriter writer = new IndexWriter(directory, cfg);
      writer.deleteAll(); //清除以前的index
        // 通過IndexWriter對象將Document寫入到索引庫中
        for (Document doc : docList) {
            writer.addDocument(doc);
        }

        // 關閉writer
        writer.close();
    }

}

  

 

/**
 * Created by  on 2017/4/25.
 */
public class IndexSearch {
    private void doSearch(Query query) {
        // 創建IndexSearcher
        // 指定索引庫的地址
        try {
//			File indexFile = new File("D:\\Lpj\\Eclipse\\lecencedemo\\");
//			Directory directory = FSDirectory.open(indexFile);
            // 1、創建Directory
            //JDK 1.7以后 open只能接收Path
            Directory directory = FSDirectory.open(FileSystems.getDefault().getPath("D:\\Lpj\\JetBrains\\lucenceIndex1\\"));
            IndexReader reader = DirectoryReader.open(directory);
            IndexSearcher searcher = new IndexSearcher(reader);
            // 通過searcher來搜索索引庫
            // 第二個參數:指定需要顯示的頂部記錄的N條
            TopDocs topDocs = searcher.search(query, 10);

            // 根據查詢條件匹配出的記錄總數
            int count = topDocs.totalHits;
            System.out.println("匹配出的記錄總數:" + count);
            // 根據查詢條件匹配出的記錄
            ScoreDoc[] scoreDocs = topDocs.scoreDocs;

            for (ScoreDoc scoreDoc : scoreDocs) {
                // 獲取文檔的ID
                int docId = scoreDoc.doc;

                // 通過ID獲取文檔
                Document doc = searcher.doc(docId);
                System.out.println("商品ID:" + doc.get("id"));
                System.out.println("商品名稱:" + doc.get("name"));
                System.out.println("商品價格:" + doc.get("price"));
                System.out.println("商品圖片地址:" + doc.get("pic"));
                System.out.println("==========================");
                // System.out.println("商品描述:" + doc.get("description"));
            }
            // 關閉資源
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void indexSearch() throws Exception {
        // 創建query對象
        Analyzer analyzer = new StandardAnalyzer();
        // 使用QueryParser搜索時,需要指定分詞器,搜索時的分詞器要和索引時的分詞器一致
        // 第一個參數:默認搜索的域的名稱
        QueryParser parser = new QueryParser("description", analyzer);

        // 通過queryparser來創建query對象
        // 參數:輸入的lucene的查詢語句(關鍵字一定要大寫)
        Query query = parser.parse("description:java AND lucene");

        doSearch(query);

    }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM