lucene的查詢與排序

2021-09-19 09:41:25 字數 4175 閱讀 8326

/**

* 查詢指定field中包含某個關鍵字

* @throws ioexception

*/@test

public void termquery() throws ioexception

/**

* 查詢指定欄位中包含與關鍵字相似的文件

* 查詢用於匹配與指定項相似的項

* 編輯距離演算法,兩個字串之間相似度的乙個度量方法

* 用來決定索引檔案中的項與指定目標項的相似程度.

* 取所有相同字首(字首長度可以設定)的詞項做編輯距離

** 編輯距離實際上是表明兩個不同的字串需要經過多少次編輯和變換才能變為對方。

* 通常的編輯行為包括了增加乙個檢索項,刪除乙個檢索項,修改乙個檢索項,

* 與普通的字串匹配函式不同,模糊搜尋裡的編輯距離是以索引項為單位的。**

* @throws ioexception

*/@test

public void fuzzyquery() throws ioexception

/**

* */

@test

public void multifieldquerycrossfields() throws parseexception, ioexception ;

string querystr = "good";

mapboosts = new hashmap();

//設定它們在搜尋結果排序過程中的權重,權重越高,排名越靠前

boosts.put("title", 1.0f);

boosts.put("desc", 0.7f);

multifieldqueryparser parser = new multifieldqueryparser(fields, new standardanalyzer(),boosts);

query query = parser.parse(querystr);

executequery(query);

}

使用多個關鍵字及多個field進行查詢
/**

* 使用多個關鍵字,及多個field進行查詢

*/@test

public void multifieldquerymultikeyword() throws parseexception, ioexception ;

string fields = ;

booleanclause.occur clauses = ;

query query = multifieldqueryparser.parse(queries,fields,clauses,new standardanalyzer());

executequery(query);

}

/**

* 萬用字元查詢

* 星號*:代表0個或多個字母

* 問號?:代表0個或1個字母

*/@test

public void wildcardquery() throws ioexception

/**

* 字首查詢

*/@test

public void prefixquery() throws ioexception

/**

* * slop的概念:slop是指兩個項的位置之間允許的最大間隔距離

* 例如:slop設定為1,則 quick brown fox 可以匹配 quick fox

*/@test

public void phrasequery() throws ioexception

/**

* * 跨度查詢,用於查詢多個詞的時候考慮幾個詞在文件中的匹配位置

* 與phrasequery和multifieldquery很相似,都是通過位置限制匹配

* 但是spanquery更加靈活

** spanquery包括以下幾種:

* spantermquery:詞距查詢的基礎,結果和termquery相似,只不過是增加了查詢結果中單詞的距離資訊。

* spanfirstquery:在指定距離可以找到第乙個單詞的查詢。

* spannearquery:查詢的幾個語句之間保持者一定的距離。

* spanorquery:同時查詢幾個詞句查詢。

* spannotquery:從乙個詞距查詢結果中,去除乙個詞距查詢。

*/@test

public void spanquery() throws ioexception

/*** 第一次出現在指定位置

* @throws ioexception

*/@test

public void spanfirstquery() throws ioexception

/*** spannearquery中將spantermquery物件作為spanquery物件使用的效果,與使用pharsequery的效果非常相似。

* 最大的區別是:在spannearquery的建構函式中的第三個引數為inorder標誌,設定這個標誌為true,項新增的順序和其文件**現的順序相同

*/@test

public void spannearquery() throws ioexception ;

int slop = 2;//science 與 art兩個詞間隔在2以內

boolean inorder = false;//不需要按陣列中的順序出現在文件中

spannearquery query = new spannearquery(queries,slop,inorder);

executequery(query);

}@test

public void spanorquery() throws ioexception ;

int slop = 2;//science 與 art兩個詞間隔在2以內

boolean inorder = false;//不需要按陣列中的順序出現在文件中

spannearquery spannearquery = new spannearquery(queries,slop,inorder);

spantermquery querycomputer = new spantermquery(new term("title","lucene"));

spanorquery query = new spanorquery(new spanquery);

executequery(query);

}

/**

* 組合查詢

* must與must組合表示並集

* must與must_not表示包含與不包含

* must_not與must_not組合沒有意義

* should與should組合表示或

* should與must表示must,其中should沒有任何價值

* should與must_not相當於must與must_not表示包含與不包含

*/@test

public void booleanquery() throws ioexception

/**

* 按指定字段排序

* @throws ioexception

* @throws parseexception

*/@test

public void sortbyfield() throws ioexception, parseexception

/**

* 按索引順序排序

* @throws ioexception

*/@test

public void sortbyindexorder() throws ioexception

/**

* 按文件的得分排序

* @throws ioexception

*/@test

public void sortbyrelevance() throws ioexception

本工程github

關於Lucene分組查詢後的排序

int total 0 if groupfield null 如果不分組,直接對查詢結果進行返回 topdocs topfielddocs null if equals util.strnull sortfield else if sortfield.startswith nprice topfie...

lucene索引的新增與查詢

public class indexfiles 使用方法 indexfiles 索引輸出目錄 索引的檔案列表 public static void main string args throws exception string indexpath args 0 indexwriter writer...

lucene 文件的查詢與刪除

1 查詢 term term new term bookname 女 bookname是field名稱 docs reader.termdocs term 2 刪除單個文件 indexreader reader indexreader.open path reader.deletedocument ...