Spring Boot 菜鸟教程 20 elasticsearch

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


    4.0.0

    com.jege.spring.boot
    spring-boot-elasticsearch
    1.0.0.RELEASE
    jar

    spring-boot-elasticsearch
    http://blog.csdn.net/je_ge

    
        
            je_ge
            je_ge
            [email protected]
            http://blog.csdn.net/je_ge
            8
        
    

    
        org.springframework.boot
        spring-boot-starter-parent
        1.4.2.RELEASE
        
    

    
        UTF-8
        1.8
    

    

        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-data-elasticsearch
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

    

    
        spring-boot-elasticsearch
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    ${java.version}
                    ${java.version}
                
            
        
    


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 [email protected]
 * @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 

UserServiceImpl

@Service
public class UserServiceImpl implements UserService {
  @Autowired
  private UserRepository userRepository;

  @Override
  public void save(User user) {
    userRepository.save(user);
  }

  @Override
  public Iterable findAll() {
    return userRepository.findAll();
  }

}

UserController

@RestController
@RequestMapping(value = "/es/")
public class UserController {
  @Autowired
  private UserService userService;

  @RequestMapping("/list")
  public Iterable 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

如果觉得我的文章或者代码对您有帮助,可以请我喝杯咖啡。
**您的支持将鼓励我继续创作!谢谢! **

Spring Boot 菜鸟教程 20 elasticsearch_第1张图片
微信打赏

Spring Boot 菜鸟教程 20 elasticsearch_第2张图片
支付宝打赏

你可能感兴趣的:(Spring Boot 菜鸟教程 20 elasticsearch)