---
title: "solr搜索打分规制排序"
author: "lavafree"
date: "2012-09-26T15:04:44Z"
tags: ["Solr"]
categories: ["开源软件"]
slug: "solr"
draft: false
lastmod: "2012-12-21T17:10:56Z"
id: 1685766
url: "https://lavafree.iteye.com/blog/1685766"
---

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中如下配置

 

```
<requestHandler name="standard" class="solr.StandardRequestHandler" default="true" >
	<lst name="defaults">
		<str name="echoParams">explicit</str>
		<str name="rows">10</str>
		<str name="hl">on</str>
		<str name="hl.fl">name,content</str>
		<str name="f.content.hl.fragsize">200</str>
		<str name="defType">edismax</str>
		<str name="bf">
			sum(recip(ms(NOW,pub_date),1,1,100),div(point,5632000),div(sale_count,1000000))
		</str>	   
		<str name="pf">
			content
		</str>	   
		<str name="qf">
			name^1.9 
		</str>
	</lst>
</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这种排序方式，已经在我的[诚交网易货商品搜索](http://www.chj360.com "诚交网易货商品搜索")里实现，效果还算不错。