Sprint Boot 集成Elasticsearch 介绍及开发

Elasticsearch也使用Java开发并使用Lucene作为其核心来实现所有索引和搜索的功能,但是它的目的是通过简单的RESTful API来隐藏Lucene的复杂性,从而让全文搜索变得简单。
不过,Elasticsearch不仅仅是Lucene和全文搜索,我们还能这样去描述它:
    (1) 分布式的实时文件存储,每个字段都被索引并可被搜索
    (2) 分布式的实时分析搜索引擎
    (3) 可以扩展到上百台服务器,处理PB级结构化或非结构化数据

1. ES 基础环境部署
    Linux 环境:centos 7.4
    elasticsearch version:"6.3.2",
    cluster_name:elasticsearch

    
    访问:ip:9200 即可查看 ES 相关信息,包括集群名,版本信息等
    这里不做ES的安装配置,后面会有专门讲解 ES的安装及配置,启动等操作的章节,请出门左拐.
    
2. Spring Boot 工程建立

    依赖devops,web及lombok

3. 修改 pom.xml 文件

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

4. 修改 application.properties 配置文件

# elasticsearch集群名称,默认的是elasticsearch
spring.data.elasticsearch.cluster-name=elasticsearch
##配置ES的访问地址
#节点的地址 注意api模式下端口号是9300,千万不要写成9200
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300

5. Entity 实体类编写

import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;

@Document(indexName = "mymayikt", type = "user")
@Data
public class UserEntity {
  @Id
  private String id;
  private String name;
  private int sex;
  private int age;
}

6. Container 接口编写

import com.test.springes.dao.UserReposiory;
import com.test.springes.entity.UserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import java.util.Optional;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(description = "用户操作接口asdfasdfa")
public class EsController {

  @Autowired
  private UserReposiory userReposiory;

  @PostMapping("/addUser")
  @ApiOperation(value = "获取asdfasdotp", notes="sdfasdfasd")
  public UserEntity addUser(@RequestBody UserEntity user) {
    return userReposiory.save(user);
  }

  @GetMapping("/findUser")
  @ApiOperation(value = "获取otp", notes="通过手机号获取OTP验证码")
  @ApiImplicitParam(name = "id", value = "电话号码", paramType = "query", required = true, dataType = "String")
  public Optional<UserEntity> findUser(@RequestParam(value = "id")  String id) {
    return userReposiory.findById(id);
  }
}

7. Dao 持久层编写

import com.test.springes.entity.UserEntity;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.data.repository.CrudRepository;

public interface UserReposiory extends ElasticsearchRepository<UserEntity, String> {

}

8. main 添加注解

@EnableElasticsearchRepositories(basePackages = "com.test.springes.dao")

代码如下:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

@SpringBootApplication
@EnableElasticsearchRepositories(basePackages = "com.test.springes.dao")
public class SpringEsApplication {

  public static void main(String[] args) {
    SpringApplication.run(SpringEsApplication.class, args);
  }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Freedom3568

技术域不存在英雄主义,不进则退

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值