solr使用了Lucene的内核,也继承了Lucene的打分规则,关于Lucene打分规则可以参考如下博客
http://blog.chenlb.com/2009/08/lucene-scoring-architecture.html
Solr内改变打分规则有几种形式:
1.配置solr的solrconfig.xml中edismax,来改变Boost打分规则
2.在solr的schema中增加一个字段,该字段专门用于排序
3.自写一个solr的评分规则。
一般简单的应用1和2就能满足。
举一个例子,电商类网站(比如淘宝)的商品搜索:
1.在商品名称上出现搜索关键字排序靠前,而内容的次之
2.对多皇冠的买家排序靠前等
3.对近期发布的商品排序靠前
4.对最近销售多商品靠前
综上获得一个综合排名
在solrconfig.xml的SearchHandler中如下配置
01 | < requestHandler name = "standard" class = "solr.StandardRequestHandler" default = "true" > |
02 | < lst name = "defaults" > |
03 | < str name = "echoParams" >explicit</ str > |
04 | < str name = "rows" >10</ str > |
05 | < str name = "hl" >on</ str > |
06 | < str name = "hl.fl" >name,content</ str > |
07 | < str name = "f.content.hl.fragsize" >200</ str > |
08 | < str name = "defType" >edismax</ str > |
09 | < str name = "bf" > |
10 | sum(recip(ms(NOW,pub_date),1,1,100),div(point,5632000),div(sale_count,1000000)) |
11 | </ str > |
12 | < str name = "pf" > |
13 | content |
14 | </ str > |
15 | < str name = "qf" > |
16 | name^1.9 |
17 | </ str > |
18 | </ lst > |
19 | </ requestHandler > |
bf用函数计算某个字段的权重,如上例子中pub_date发布日期的权重,point比如诚信指数,sale_count销售数量
bf内字段必须是索引的,bf的函数查看solr api文档 http://wiki.apache.org/solr/FunctionQuery
pf查询字段,这样在schema不用制定默认字段
qf对默认查询增加权重比值,比如标题是content的1.9倍,值越大权重越大
这样查询就会计算如下的一个综合评分值了
对于其他排序,比如说价格排序,在schema增加price字段,然后查询是增加sort=price desc就可以了
solr这种排序方式,已经在我的诚交网易货商品搜索里实现,效果还算不错。