elasticsearch详解
ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。
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>zsh</id><name>zsh</name><email>1744218382@qq.com</email><url>http://www.baidu.com</url><timezone>6</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;/*** elasticsearch模型类*/
@Document(indexName = "user", type = "user", shards = 1, replicas = 0, refreshInterval = "-1")
public class User {@Idprivate String id;private String name;private int age;
UserRepository
public interface UserRepository extends ElasticsearchRepository<User, Long>
UserServiceImpl
@Service
public class UserServiceImpl implements UserService {@Autowiredprivate UserRepository userRepository;@Overridepublic void save(User user) {userRepository.save(user);}@Overridepublic Iterable<User> findAll() {return userRepository.findAll();}}
UserController
@RestController
@RequestMapping(value = "/es/")
public class UserController {@Autowiredprivate 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":"zsh","age":33}],"totalElements":1,"totalPages":1,"last":true,"number":0,"size":1,"sort":null,"numberOfElements":1,"first":true}
如果感觉不错的话记得点赞哟!!!