elasticsearch_springboot初次使用

elasticsearch_springboot初次使用

先运行一波

elasticsearch下载及启动说明

源码

pom

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

实体


    @NoArgsConstructor@Data@AllArgsConstructor
@Document(indexName = "database",type = "employee")
public class Employee {

    private String id;

    private String firstName;

    private String lastName;

    private Integer age;

    private String about;

}

repository


@Component
public interface EmployeeRepository extends ElasticsearchRepository<Employee, String> {
    Employee queryEmployeeById(String id);

    Page<Employee> queryEmployeeByFirstNameContaining(String firstNameContain, Pageable pageable);
}

application.properties

elasticsearch


spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
#spring.data.elasticsearch.local=false
spring.data.elasticsearch.repositories.enable=true

controller


@Slf4j
@RestController
@RequestMapping("/es")
public class EmployeeController {


    @Autowired
    EmployeeRepository employeeRepository;

    @GetMapping("/find/{id}")
    public Employee findById(@PathVariable("id") String employeeId) {
        Employee employee = employeeRepository.queryEmployeeById(employeeId);

        return employee;
    }

    @PostMapping("/add")
    public void saveEmployee(
            @RequestParam("id") String employeeId,
            @RequestParam("firstName") String firstName,
            @RequestParam("lastName") String lastName,
            @RequestParam("age") Integer age,
            @RequestParam("about") String about
    ) {
        Employee employee = new Employee(employeeId, firstName, lastName, age, about);
        Employee save = employeeRepository.save(employee);
        log.info("[新增职员] id:{}", employee.getId());

    }

    //deleteMapping 不能用 怎么回事 wori 开着全局vpn 全部不能用 害的整了半天
    @GetMapping("/delete/{id}")
    public void deleteEmployee(@PathVariable("id") String employeeId) {
        employeeRepository.deleteById(employeeId);

        log.info("[删除职员] id:{}", employeeId);

    }

    @GetMapping("/find_firstname/{firstNameContain}")
    public Page<Employee> findByContainFirstName(@PathVariable("firstNameContain") String firstNameContain) {
        Pageable pageable = new PageRequest(0, 5);
        Page<Employee> employeePage = employeeRepository.queryEmployeeByFirstNameContaining(firstNameContain, pageable);

        return employeePage;
    }
}

运行情况

image

image

image

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值