package com.git.edismax;
import java.io.ioexception;
import org.apache.solr.client.solrj.solrclient;
import org.apache.solr.client.solrj.solrquery;
import org.apache.solr.client.solrj.solrserverexception;
import org.apache.solr.client.solrj.solrquery.order;
import org.apache.solr.client.solrj.impl.httpsolrclient;
import org.apache.solr.client.solrj.response.queryresponse;
import org.apache.solr.common.solrdocument;
import org.apache.solr.common.solrdocumentlist;
import org.apache.solr.common.params.commonparams;
/**
* 测试edismax的代码实现自定义权重排序股则
* @author songqinghu
*
*/
public class scorebyedismax {
private final static string baseurl = "http://localhost:8983/solr/dismax";
public static void main(string[] args) throws exception{
scorebysum() ;
system.out.println("=====分割线==========");
scorebyproportion();
}
/**
*
* @描述:按照各个数值的比重来进行权重计算 count 10% point 45% hot 45% --假设总分为100分 那么 分别最大分为 10 45 45
* @return void
* @exception
* @createtime:2016年4月18日
* @author: songqinghu
* @throws ioexception
* @throws solrserverexception
*/
public static void scorebyproportion() throws solrserverexception, ioexception{
//先查询出来三个字段中每个字段的最大值--编写好计算权重的公式---实际项目中应该还要加入是否存在的判断
long countmax = getmaxforfield("count");
long pointmax = getmaxforfield("point");
long hotmax = getmaxforfield("hot");
string scoremethod = "sum(product(div(count,"+countmax+"),10),product(div(point,"+pointmax+"),45),product(div(hot,"+hotmax+"),45))^1000000000";
solrquery query = new solrquery();
query.set(commonparams.q, "国美*");
query.set(commonparams.fl,"id","name","count","point","hot","score");
query.set("deftype","edismax");
query.set("bf", scoremethod);
queryresponse response = getclient().query(query);
resultshow(response);
* @描述:xxxxxxx
* @return
* @return long
private static long getmaxforfield(string fieldname) throws solrserverexception, ioexception{
solrquery query = new solrquery();
query.set(commonparams.q, "*:*");
query.set(commonparams.fl, fieldname);
query.setsort(fieldname, order.desc);
query.setrows(1);
queryresponse countresponse = getclient().query(query);
solrdocument maxcount = countresponse.getresults().get(0);
long result = (long) maxcount.getfieldvalue(fieldname);
system.out.println(fieldname + ":" + result);
return result;
* @描述:按照 count point 和 hot 的 和的数量来进行权重计算
public static void scorebysum() throws solrserverexception, ioexception{
solrquery query = new solrquery();
//开启edismax方式来进行自定义权重算法
query.set("deftype", "edismax");
query.set("bf","sum(count,point,hot)^1000000000");
* @描述:查询结果显示类
* @param response
private static void resultshow(queryresponse response){
int time = response.getqtime();
system.out.println("响应时间:"+ time+"ms");
solrdocumentlist results = response.getresults();
long numfound = results.getnumfound();
system.out.println("总数量:"+numfound);
for (solrdocument doc : results) {
system.out.println("id:"+ doc.getfieldvalue("id").tostring());
system.out.println("name:"+ doc.getfieldvalue("name").tostring());
system.out.println("count:"+ doc.getfieldvalue("count").tostring());
system.out.println("point:"+ doc.getfieldvalue("point").tostring());
system.out.println("hot:"+ doc.getfieldvalue("hot").tostring());
system.out.println("score:"+ doc.getfieldvalue("score").tostring());
system.out.println();
}
* @描述:获取单机版本的连接信息
* @return solrclient
public static solrclient getclient(){
solrclient solrclient = new httpsolrclient(baseurl);
return solrclient;
}

执行结果:
响应时间:1ms
总数量:6
id:6
name:国美手机
count:2314
point:1123
hot:717
score:4154.0
id:1
name:国美集团
count:5
point:4
hot:3
score:12.0
id:5
name:国美美信
count:1
point:5
hot:4
score:10.0
id:2
name:国美电器
count:4
point:3
hot:2
score:9.0
id:4
name:国美金融
count:2
point:1
hot:5
score:8.0
id:3
name:国美在线
count:3
point:2
hot:1
score:6.0
=====分割线==========
响应时间:2ms
score:100.0
score:0.45572376
score:0.3701771
score:0.36252183
score:0.26302284
score:0.15586855
权重计算分析:
权重影响参数设置表:
主查询权重*自定义权重
查询主条件比例
权重公式比例
solr计算
自己计算
^1
^100000000
0.45572376
0.45572373506010969850068170504362
^1000000000000
^0.0000000001
实践得出 这里score的计算是两部分组成 一个是主查询的权重匹配值还有一个是自己设置的权重计算
可以使其中一个无限的趋近于一个最大值或者最小值但是不能使之为0
看到不去刻意的改变solr自身的两部分打分机制,其打分结果和理想的误差为0.025*0.000001 这个误差,一般情况应该都是能满足的,不影响结果的