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(); } }