效果圖
thymeleaf
pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hr</groupId>
<artifactId>solr-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>solr-1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--依賴-->
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.solr/solr-solrj -->
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>4.10.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- freemark依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--熱部署-->
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.properties
server.port=8888
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=52Java
# thymeleaf
#spring.thymeleaf.prefix=classpath:/templates/
#spring.thymeleaf.check-template-location=true
#spring.thymeleaf.suffix=.html
#spring.thymeleaf.encoding=UTF-8
#spring.thymeleaf.content-type=text/html
#spring.thymeleaf.mode=HTML5
#spring.thymeleaf.cache=false
常量類
package com.hr.solr1.constant;
/**
* @ClassName SolrConstant
* @Description: TODO
* @Author 湯永紅
* @Date 2020/5/28 0028
* @Version V1.0
**/
public class SolrConstant {
//把常量寫在此類
public static final String SOLR_SERVER_URL = "http://127.0.0.1:7778/solr/";
}
工具類
package com.hr.solr1.utils;
import com.hr.solr1.constant.SolrConstant;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @ClassName ProductUtil
* @Description: TODO
* @Author 湯永紅
* @Date 2020/5/28 0028
* @Version V1.0
**/
@Component
public class ProductUtil {
//1.連到solr服務器
public SolrServer getSolrServer(){
//1.1獲取服務器路徑 SolrConstant.SOLR_SERVER_URL;
return new HttpSolrServer(SolrConstant.SOLR_SERVER_URL);//ctlr+shift+t查看子類
}
//2.獲得查詢對象
public SolrQuery getSolrQuery(){
return new SolrQuery();
}
//3. 對products索引庫進行搜索(操作)
public List<String> queryProducts(String field,String keyWords) throws Exception{
//3.1 查詢條件設置
SolrQuery query = getSolrQuery();
query.setQuery(field+":"+keyWords);// 查詢的索引列(索引庫中的) q
query.setHighlight(true);//點那個勾
query.addHighlightField(field);//hl.fl 設置高亮的字段
query.setHighlightSimplePre("<span style='color:red'>");//pre
query.setHighlightSimplePost("</span>");//post
//3.2獲取帶高亮的部分
//先連服務器,並帶上條件
QueryResponse response = getSolrServer().query(query);
SolrDocumentList documents = response.getResults();
//再獲取高亮
Map<String, Map<String, List<String>>> map = response.getHighlighting();
//先判斷查到沒
List<String> lists=null;
if(documents.getNumFound()>0){
lists = new ArrayList<>();
//只要有集合就會有循環
for (SolrDocument document : documents) {
//獲取field,就可以獲取id product_name
Map<String, List<String>> listMap = map.get(document.get("id"));//
List<String> product_name = listMap.get("product_name");//直接拿
String name =product_name!=null ?product_name.get(0):"";//有可能爲空
lists.add(name);
}
}
return lists;
}
}
控制層
package com.hr.solr1.web;
import com.hr.solr1.utils.ProductUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.List;
/**
* @ClassName ProductController
* @Description: TODO
* @Author 湯永紅
* @Date 2020/5/28 0028
* @Version V1.0
**/
// /products/get/小黃人
@Controller
@RequestMapping("/products")
public class ProductController {
@Resource
private ProductUtil productUtil;
@RequestMapping(value = "/get/{keyWords}",produces = {"application/json;charset=utf-8"})
@ResponseBody
public List<String> getProducts(@PathVariable("keyWords")String keyWords){
List<String> product_name=null;
try {//ctrl+alt+t
product_name = productUtil.queryProducts("product_name", keyWords);
} catch (Exception e) {
e.printStackTrace();
}
return product_name;
}
//跳頁面
@RequestMapping(value = "/get2/{keyWords}",produces = {"application/json;charset=utf-8"})
public String getProducts2(Model model, @PathVariable("keyWords")String keyWords){
List<String> product_name=null;
try {//ctrl+alt+t
product_name = productUtil.queryProducts("product_name", keyWords);
model.addAttribute("product_name",product_name);
} catch (Exception e) {
e.printStackTrace();
}
return "list";
}
}
list.jsp
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<ul>
<li th:each="name : ${product_name}"><div th:utext="${name}"></div></li>
</ul>
</body>
</html>
當然,也可以用ajax請求JSON