Java programmers must-see: Solr vip PPC

Features

This article will use solr complete vip Ranking, here is not simply ordered by vip level. But when using solr query conditions (without using the sort) will meet the conditions and have a vip level data displayed on the front, this time we will use a custom scoring mechanism underlying solr provided to complete.

Environment Introduction

Development Environment : IDEA + SpringBoot  S OLR environment : solr4.10 + tomcat7

Note: solr ambient environment + IK + tomcat Chinese word self-installation configuration

Ready to work

Installation Environment solr + IK + tomcat environment configuration word (self completion) Check solr environment:

Check the IK Chinese word, a Chinese word has the following effects can be

Add the following business domains in the collection schema.xml solr directory service domain that contains in it: the commodity title, creation time, the number of clicks goods, commodities owned businesses vip level, product reviews product description, product price, product.

<!-- 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"/>

Engineering structures

IDEA project built using maven

Add the following jar dependency in pom.xml

<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 start writing class 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);
}
}

Application.properties created in the resources directory is added at the contents:

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

Write CustomSortTest.java initialization query data:

@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();
}
}

Writing about the ways of looking at the default condition query: Samsung effects:

@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+" " );
}
}

The results are as follows:

Queried Total: 100

id: 0 Title: new0- W0 black Samsung 3G mobile telecommunications dual card dual standby dual-pass rating: 5 Hits: 9.0 vip rating: 0.0 
the above mentioned id: 1 title: new1- black Samsung W100 3G mobile telecommunications dual card dual standby dual-pass evaluation : 6 hits: 10.0 vip rating: 1.0 
the above mentioned id: 2 title: new2- black Samsung W200 3G mobile telecommunications dual card dual standby dual-pass rating: 7 hits: 11.0 vip rating: 2.0 
the above mentioned id: 3 title: new3- Samsung W300 black 3G mobile telecommunications dual card dual standby dual-pass rating: 8 hits: 12.0 vip rating: 3.0 
the above mentioned id: 4 title: new4- black Samsung W400 3G mobile telecommunications dual card dual standby dual-pass rating: 9 hits: 13.0 vip rating: 4.0
id: 5 title: new5- black Samsung W500 3G mobile telecommunications dual card dual standby dual-pass rating: 10 hits: 14.0 vip rating: 0.0 
the above mentioned id: 6 title: new6- black Samsung W600 3G mobile telecommunications dual card dual standby dual-pass evaluation : 11 hits: 15.0 vip rating: 1.0
the above mentioned id: 7 title: new7- black Samsung W700 3G mobile telecommunications dual card dual standby dual-pass rating: 12 hits: 16.0 vip rating: 2.0 
the above mentioned id: 8 title: new8- Samsung W800 black 3G mobile telecommunications dual card dual standby dual-pass rating: 13 hits: 17.0 vip rating: 3.0 
the above mentioned id: 9 title: new9- Samsung W90 0 Black 3G mobile telecommunications dual card dual standby dual-pass rating: 14 Hits: 9.0 vip rating: 4.0
id: 10 Title: new10- black Samsung W1000 3G mobile telecommunications dual card dual standby dual-pass rating: 15 Hits: 10.0 vip rating: 0.0

From the results of the default sort is sorted id.

Finished customizing your score, while vip ranking in the default sort order as a condition for Samsung

@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+" " );
}
}

The results are as follows:

The total number of queries to: 100
the above mentioned id: 4 Title: new4- black Samsung W400 3G mobile telecommunications dual card dual standby dual pass Samsung Rating: 9 Hits: 13 vip rating: 4
the above mentioned id: 9 Title: new9- telecommunications Samsung W900 Black 3G mobile phone dual card dual standby dual-pass rating: 14 hits: 9 vip rating: 4
the above mentioned id: 14 title: new14- black Samsung W1400 3G mobile telecommunications dual card dual standby dual pass Samsung rating: 8 hits: 14 vip rating: 4 
id: 19 title: new19- black Samsung W1900 3G mobile telecommunications dual card dual standby dual-pass rating: 13 hits: 10 vip rating: 4 id: 24 title: new24- black Samsung W2400 3G mobile telecommunications dual card dual standby dual pass Samsung rating: 7 hits: 15 vip rating: 4
the above mentioned id: 29 title: new29- black Samsung W2900 3G mobile telecommunications dual card dual standby dual-pass rating: 12 hits: 11 vip rating: 4 
the above mentioned id: 34 title: new34- Samsung W3400 black 3G mobile telecommunications dual card dual standby dual pass Samsung rating: 6 hits: 16 vip rating: 4 
the above mentioned id: 39 title: new39- black Samsung W3900 3G mobile telecommunications dual card dual standby dual-pass rating: 11 hits: 12 vip rating : 4
the above mentioned id: 44 title: new44- black Samsung W4400 3G mobile telecommunications dual card dual standby dual pass Samsung Rating: 5 hits 17 vip rating: 4 
id: 49 Title: new49- black Samsung W4900 3G mobile telecommunications dual card dual standby dual-pass rating: 10 Hits: 13 vip rating: 4 id: 54 Title: new54- black Samsung W5400 3G mobile telecommunications dual card dual standby dual pass Samsung rating: 15 hits: 9 vip rating: 4 
the above mentioned id: 59 title: new59- black Samsung W5900 3G mobile telecommunications dual card dual standby dual-pass rating: 9 hits: 14 vip rating: 4
the above mentioned id: 64-title: new64- Samsung W6400 black 3G mobile telecommunications dual card dual standby dual pass Samsung rating: 14 hits: 10 vip rating: 4 
the above mentioned id: 69 title: new69- black Samsung W6900 3G mobile telecommunications dual card dual standby dual-pass rating: 8 hits: 15 vip rating : 4
the above mentioned id: 74 title: new74- black Samsung W7400 3G mobile telecommunications dual card dual standby dual pass Samsung rating: 13 hits: 11 vip rating: 4 
the above mentioned id: 79 title: new79- black Samsung W7900 3G mobile telecommunications dual card dual standby double-pass rating: 7 hits: 16 vip rating: 4
the above mentioned id: 84 title: new84- black Samsung W8400 3G mobile telecommunications dual card dual standby dual pass Samsung rating: 12 hits: 12 vip rating: 4
the above mentioned id: 89 title: new89 - Samsung W8900 black 3G mobile telecommunications dual card dual standby dual-pass rating: 6 hits: 17 vip rating: 4
the above mentioned id: 94 standard Title: new94- black Samsung W9400 3G mobile telecommunications dual card dual standby dual pass Samsung rating: 11 Hits: 13 vip rating: 4 
the above mentioned id: 99 Title: new99- black Samsung W9900 3G mobile telecommunications dual card dual standby dual-pass rating: 5 hits: 9 vip rating: 4

We can see a way to customize the score, the result would be the highest ranking vip

Note: For more solr

Are sorted using custom solr of the Function Query function. You can view solr self-learning api

Published 682 original articles · won praise 1391 · Views 1.71 million +

Guess you like

Origin blog.csdn.net/itcast_cn/article/details/104770048