elasticsearch
- ElasticSearch是一个基于Lucene的搜索服务器。
- 它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。
- 设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。
开发环境
- jdk1.8.0_111(建议Oracle JDK版本1.8.0_73后)
- org.springframework.boot 1.4.2.RELEASE
- spring-data-elasticsearch-2.0.5.RELEASE.jar
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jege.spring.boot</groupId>
<artifactId>spring-boot-elasticsearch</artifactId>
<version>1.0.0.RELEASE</version>
<packaging>jar</packaging>
<name>spring-boot-elasticsearch</name>
<url>http://blog.csdn.net/je_ge</url>
<developers>
<developer>
<id>je_ge</id>
<name>je_ge</name>
<email>1272434821@qq.com</email>
<url>http://blog.csdn.net/je_ge</url>
<timezone>8</timezone>
</developer>
</developers>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>spring-boot-elasticsearch</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.properties
#存储索引的位置
spring.data.elasticsearch.properties.path.home=target/elastic
#连接超时的时间
spring.data.elasticsearch.properties.transport.tcp.connect_timeout=120s
User
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
/**
* @author JE哥
* @email 1272434821@qq.com
* @description:elasticsearch模型类
*/
@Document(indexName = "user", type = "user", shards = 1, replicas = 0, refreshInterval = "-1")
public class User {
@Id
private String id;
private String name;
private int age;
UserRepository
public interface UserRepository extends ElasticsearchRepository<User, Long>
UserServiceImpl
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
@Override
public void save(User user) {
userRepository.save(user);
}
@Override
public Iterable<User> findAll() {
return userRepository.findAll();
}
}
UserController
@RestController
@RequestMapping(value = "/es/")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/list")
public Iterable<User> list() {
userService.save(new User("je_ge", 33));
return userService.findAll();
}
}
访问流程
- 启动Application
- 访问http://localhost:8080/es/list
- 输出
{"content":[{"id":"AVnQx4i3pXUZjTYvVwwS","name":"je_ge","age":33}],"totalElements":1,"totalPages":1,"last":true,"number":0,"size":1,"sort":null,"numberOfElements":1,"first":true}
其他关联项目
-
Spring Boot 菜鸟教程1-HelloWorld
http://blog.csdn.net/je_ge/article/details/53270821
源码地址
https://github.com/je-ge/spring-boot
如果觉得我的文章或者代码对您有帮助,可以请我喝杯咖啡。
**您的支持将鼓励我继续创作!谢谢! **
支付宝打赏
网友评论