# Base Code Java org.apache.solr solr-solrj 8.4.0 # spring boot org.springframework.boot spring-boot-starter-data-solrhtml
# Base Code Java <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> <version>8.4.0</version> </dependency> # spring boot <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-solr</artifactId> </dependency>
對Solr的全部請求均由發送SolrClient
java
SolrClient
有一些具體的實現,每一個實現都針對不一樣的使用模式或彈性模型:web
HttpSolrClient
-面向以查詢爲中心的工做負載,儘管它也是一個很好的通用客戶端。直接與單個Solr節點通訊。Http2SolrClient
-利用HTTP / 2的異步,非阻塞和通用客戶端。此類是實驗性的,所以它的API可能會在次要版本的SolrJ中更改或刪除。LBHttpSolrClient
-在Solr節點列表之間平衡請求負載。根據節點運行情況調整「服務中」節點的列表。LBHttp2SolrClient
-就像LBHttpSolrClient
但Http2SolrClient
改用。此類是實驗性的,所以它的API可能會在次要版本的SolrJ中更改或刪除。CloudSolrClient
-旨在與SolrCloud部署進行通訊。使用已經記錄的ZooKeeper狀態來發現請求並將其路由到健康的Solr節點。ConcurrentUpdateSolrClient
-面向以索引爲中心的工做負載。在將較大的批次發送到Solr以前,在內部緩衝文檔。ConcurrentUpdateSolrClient
-就像ConcurrentUpdateSolrClient
但Http2SolrClient
改用。此類是實驗性的,所以它的API可能會在次要版本的SolrJ中更改或刪除大多數SolrClient
實現(CloudSolrClient
和除外Http2SolrClient
)都要求用戶指定一個或多個Solr基本URL,而後客戶端將其用於將HTTP請求發送到Solr。從那時起,用戶在其提供的基本URL上包括的路徑會影響所建立客戶端的行爲。spring
http://hostname:8983/solr/core1
)的路徑的URL 。若是在基本URL中指定了核心或集合,則不須要對該客戶端進行的後續請求來從新指定受影響的集合。可是,客戶端僅限於將請求發送到該核心/集合,而不能將請求發送到任何其餘請求。http://hostname:8983/solr
)。若是在基本URL中未指定任何核心或集合,則能夠對任何核心/集合進行請求,可是必須在全部請求上指定受影響的核心/集合。SolrClient實現都容許用戶指定鏈接並讀取與Solr通訊的超時apache
final String solrUrl = "http://localhost:8983/solr"; return new HttpSolrClient.Builder(solrUrl) .withConnectionTimeout(10000) .withSocketTimeout(60000) .build();
Solr Cloud 集羣實現是 SolrJ的CloudSolrClient
實現(CloudSolrClient
和CloudHttp2SolrClien
springboot
//http://localhost:8080/saveOrUpdate?id=1&name=dgw&age=24 @GetMapping("saveOrUpdate") public String saveOrUpdate(Admin admin) { if (admin.getId() == null) { admin.setId(System.currentTimeMillis()); } try { #使用 SolrInputDocument final SolrInputDocument doc = new SolrInputDocument(); doc.setField("id", admin.getId()); doc.setField("name", admin.getName()); doc.setField("age", admin.getAge()); // collectionName 文檔名稱 solrClient.add(solrConfig.collectionName, doc); solrClient.commit(solrConfig.collectionName); return admin.toString(); } catch (SolrServerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return "error"; }
final SolrClient client = getSolrClient(); final Map<String, String> queryParamMap = new HashMap<String, String>(); queryParamMap.put("q", "*:*"); queryParamMap.put("fl", "id, name"); queryParamMap.put("sort", "id asc"); MapSolrParams queryParams = new MapSolrParams(queryParamMap); final QueryResponse response = client.query("techproducts", queryParams); final SolrDocumentList documents = response.getResults(); print("Found " + documents.getNumFound() + " documents"); for(SolrDocument document : documents) { final String id = (String) document.getFirstValue("id"); final String name = (String) document.getFirstValue("name"); print("id: " + id + "; name: " + name); }
package com.dgw.springbootsolr.controller; import com.dgw.springbootsolr.config.SolrConfig; import com.dgw.springbootsolr.entity.Admin; import com.dgw.springbootsolr.entity.Goods; 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.response.QueryResponse; import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocumentList; import org.apache.solr.common.SolrInputDocument; import org.apache.solr.common.util.NamedList; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @program: springboot-solr * @description: * @author: Mr.Dai * @create: 2020-02-26 21:09 **/ @RestController public class SolrController { @Autowired public SolrClient solrClient; @Autowired private SolrConfig solrConfig; //http://localhost:8080/saveOrUpdate?id=1&name=dgw&age=24 @GetMapping("saveOrUpdate") public String saveOrUpdate(Admin admin) { if (admin.getId() == null) { admin.setId(System.currentTimeMillis()); } try { final SolrInputDocument doc = new SolrInputDocument(); doc.setField("id", admin.getId()); doc.setField("name", admin.getName()); doc.setField("age", admin.getAge()); // collectionName 文檔名稱 solrClient.add(solrConfig.collectionName, doc); solrClient.commit(solrConfig.collectionName); return admin.toString(); } catch (SolrServerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return "error"; } /** * 根據id刪除索引 * //http://localhost:8080/delete?id=1 */ @GetMapping("delete") public String delete(String id) { try { solrClient.deleteById(solrConfig.collectionName, id); solrClient.commit(solrConfig.collectionName); return id; } catch (Exception e) { e.printStackTrace(); } return "error"; } /** * 刪除全部的索引 * http://localhost:8080/deleteAll */ @GetMapping("deleteAll") public String deleteAll() { try { solrClient.deleteByQuery(solrConfig.collectionName, "*:*"); solrClient.commit(solrConfig.collectionName); return "success"; } catch (Exception e) { e.printStackTrace(); } return "error"; } /** * 根據id查詢索引 * http://localhost:8080/getGoodsById?id=1 */ @GetMapping("getGoodsById") public String getGoodsById(String id) throws Exception { SolrDocument document = solrClient.getById(solrConfig.collectionName, id); return document.toString(); } //http://localhost:8080/addBean @GetMapping("addBean") public String addBean() { Admin dgw2 = new Admin(1L, "dgw2", 25); try { solrClient.addBean(solrConfig.collectionName, dgw2); solrClient.commit(solrConfig.collectionName); } catch (IOException | SolrServerException e) { e.printStackTrace(); } return "error"; } // http://localhost:8080/search @GetMapping("search") public Map<String, Object> search() { final SolrQuery solrQuery = new SolrQuery("*:*"); solrQuery.addField("id"); solrQuery.addField("name"); solrQuery.addField("age"); solrQuery.setSort("id", SolrQuery.ORDER.asc); //設置高亮前綴 solrQuery.setHighlightSimplePre("<span style='color:red'>"); //設置高亮後綴 solrQuery.setHighlightSimplePost("</span>"); //指定高亮域 solrQuery.addHighlightField("id"); solrQuery.addHighlightField("name"); solrQuery.setHighlightFragsize(1); //打開開關 solrQuery.setHighlight(true); //返回集合 Map<String, Object> returnMap = new HashMap(16); try { QueryResponse query = solrClient.query(solrConfig.collectionName, solrQuery); SolrDocumentList results = query.getResults(); //返回行數 long numFound = results.getNumFound(); //獲取高亮顯示的結果, 高亮顯示的結果和查詢結果是分開放的 Map<String, Map<String, List<String>>> highlight = query.getHighlighting(); System.out.println(results); System.out.println(highlight); results.forEach(result -> { Map map = highlight.get(result.get("id")); result.addField("nameHH", map.get("name")); }); returnMap.put("results", results); return returnMap; } catch (SolrServerException | IOException e) { e.printStackTrace(); } return new HashMap<String, Object>() { { put("id", "1"); put("name", "ggg"); put("age", "12"); } }; } /** * 綜合查詢: 在綜合查詢中, 有按條件查詢, 條件過濾, 排序, 分頁, 高亮顯示, 獲取部分域信息 * * @return */ @GetMapping("search2") public Map<String, Object> search2(String keyword) { //返回集合 Map<String, Object> returnMap = new HashMap(); try { SolrQuery params = new SolrQuery(); //查詢條件 params.set("q", keyword); //過濾條件 params.set("fq", "goodsPrice:[100 TO 100000]"); //排序 params.addSort("id", SolrQuery.ORDER.asc); //分頁 //從第幾條記錄開始 params.setStart(0); //最多返回多少條記錄 params.setRows(20); //默認域 params.set("df", "goodsIntroduce"); //只查詢指定域 params.set("fl", "id,goodsName,goodsIntroduce,goodsPrice"); //高亮 //打開開關 params.setHighlight(true); //指定高亮域 params.addHighlightField("goodsIntroduce"); params.addHighlightField("goodsName"); //設置高亮前綴 params.setHighlightSimplePre("<span style='color:red'>"); //設置高亮後綴 params.setHighlightSimplePost("</span>"); QueryResponse queryResponse = solrClient.query(solrConfig.collectionName, params); SolrDocumentList results = queryResponse.getResults(); //返回行數 long numFound = results.getNumFound(); //獲取高亮顯示的結果, 高亮顯示的結果和查詢結果是分開放的 Map<String, Map<String, List<String>>> highlight = queryResponse.getHighlighting(); results.forEach(result -> { Map map = highlight.get(result.get("id")); result.addField("goodsNameHH", map.get("goodsName")); result.addField("goodsIntroduceHH", map.get("goodsIntroduce")); }); returnMap.put("numFound", numFound); returnMap.put("results", results); return returnMap; } catch (Exception e) { e.printStackTrace(); } return null; } //http://localhost:8080/test @GetMapping("/test") public String test() { //System.out.println(solrConfig.collectionName); return solrConfig.collectionName; } }