springboot es
1
import lombok.Data; import org.springframework.data.elasticsearch.annotations.Document; @Document(indexName = "users", type = "user",useServerConfiguration = true,createIndex = false) @Data public class UserEntity { private String id; private String name; private String sex; private String age; }
2
import org.springframework.data.repository.CrudRepository; public interface UserReposiory extends CrudRepository<UserEntity, String> { }
3
import java.util.Optional; @RestController public class EsController { @Autowired private UserReposiory userReposiory; @RequestMapping("/addUser") public UserEntity addUser(@RequestBody UserEntity user) { return userReposiory.save(user); } @RequestMapping("/findUser") public Optional<UserEntity> findUser(String id) { return userReposiory.findById(id); } @RequestMapping("/findAllUser") public Iterable<UserEntity> findAllUser() { return userReposiory.findAll(); } @RequestMapping("/deleteUser") public String deltestr(String id){ userReposiory.deleteById(id); return "1"; } }
4
server.port=8092
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
5
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/transport --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies>
@SpringBootApplication
@EnableElasticsearchRepositories(basePackages = "com.sys.esdemo.es")
public class EsdemoApplication {
public static void main(String[] args) {
SpringApplication.run(EsdemoApplication.class, args);
}
}