直接撸代码
模层图
需要导入的依赖
<!-- springboot依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<dependencies>
<!-- Web模块:spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- solr依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-solr</artifactId>
</dependency>
<!-- 小辣椒依赖 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
属性
package cn.lp.entity;
import org.apache.solr.client.solrj.beans.Field;
import org.springframework.data.solr.core.mapping.SolrDocument;
import lombok.Data;
/**
* @Field指定core里的参数
* @SolrDocument(solrCoreName="mycore")指定core(核)
* @author Administrator
* 我只把后缀为_ik的做了分词
*/
@Data
@SolrDocument(solrCoreName="mycore")
public class Person {
private String id;
@Field("country_ik")
private String country;
@Field("name_ik")
private String name;
@Field("desc_ik")
private String desc;
@Field("provice_ik")
private String provice;
@Field("city_ik")
private String city;
@Field("age_i")
private String age;
}
Main方法
package cn.lp;
import org.apache.solr.client.solrj.SolrClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.solr.core.SolrTemplate;
@SpringBootApplication
public class SolrMain {
//bean方法 被jpa代替
/* @Bean
public SolrTemplate solrTemplate(SolrClient client) {
return new SolrTemplate(client);
}*/
public static void main(String[] args) {
SpringApplication.run(SolrMain.class, args);
}
}
接口
package cn.lp.dao;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.solr.repository.Query;
import org.springframework.data.solr.repository.SolrCrudRepository;
import cn.lp.entity.Person;
public interface PersonDao extends SolrCrudRepository<Person, String> {
//继承SolrCrudRepository 使该条命令等同于Is----findByDesc----q=desc:?0
//这种方法只需要方法名对应即可使用,条件少时使用最佳,条件多容易混所以用第二种
public List<Person> findByDesc(String keyword);
//手工查询 必须加注解 等同于 查询所有name_s=值 的数据
@Query("name_ik:?0")
public List<Person> query(String keyword);
//分页查询
public Page<Person> findByDesc(String keyword,Pageable page);
//分页查询 加上排序 必须是已经索引的的属性 比如id不可设置
public Page<Person> findByCountry(String keyword,Pageable page);
//只排序
public List<Person> findByCountry(String keyword,Sort age);
}
控制层
package cn.lp.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.lp.dao.PersonDao;
import cn.lp.entity.Person;
/**
* !!!!!!!!!!!!!!!!!!!!注意从html传过来的String keyword属性名必须一一对应!!!!!!!!!!!!
* @author Administrator
*
*/
@RestController
public class SolrController {
//管理bean
/* @Autowired
private SolrTemplate st;*/
//用jpa淘汰bean方法
@Autowired
private PersonDao nd;
@GetMapping("/queryNews")
public List<Person> queryNews(String keyword){
/*//查询方法
SimpleQuery sq = new SimpleQuery("newtitle_ik:"+keyword);
//查询并返回所得的数据
Page<News> query = st.query(sq, News.class);*/
//转换成List方法
return nd.findByDesc(keyword);
}
@GetMapping("/query")
public List<Person> query(String keyword2){
return nd.query(keyword2);
}
@GetMapping("/pageQuery")
public Page<Person> pageQuery(String keyword3){
//分页需要该方法 代表当前页和当前显示的条数
PageRequest pr=new PageRequest(0, 10);
return nd.findByDesc(keyword3, pr);
}
@GetMapping("/pageSortQuery")
public Page<Person> pageSortQuery(String keyword4){
//分页需要该方法 代表当前页和当前显示的条数
PageRequest pr=new PageRequest(0, 3,new Sort(Direction.ASC,"age_i"));
return nd.findByCountry(keyword4, pr);
}
@GetMapping("/sortQuery")
public List<Person> sortQuery(String keyword5){
return nd.findByCountry(keyword5,new Sort(Direction.ASC,"age_i"));
}
}
配置文件
spring:
data:
solr:
host: http://192.168.146.133:8983/solr
server:
port: 8888
html页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-3.3.1.js"></script>
<script type="text/javascript">
function query(){
$.ajax({
url:'queryNews',
dataType:'json',
type:'get',
data:'keyword='+$("#myKeyWord").val(),
success:function(r){
$("#myNews").html(JSON.stringify(r));
}
});
}
function query2(){
$.ajax({
url:'query',
dataType:'json',
type:'get',
data:'keyword2='+$("#myKeyWord2").val(),
success:function(r){
$("#myNews2").html(JSON.stringify(r));
}
});
}
function query3(){
$.ajax({
url:'pageQuery',
dataType:'json',
type:'get',
data:'keyword3='+$("#myKeyWord3").val(),
success:function(r){
$("#myNews3").html(JSON.stringify(r));
}
});
}
function query4(){
$.ajax({
url:'pageSortQuery',
dataType:'json',
type:'get',
data:'keyword4='+$("#myKeyWord4").val(),
success:function(r){
$("#myNews4").html(JSON.stringify(r));
}
});
}
function query5(){
$.ajax({
url:'sortQuery',
dataType:'json',
type:'get',
data:'keyword5='+$("#myKeyWord5").val(),
success:function(r){
$("#myNews5").html(JSON.stringify(r));
}
});
}
</script>
</head>
<body>
关键字查询:<input id="myKeyWord" type="text" name="keyword"><button onclick="query()">查询</button>
<div id="myNews">
</div>
注释查询:<input id="myKeyWord2" type="text" name="keyword2"><button onclick="query2()">查询</button>
<div id="myNews2">
</div>
分页查询:<input id="myKeyWord3" type="text" name="keyword3"><button onclick="query3()">查询</button>
<div id="myNews3">
</div>
分页排序查询:<input id="myKeyWord4" type="text" name="keyword4"><button onclick="query4()">查询</button>
<div id="myNews4">
</div>
排序查询:<input id="myKeyWord5" type="text" name="keyword5"><button onclick="query5()">查询</button>
<div id="myNews5">
</div>
</body>
</html>
jquery版本随意这里用的是jquery-3.3.1版本
core里的参数
{"id":"1","country_ik":"美国","provice_ik":"加利福尼亚州","city_ik":"旧金山","age_i":"30","name_ik":"John","desc_ik":"John is come from austrina John,s Dad is Johh Super"}
{"id":"2","country_ik":"美国","provice_ik":"加利福尼亚州","city_ik":"好莱坞","age_i":"40","name_ik":"Mike","desc_ik":"Mike is come from austrina Mike,s Dad is Mike Super"}
{"id":"3","country_ik":"美国","provice_ik":"加利福尼亚州","city_ik":"圣地牙哥","age_i":"50","name_ik":"Cherry","desc_ik":"Cherry is come from austrina Cherry,s Dad is Cherry Super"}
{"id":"4","country_ik":"美国","provice_ik":"德克萨斯州","city_ik":"休斯顿","age_i":"60","name_ik":"Miya","desc_ik":"Miya is come from austrina Miya,s Dad is Miya Super"}
{"id":"5","country_ik":"美国","provice_ik":"德克萨斯州","city_ik":"大学城","age_i":"70","name_ik":"fubos","desc_ik":"fubos is come from austrina fubos,s Dad is fubos Super"}
{"id":"6","country_ik":"美国","provice_ik":"德克萨斯州","city_ik":"麦亚伦","age_i":"20","name_ik":"marry","desc_ik":"marry is come from austrina marry,s Dad is marry Super"}
{"id":"7","country_ik":"中国","provice_ik":"湖南省","city_ik":"长沙市","age_i":"18","name_ik":"张三","desc_ik":"张三来自长沙市 是公务员一名"}
{"id":"8","country_ik":"中国","provice_ik":"湖南省","city_ik":"岳阳市","age_i":"15","name_ik":"李四","desc_ik":"李四来自岳阳市 是一名清洁工"}
{"id":"9","country_ik":"中国","provice_ik":"湖南省","city_ik":"株洲市","age_i":"33","name_ik":"李光四","desc_ik":"李光四 老家岳阳市 来自株洲 是李四的侄子"}
{"id":"10","country_ik":"中国","provice_ik":"广东省","city_ik":"深圳市","age_i":"67","name_ik":"王五","desc_ik":"王五来自深圳市 是来自深圳的一名海关缉私精英"}
{"id":"11","country_ik":"中国","provice_ik":"广东省","city_ik":"广州市","age_i":"89","name_ik":"王冠宇","desc_ik":"王冠宇是王五的儿子"}