您好,登錄后才能下訂單哦!
本篇文章為大家展示了springboot如何整合solr,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
下載地址 ,選擇你想要下載的版本
http://archive.apache.org/dist/lucene/solr/7.5.0/
下載解壓
solr 的啟動(dòng)方式有多種,可以結(jié)合 tomcat,新版本的 solr 可以直接啟動(dòng)。這里通過(guò)命令行啟動(dòng)。
打開(kāi) cmd,啟動(dòng)項(xiàng)目
這里記下兩個(gè)常用命令
啟動(dòng) bin\solr.cmd start 停止 bin\solr.cmd stop -all
注意:如果啟動(dòng)失敗 提示 缺少 managed-schema 的話,解決辦法如下
把 \server\resources 下的 conf 文件夾 復(fù)制到新建的 core 中 \server\solr\new_core
啟動(dòng)成功后,打開(kāi)瀏覽器進(jìn)入 solr 控制臺(tái) http://localhost:8983/solr/#/
core相當(dāng)于數(shù)據(jù)表,我們可以對(duì) core 進(jìn)行管理
選中 core ,創(chuàng)建字段,輸入字段名和所對(duì)應(yīng)的分詞器
/update : 添加或更新數(shù)據(jù)
JSON : 以 json 格式添加數(shù)據(jù)
添加成功后,我們查詢(xún)一下
q :查詢(xún)關(guān)鍵字 ,可以通過(guò) 字段名=key 指定查詢(xún)字段名 sort : 排序 start rows : 分頁(yè)查詢(xún) fl:指定返回結(jié)果需要顯示的字段 df:默認(rèn)域 指定需要查詢(xún)的字段
鏈接:https://pan.baidu.com/s/1i5DJbj0lBeaJpgd1BInMxg 提取碼:prfe
下載后,放到 \server\solr-webapp\webapp\WEB-INF\lib 目錄下
然后編輯 managed-schema.xml。增加以下內(nèi)容 ,最后重啟 solr
<fieldType name="text_ik" class="solr.TextField"> <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/> </fieldType> <field name="text_ik" type="text_ik" indexed="true" stored="true" multiValued="false" />
現(xiàn)在來(lái)測(cè)試一下未使用中文分詞器之前和之后的區(qū)別
可以看到 text_ik 分詞器更加符合漢字的分詞習(xí)慣
jar包依賴(lài)
<!-- 引入solr依賴(lài) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-solr</artifactId> </dependency>
application.properties配置,這是指定了 core 的情況,也可以不指定,在操作的時(shí)候再指定
spring.data.solr.host=http://localhost:8983/solr/new_core
測(cè)試用例
package org.elvin.mysolr.controller; import org.apache.solr.client.solrj.SolrClient; import org.apache.solr.client.solrj.SolrQuery; 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.SolrInputDocument; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; import java.util.Map; import java.util.UUID; @RestController @RequestMapping("solr") public class SolrController { @Autowired private SolrClient client; /** * 新增/修改 索引 * 當(dāng) id 存在的時(shí)候, 此方法是修改(當(dāng)然, 我這里用的 uuid, 不會(huì)存在的), 如果 id 不存在, 則是新增 * @return */ @RequestMapping("add") public String add() { String uuid = UUID.randomUUID().toString().replaceAll("-", ""); try { SolrInputDocument doc = new SolrInputDocument(); doc.setField("id", uuid); doc.setField("studentName", "小王同學(xué)"); /* 如果spring.data.solr.host 里面配置到 core了, 那么這里就不需要傳 new_core 這個(gè)參數(shù) * 下面都是一樣的 */ client.add("new_core", doc); //client.commit(); client.commit("new_core"); return uuid; } catch (Exception e) { e.printStackTrace(); } return "error"; } /** * 根據(jù)id刪除索引 * @param id * @return */ @RequestMapping("delete") public String delete(String id) { try { client.deleteById("new_core",id); client.commit("new_core"); return id; } catch (Exception e) { e.printStackTrace(); } return "error"; } /** * 刪除所有的索引 * @return */ @RequestMapping("deleteAll") public String deleteAll(){ try { client.deleteByQuery("new_core","*:*"); client.commit("new_core"); return "success"; } catch (Exception e) { e.printStackTrace(); } return "error"; } /** * 根據(jù)id查詢(xún)索引 * @return * @throws Exception */ @RequestMapping("getById") public String getById() throws Exception { SolrDocument document = client.getById("new_core", "536563"); System.out.println(document); return document.toString(); } /** * 綜合查詢(xún): 在綜合查詢(xún)中, 有按條件查詢(xún), 條件過(guò)濾, 排序, 分頁(yè), 高亮顯示, 獲取部分域信息 * @return */ @RequestMapping("search") public Map<String, Map<String, List<String>>> search(){ try { SolrQuery params = new SolrQuery(); //查詢(xún)條件, 這里的 q 對(duì)應(yīng) 下面圖片標(biāo)紅的地方 params.set("q", "手機(jī)"); //過(guò)濾條件 params.set("fq", "product_price:[100 TO 100000]"); //排序 params.addSort("product_price", SolrQuery.ORDER.asc); //分頁(yè) params.setStart(0); params.setRows(20); //默認(rèn)域 params.set("df", "product_title"); //只查詢(xún)指定域 params.set("fl", "id,product_title,product_price"); //高亮 //打開(kāi)開(kāi)關(guān) params.setHighlight(true); //指定高亮域 params.addHighlightField("product_title"); //設(shè)置前綴 params.setHighlightSimplePre("<span style='color:red'>"); //設(shè)置后綴 params.setHighlightSimplePost("</span>"); QueryResponse queryResponse = client.query(params); SolrDocumentList results = queryResponse.getResults(); long numFound = results.getNumFound(); System.out.println(numFound); //獲取高亮顯示的結(jié)果, 高亮顯示的結(jié)果和查詢(xún)結(jié)果是分開(kāi)放的 Map<String, Map<String, List<String>>> highlight = queryResponse.getHighlighting(); for (SolrDocument result : results) { System.out.println(result.get("id")); System.out.println(result.get("product_title")); //System.out.println(result.get("product_num")); System.out.println(result.get("product_price")); //System.out.println(result.get("product_image")); Map<String, List<String>> map = highlight.get(result.get("id")); List<String> list = map.get("product_title"); System.out.println(list.get(0)); System.out.println("------------------"); System.out.println(); } return highlight; } catch (Exception e) { e.printStackTrace(); } return null; } }
上述內(nèi)容就是springboot如何整合solr,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。