solr入门之edismax权重排序使用之Java代码实现自定义权重
2016-04-18 18:48
323 查看
实现代码:
执行结果:
响应时间: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
=====分割线==========
count:2314
point:1123
hot:717
响应时间:2ms
总数量:6
id:6
name:国美手机
count:2314
point:1123
hot:717
score:100.0
id:5
name:国美美信
count:1
point:5
hot:4
score:0.45572376
id:1
name:国美集团
count:5
point:4
hot:3
score:0.3701771
id:4
name:国美金融
count:2
point:1
hot:5
score:0.36252183
id:2
name:国美电器
count:4
point:3
hot:2
score:0.26302284
id:3
name:国美在线
count:3
point:2
hot:1
score:0.15586855
权重计算分析:
权重影响参数设置表:
主查询权重*自定义权重
实践得出 这里score的计算是两部分组成 一个是主查询的权重匹配值还有一个是自己设置的权重计算
可以使其中一个无限的趋近于一个最大值或者最小值但是不能使之为0
看到不去刻意的改变solr自身的两部分打分机制,其打分结果和理想的误差为0.025*0.000001 这个误差,一般情况应该都是能满足的,不影响结果的
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 * @exception * @createTime:2016年4月18日 * @author: songqinghu * @throws IOException * @throws SolrServerException */ 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 的 和的数量来进行权重计算 * @return void * @exception * @createTime:2016年4月18日 * @author: songqinghu * @throws IOException * @throws SolrServerException */ public static void scoreBySum() throws SolrServerException, IOException{ SolrQuery query = new SolrQuery(); query.set(CommonParams.Q, "国美*"); query.set(CommonParams.FL,"id","name","count","point","hot","score"); //开启edismax方式来进行自定义权重算法 query.set("defType", "edismax"); query.set("bf","sum(count,point,hot)^1000000000"); QueryResponse response = getClient().query(query); resultShow(response); } /** * * @描述:查询结果显示类 * @param response * @return void * @exception * @createTime:2016年4月18日 * @author: songqinghu */ 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 * @return SolrClient * @exception * @createTime:2016年4月18日 * @author: songqinghu */ 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
=====分割线==========
count:2314
point:1123
hot:717
响应时间:2ms
总数量:6
id:6
name:国美手机
count:2314
point:1123
hot:717
score:100.0
id:5
name:国美美信
count:1
point:5
hot:4
score:0.45572376
id:1
name:国美集团
count:5
point:4
hot:3
score:0.3701771
id:4
name:国美金融
count:2
point:1
hot:5
score:0.36252183
id:2
name:国美电器
count:4
point:3
hot:2
score:0.26302284
id:3
name:国美在线
count:3
point:2
hot:1
score:0.15586855
权重计算分析:
权重影响参数设置表:
主查询权重*自定义权重
查询主条件比例 | 权重公式比例 | solr计算 | 自己计算 |
^1 | ^100000000 | 0.45572376 | 0.45572373506010969850068170504362 |
^1 | ^1000000000000 | 0.45572376 | |
^0.0000000001 | ^1000000000000 | 0.45572376 |
可以使其中一个无限的趋近于一个最大值或者最小值但是不能使之为0
看到不去刻意的改变solr自身的两部分打分机制,其打分结果和理想的误差为0.025*0.000001 这个误差,一般情况应该都是能满足的,不影响结果的
相关文章推荐
- Spring MVC Controller中解析GET方式的中文参数会乱码的问题
- springmvc 后端返回到前端乱码
- 使用maven编译dubbo,导入eclipse
- java中的I/O
- Spring配置文件中component-scan 扫描指定的包中的类上常用的注解
- 深入理解Java 8 Lambda(语言篇——lambda,方法引用,目标类型和默认方法)
- 关于Java变量的可见性问题
- Java并发编程:线程间协作的两种方式:wait、notify、notifyAll和Condition
- java调用com组件操作word使用总结(jacob)
- struts标签以及url下发下一个url带参数
- Eclipse新建Activity并启用(二):启用新建Activity
- Maven+spring+springMVC+mybatis+Junit+Log4j配置个人总结
- 回归基础系列-重载与覆盖(重写)[JAVA]
- 【转】eclipse 错误信息 "File Search" has encounter a problem 解决
- Spring+SpringMVC+MyBaties学习笔记(二)集成Spring SpringMVC MyBatis
- 超详细Eclipse中导入XUtils3
- Java中字符串反转
- Java实现文件复制
- [IDE工具配置]myeclipse 2014 专业版 安装 svn插件
- Java实现文件复制