Lucene相關度排序學習筆記

2021-10-20 19:41:33 字數 1732 閱讀 2156

lucene對查詢關鍵字和索引文件的相關度進行打分,得分高的就排在前邊。

1.1 如何打分

lucene是在使用者進行檢索時實時根據搜尋的關鍵字計算出來的,分兩步:

1、計算出詞(term)的權重。

2、根據詞的權重值,計算文件相關度得分。

明確索引的最小單位是乙個term(索引詞典的乙個詞),搜尋也是從term中搜素,再根據term找到文件,term對文件的重要性成為權重,影響term權重有兩個因素:

1.2 怎樣影響相關度排序

boost是乙個加權值(預設加權值為1.0f),它可以影響權重的計算。

設定boost是給域(field)或者document設定的。

1.3 人為影響相關度排序

查詢的時候通過設定域的權重,從而影響查詢結果

/**

* 測試相關度排序

* @throws exception

*/@test

public

void

testindexsearch2()

throws exception

;//設定影響排序的權重, 這裡設定域的權重

mapboots =

newhashmap

<

>()

; boots.

put(

"categoryname"

,10000000000f);

//從多個域查詢物件

multifieldqueryparser multifieldqueryparser =

newmultifieldqueryparser

(fields, analyzer, boots)

; query query = multifieldqueryparser.

parse

("手機");

//4. 建立directory目錄物件, 指定索引庫的位置

directory dir = fsdirectory.

open

(paths.

get(

"e:\\dir"))

;//5. 建立輸入流物件

indexreader indexreader = directoryreader.

open

(dir)

;//6. 建立搜尋物件

indexsearcher indexsearcher =

newindexsearcher

(indexreader)

;//7. 搜尋, 並返回結果

//第二個引數: 是返回多少條資料用於展示, 分頁使用

topdocs topdocs = indexsearcher.

search

(query,10)

;//獲取查詢到的結果集的總數, 列印

system.out.

println

("*****==count*****=="

+ topdocs.totalhits)

;//8. 獲取結果集

scoredoc[

] scoredocs = topdocs.scoredocs;

//9. 遍歷結果集

if(scoredocs != null)

}//10. 關閉流

}

Lucene 中相關度排序

lucene 中的相似度排序主要是在org.apache.lucene.search 包下的 similarity類中定義的,其排序演算法如下 score q,d tf t in d idf t 2 getboost t in q getboost t.field in d lengthnorm t...

Lucene相關度排序的調整

看sort的預設建構函式,相關度就是sortfield.field score和sortfield.field doc的組合。sorts by computed relevance.this is the same sort criteria as calling without a sort cr...

lucene 3(相關度排序)

相關度排序這個東西顧名思義,在上文中我們講到了乙個打分的問題,就是說的在查詢關鍵字匹配的時候,相識度越高的就會打分越高,就會越靠前。打分的兩個步驟 1.根據詞計算詞的權重。2.根據詞的權重打分。詞的權重 意思就是詞的重要性,而且詞就是我們上文講到的term,而影響詞的權重的有兩個東西 tf 詞在該文...