Java程序员必看:Solr vip竞价排名

功能介绍

本文将使用solr完成vip等级排名,这里并不是简单的按照vip等级排序。而是在使用solr条件查询(不使用排序)的时候将符合条件并且具有vip等级的数据显示在前面,这个时候咱们就要使用solr底层提供的自定义评分机制来完成。

环境介绍

开发环境:IDEA + SpringBoot solr环境:solr4.10 + tomcat7

备注:solr环境+tomcat环境+IK中文分词配置自行安装

准备工作

solr环境安装+tomcat环境+IK分词配置(自行完成) 检查solr环境:

检查IK中文分词器,有如下中文分词效果即可

在solr的collection目录下的schema.xml中添加如下业务域. 一下业务域中包含:商品标题、商品介绍、商品价格、商品创建时间、商品点击次数、商品所属商家vip等级、商品评价。

扫描二维码关注公众号,回复: 10598936 查看本文章
<!-- general -->
<!-- 商品标题 -->
<field name="t_title" type="text_ik" indexed="true" stored="true" />
<!-- 商品介绍 -->
<field name="t_intr" type="text_ik" indexed="true" stored="true" />
<!-- 商品价格 -->
<field name="t_price" type="float" indexed="true" stored="true" />
<!-- 商品创建时间 -->
<field name="t_createTime" type="tdate" indexed="true" stored="true" />
<!-- 商品点击次数-->
<field name="t_point" type="long" indexed="true" stored="true" />
<!-- 商品所属商家vip等级[1-5级] -->
<field name="t_vip" type="long" indexed="true" stored="true" />
<!-- 商品评价-->
<field name="t_assess" type="long" indexed="true" stored="true" />

<!-- 设置关键字搜索域-->
<field name="t_searchText" type="text_ik" indexed="true" stored="false" multiValued="true" />

<!-- 设置关键字域复制标题和介绍 -->
<copyField source="t_title" dest="t_searchText" />
<copyField source="t_intr" dest="t_searchText" />
<!-- 将关键字搜索域设置默认搜索域-->
<defaultSearchField>t_searchText</defaultSearchField>

<solrQueryParser defaultOperator="AND"/>

工程搭建

使用IDEA搭建maven工程

在pom.xml中加入以下jar依赖

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
</dependency>
</dependencies>

编写springBoot启动类SpringbootSolr5Application.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootSolr5Application {

public static void main(String[] args) { SpringApplication.run(SpringbootSolr5Application.class, args);
}
}

在resources目录下创建application.properties加入一下内容:

spring.data.solr.host=//localhost:8080/solr/

编写CustomSortTest.java初始化查询数据:

@RunWith(SpringRunner.class) @SpringBootTest
public class CustomSortTest {
@Autowired
private SolrClient client;
/**
* 初始化solr索引数据
* */ @Test
public void initSolrData() throws Exception{
List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>(); for(int i=0;i<100;i++){
SolrInputDocument document = new SolrInputDocument();
// 文 档 id document.setField("id",i);
//商品标题
document.setField("t_title","new"+i+"- 三星 W"+i*100+" 黑色 电信3G手机 双卡双待双通");
//商品介绍
document.setField("t_intr","下单送12000毫安移动电源!双3.5英寸魔焕炫屏,以非凡视野纵观天    下时局,尊崇翻盖设计,张弛中,尽显从容气度!");
//价格
document.setField("t_price","8000");
//创建日期
document.setField("t_createTime",new Date());
//点击率document.setField("t_point",i%9+9);
//评价分数
document.setField("t_assess",i%11+5);
//vip 等 级 [1-5] document.setField("t_vip",i%5); docs.add(document);
}
client.add(docs); client.commit();
}
}

编写一下方法看一下默认条件查询:三星的效果:

