Lucene-3.0.0配置
一、Lucene開發環境配置
step1.Lucene開發包下載
step2.Java開發環境配置
step3.Tomcat安裝
step4.Lucene開發環境配置
解壓下載的lucene-3.0.0.zip,可以看到lucene-core-3.0.0.jar和lucene-demos-3.0.0.jar這兩個文件,將其解壓(建議放在安裝jdk的lib文件夾內),並把路徑添加到環境變量的classpath。 二、Lucene開發包中Demo調試 控制檯應用程序 step1.建立索引 >java org.apache.lucene.demo.IndexFiles [C:\Java](已經存在的任意文件路徑) 將對C:\Java下所有文件建立索引,同時,在當前命令行位置將生成“index”文件夾。 step2.執行查詢 >java org.apache.lucene.demo.SearchFiles 將會出現“Query:”提示符,在其後輸入關鍵字,回車,即可得到查詢結果。 Web應用程序 step1.將lucene-core-3.0.0.jar和lucene-demos-3.0.0jar這兩個文件複製到安裝Tomcat 的\common\lib中 step2.解壓下載的lucene-3.0.0.zip,可以看到luceneweb.war文件。將該文件複製到安裝Tomcat的\webapps step3.重啓Tomcat服務器。 step4.建立索引
>java org.apache.lucene.demo.IndexHTML -create -index [索引數據存放路徑] [被索引文件路徑](如:D:\lucene\temp\index D:\lucene\temp\docs) step5.打開安裝Tomcat的\webapps\luceneweb\configuration.jsp文件,找到String indexLocation = "***",將"***"改爲第四步中[索引數據存放路徑],保存關閉。 step6.執行查詢 http://localhost:8080/luceneweb 在文本框中輸入關鍵字,執行,即可得到查詢結果。
說明:本文采用lucene-3.0.0版本,運行step6 時查詢報錯,根據提示將安裝Tomcat的webapps\luceneweb\results.jsp 中 [ QueryParser qp = new QueryParser("contents", analyzer); ] 修改爲 [ QueryParser qp = new QueryParser(Version.LUCENE_CURRENT,"contents", analyzer); ]
注:本文參考YM's house |
總結一下lucene的環境搭建,查看以及瞭解lucene的原理,對其有個大概的瞭解。
1、下載lucene2.3.2
地址:http://apache.mirror.phpchina.com/lucene/java/
2、下載jdk1.6
3、下載tomcat
下載以上內容完成後,開始安裝。
1、安裝jdk
一路確定下去,無需選擇。
2、安裝tomcat
一路確定下去,無需選擇。
3、解壓文件即可
假設解壓文件路徑爲d:\lucene\
現在可以建立目錄(此處的目錄爲我們要進行檢索的信息的原始數據文件,我們放置在docs中,還有一個是lucene生成的檢索信息,我們放置於index中),即可以在d:\lucene下建立一個temp\docs以及temp\index,此處兩個文件夾目錄可以隨意,當然不一定非得放置於d:\lucene。
然後將需要檢索的原始數據文件放置於docs文件夾中。
拷貝解壓的lucene文件夾中的lucene-core-2.3.2.jar以及lucene-demos-2.3.2.jar到temp文件夾中,解壓。
如果沒有配置jdk環境,參考下方:
打開我的電腦-屬性-高級-環境變量:
在系統變量中添加:
JAVA_HOME C:\Program Files\Java\jdk1.6.0
PATH %JAVA_HOME%\bin
CLASSPATH .;%JAVA_HOME%\lib\tools.jar;%JAVA_HOME%\jre\lib\rt.jar;
打開命令行:將目錄定位到temp文件夾。
輸入命令:
java org.apache.lucene.demo.IndexHTML -create -index D:\lucene\temp\index D:\lucene\temp\docs
即建立索引與原始數據文件的關係。
完成後,會發現index文件夾中多處一部分數據,以後再研究。
然後找到tomcat的安裝目錄,拷貝lucene中的luceneweb.war進入tomcat的webapps\文件夾中,啓動tomcat,會看見webapps\下多出一個文件夾,找到configuration.jsp文件,將其中的String indexLocation = "/opt/lucene/index";修改爲String indexLocation = "D:/lucene/temp/index";就是剛纔生成的文件。
打開瀏覽器,輸入http://127.0.0.1:8080/luceneweb/
輸入需要查詢的信息,看看結果如何。
簡單地說:首先建立索引文件放置目錄,cmd命令生成索引文件,部署工程,修改工程文件中目標爲索引文件目錄。
搜索引擎的組成
搜索引擎一般由搜索器、索引器、檢索器和用戶接口四個部分組成:
搜索器
其功能是在互聯網中漫遊,發現和蒐集信息;
索引器
其功能是理解搜索器所搜索到的信息,從中抽取出索引項,用於表示文檔以及生成文檔庫的索引表;
檢索器
其功能是根據用戶的查詢在索引庫中快速檢索文檔,進行相關度評價,對將要輸出的結果排序,並能按用戶的查詢需求合理反饋信息;
用戶接口
其作用是接納用戶查詢、顯示查詢結果、提供個性化查詢項。
d:\lucene\index是上一篇學習筆記([Lucene3.0學習筆記1(建立索引)] )中生成的索引文件的存放地址。具體步驟簡介如下:
1、創建Directory對象,索引文件夾
2、創建IndexSearch對象,建立查詢(參數是Directory對象)
3、創建QueryParser對象(lucene版本,查詢Field字段,所用分詞器)
4、生成Query對象,由QueryParser對象的parse函數生成(參數是所查的關鍵字)
5、建立TopDocs對象(IndexSearch的search函數,參數是Query查詢對象,)
6、TopDocs對象數組裏存放查詢信息
7、關閉IndexSearch
索引創建和搜索過程所一個總結
Lucene教程
Lucene是apache組織的一個用java實現全文搜索引擎的開源項目。 其功能非常的強大,api也很簡單。總得來說用Lucene來進行建立 和搜索和操作數據庫是差不多的(有點像),Document可以看作是 數據庫的一行記錄,Field可以看作是數據庫的字段。用lucene實 現搜索引擎就像用JDBC實現連接數據庫一樣簡單。
Lucene2.0,它與以前廣泛應用和介紹的Lucene 1.4.3並不兼容。 Lucene2.0的下載地址是http://apache.justdn.org/lucene/java/
例子一 :
1、在windows系統下的的C盤,建一個名叫s的文件夾,在該文件夾裏面隨便建三個txt文件,隨便起名啦,就叫"1.txt","2.txt"和"3.txt"啦
其中1.txt的內容如下:
中華人民共和國
全國人民
2006年
而"2.txt"和"3.txt"的內容也可以隨便寫幾寫,這裏懶寫,就複製一個和1.txt文件的內容一樣吧
2、下載lucene包,放在classpath路徑中
建立索引:
package lighter.javaeye.com;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
/** */ /**
* author lighter date 2006-8-7
*/
public class TextFileIndexer {
public static void main(String[] args) throws Exception {
/**/ /* 指明要索引文件夾的位置,這裏是C盤的S文件夾下 */
File fileDir = new File( " c://s " );
/**/ /* 這裏放索引文件的位置 */
File indexDir = new File( " c://index " );
Analyzer luceneAnalyzer = new StandardAnalyzer(); //建立一個標準分析器
IndexWriter indexWriter = new IndexWriter(indexDir, luceneAnalyzer,
true ); //創建一個索引器
File[] textFiles = fileDir.listFiles();
long startTime = new Date().getTime();
//增加document到索引去
for ( int i = 0 ; i < textFiles.length; i ++ ) {
if (textFiles[i].isFile()
&& textFiles[i].getName().endsWith( " .txt " )) {
System.out.println( " File " + textFiles[i].getCanonicalPath()
+ "正在被索引 . " );
String temp = FileReaderAll(textFiles[i].getCanonicalPath(),
" GBK " );
System.out.println(temp);
Document document = new Document(); //Document是一個記錄。用來表示一個條目。就是搜索建立的倒排索引的條目。比如說,你要搜索自己電腦上的文件。這個時候就可以創建field。然後用field組合成 document 。最後會變成若干文件。這個document和 文件系統document不是一個概念。
Field FieldPath = new Field( " path " , textFiles[i].getPath(),
Field.Store.YES, Field.Index.NO); //創建一個字段
Field FieldBody = new Field( " body " , temp, Field.Store.YES,
Field.Index.TOKENIZED,
Field.TermVector.WITH_POSITIONS_OFFSETS);
document.add(FieldPath);
document.add(FieldBody);
indexWriter.addDocument(document);
}
}
// optimize()方法是對索引進行優化
indexWriter.optimize();
indexWriter.close();
//測試一下索引的時間
long endTime = new Date().getTime();
System.out
.println( "這花費了 "
+ (endTime - startTime)
+ " 毫秒來把文檔增加到索引裏面去! "
+ fileDir.getPath());
}
public static String FileReaderAll(String FileName, String charset)
throws IOException {
BufferedReader reader = new BufferedReader( new InputStreamReader(
new FileInputStream(FileName), charset));
String line = new String();
String temp = new String();
while ((line = reader.readLine()) != null ) {
temp += line;
}
reader.close();
return temp;
}
}
索引的結果:
File C:/s/ 1 .txt正在被索引 .
中華人民共和國全國人民2006年
File C:/s/ 2 .txt正在被索引 .
中華人民共和國全國人民2006年
File C:/s/ 3 .txt正在被索引 .
中華人民共和國全國人民2006年
這花費了297 毫秒來把文檔增加到索引裏面去 ! c:/s
3、建立了索引之後,查詢啦....
package lighter.javaeye.com;
import java.io.IOException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
public class TestQuery {
public static void main(String[] args) throws IOException, ParseException {
Hits hits = null ;
String queryString = "中華 " ;
Query query = null ;
IndexSearcher searcher = new IndexSearcher( " c://index " );
Analyzer analyzer = new StandardAnalyzer();
try {
QueryParser qp = new QueryParser( " body " , analyzer);
query = qp.parse(queryString);
} catch (ParseException e) {
}
if (searcher != null ) {
hits = searcher.search(query);
if (hits.length() > 0 ) {
System.out.println( "找到: " + hits.length() + " 個結果! " );
}
}
}
}
其運行結果:
找到: 3 個結果 !
Lucene其實很簡單的,它最主要就是做兩件事:建立索引和進行搜索
來看一些在lucene中使用的術語,這裏並不打算作詳細的介紹,只是點一下而已----因爲這一個世界有一種好東西,叫搜索。
IndexWriter:lucene中最重要的的類之一,它主要是用來將文檔加入索引,同時控制索引過程中的一些參數使用。
Analyzer:分析器,主要用於分析搜索引擎遇到的各種文本。常用的有StandardAnalyzer分析器,StopAnalyzer分析器,WhitespaceAnalyzer分析器等。
Directory:索引存放的位置;lucene提供了兩種索引存放的位置,一種是磁盤,一種是內存。一般情況將索引放在磁盤上;相應地lucene提供了FSDirectory和RAMDirectory兩個類。
Document:文檔;Document相當於一個要進行索引的單元,任何可以想要被索引的文件都必須轉化爲Document對象才能進行索引。
Field:字段。
IndexSearcher:是lucene中最基本的檢索工具,所有的檢索都會用到IndexSearcher工具;
Query:查詢,lucene中支持模糊查詢,語義查詢,短語查詢,組合查詢等等,如有TermQuery,BooleanQuery,RangeQuery,WildcardQuery等一些類。
QueryParser:是一個解析用戶輸入的工具,可以通過掃描用戶輸入的字符串,生成Query對象。
Hits:在搜索完成之後,需要把搜索結果返回並顯示給用戶,只有這樣纔算是完成搜索的目的。在lucene中,搜索的結果的集合是用Hits類的實例來表示的。
上面作了一大堆名詞解釋,下面就看幾個簡單的實例吧:
1、簡單的的StandardAnalyzer測試例子
package lighter.javaeye.com;
import java.io.IOException;
import java.io.StringReader;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
public class StandardAnalyzerTest
{
//構造函數,
public StandardAnalyzerTest()
{
}
public static void main(String[] args)
{
//生成一個StandardAnalyzer對象
Analyzer aAnalyzer = new StandardAnalyzer();
//測試字符串
StringReader sr = new StringReader( " lighter javaeye com is the are on " );
//生成TokenStream對象
TokenStream ts = aAnalyzer.tokenStream( " name " , sr);
try {
int i = 0 ;
Token t = ts.next();
while (t != null )
{
//輔助輸出時顯示行號
i ++ ;
//輸出處理後的字符
System.out.println( "第 " + i + "行: " + t.termText());
//取得下一個字符
t = ts.next();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
顯示結果:
第1行:lighter
第2行:javaeye
第3行:com
提示一下:
StandardAnalyzer是lucene中內置的"標準分析器",可以做如下功能:
1、對原有句子按照空格進行了分詞
2、所有的大寫字母都可以能轉換爲小寫的字母
3、可以去掉一些沒有用處的單詞,例如"is","the","are"等單詞,也刪除了所有的標點
查看一下結果與"new StringReader("lighter javaeye com is the are on")"作一個比較就清楚明瞭。
這裏不對其API進行解釋了,具體見lucene的官方文檔。需要注意一點,這裏的代碼使用的是lucene2的API,與1.43版有一些明顯的差別。
2、看另一個實例,簡單地建立索引,進行搜索
package lighter.javaeye.com;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.FSDirectory;
public class FSDirectoryTest {
//建立索引的路徑
public static final String path = " c://index2 " ;
public static void main(String[] args) throws Exception {
Document doc1 = new Document();
doc1.add( new Field( " name " , " lighter javaeye com " ,Field.Store.YES,Field.Index.TOKENIZED));
Document doc2 = new Document();
doc2.add( new Field( " name " , " lighter blog " ,Field.Store.YES,Field.Index.TOKENIZED));
IndexWriter writer = new IndexWriter(FSDirectory.getDirectory(path, true ), new StandardAnalyzer(), true );
writer.setMaxFieldLength( 3 );
writer.addDocument(doc1);
writer.setMaxFieldLength( 3 );
writer.addDocument(doc2);
writer.close();
IndexSearcher searcher = new IndexSearcher(path);
Hits hits = null ;
Query query = null ;
QueryParser qp = new QueryParser( " name " , new StandardAnalyzer());
query = qp.parse( " lighter " );
hits = searcher.search(query);
System.out.println( "查找/ " lighter/ " 共 " + hits.length() + "個結果 " );
query = qp.parse( " javaeye " );
hits = searcher.search(query);
System.out.println( "查找/ " javaeye/ " 共 " + hits.length() + "個結果 " );
}
}
運行結果:
查找 " lighter " 共2個結果
查找 " javaeye " 共1個結果
到現在我們已經可以用lucene建立索引了
下面介紹一下幾個功能來完善一下:
1.索引格式
其實索引目錄有兩種格式,
一種是除配置文件外,每一個Document獨立成爲一個文件(這種搜索起來會影響速度)。
另一種是全部的Document成一個文件,這樣屬於複合模式就快了。
2.索引文件可放的位置:
索引可以存放在兩個地方1.硬盤,2.內存
放在硬盤上可以用FSDirectory(),放在內存的用RAMDirectory()不過一關機就沒了
FSDirectory.getDirectory(File file, boolean create)
FSDirectory.getDirectory(String path, boolean create)
兩個工廠方法返回目錄
New RAMDirectory()就直接可以
再和
IndexWriter(Directory d, Analyzer a, boolean create)
一配合就行了
如:
IndexWrtier indexWriter = new IndexWriter(FSDirectory.getDirectory(“c://index”, true ), new StandardAnlyazer(), true );
IndexWrtier indexWriter = new IndexWriter( new RAMDirectory(), new StandardAnlyazer(), true );
3.索引的合併
這個可用
IndexWriter.addIndexes(Directory[] dirs)
將目錄加進去
來看個例子:
public void UniteIndex() throws IOException
{
IndexWriter writerDisk = new IndexWriter(FSDirectory.getDirectory( " c://indexDisk " , true ), new StandardAnalyzer(), true );
Document docDisk = new Document();
docDisk.add( new Field( " name " , "程序員之家 " ,Field.Store.YES,Field.Index.TOKENIZED));
writerDisk.addDocument(docDisk);
RAMDirectory ramDir = new RAMDirectory();
IndexWriter writerRam = new IndexWriter(ramDir, new StandardAnalyzer(), true );
Document docRam = new Document();
docRam.add( new Field( " name " , "程序員雜誌 " ,Field.Store.YES,Field.Index.TOKENIZED));
writerRam.addDocument(docRam);
writerRam.close(); //這個方法非常重要,是必須調用的
writerDisk.addIndexes( new Directory[] {ramDir} );
writerDisk.close();
}
public void UniteSearch() throws ParseException, IOException
{
QueryParser queryParser = new QueryParser( " name " , new StandardAnalyzer());
Query query = queryParser.parse( "程序員 " );
IndexSearcher indexSearcher = new IndexSearcher( " c://indexDisk " );
Hits hits = indexSearcher.search(query);
System.out.println( "找到了 " + hits.length() + "結果 " );
for ( int i = 0 ;i
{
Document doc = hits.doc(i);
System.out.println(doc.get( " name " ));
}
}
這個例子是將內存中的索引合併到硬盤上來.
注意:合併的時候一定要將被合併的那一方的IndexWriter的close()方法調用。
4.對索引的其它操作:
IndexReader類是用來操作索引的,它有對Document,Field的刪除等操作。
下面一部分的內容是:全文的搜索
全文的搜索主要是用:IndexSearcher,Query,Hits,Document(都是Query的子類),有的時候用QueryParser
主要步驟:
1 . new QueryParser(Field字段, new 分析器)
2 .Query query = QueryParser.parser(“要查詢的字串”);這個地方我們可以用反射api看一下query究竟是什麼類型
3 . new IndexSearcher(索引目錄).search(query);返回Hits
4 .用Hits.doc(n);可以遍歷出Document
5 .用Document可得到Field的具體信息了。
其實1 ,2兩步就是爲了弄出個Query實例,究竟是什麼類型的看分析器了。
拿以前的例子來說吧
QueryParser queryParser = new QueryParser( " name " , new StandardAnalyzer());
Query query = queryParser.parse( "程序員 " );
/**/ /*這裏返回的就是org.apache.lucene.search.PhraseQuery */
IndexSearcher indexSearcher = new IndexSearcher( " c://indexDisk " );
Hits hits = indexSearcher.search(query);
不管是什麼類型,無非返回的就是Query的子類,我們完全可以不用這兩步直接new個Query的子類的實例就ok了,不過一般還是用這兩步因爲它返回的是PhraseQuery這個是非常強大的query子類它可以進行多字搜索用QueryParser可以設置各個關鍵字之間的關係這個是最常用的了。
IndexSearcher:
其實IndexSearcher它內部自帶了一個IndexReader用來讀取索引的,IndexSearcher有個close()方法,這個方法不是用來關閉IndexSearche的是用來關閉自帶的IndexReader。
QueryParser呢可以用parser.setOperator()來設置各個關鍵字之間的關係(與還是或)它可以自動通過空格從字串裏面將關鍵字分離出來。
注意:用QueryParser搜索的時候分析器一定的和建立索引時候用的分析器是一樣的。
Query:
可以看一個lucene2.0的幫助文檔有很多的子類:
BooleanQuery, ConstantScoreQuery, ConstantScoreRangeQuery, DisjunctionMaxQuery, FilteredQuery, MatchAllDocsQuery, MultiPhraseQuery, MultiTermQuery, PhraseQuery, PrefixQuery, RangeQuery, SpanQuery, TermQuery
各自有用法看一下文檔就能知道它們的用法了
下面一部分講一下lucene的分析器:
分析器是由分詞器和過濾器組成的,拿英文來說吧分詞器就是通過空格把單詞分開,過濾器就是把the,to,of等詞去掉不被搜索和索引。
我們最常用的是StandardAnalyzer()它是lucene的標準分析器它集成了內部的許多的分析器。
最後一部分了:lucene的高級搜索了
1.排序
Lucene有內置的排序用IndexSearcher.search(query,sort)但是功能並不理想。我們需要自己實現自定義的排序。
這樣的話得實現兩個接口: ScoreDocComparator, SortComparatorSource
用IndexSearcher.search(query,new Sort(new SortField(String Field,SortComparatorSource)));
就看個例子吧:
這是一個建立索引的例子:
public void IndexSort() throws IOException
{
IndexWriter writer = new IndexWriter( " C://indexStore " , new StandardAnalyzer(), true );
Document doc = new Document()
doc.add( new Field( " sort " , " 1 " ,Field.Store.YES,Field.Index.TOKENIZED));
writer.addDocument(doc);
doc = new Document();
doc.add( new Field( " sort " , " 4 " ,Field.Store.YES,Field.Index.TOKENIZED));
writer.addDocument(doc);
doc = new Document();
doc.add( new Field( " sort " , " 3 " ,Field.Store.YES,Field.Index.TOKENIZED));
writer.addDocument(doc);
doc = new Document();
doc.add( new Field( " sort " , " 5 " ,Field.Store.YES,Field.Index.TOKENIZED));
writer.addDocument(doc);
doc = new Document();
doc.add( new Field( " sort " , " 9 " ,Field.Store.YES,Field.Index.TOKENIZED));
writer.addDocument(doc);
doc = new Document();
doc.add( new Field( " sort " , " 6 " ,Field.Store.YES,Field.Index.TOKENIZED));
writer.addDocument(doc);
doc = new Document();
doc.add( new Field( " sort " , " 7 " ,Field.Store.YES,Field.Index.TOKENIZED));
writer.addDocument(doc);
writer.close();
}
下面是搜索的例子:
[code]
public void SearchSort1() throws IOException, ParseException
{
IndexSearcher indexSearcher = new IndexSearcher("C://indexStore");
QueryParser queryParser = new QueryParser("sort",new StandardAnalyzer());
Query query = queryParser.parse("4");
Hits hits = indexSearcher.search(query);
System.out.println("有"+hits.length()+"個結果");
Document doc = hits.doc(0);
System.out.println(doc.get("sort"));
}
public void SearchSort2() throws IOException, ParseException
{
IndexSearcher indexSearcher = new IndexSearcher("C://indexStore");
Query query = new RangeQuery(new Term("sort","1"),new Term("sort","9"),true);//這個地方前面沒有提到,它是用於範圍的Query可以看一下幫助文檔.
Hits hits = indexSearcher.search(query,new Sort(new SortField("sort",new MySortComparatorSource())));
System.out.println("有"+hits.length()+"個結果");
for(int i=0;i
{
Document doc = hits.doc(i);
System.out.println(doc.get("sort"));
}
}
public class MyScoreDocComparator implements ScoreDocComparator
{
private Integer[]sort;
public MyScoreDocComparator(String s,IndexReader reader, String fieldname) throws IOException
{
sort = new Integer[reader.maxDoc()];
for(int i = 0;i
{
Document doc =reader.document(i);
sort[i]=new Integer(doc.get("sort"));
}
}
public int compare(ScoreDoc i, ScoreDoc j)
{
if(sort[i.doc]>sort[j.doc])
return 1;
if(sort[i.doc]
return -1;
return 0;
}
public int sortType()
{
return SortField.INT;
}
public Comparable sortValue(ScoreDoc i)
{
// TODO自動生成方法存根
return new Integer(sort[i.doc]);
}
}
public class MySortComparatorSource implements SortComparatorSource
{
private static final long serialVersionUID = -9189690812107968361L;
public ScoreDocComparator newComparator(IndexReader reader, String fieldname)
throws IOException
{
if(fieldname.equals("sort"))
return new MyScoreDocComparator("sort",reader,fieldname);
return null;
}
}[/code]
SearchSort1()輸出的結果沒有排序,SearchSort2()就排序了。
2.多域搜索MultiFieldQueryParser
如果想輸入關鍵字而不想關心是在哪個Field裏的就可以用MultiFieldQueryParser了
用它的構造函數即可後面的和一個Field一樣。
MultiFieldQueryParser. parse(String[] queries, String[] fields, BooleanClause.Occur[] flags, Analyzer analyzer) ~~~~~~~~~~~~~~~~~
第三個參數比較特殊這裏也是與以前lucene1.4.3不一樣的地方
看一個例子就知道了
String[] fields = {"filename", "contents", "description"};
BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
BooleanClause.Occur.MUST,//在這個Field裏必須出現的
BooleanClause.Occur.MUST_NOT};//在這個Field裏不能出現
MultiFieldQueryParser.parse("query", fields, flags, analyzer);
1、lucene的索引不能太大,要不然效率會很低。大於1G的時候就必須考慮分佈索引的問題
2、不建議用多線程來建索引,產生的互鎖問題很麻煩。經常發現索引被lock,無法重新建立的情況
3、中文分詞是個大問題,目前免費的分詞效果都很差。如果有能力還是自己實現一個分詞模塊,用最短路徑的切分方法,網上有教材和demo源碼,可以參考。
4、建增量索引的時候很耗cpu,在訪問量大的時候會導致cpu的idle爲0
5、默認的評分機制不太合理,需要根據自己的業務定製
整體來說lucene要用好不容易,必須在上述方面擴充他的功能,才能作爲一個商用的搜索引擎
\
爭取每日記錄一些
Index選項
Index.ANALYZED – 索引並分詞(適用於body, title, abstract等.).
Index.NOT_ANALYZED – 索引但不分詞,可以使用NORM方式.(可以人爲干預提權)
Index.ANALYZED_NO_NORMS – 索引並分詞但不使用NORM方式(不可認爲提權)
Index.NOT_ANALYZED_NO_NORMS – 索引但不分詞也不使用NORM方式(經常用到,存儲標誌值最好的方式.)
Index.NO – 不索引
Store選項
Store.YES – 存儲
Store.NO – 不存儲
TermVector選項
(除TermVector.NO外其他必須要求Index選項爲Index.ANALYZED或Index.NOT_ANALYZED)
TermVector.YES – 最基本的向量存儲(特殊性,數量,在哪個文檔)
TermVector.WITH_POSITIONS – TermVector.YES+位置
TermVector.WITH_OFFSETS – TermVector.YES+偏移
TermVector.WITH_POSITIONS_OFFSETS – TermVector.YES+位置+偏移
TermVector.NO – 不做向量存儲
各選項組合應用場景
Index |
Store |
TermVector |
事例 |
NOT_ANALYZ Technorati 標籤: LUCENE FIELD INDEX ANALYZED NOT_ANALYZED TermVector ED_NO_NORMS |
YES |
NO |
主鍵,電話,身份證號,URLs,日期和需要排序的字段 |
ANALYZED |
YES |
WITH_POSITIONS_OFFSETS |
文檔標題,摘要. |
ANALYZED |
NO |
WITH_POSITIONS_OFFSETS |
文檔主體 |
NO |
YES |
NO |
文檔類型,數據庫主鍵(如果不需要檢索該字段的話) |
NOT_ANALYZED |
NO |
NO |
隱藏字段 |
排序的注意事項
如果需要排序的字段是數字就用NumericField,如果是文本,一定要記得使用FIELD.Index.NOT_ANALYZED.
如果不需要提權則應該使用NOT_ANALYZED_NO_NORMS
多值字段的保存
在同一個Document下可以給同一個字段賦不同的值.例如
Document doc = new Document();
for (int i = 0; i < authors.length; i++) {
doc.add(new Field("author", authors[i],
Field.Store.YES,
Field.Index.ANALYZED));
}
LUCENE.NET QQ交流羣(81361051)
Lucene API
l 被索引的文檔用Document對象表示。
l IndexWriter通過函數addDocument將文檔添加到索引中,實現 創建索引的過程。
l Lucene的索引是應用反向索引。
l 當用戶有請求時,Query代表用戶的查詢語句。
l IndexSearcher通過函數search搜索Lucene Index。
l IndexSearcher計算term weight和score並且將結果返回給用戶。
l 返回給用戶的文檔集合用TopDocsCollector表示。
Lucene搜索的api的類主要有4個 IndexSearcher ,Query(包括子類),QueryParser,Hits
一:IndexSearcher是搜索的入口,他的search方法提供了搜索功能
Query有很多子類, 各種不同的子類代表了不同的查詢條件,下文詳述
QueryParser是一個非常通用的幫助類,他的作用是把用戶輸入的文本轉換爲內置的Query對象(大多數web搜索引擎都提供一個查詢輸入框來讓用戶輸入查詢條件)。QueryParser內置提供了很多語法來使使用可以輸入各種高級條件的Query。比如: "Hello AND world"會被解析爲一個AND關係的BooleanQuery,他包含兩個TermQuery(Hell和world)。這些語法雖然強大,但都針對英文設計,對我們需要中文搜索來說都不需要了解太多的Query類型,一般幾個簡單的就夠用了。QueryParser的使用如下
QueryParser.parse(String query, String field, Analyzer analyzer) throws ParseException
其中:query是用戶輸入的內容,field是搜索默認的field(其他field需要顯式指定),analyzer是用來將用戶輸入的內容也作分析處理(分詞),一般情況下這裏的anaylyzer是index的時候採用的同一analyzer。
另外我們也可以自己構造一個QueryParser: new QueryParser(String field, Analyzer a)(含義同上),這樣做的好處是可以自己定義調整一些參數.
搜索結果的處理:Hits對象
Hits對象是搜索結果的集合 主要有下面幾個方法
length() ,這個方法記錄有多少條結果返回(lazy loading)
doc(n) 返回第n個記錄
id(in) 返回第n個記錄的Document ID
score(n) 第n個記錄的相關度(積分)
由於搜索的結果一般比較大,從性能上考慮,Hits對象並不會真正把所有的結果全部取回,默認情況下是保留前100個記錄(對於一般的搜索引擎,100個記錄足夠了).
分頁的處理
100條記錄還是太多,我們多半會每頁顯示20條記錄,然後分爲若干頁顯示,對於分頁,一般有兩個辦法
在session中保留indexreader對象和hit對象,翻頁的時候提取內容
不使用session,每次都簡單處理爲重新查詢
lucene推薦先使用第二個辦法,即每次都重新查詢,這樣做的好處是簡單方便,不需要考慮session的問題,lucene的查詢效率也能保證每次查詢時間不長,除非真正有了性能問題,否則不用考慮第一個辦法。
緩存:RAMDirectory的用法
RAMDirectory對象很好用,通過它,我們可以把一個普通的index完全讀取到內存中,用法如下:
RAMDirectory ramDir = new RAMDirectory(dir);
這樣的ramdir效率自然比真正的文件系統快很多
Lucene的scoring算法
lucence查詢的紀錄默認按照相關度排序,這個相關度就是score,scoring的算法是比較複雜的,對於我們做應用的人似乎沒有什麼幫助,(先說一下Term: 我的理解是Term爲一個獨立的查詢詞,用戶輸入的的查詢通過各種分詞,大小寫處理(正規化),消除stopwords等)以後,會已Term爲基本單位),幾個關鍵參數稍微留意一下即可。
Term在文章中出現的頻率量,包含同一個Term的文章的頻率
field中的boosting參數
term的長度
term在文章中的數量
一般來說,這些參數我們都不可能去調整, 如果你想了解更多,IndexSearcher還提供了一個explain方法, 通過傳入一個Query和document ID,你可以得到一個Explaination對象,他是對內部算法信息的簡單封裝,toString()一下就可以看到詳細的說明
二:創建Query:各種query介紹
最普通的TermQuery
TermQuery最普通, 用Term t=new Term("contents","cap"); new TermQuery(t)就可以構造
TermQuery把查詢條件視爲一個key, 要求和查詢內容完全匹配,比如Field.Keyword類型就可以使用TermQuery
RangeQuery
RangeQuery表示一個範圍的搜索條件,RangeQuery query = new RangeQuery(begin, end, included);
最後一個boolean值表示是否包含邊界條件本身, 用字符表示爲"[begin TO end]" 或者"{begin TO end}"
PrefixQuery
顧名思義,就是表示以某某開頭的查詢, 字符表示爲"something*"
BooleanQuery
這個是一個組合的Query,你可以把各種Query添加進去並標明他們的邏輯關係,添加條件用
public void add(Query query, boolean required, boolean prohibited)
方法, 後兩個boolean變量是標示AND or NOT三種關係 字符表示爲" AND or NOT" 或 "+ -" ,一個BooleanQuery中可以添加多個Query, 如果超過setMaxClauseCount(int)的值(默認1024個)的話,會拋出TooManyClauses錯誤.
PhraseQuery
表示不嚴格語句的查詢,比如"red pig"要匹配"red fat pig","red fat big pig"等,PhraseQuery所以提供了一個setSlop()參數,在查詢中,lucene會嘗試調整單詞的距離和位置,這個參數表示可以接受調整次數限制,如果實際的內容可以在這麼多步內調整爲完全匹配,那麼就被視爲匹配.在默認情況下slop的值是0, 所以默認是不支持非嚴格匹配的, 通過設置slop參數(比如"red pig"匹配"red fat pig"就需要1個slop來把pig後移動1位),我們可以讓lucene來模糊查詢. 值得注意的是,PhraseQuery不保證前後單詞的次序,在上面的例子中,"pig red"需要2個slop,也就是如果slop如果大於等於2,那麼"pig red"也會被認爲是匹配的.
WildcardQuery
使用?和*來表示一個或多個字母比如wil*可以匹配 wild ,wila ,wilxaaaa...,值得注意的是,在wildcard中,只要是匹配上的紀錄,他們的相關度都是一樣的,比如wilxaaaa和wild的對於wil*的相關度就是一樣的.
FuzzyQuery
這個Query對中文沒有什麼用處,他能模糊匹配英文單詞(前面的都是詞組),比如fuzzy和wuzzy他們可以看成類似, 對於英文的各種時態變化和複數形式,這個FuzzyQuery還算有用,匹配結果的相關度是不一樣的.字符表示爲 "fuzzy~"
三:QueryParser使用
對於搜索引擎, 很多情況下用戶只需要一個輸入框就要輸入所有的查詢條件(比如google), 這時,QueryParser就派上用場了,他的作用就是把各種用戶輸入轉爲Query或者Query組, 他把上面提到的Query的字符表示(Query.toString)轉化爲實際的Query對象,比如"wuzzy~"就會轉換爲FuzzyQuery, 不過QueryParser用到了Analyzer,所以QueryParser parse過後的Query再toString未必和原來的一樣.Query額外的語法有:
分組:Groupping
比如"(a AND b) or C",就是括號分組,很容易理解
FieldSelectiong
QueryParser的查詢條件是對默認的Field進行的, 它在QueryParser解析的時候編碼指定, 如果用戶需要在查詢條件中選用另外的Field, 可以使用如下語法: fieldname:fielda, 如果是多個分組,可以用fieldname:(fielda fieldb fieldc)表示.
*號問題
QueryParse默認不允許*號出現在開始部分,這樣做的目的主要是爲了防止用戶誤輸入*來頭導致嚴重的性能問題(會把所有記錄讀出)
boosting
通過hello^2.0 可以對hello這個term進行boosting,(我想不到什麼用戶會這樣麼bt)
QueryParser是一個準備好的,立即可以工作的幫助類,不過他還是提供了很多參數供程序員調整,首先,我們需要自己構造一個新的QueryParser,然後對他的各種參數來定製化
Lucene分析
1.創建索引的步驟:
1)把要轉換爲索引的磁盤上的文件轉換爲Luncene文檔:
Document doc = File2DocumentUtils.file2Document(filePath);
轉換代碼
public static Document file2Document(String filePath) {
// TODO Auto-generated method stub
File file = new File(filePath);
Document doc = new Document();
doc.add(new Field("name", file.getName(), Store.YES, Index.ANALYZED));
doc.add(new Field("content", readFileContent(file), Store.YES,
Index.ANALYZED));
doc.add(new Field("size", String.valueOf(file.length()), Store.YES,
Index.ANALYZED));
doc.add(new Field("path", file.getAbsolutePath(), Store.YES,
Index.ANALYZED));
return doc;
}
讀取文件內容代碼
public static String readFileContent(File file) {
// TODO Auto-generated method stub
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(file)));
StringBuffer buffer = new StringBuffer();
for (String line; (line = br.readLine()) != null;) {
buffer.append(line).append("\n");
}
return buffer.toString();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null ;
}
2)創建IndexWriter
IndexWriter indexWriter = new IndexWriter(indexPath, analyzer, true,
new MaxFieldLength(10000));
IndexWriter是用來操作(增、刪、改)索引庫的
3)把document文檔加到IndexWriter
indexWriter.addDocument(doc);
4)關閉IndexWriter
Indexwriter。Close();
2.在索引庫的搜素步驟
1)把要搜索的索引解析爲query
String querystring="document";
String []fields={"name","content"};
QueryParser parser=new MultiFieldQueryParser(fields,analyzer);
//QueryParser是一個解析用戶輸入的工具,可以掃描用戶輸入的字符串,生成query對象。
Query query=parser.parse(querystring);
2)進行查詢
IndexSearcher indexSearcher=new IndexSearcher(indexPath);
Filter filter=null;
TopDocs topDocs=indexSearcher.search(query,(org.apache.lucene.search.Filter) filter,10000);
System.out.println("總共有【"+topDocs.totalHits+"】條匹配結果");
注:TopDocs 根據關鍵字搜索整個索引庫,然後對所有結果進行排序,然後取前10000條結果
3)輸出搜索結果
for(ScoreDoc scoreDoc:topDocs.scoreDocs){
int docSn=scoreDoc.doc;//文檔內部編號
Document doc=indexSearcher.doc(docSn);//根據編號取出相應的文檔
File2DocumentUtils.printDocumentInfo(doc);//打印出文檔信息
}
/**
獲取name屬性的值的兩種方法
1.Filed f=doc.getFiled("name");
f.stringValue();
2.doc.get("name")
*/
public static void printDocumentInfo(Document doc){
//Filed f=doc.getFiled("name");
// f.stringValue();
System.out.println("-------------------------------------------");
System.out.println("name ="+doc.get("name"));
System.out.println("content ="+doc.get("content"));
System.out.println("size ="+doc.get("size"));
System.out.println("path ="+doc.get("path"));
}