Solr的學習使用之(七)Solr高階查詢facet、facet.pivot簡介
1 、什麼是Faceted Search
Facet['fæsɪt]很難翻譯,只能靠例子來理解了。Solr作者Yonik Seeley也給出更為直接的名字:導航(Guided Navigation)、引數化查詢(Paramatic Search)。
上面是比較直接的Faceted Search例子,品牌、產品特徵、賣家,均是 Facet 。而Apple、Lenovo等品牌,就是 Facet values 或者說 Constraints ,而Facet values所帶的統計值就是 Facet count/Constraint count 。
2 、Facet 使用
q = 超級本
facet = true
facet.field = 產品特性
facet.field = 品牌
facet.field = 賣家
http://…/select?q=超級本&facet=true&wt=json
&facet.field=品牌&facet.field=產品特性&facet.field=賣家
也可以提交查詢條件,設定fq(filter query)。
q = 電腦
facet = true
fq = 價格:[8000 TO *]
facet.mincount = 1 // fq將不符合的欄位過濾後,會顯示count為0
facet.field = 產品特性
facet.field = 品牌
facet.field = 賣家
http://…/select?q=超級本&facet=true&wt=json
&fq=價格:[8000 TO *]&facet.mincount=1
&facet.field=品牌&facet.field=產品特性&facet.field=賣家
"facet_counts": { "facet_fields": { "品牌": [ "Apple", 4, "Lenovo", 39 …] "產品特性": [ "顯示卡", 42, "酷睿", 38 …] …}}
如果使用者選擇了Apple這個分類,查詢條件中需要新增另外一個fq查詢條件,並移除Apple所在的facet.field。
http://…/select?q=超級本&facet=true&wt=json
&fq=價格:[8000 TO *]&fq=品牌:Apple&facet.mincount=1
&facet.field= 品牌&facet.field=產品特性&facet.field=賣家
3 、Facet 引數
facet.prefix – 限制constaints的字首
facet.mincount=0 – 限制constants count的最小返回值,預設為0
facet.sort=count – 排序的方式,根據count或者index
facet.offset=0 – 表示在當前排序情況下的偏移,可以做分頁
facet.limit=100 – constraints返回的數目
facet.missing=false – 是否返回沒有值的field
facet.date – Deprecated, use facet.range
facet.query
指定一個查詢字串作為Facet Constraint
facet.query = rank:[* TO 20]
facet.query = rank:[21 TO *]
"facet_counts": { "facet_fields": { "品牌": [ "Apple", 4, "Lenovo", 10 …] "產品特性": [ "顯示卡", 11, "酷睿", 20 …] …}}
facet.range
http://…/select?&facet=true
&facet.range=price
&facet.range.start=5000
&facet.range.end=8000
&facet.range.gap=1000
<result numFound="27" ... /> ... <lst name="facet_counts"> <lst name="facet_queries"> <int name="rank:[* TO 20]">2</int> <int name="rank:[21 TO *]">15</int> </lst> ...
WARNING: range範圍是左閉右開,[start, end)
facet.pivot
這個是Solr 4.0的新特性,pivot和facet一樣難理解,還是用例子來講吧。
Syntax: facet.pivot=field1,field2,field3...
e.g. facet.pivot=comment_user, grade
#docs |
#docs grade:好 |
#docs 等級:中 |
#docs 等級:差 |
|
comment_user:1 |
10 |
8 |
1 |
1 |
comment_user:2 |
20 |
18 |
2 |
0 |
comment_user:3 |
15 |
12 |
2 |
1 |
comment_user:4 |
18 |
15 |
2 |
1 |
"facet_counts":{ "facet_pivot":{ "comment_user, grade ":[{ "field":"comment_user", "value":"1", "count":10, "pivot":[{ "field":"grade", "value":"好", "count":8}, { "field":"grade", "value":"中", "count":1}, { "field":"grade", "value":"差", "count":1}] }, { "field":" comment_user ", "value":"2", "count":20, "pivot":[{ …
沒有pivot機制的話,要做到上面那點可能需要多次查詢:
http://...q= comment&fq= grade:好&facet=true&facet.field=comment_user
http://...q=comment&fq=grade:中&facet=true&facet.field=comment_user
http://...q=comment&fq=grade:差&facet=true&facet.field=comment_user
Facet.pivot - Computes a Matrix of Constraint Counts across multiple Facet Fields. by Yonik Seeley.
上面那個解釋很不錯,只能理解不能翻譯。