Elasticsearch 推荐文档
Elasticsearch 简介
Elasticsearch
MySQL
索引
数据库
类型
表
文档
记录
属性
字段
Elasticsearch 安装 Spring Boot 与 Elasticsearch 整合 两种方式
方式一1 2 3 4 5 <dependency > <groupId > io.searchbox</groupId > <artifactId > jest</artifactId > <version > 6.3.1</version > </dependency >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 @Autowired JestClient jestClient; @Testvoid put() throws IOException { UserBO userBO = new UserBO(); userBO.setAccount("account"); userBO.setId(1 ); userBO.setJumpToken("jumpToken"); userBO.setPassword("password"); userBO.setUsername("username"); Index index = new Index .Builder(userBO).index ("blog").type ("user").build(); jestClient.execute (index ); } @Testvoid get () throws IOException { String json = "{" + "\"query\": {" + " \"match\":{" + " \"name \": \"username\"" + " } " + " }" + "}"; Search search = new Search .Builder(json ).addIndex("blog").addType("user").build(); SearchResult result = jestClient.execute (search ); System .out .println("#########################################"); System .out .println(result.getJsonString()); }
方式二 maven 坐标
org.springframework.boot
spring-boot-starter-data-elasticsearch
配置文件 spring: data: elasticsearch: repositories: enabled: true cluster-nodes: 127.0.0.1:9300 # es的连接地址及端口号 cluster-name: docker-cluster # es集群的名称
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 @Data @ToString @Document (indexName = "blog" ,type = "user" ,shards = 1 ,replicas = 0 ,refreshInterval = "-1" )public class User { @Id private Integer id; @Field (type = FieldType.Keyword) private String name; public User () { } public User (Integer id, String name) { this .id = id; this .name = name; } }public interface UserRepository extends ElasticsearchRepository <User , Integer > { User findByName (String name) ; }@RestController public class UserController { @Autowired private ElasticsearchTemplate elasticsearchTemplate; @Autowired private UserRepository userRepository; @GetMapping ("/user/{name}" ) public String get (@PathVariable("name" ) String name) { User user = userRepository.findByName(name); return user.toString(); } @GetMapping ("/user/put" ) public String put () { User user = new User(); user.setId(10 ); user.setName("zhaoliu" ); User save = userRepository.save(user); System.out.println(save); return save.toString(); } }