springboot集成elasticsearch

1. pom.xml添加依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</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>

    <!--集成es-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>
</dependencies>

 2. application-dev.xml配置

server:
  port: 8888

spring:
  elasticsearch:
    rest:
      uris:
        - "192.168.56.101:9200"
      username: admin
      password: admin
  data:
    elasticsearch:
      client:
        reactive:
          endpoints: 192.168.56.101:9200
          username: admin
          password: admin
          connection-timeout: 60000
          socket-timeout: 60000
      repositories:
        enabled: true

 3. 启动类添加注解

package com.powertrade;

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

@SpringBootApplication
@EnableElasticsearchRepositories(basePackages = "com.code")
public class ElasticsearchApplication {
    public static void main(String[] args) {
        SpringApplication.run(ElasticsearchApplication.class, args);
    }

}

 4. 创建索引 及增删改查

package com.powertrade.es.entity;

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

/**
 * @author code
 * @description (创建对象类)
 * @Date 
 **/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(indexName = "person",refreshInterval = "30s",indexStoreType = "niofs",shards = 1,replicas = 0)
public class PersonDo {
    @Id
    private String id;
    private String name;
    private Integer age;
}
package com.powertrade.es.dao;

import com.powertrade.es.entity.PersonDo;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

/**
 * @author code
 * @description (创建接口)
 * @Date 
 **/
public interface PersonDoDao extends ElasticsearchRepository<PersonDo, String> {

}
package com.powertrade.es;

import com.powertrade.es.dao.PersonDoDao;
import com.powertrade.es.entity.PersonDo;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * @author code
 * @description (创建测试类)
 * @Date 
 **/
@Slf4j
@SpringBootTest
public class ElasticSearchDemoApplicationTests {

    @Autowired
    PersonDoDao personDoDao;

    @Test
    public void save() {
        PersonDo personDo = new PersonDo();
        personDo.setName("LiLei");
        personDo.setAge(12);
        personDoDao.save(personDo);
    }

    @Test
    public void update() {
        PersonDo personDo = new PersonDo();
        personDo.setId("xxxxxx");
        personDo.setName("HanMeimei");
        personDo.setAge(15);
        personDoDao.save(personDo);
    }

   @Test
    public void query() {
        Iterable<PersonDo> personDoIterable = personDoDao.findAll();
        personDoIterable.forEach(personDo -> System.out.println(personDo));
    }


    @Test
    public void deleteAll() {
        personDoDao.deleteAll();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值