java中使用solr SpringBoot集成solr
创建maven项目引入jar包
<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.pjj</groupId>
<artifactId>SpringBoot_solr</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.14.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 引入solr库连接jar-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-solr</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.14</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
application.yml配置
spring:
data:
solr:
host: http://192.168.162.131:8983/solr
server:
port: 8888
HTML代码
<!DOCTYPE html>
<html>
<head>
<script src="jquery-3.3.1.js"></script>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
查找:<input id="keyy" type="text" name="key"> <br/>
<button onclick="query()">提交</button>
<div id="dat">
</div>
</body>
<script type="text/javascript">
function query(){
$.ajax({
url:'query',
dataType:'json',
data:"key="+$("#keyy").val(),
type:'get',
success:function(s){
$("#dat").text(JSON.stringify(s));
}
});
}
</script>
</html>
java代码
package cn.pjj;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.SimpleQuery;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SolrStart {
/**
* 自动装配 连接Solr的类
*/
@Autowired
SolrTemplate solrTemplate;
@GetMapping("/query")
public List<EBean> text(@RequestParam("key") String key) {
//指定查询索引
SimpleQuery sq=new SimpleQuery("name_ik:"+key);
//查到查询数据
Page<EBean> query = solrTemplate.query(sq, EBean.class);
List<EBean> content = query.getContent();
//将数据返回带html
return content;
}
}
package cn.pjj;
import org.apache.solr.client.solrj.beans.Field;
import org.springframework.data.solr.core.mapping.SolrDocument;
import lombok.Data;
@Data
//指定核(core)
@SolrDocument(solrCoreName="mycore")
public class EBean {
String id;
//指定对应键名
@Field("name_ik")
String name;
@Field("sex_s")
String sex;
@Field("age_i")
String age;
@Field("email_s")
String email;
@Field("phone_s")
String phone;
}
package cn.pjj;
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 SolrM {
/**
* 管理bean solrTemplate 实现自动装配
* @param client
* @return
*/
@Bean
public SolrTemplate solrTemplate(SolrClient client) {
return new SolrTemplate(client);
}
public static void main(String[] args) {
SpringApplication.run(SolrM.class, args);
}
}
运行效果: