lucene常用搜尋排序摘錄

2021-08-29 16:55:16 字數 4090 閱讀 4880

一,查詢

乙個關鍵字,對乙個字段進行查詢

queryparser qp = new queryparser("content",analyzer);

query = qp.parse(keyword);

hits hits = searcher.search(query);

模糊查詢

term term = new term("content",keyword);

fuzzyquery fq = new fuzzyquery(term);

hits hits = searcher.search(fq);

乙個關鍵字,在兩個欄位中查詢

1.booleanclause.occur的三種型別: must : + and must_not : - not should : or

2.下面查詢的意思是:content中必須包含該關鍵字,而title有沒有都無所謂

3.下面的這個查詢中,occur的長度必須和fields的長度一致。每個限制條件對應乙個字段

booleanclause.occur flags = new booleanclause.occur;

query=multifieldqueryparser.parse(keyword,new string,flags,analyzer);

兩個(多個)關鍵字對兩個(多個)字段進行查詢,預設匹配規則

1.關鍵字的個數必須和字段的個數相等

2.由於沒有指定匹配規定,預設為"should" 因此,下面查詢的意思是:"title"中含有keyword1 或 "content"含有keyword2。在此例中,把keyword1和keyword2相同

query=multifieldqueryparser.parse(new string,new

string,analyzer);

兩個(多個)關鍵字對兩個(多個)字段進行查詢,手工指定匹配規則

1.必須 關鍵字的個數 == 欄位名的個數 == 匹配規則的個數

2.下面查詢的意思是:"title"必須不含有keyword1,並且"content"中必須含有keyword2

booleanclause.occur flags = new

booleanclause.occur;

query=multifieldqueryparser.parse(new string,new

string,flags,analyzer);

對數字範圍進行查詢

1.兩個條件必須是同乙個字段

2.前面乙個條件必須比後面乙個條件小,否則找不到資料

3.new rangequery中的第三個引數,表示是否包含"=" true: >= 或 <= false: > 或 <

4.找出 55>=id>=53 or 60>=id>=57:

term lowerterm1 = new term("id","53");

term upperterm1 = new term("id","55");

rangequery rq1 = new rangequery(lowerterm1,upperterm1,true);

term lowerterm2 = new term("id","57");

term upperterm2 = new term("id","60");

rangequery rq2 = new rangequery(lowerterm2,upperterm2,true);

booleanquery bq = new booleanquery();

bq.add(rq1,booleanclause.occur.should);

bq.add(rq2,booleanclause.occur.should);

hits hits = searcher.search(bq);

二:結果排序

排序的關鍵點有兩個:

1:首先你要排序的字段必須是被index的,並且是untokenized的。如:

doc.add(new field("click", dv.get("click").tostring(), field.store.no, field.index.un_tokenized));
2:在檢索時候:如:  

排序1.被排序的字段必須被索引過(indexecd),在索引時不能 用 field.index.tokenized (用un_tokenized可以正常實現.用no時查詢正常,但排序不能正常設定公升降序)

2.sortfield型別 score、doc、auto、string、int、float、custom 此型別主要是根據欄位的型別選擇

3.sortfield的第三個引數代表是否是降序true:降序  false:公升序

sort sort = new sort(new sortfield);

hits hits = searcher.search(querystring,sort);

按日期排序

sort sort = new sort(new sortfield);
過濾器

queryparser qp1 = new queryparser("content",analyzer);

query fquery = qp1.parse("我");

booleanquery bqf = new booleanquery();

bqf.add(fquery,booleanclause.occur.should);

queryfilter qf = new queryfilter(bqf);

hits hits = searcher.search(query);

預設情況下,indexsearcher類的search方法返回查詢結果時,是按文件的分值排序的,可以使用過載的search方法對結果排序

indexsearcher.search(query,sort);

new sort() 和 sort.relevance,以及null一樣,採用預設排序,要定義排序字段,方法是將字段傳入sort物件

sort sort = new sort(string field);

也可以對多個字段排序sort sort = new sort(string fields); 例:

sort sort = new sort(new sortfield);

hits hits=searcher.search(query,sort);

多欄位查詢multifieldqueryparser

只在某些term中查詢,不關心在哪個字段

query query = new multifieldqueryparser.parse(「word」,new string,analyzer); //在title和content中找word

多字段時預設是or關係,要改變它,使用以下方法:

query query = multifieldqueryparser.parse(「word」,new string,new int,analyzer);

其中:required_field 表示該條件必須有

prohibited_field 表示必須不含

搜尋多個索引檔案multisearcher

1) 建立多個索引:使用不同的索引目錄,例項化不同的indexwriter

2) 建立多索引搜尋器:

searcher searchers = new searcher[2];

searchers[0] = new indexsearcher(dir1); //搜尋索引目錄一

searchers[1]= new indexsearcher(dir2);//搜尋索引目錄二

searcher searcher = new multisearcher(serarchers);

3) 開始查詢:hits hits = searcher.search(query);

lucene常用搜尋例子

public class test 按詞條搜尋 public void termsearcher throws ioexception 短語搜尋 public void phrasesearcher throws ioexception 萬用字元搜尋 wildcardquery 萬用字元包括 匹配乙...

使用lucene對搜尋結果排序

lucene預設根據匹配度對搜尋結果降序排,如果對某個域進行排序?通常分兩步 step1 建索引時 newfield audittime row.get audittime tostring 關鍵點是你需要排序的字段建索引時應該採用 field.index.un tokenized,至於需不需要 f...

使用lucene對搜尋結果排序

lucene預設根據匹配度對搜尋結果降序排,如果對某個域進行排序?通常分兩步 step1 建索引時 newfield audittime row.get audittime tostring 關鍵點是你需要排序的字段建索引時應該採用 field.index.un tokenized,至於需不需要 f...