solr入门之edismax权重排序使用之Java代码实现自定义权重

实现代码:

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 = (lo
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值