@Test
public void defualtQuerySort() throws Exception{ SolrQuery solrQuery = new SolrQuery();
//关键词
solrQuery.set("q","t_searchText:*三星*");
//分页,0开始,每页10条,setStart设置的就是显示第几页
solrQuery.setStart(0); solrQuery.setRows(10);
//执行查询
QueryResponse response = client.query(solrQuery);
//文档结果集
SolrDocumentList results = response.getResults(); System.out.println("查询到的总条数:"+ results.getNumFound());
//遍历查询的结果
for (SolrDocument solrDocument : results) {
String id = solrDocument.get("id").toString();
String title = solrDocument.get("t_title").toString(); String assess = solrDocument.get("t_assess").toString();
double point = Double.valueOf(solrDocument.get("t_point").toString()); double vip = Double.valueOf(solrDocument.get("t_vip").toString());
System.out.println("id:"+id+" 标题:"+title+" 评价:"+assess+ "点击率:"+point+" vip等
级 :"+vip+" " );
}
}

结果如下:

查询到的总条数:100

id:0 标题:new0- 三星 W0 黑色 电信3G手机 双卡双待双通 评价:5点击率:9.0 vip等级:0.0 
id:1 标题:new1- 三星 W100 黑色 电信3G手机 双卡双待双通 评价:6点击率:10.0 vip等级:1.0 
id:2 标题:new2- 三星 W200 黑色 电信3G手机 双卡双待双通 评价:7点击率:11.0 vip等级:2.0 
id:3 标题:new3- 三星 W300 黑色 电信3G手机 双卡双待双通 评价:8点击率:12.0 vip等级:3.0 
id:4 标题:new4- 三星 W400 黑色 电信3G手机 双卡双待双通 评价:9点击率:13.0 vip等级:4.0
id:5 标题:new5- 三星 W500 黑色 电信3G手机 双卡双待双通 评价:10点击率:14.0 vip等级:0.0 
id:6 标题:new6- 三星 W600 黑色 电信3G手机 双卡双待双通 评价:11点击率:15.0 vip等级:1.0
id:7 标题:new7- 三星 W700 黑色 电信3G手机 双卡双待双通 评价:12点击率:16.0 vip等级:2.0 
id:8 标题:new8- 三星 W800 黑色 电信3G手机 双卡双待双通 评价:13点击率:17.0 vip等级:3.0 
id:9 标题:new9- 三星 W900 黑色 电信3G手机 双卡双待双通 评价:14点击率:9.0 vip等级:4.0
id:10 标题:new10- 三星 W1000 黑色 电信3G手机 双卡双待双通 评价:15点击率:10.0 vip等级:0.0

从结果可以看出默认排序是根据id进行排序。

完成自定义评分,在默认排序以三星为条件作同时以vip等级排序

@Test
public void testVipPageQuery()throws Exception{ SolrQuery solrQuery = new SolrQuery();
//关键词
solrQuery.set("q","t_searchText:*三星*");
//分页,0开始,每页20条,setStart设置的就是显示第几页
solrQuery.setStart(0); solrQuery.setRows(20);
//设置权重方式为edismax
solrQuery.set("defType","edismax");
//scoreMethod为自定义评分规则,这里就是以t_vip+0的和来得到评分,然后以该评分进行排序String scoreMethod = "sum(t_vip,0)";
solrQuery.set("bf", scoreMethod);
//执行查询
QueryResponse response = client.query(solrQuery);
//文档结果集
SolrDocumentList results = response.getResults(); System.out.println("查询到的总条数:"+ results.getNumFound());
//遍历查询的结果
for (SolrDocument solrDocument : results) {
String id = solrDocument.get("id").toString();
String title = solrDocument.get("t_title").toString(); String assess = solrDocument.get("t_assess").toString(); String point = solrDocument.get("t_point").toString(); String vip = solrDocument.get("t_vip").toString();
//double point = Double.valueOf(solrDocument.get("t_point").toString());
//double vip = Double.valueOf(solrDocument.get("t_vip").toString());
System.out.println("id:"+id+" 标题:"+title+" 评价:"+assess+ "点击率:"+point+" vip等级 :"+vip+" " );
}
}

结果如下:

查询到的总条数:100
id:4 标题:new4- 三星 W400 黑色 电信3G手机 双卡双待双通 三星 评价:9点击率:13 vip等级:4
id:9 标题:new9- 三星 W900 黑色 电信3G手机 双卡双待双通 评价:14点击率:9 vip等级:4
id:14 标题:new14- 三星 W1400 黑色 电信3G手机 双卡双待双通 三星 评价:8点击率:14 vip等级:4 
id:19 标题:new19- 三星 W1900 黑色 电信3G手机 双卡双待双通 评价:13点击率:10 vip等级:4 id:24 标题:new24- 三星 W2400 黑色 电信3G手机 双卡双待双通 三星 评价:7点击率:15 vip等级:4
id:29 标题:new29- 三星 W2900 黑色 电信3G手机 双卡双待双通 评价:12点击率:11 vip等级:4 
id:34 标题:new34- 三星 W3400 黑色 电信3G手机 双卡双待双通 三星 评价:6点击率:16 vip等级:4 
id:39 标题:new39- 三星 W3900 黑色 电信3G手机 双卡双待双通 评价:11点击率:12 vip等级:4
id:44 标题:new44- 三星 W4400 黑色 电信3G手机 双卡双待双通 三星 评价:5点击率:17 vip等级:4 
id:49 标题:new49- 三星 W4900 黑色 电信3G手机 双卡双待双通 评价:10点击率:13 vip等级:4 id:54 标题:new54- 三星 W5400 黑色 电信3G手机 双卡双待双通 三星 评价:15点击率:9 vip等级:4 
id:59 标题:new59- 三星 W5900 黑色 电信3G手机 双卡双待双通 评价:9点击率:14 vip等级:4
id:64 标题:new64- 三星 W6400 黑色 电信3G手机 双卡双待双通 三星 评价:14点击率:10 vip等级:4 
id:69 标题:new69- 三星 W6900 黑色 电信3G手机 双卡双待双通 评价:8点击率:15 vip等级:4
id:74 标题:new74- 三星 W7400 黑色 电信3G手机 双卡双待双通 三星 评价:13点击率:11 vip等级:4 
id:79 标题:new79- 三星 W7900 黑色 电信3G手机 双卡双待双通 评价:7点击率:16 vip等级:4
id:84 标题:new84- 三星 W8400 黑色 电信3G手机 双卡双待双通 三星 评价:12点击率:12 vip等级:4
id:89 标题:new89- 三星 W8900 黑色 电信3G手机 双卡双待双通 评价:6点击率:17 vip等级:4
id:94 标题:new94- 三星 W9400 黑色 电信3G手机 双卡双待双通 三星 评价:11点击率:13 vip等级:4 
id:99 标题:new99- 三星 W9900 黑色 电信3G手机 双卡双待双通 评价:5点击率:9 vip等级:4

可以看出我们以自定义评分的方式,该结果就以vip等级最高的进行排序

备注:关于solr更多的

自定义排序都是利用solr的Function Query函数进行的。可以自行查看solr的api进行学习

发布了682 篇原创文章 · 获赞 1391 · 访问量 171万+

猜你喜欢

转载自blog.csdn.net/itcast_cn/article/details/104770048
今日推荐