功能介紹
本文將使用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等級、商品評價。
<!-- 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;@SpringBootApplicationpublic 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) @SpringBootTestpublic class CustomSortTest {@Autowiredprivate SolrClient client;/*** 初始化solr索引數據* */ @Testpublic 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();}}
編寫一下方法看一下默認條件查詢:三星的效果:
@Testpublic 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等級排序
@Testpublic void testVipPageQuery()throws Exception{ SolrQuery solrQuery = new SolrQuery();//關鍵詞solrQuery.set("q","t_searchText:*三星*");//分頁,0開始,每頁20條,setStart設置的就是顯示第幾頁solrQuery.setStart(0); solrQuery.setRows(20);//設置權重方式為edismaxsolrQuery.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+" " );}}
結果如下:
查詢到的總條數:100id: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進行學習