Elasticsearch简介
ElasticSearch是一个基于Lucene的分布式、高扩展、高实时的搜索与数据分析引擎。它能很方便的使大量数据具有搜索、分析和探索的能力。充分利用ElasticSearch的水平伸缩性,能使数据在生产环境变得更有价值。
ElasticSearch用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。官方客户端在Java、.NET(C#)、PHP、Python、Apache Groovy、Ruby和许多其他语言中都是可用的。
Elasticsearch整合SpringBoot步骤
1.pom.xml加入maven依赖,我是用的swagger,所以会加上相关配置,不需要用可以不加。
<!-- Swagger核心包 start -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<!-- elasticsearch -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
2.application.properties加入配置
# elasticsearch集群名称,默认的是elasticsearch
spring.data.elasticsearch.cluster-name=my-application
# 节点的地址 注意api模式下端口号是9300,格式=ip:9300
spring.data.elasticsearch.cluster-nodes=**.**.**.**:9300
# 是否开启本地存储
spring.data.elasticsearch.repositories.enabled=true
#配置Swagger相关信息
eureka.instance.prefer-ip-address=true
eureka.instance.status-page-url=http://${spring.cloud.client.ipAddress}:${server.port}/swagger-ui.html
3.创建实体类,indesxName索引值必须全部小写
@Document(indexName = "userinfoindex", type = "userInfo")
public class UserInfo implements Serializable {
private static final long serialVersionUID = 1L;
/** 编号 */
private Long id;
/** 姓名 */
private String name;
/** 年龄 */
private Integer age;
/** 描述 */
private String description;
/** 创建时间 */
private Date createTime;
// getter和setter 略
4.创建Dao层
public interface ESUserInfoRepository extends ElasticsearchRepository<UserInfo, String> {
}
5. 创建Service层
@Service("eSUserInfoService")
public class ESUserInfoServiceImpl implements ESUserInfoService{
@Autowired
private ESUserInfoRepository eSUserInfoRepository;
@Autowired
ElasticsearchTemplate elasticsearchTemplate;
@Override
public boolean insert(UserInfo user) {
boolean falg=false;
try{
eSUserInfoRepository.save(user);
falg=true;
}catch(Exception e){
e.printStackTrace();
}
return falg;
}
@Override
public List<UserInfo> search(String searchContent) {
//查询单个任意值
//SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(queryStringQuery(searchContent)).build();
//分页查询
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(queryStringQuery(searchContent)).withPageable(PageRequest.of(0, 20)).build();
//查询单个指定值
//SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(QueryBuilders.termQuery("name",searchContent)).build();
List<UserInfo> poetryList = elasticsearchTemplate.queryForList(searchQuery, UserInfo.class);
return poetryList;
}
}
6.创建controller
@RestController
@RequestMapping("/swaggerTest")
public class SwaggerTestController {
@Autowired
private ESUserInfoService esUserInfoService;
@ResponseBody
@PostMapping("/search")
public String queryAccountInfoByAccountName(
@ApiParam(value = "任意值") @RequestParam(required = true) String searchContent
) {
List<UserInfo> userInfo = esUserInfoService.search(searchContent);
return JSON.toJSONString(userInfo);
}
@ResponseBody
@PostMapping("/save")
public String save(
@ApiParam(value = "姓名") @RequestParam(required = false) String name,
@ApiParam(value = "年龄") @RequestParam(required = false) Integer age,
@ApiParam(value = "描述") @RequestParam(required = false) String description
) {
UserInfo userInfo = new UserInfo(name,age,description,new Date());
esUserInfoService.insert(userInfo);
return JSON.toJSONString(userInfo);
}
}
最后可以直接访问了,添加数据了对应的本地es也能查看到对应的数据和索引