1、lucene簡介java
Lucene是apache下的一個靠性能的、功能全面的用純java開發的一個全文搜索引擎庫。它幾乎適合任何須要全文搜索應用程序,尤爲是跨平臺。lucene是開源的免費的工程。lucene使用簡單可是提供的功能很是強大。相關特色以下:數據庫
lucene下載地址爲:http://lucene.apache.org/ apache
文本示例工程使用maven構建,lucene版本爲5.2.1。相關依賴文件以下:app
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.shh</groupId> <artifactId>lucene</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>lucene Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <lucene.version>5.2.1</lucene.version> </properties> <dependencies> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-core</artifactId> <version>${lucene.version}</version> </dependency> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-queryparser</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-analyzers-smartcn</artifactId> <version>${lucene.version}</version> </dependency> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-highlighter</artifactId> <version>${lucene.version}</version> </dependency> </dependencies> <build> <finalName>lucene</finalName> </build> </project>
2、示例maven
一、索引的建立性能
相關代碼以下:ui
1 package com.test.lucene; 2 3 import java.io.IOException; 4 import java.nio.file.Paths; 5 6 import org.apache.lucene.analysis.Analyzer; 7 import org.apache.lucene.analysis.standard.StandardAnalyzer; 8 import org.apache.lucene.document.Document; 9 import org.apache.lucene.document.Field.Store; 10 import org.apache.lucene.document.IntField; 11 import org.apache.lucene.document.StringField; 12 import org.apache.lucene.document.TextField; 13 import org.apache.lucene.index.IndexWriter; 14 import org.apache.lucene.index.IndexWriterConfig; 15 import org.apache.lucene.index.IndexWriterConfig.OpenMode; 16 import org.apache.lucene.store.Directory; 17 import org.apache.lucene.store.FSDirectory; 18 19 /** 20 * 建立索引 21 */ 22 public class IndexCreate { 23 24 public static void main(String[] args) { 25 // 指定分詞技術,這裏使用的是標準分詞 26 Analyzer analyzer = new StandardAnalyzer(); 27 28 // indexWriter的配置信息 29 IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer); 30 31 // 索引的打開方式:沒有則建立,有則打開 32 indexWriterConfig.setOpenMode(OpenMode.CREATE_OR_APPEND); 33 34 Directory directory = null; 35 IndexWriter indexWriter = null; 36 try { 37 // 索引在硬盤上的存儲路徑 38 directory = FSDirectory.open(Paths.get("D://index/test")); 39 //indexWriter用來建立索引文件 40 indexWriter = new IndexWriter(directory, indexWriterConfig); 41 } catch (IOException e) { 42 e.printStackTrace(); 43 } 44 45 //建立文檔一 46 Document doc1 = new Document(); 47 doc1.add(new StringField("id", "abcde", Store.YES)); 48 doc1.add(new TextField("content", "中國廣州", Store.YES)); 49 doc1.add(new IntField("num", 1, Store.YES)); 50 51 //建立文檔二 52 Document doc2 = new Document(); 53 doc2.add(new StringField("id", "asdff", Store.YES)); 54 doc2.add(new TextField("content", "中國上海", Store.YES)); 55 doc2.add(new IntField("num", 2, Store.YES)); 56 57 try { 58 //添加須要索引的文檔 59 indexWriter.addDocument(doc1); 60 indexWriter.addDocument(doc2); 61 62 // 將indexWrite操做提交,若是不提交,以前的操做將不會保存到硬盤 63 // 可是這一步很消耗系統資源,索引執行該操做須要有必定的策略 64 indexWriter.commit(); 65 } catch (IOException e) { 66 e.printStackTrace(); 67 } finally { 68 // 關閉資源 69 try { 70 indexWriter.close(); 71 directory.close(); 72 } catch (IOException e) { 73 e.printStackTrace(); 74 } 75 } 76 } 77 }
二、搜索搜索引擎
相關代碼以下:url
1 package com.test.lucene; 2 3 import java.io.IOException; 4 import java.nio.file.Paths; 5 6 import org.apache.lucene.analysis.Analyzer; 7 import org.apache.lucene.analysis.standard.StandardAnalyzer; 8 import org.apache.lucene.document.Document; 9 import org.apache.lucene.index.DirectoryReader; 10 import org.apache.lucene.queryparser.classic.ParseException; 11 import org.apache.lucene.queryparser.classic.QueryParser; 12 import org.apache.lucene.search.IndexSearcher; 13 import org.apache.lucene.search.Query; 14 import org.apache.lucene.search.TopDocs; 15 import org.apache.lucene.store.Directory; 16 import org.apache.lucene.store.FSDirectory; 17 18 /** 19 * 搜索 20 */ 21 public class IndexSearch { 22 23 public static void main(String[] args) { 24 //索引存放的位置 25 Directory directory = null; 26 try { 27 // 索引硬盤存儲路徑 28 directory = FSDirectory.open(Paths.get("D://index/test")); 29 // 讀取索引 30 DirectoryReader directoryReader = DirectoryReader.open(directory); 31 // 建立索引檢索對象 32 IndexSearcher searcher = new IndexSearcher(directoryReader); 33 // 分詞技術 34 Analyzer analyzer = new StandardAnalyzer(); 35 // 建立Query 36 QueryParser parser = new QueryParser("content", analyzer); 37 Query query = parser.parse("廣州");// 查詢content爲廣州的 38 // 檢索索引,獲取符合條件的前10條記錄 39 TopDocs topDocs = searcher.search(query, 10); 40 if (topDocs != null) { 41 System.out.println("符合條件的記錄爲: " + topDocs.totalHits); 42 for (int i = 0; i < topDocs.scoreDocs.length; i++) { 43 Document doc = searcher.doc(topDocs.scoreDocs[i].doc); 44 System.out.println("id = " + doc.get("id")); 45 System.out.println("content = " + doc.get("content")); 46 System.out.println("num = " + doc.get("num")); 47 } 48 } 49 directory.close(); 50 directoryReader.close(); 51 } catch (IOException e) { 52 e.printStackTrace(); 53 } catch (ParseException e) { 54 e.printStackTrace(); 55 } 56 } 57 }
運行結果以下:spa
3、lucene的工做原理
lucene全文搜索分爲兩個步驟:
索引建立:將數據(包括數據庫數據、文件等)進行信息提取,並建立索引文件。
搜索索引:根據用戶的搜索請求,對建立的索引進行搜索,並將搜索的結果返回給用戶。
相關示意圖以下: