一 .什麼是Lucene
lucene是一款高性能的、可擴展,純java語言編寫的信息檢索(IR)工具庫
它適合幾乎任何需要全文本搜索(特別是跨平臺)的應用程序
http://lucene.apache.org/java
二 .Lucene的原理
lucene是基於關鍵詞索引和查詢
全文分析:把文本解析爲一個個關鍵字存儲到索引文件中倒排索引: (英語:Invertedindex),也常被稱爲反向索引、置入檔案或反向檔案,是一種索引方法,
被用來存儲在全文搜索下某個單詞在一個文檔或者一組文檔中的存儲位置的映射。它是文檔檢索系統中最常用的數據結構
設有兩篇文章1和2
文章1的內容爲:
Tom lives in Guangzhou,I live in Guangzhoutoo.
文章2的內容爲:
He once lived inShanghai.
)全文分析
首先我們要取得這兩篇文章的關鍵詞,通常我們需要如下處理措施
a.我們現在有的是文章內容,即一個字符串,我們先要找出字符串中的所有單詞,即分詞。英文單詞由於用空格分隔,比較好處理。中文單詞間是連在一起的需要特殊的分詞處理。
b.文章中的”in”, “once” “too”等詞沒有什麼實際意義,中文中的“的”“是”等字通常也無具體含義,這些不代表概念的詞可以過濾掉 。
c.用戶通常希望查“He”時能把含“he”,“HE”的文章也找出來,所以所有單詞需要統一大小寫。
d.用戶通常希望查“live”時能把含“lives”,“lived”的文章也找出來,所以需要把“lives”,“lived”還原成“live”
e.文章中的標點符號通常不表示某種概念,也可以過濾掉
在lucene中以上措施由Analyzer類完成
經過上面處理後
文章1的所有關鍵詞爲:
[tom] [live] [guangzhou] [i] [live] [guangzhou]
文章2的所有關鍵詞爲:
[he] [live] [shanghai]
2) 倒排索引:有了關鍵詞後,我們就可以建立倒排索引了。
上面的對應關係是:“文章號”對“文章中所有關鍵詞”。
倒排索引把這個關係倒過來,變成:“關鍵詞”對“擁有該關鍵詞的所有文章號”。
文章1,2經過倒排後變成
通常僅知道關鍵詞在哪些文章中出現還不夠,我們還需要知道關鍵詞在文章中出現次數和出現的位置,通常有兩種位置:
a)字符位置,即記錄該詞是文章中第幾個字符(優點是關鍵詞亮顯時定位快);
b)關鍵詞位置,即記錄該詞是文章中第幾個關鍵詞(優點是節約索引空間、詞組(phase)查詢快),lucene中記錄的就是這種位置。
以live 這行爲例我們說明一下該結構:live在文章1中出現了2次,文章2中出現了一次,它的出現位置爲“2,5,2”這表示什麼呢?我們需要結合文章號和出現
頻率來分析,文章1中出現了2次,那麼“2,5”就表示live在文章1中出現的兩個位置,文章2中出現了一次,剩下的“2”就表示live是文章2中第2個關鍵字
三.簡單的Lucene的調用
創建maven項目 ,在pom.xml中配置分詞器的架包
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.et</groupId>
<artifactId>Lucene</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- 加載IKAnalyzer的架包 -->
<dependency>
<groupId>com.janeluo</groupId>
<artifactId>ikanalyzer</artifactId>
<version>2012_u6</version>
</dependency>
</dependencies>
</project>
簡單的搜索和創建索引庫package cn.et;
import java.io.File;
import java.io.IOException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;
public class IndexDemo {
static String drc="E:\\index";
//定義存儲目錄
static Analyzer aly= new IKAnalyzer();
public static void main(String[] args) throws Exception {
search();
}
//搜索
public static void search() throws Exception {
Directory dr= FSDirectory.open(new File(drc));
//讀取存儲目錄
DirectoryReader ird=DirectoryReader.open(dr);
//搜索類
IndexSearcher isearcher=new IndexSearcher(ird);
//Lucene查詢解析 用於指定查詢的屬性名和分詞器
QueryParser parser = new QueryParser(Version.LUCENE_47,"userDesc", aly);
//開始搜索
Query query=parser.parse("來");
//獲取搜索的結果指定返回的docuemnt個數
ScoreDoc[] hits= isearcher.search(query, null, 10).scoreDocs;
for(int i=0;i<hits.length;i++){
//獲取單獨的document
Document hitDoc = isearcher.doc(hits[i].doc);
System.out.println(hitDoc.getField("userName").stringValue());
}
ird.close();
dr.close();
}
//創建索引庫
public static void write() throws IOException{
//索引庫的存儲目錄
Directory dr= FSDirectory.open(new File(drc));
//管聯Lucene版本和當前分詞器
IndexWriterConfig cfi= new IndexWriterConfig(Version.LUCENE_47,aly);
//傳入目錄和分詞器
IndexWriter iwriter = new IndexWriter(dr,cfi);
//document對象Field屬性
Document doc= new Document();
//TextField.TYPE_STORED 寫入存儲目錄
Field field= new Field("userName","張三",TextField.TYPE_STORED);
doc.add(field);
field= new Field("userDesc","張三來自永州,喜歡吃永州血鴨",TextField.TYPE_STORED);
doc.add(field);
Document doce= new Document();
//TextField.TYPE_STORED 寫入存儲目錄
Field field1= new Field("userName","李四",TextField.TYPE_STORED);
doce.add(field1);
field1= new Field("userDesc","李四來自北京,喜歡吃烤鴨",TextField.TYPE_STORED);
doce.add(field1);
//寫入存儲目錄
iwriter.addDocument(doc);
iwriter.addDocument(doce);
iwriter.commit();
iwriter.close();
}
}