Lucene 2 0學習文件( )

2021-08-29 06:02:13 字數 2220 閱讀 4169

indexwriter(file path, analyzer a, boolean create)

indexwriter(string path, analyzer a, boolean create)

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

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

看個例子:

[code]

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++)

}[/code]

它的執行結果為:

總結一下:

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

大家還可以試一下別的,以便加深一下印象

(未完)

lucene2 0學習文件二

下面講一下索引的建立 其實從上面的例子就可以看出建立索引就用到document,indexwriter,field。最簡單的步驟就是 首先分別new 乙個document,indexwriter,field,然後用doument.add 方法加入field.其次用indexwrtier.adddoc...

lucene2 0學習文件三

先看一下它的建構函式 indexwriter directory d,analyzer a,boolean create indexwriter file path,analyzer a,boolean create indexwriter string path,analyzer a,boolea...

lucene2 0學習文件四

public void searchermaxfield throws parseexception,ioexception query queryparser.parse 程式設計師之家 indexsearcher new indexsearcher c index hits indexsearc...