MAVEN依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.hsk</groupId>
<artifactId>springboot_solr</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot_solr</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-solr</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<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>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
端口配置,使用solr啓動的端口,在application.yml中配置,換成你喜歡的閒置端口
spring:
data:
solr:
host: http://localhost:8080/solr
server:
port: 8081
然後下面是增、刪、查、改
package cn.hsk.springboot_solr;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@RestController
public class SolrController {
@Autowired
private SolrClient solrClient;
/**
* 1、查 id
* @param id
* @return
* @throws SolrServerException
* @throws IOException
*/
@GetMapping("/get/{id}")
public String getDocumentById(@PathVariable String id) throws SolrServerException, IOException {
SolrDocument document = solrClient.getById("han_core", id);
System.out.println(document);
return document.toString();
}
/**
* 2、增
* @param title
* @return
* @throws IOException
* @throws SolrServerException
*/
@GetMapping("/add/{title}")
public String add(@PathVariable("title") String title) {
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
try {
SolrInputDocument doc = new SolrInputDocument();
doc.setField("id", uuid);
doc.setField("title", title);
doc.setField("msg", "好手機");
doc.setField("desc", "hahaha");
/* 如果spring.data.solr.host 裏面配置到 core了, 那麼這裏就不需要傳 han_core 這個參數
* 下面都是一樣的
*/
solrClient.add("han_core", doc);
//client.commit();
solrClient.commit("han_core");
return uuid;
} catch (Exception e) {
e.printStackTrace();
}
return "error";
}
/**
* 3、刪 id
* @return
*/
@DeleteMapping("/delete/{id}")
public String getAllDocuments(@PathVariable String id) {
try {
solrClient.deleteById("han_core", id);
solrClient.commit("han_core");
return id;
} catch (Exception e) {
e.printStackTrace();
}
return "error";
}
/**
* 4、刪 all
* @return
*/
@DeleteMapping("/deleteAll")
public String deleteAll() {
try {
solrClient.deleteByQuery("han_core", "*:*");
solrClient.commit("han_core");
return "success";
} catch (Exception e) {
e.printStackTrace();
}
return "error";
}
/**
* 5、改
* @param msg
* @return
* @throws IOException
* @throws SolrServerException
*/
@PutMapping("/update")
public String update(String id, String title,String msg,String desc) throws IOException, SolrServerException {
try {
SolrInputDocument doc = new SolrInputDocument();
doc.setField("id", id);
doc.setField("title", title);
doc.setField("msg", msg);
doc.setField("desc", desc);
/*
* 如果 spring.data.solr.host 裏面配置到 core了, 那麼這裏就不需要傳 itaem 這個參數 下面都是一樣的 即
* client.commit();
*/
solrClient.add("han_core", doc);
solrClient.commit("han_core");
return doc.toString();
} catch (Exception e) {
e.printStackTrace();
}
return "error";
}
@GetMapping("/select/{q}/{page}/{size}")
public Map<String, Object> select(@PathVariable String q, @PathVariable Integer page, @PathVariable Integer size)
throws SolrServerException, IOException {
SolrQuery params = new SolrQuery();
// 查詢條件
params.set("q", q);
// 排序
params.addSort("id", SolrQuery.ORDER.desc);
// 分頁
params.setStart(page);
params.setRows(size);
// 默認域
//params.set("df", "title");
params.set("df", "text");
// 只查詢指定域
params.set("fl", "id,filePath,text");
// 開啓高亮
params.setHighlight(true);
// 設置前綴
params.setHighlightSimplePre("<****>");
// 設置後綴
params.setHighlightSimplePost("</****>");
params.setHighlightFragsize(100000);
// solr數據庫是 han_core
QueryResponse queryResponse = solrClient.query("hehe_core", params);
SolrDocumentList results = queryResponse.getResults();
// 數量,分頁用
long total = results.getNumFound();// JS 使用 size=MXA 和 data.length 即可知道長度了(但不合理)
// 獲取高亮顯示的結果, 高亮顯示的結果和查詢結果是分開放的
Map<String, Map<String, List<String>>> highlight = queryResponse.getHighlighting();
//Map<String, Object> map = new HashMap<String, Object>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("total", total);
map.put("data", highlight);
return map;
}
}
當然,其中han_core爲你創建的實例,當然在managed_schema中你還得配置自己的插入的字段,如果你存進數據庫還得做更多的配置,跟前兩篇文章的方法一樣。接下來用postman測試就可以了,也可以拿swagger測試,只要你學的工具多。