Solr分页,
1. Solr查询
- Controller (Class控制层)
package cn.ps.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.ps.dao.PersonDao;
import cn.ps.entity.Person;
@RestController
public class SolrController {
// @Autowired
// private SolrTemplate st;
@Autowired
private PersonDao dao;
@GetMapping("/queryNews")
public List<Person> queryNews(String keyword) {
// SimpleQuery sq=new SimpleQuery( "newtitle_ik:"+keyword); //参数放条件 solr中的q
// Page<Person> query = st.query(sq, Person.class);
return dao.findByDesc(keyword);
}
}
- dao (接口)
package cn.ps.dao;
import java.util.List;
import org.springframework.data.solr.repository.SolrCrudRepository;
import cn.ps.entity.News;
import cn.ps.entity.Person;
public interface PersonDao extends SolrCrudRepository<Person, String> {
public List<Person> findByDesc(String keyword);
}
- entity (实体类)
package cn.ps.entity;
import org.apache.solr.client.solrj.beans.Field;
import org.springframework.data.solr.core.mapping.SolrDocument;
import lombok.Data;
@Data
@SolrDocument(solrCoreName="mycore")
public class Person {
@Field("id")
private String id;
@Field("country_ik")
private String country;
@Field("name_ik")
private String name;
@Field("desc_ik")
private String desc;
}
- Main 方法
package cn.ps;
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
public SolrTemplate solrTemplate(SolrClient client) {
return new SolrTemplate(client);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(SolrMain.class, args);
}
}
项目目录。。。
资源文件
- application.yml
spring:
data:
solr:
host: http://192.168.59.130:8983/solr
server:
port: 8888
- index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.min.js"></script>
<script>
function query(){
$.ajax({
url:"queryNews",
data:"keyword="+$("#mykeyword").val(),
dataType:"json",
type:"get",
success:function(r){
$("#myNews").text(JSON.stringify(r));
}
});
}
</script>
</head>
<body>
新闻:<input id="mykeyword" type="text" name="keyword">
<button onclick="query()">搜索</button>
<div id="myNews">
</div>
</body>
</html>
资源文件目录。。。
pom.xml
<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.ps</groupId>
<artifactId>SOLR</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<dependencies>
<!-- 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-solr</artifactId>
</dependency>
<!-- 小辣椒工具 -->
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Solr网页,JSON数据!!!
{"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":"王冠宇是王五的儿子"}
查询
- 网址:https://docs.spring.io/spring-data/solr/docs/2.1.17.RELEASE/reference/html/#solr.query-methods
二。分页
接口:
package cn.ps.dao;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.solr.repository.SolrCrudRepository;
import cn.ps.entity.Person;
public interface PersonDao extends SolrCrudRepository<Person, String> {
//使用了Pageable 表示分页,需要两个参数,(当前页,一页显示多少条数据)
public Page<Person> findByDesc(String keyword,Pageable page);
}
controller:
package cn.ps.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.ps.dao.PersonDao;
import cn.ps.entity.Person;
@RestController
public class SolrController {
@Autowired
private PersonDao dao;
@GetMapping("/queryNews")
public Page<Person> queryNews(String keyword) {
//分页
PageRequest pa=new PageRequest(1,2);
//与接口的参数不一样
return dao.findByDesc(keyword, pa);
}
}
三。排序
//分页
PageRequest pa=new PageRequest(1,2,new Sort(Direction.ASC,"age_i"));
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。