如何讓 Solr 回傳 hit count

2015.10.20-21

Solr 回傳結果裡的 numFound 是符合搜尋條件的 document 數量,要如何取得 keyword 出現的次數?

Faceting

有人說用 faceting search 可以得到 hit count,但其實 faceting search 回傳的應該是符合的條件的 document 數量,而不是 keyword 出現的次數。

termfreq

據說在 Solr 4.0 之後可以使用 termfreq 取得 hit count,在 field list 參數增加一個虛擬欄位如下:

q=*:*&fl=*,termfreq(field,term)

也可以為這個虛擬欄位命名:

q=*:*&fl=*,tf:termfreq(field,term)

但似乎 solr 似乎是將中文的一個字當做一個 term,如果只計算一個字的次數是沒問題:

text,termfreq(text,嚴)

但是如果是兩個字,就回傳零:

text,termfreq(text,'華嚴')

似乎 solr 是將「華嚴」當做是一個 phrase 而不是一個 term。

另外還有一個 total term frequency 回傳 keyword 在整個 index 出現的次數。

totaltermfreq

totaltermfreq(field,term) returns the number of times the term appears in the field in the entire index. ttf is an alias oftotaltermfreq.

Example Syntax: ttf(text,'memory')

Score (未實作確認)

Solr 使用 TF-IDF 機制計算每份文件與搜尋條件的相關性 (relvance),稱之為 score (其值在 0 與 1.0 之間),並根據這個 score 來為回傳的結果排序 (rank)。

可以自己撰寫程式取代 score 的計算公式,以 hit count 做為 score 回傳,這樣可以得到每份文件的 hit count.

參考:

Highlight (未實作確認)

turning highlighting on and counting the snippets returned

但是要取得 total hit count 的話,要將 rows 設為 numFound,一次回傳全部符合條件的文件,這樣恐怕速度會很慢。

TermVectorComponent

http://wiki.apache.org/solr/TermVectorComponent

Solr + Hadoop (未實作確認)

https://dzone.com/articles/solr-hadoop-big-data-love