elastaicsearch简单入门项目
application.yml
spring:
elasticsearch:
host: 目标服务器 #本机就localhost,远程则写公网地址,注意开放端口,下同,例如127.0.0.1
port: 9300
cluster-name: my-application
rest:
uris: http://目标服务器:9200
controller
package com.ruoyi.web.controller.elasticsearchTest.controller; import com.ruoyi.web.controller.elasticsearchTest.domain.Student; import com.ruoyi.web.controller.elasticsearchTest.service.StudentImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/student") public class StudentController { @Autowired private ElasticsearchRestTemplate elasticsearchRestTemplate; @Autowired private StudentImpl studentImpl; @PostMapping("/batchAdd") @ResponseBody public void add(@RequestBody List<Student>students){ System.out.println(students); studentImpl.saveAll(students); } @PostMapping("/add") public void add(@RequestBody Student student){ studentImpl.save(student); } @PostMapping("/search/name") public Object searchName(String keyword){ List<Student> students = studentImpl.findByNameLike(keyword); return students; } }
ESConfig
package com.ruoyi.web.controller.elasticsearchTest.config; import com.ruoyi.common.utils.StringUtils; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.transport.client.PreBuiltTransportClient; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.net.InetAddress; import java.net.UnknownHostException; @Configuration public class ESConfig { @Value("${spring.elasticsearch.host}") private String host; private int port; @Value("${spring.elasticsearch.port}") private void setPort(String str){ if(StringUtils.isNotBlank(str)){ port = Integer.valueOf(str); } } @Value("${spring.elasticsearch.cluster-name}") private String clusterName; @Bean public TransportClient client() throws UnknownHostException { //es集群连接 TransportAddress node = new TransportAddress( InetAddress.getByName(host), port ); //es集群配置(自定义配置) 连接自己安装的集群名称 Settings settings = Settings.builder() .put("cluster.name", clusterName) .build(); PreBuiltTransportClient client = new PreBuiltTransportClient(settings); client.addTransportAddress(node); return client; } }
domain
package com.ruoyi.web.controller.elasticsearchTest.domain; import lombok.Data; import lombok.experimental.Accessors; import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.stereotype.Component; @Component @Data @Accessors(chain = true) @Document(indexName = "student") public class Student { private static final long serialVersionUID = 1L; @Id private String id; private String name; private String gender; private Integer age; }
Impl
package com.ruoyi.web.controller.elasticsearchTest.service; import com.ruoyi.web.controller.elasticsearchTest.domain.Student; import org.springframework.data.elasticsearch.annotations.Query; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import java.util.List; @Component public interface StudentImpl extends ElasticsearchRepository<Student,String> { List<Student> findByNameLike(String keyword); @Query("{\"match_phrase\":{\"name\":\"?0\"}}") List<Student>findByNameCustom(String keyword); }
[ { "name":"张三", "gender":"男", "age":24 }, { "name":"李四", "gender":"女", "age":22 }, { "name":"王五", "gender":"男", "age":33 } ]
调用接口后
查询接口,查询的是张三的信息。