lucene學習筆記4

2021-08-24 22:30:16 字數 3922 閱讀 7978

下面講一下索引的建立

其實從上面的例子就可以看出建立索引就用到document,indexwriter,field。

最簡單的步驟就是:

首先分別new 乙個document,indexwriter,field

然後用doument.add()方法加入field,

其次用indexwrtier.adddocument()方法加入document。

最後呼叫一下indexwriter.close()方法關閉輸入索引,這一步非常的重要只有呼叫這個方法索引才會被寫入索引的目錄裡,而這是被很多

初學的人所忽略的。

document沒有什麼好介紹的,把它的作用看成資料庫中的一行記錄就行。

field是乙個比較重要的也是比較複雜的:

看一下它的建構函式有5個:

field(string name, byte value, field.store store)

field(string name, reader reader)

field(string name, reader reader, field.termvector termvector)

field(string name, string value, field.store store, field.index index)

field(string name, string value, field.store store, field.index index, field.termvector termvector)

在field中有三個內部類:field.index,field.store,field.termvector,而建構函式也用到了它們。

注意:termvector是lucene 1.4新增的它提供一種向量機制來進行模糊查詢的這個不常用,預設是false不過是什麼對於一般查詢無影響。

它們的不同的組合,在全文檢索中有著不同的作用。看看下面的表吧:

field.index

field.store

看本文上面的介紹

說明 tokenized(分詞)

yes

文章的標題或內容(如果是內容的話不能太長)是可以被搜尋的

tokenized

no 文章的標題或內容(內容可以很長)也是可以被看過的

no yes

這是不能被搜尋的,它只是被搜尋內容的附屬物。如url等

un_tokenized

yes/no

不被分詞,它作為乙個整體被搜尋,搜一部分是搜不出來的

no no

沒有這種用法

而對於field(string name, reader reader)

field(string name, reader reader, field.termvector termvector)

web開發的時候一般是將大資料放在資料庫中,不會放在檔案系統中,更不會放在索引目錄裡,因為它太大了操作會加大伺服器的負擔。

下面介紹一下indexwriter:

它就是乙個寫入索引的寫入器,它的任務比較簡單:

1.用adddocument()將已經準備好寫入索引的document們加入

2.呼叫close()將索引寫入索引目錄

先看一下它的建構函式:

indexwriter(directory d, analyzer a, boolean create)

indexwriter(file path, analyzer a, boolean create)

indexwriter(string path, analyzer a, boolean create)

可見構造它需要乙個索引檔案目錄,乙個分析器(一般用標準的這個),最後乙個引數是標識是否清空索引目錄

它有一些設定引數的功能如:設定field的最大長度

看個例子:

public void indexmaxfield() throws ioexception

public void searchermaxfield() throws parseexception, ioexception

query = queryparser.parse("程式設計師之家");

indexsearcher= new indexsearcher("c:\\\\index");

hits = indexsearcher.search(query);

system.out.println("您搜的是:程式設計師之家");

system.out.println("找到了"+hits.length()+"個結果");

system.out.println("它們分別是:");

for (int i = 0; i < hits.length(); i++)

queryparser = new queryparser("name2",new standardanalyzer());

query = queryparser.parse("welcome");

indexsearcher= new indexsearcher("c:\\\\index");

hits = indexsearcher.search(query);

system.out.println("您搜的是:welcome");

system.out.println("找到了"+hits.length()+"個結果");

system.out.println("它們分別是:");

for (int i = 0; i < hits.length(); i++)

query = queryparser.parse("the");

indexsearcher= new indexsearcher("c:\\\\index");

hits = indexsearcher.search(query);

system.out.println("您搜的是:the");

system.out.println("找到了"+hits.length()+"個結果");

system.out.println("它們分別是:");

for (int i = 0; i < hits.length(); i++)

query = queryparser.parse("home");

indexsearcher= new indexsearcher("c:\\\\index");

hits = indexsearcher.search(query);

system.out.println("您搜的是:home");

system.out.println("找到了"+hits.length()+"個結果");

system.out.println("它們分別是:");

for (int i = 0; i < hits.length(); i++)

}它的執行結果為:

總結一下:

1.設定field的長度限制只是限制了搜尋。如果用了field.store.yes的話還是會全部被儲存進索引目錄裡的。

2.為什麼搜the沒有搜出來呢是因為lucene分析英文的時候不會搜尋the to of 等無用的詞(搜這些詞是無意義的)。

3.new standardanlayzer()對於英文的分詞是按空格和一些無用的詞,而中文呢是全部的單個的字。

4.設定field的最大長度是以0開頭和陣列一樣。

程式設計師之家----------3--------程式設計師之

0 1 2 3

welcome to the home of programmers------3------welcome to the home of programmers

0 1 2

大家還可以試一下別的,以便加深一下印象,到現在我們已經可以用lucene建立索引了

Lucene 學習筆記

調整索引索引效能 在乙個典型的索引應用中,程式效能的瓶頸存在於將索引檔案寫入磁碟的過程中。如果你曾經分析過索引過索引應用程式,應該會發現執行程式大部分時間都消耗在操作索引檔案的程式段上,因此我們必要使lucene索引新物件和修改索引檔案時變得更智慧型。當新的document物件新增到lucene的索...

lucene學習筆記

public class luceneadd catch exception e public class lucenesearch public static void main string args file indexdir new file d index try 在磁碟中檢索索引 dir...

Lucene學習筆記

1 lucene的field中的store和index屬性 如果field的store是yes的話,表明這個資料是要儲存在lucene檔案中的,可以用get方法得到 如果field的index是analyzed的話,表明這個資料是有被索引的,可以被搜尋的。這個兩個屬性的四種組合分別的含義是 a st...