Springboot集成Elasticsearch简单教程

一、前言

java操作elasticsearch是作为一个无数据节点与其他节点之间通信,端口是9300.elasticsearch和jdk版本一定要适配,因为elasticsearch是用java编写的,随着版本的升级,用的也是最新版的jdk,所以低版本的jdk就和最新elasticsearch版本不匹配。但是,高版本的jdk可以向下兼容低版本的elasticsearch,因为jdk在升级的过程中,自身也要向下兼容。这一点很重要,否则项目是起不来的。我用的是jdk1.8,elasticsearch-2.4.2。现在,让我们开始集成。


二、注入elasticsearch依赖

  
   <dependency>  
           <groupId>org.elasticsearchgroupId>  
           <artifactId>elasticsearchartifactId>  
             
       dependency>  
       <dependency>  
           <groupId>org.springframework.bootgroupId>  
           <artifactId>spring-boot-starter-data-elasticsearchartifactId>  
       dependency>  

       <dependency>  
           <groupId>org.springframework.datagroupId>  
           <artifactId>spring-data-elasticsearchartifactId>  
       dependency>  
       <dependency>    
           <groupId>com.fasterxml.jackson.coregroupId>    
           <artifactId>jackson-databindartifactId>    
             
     dependency>  
     <dependency>  
           <groupId>net.java.dev.jnagroupId>  
           <artifactId>jnaartifactId>  
             
       dependency>  


三、在application.properties中添加Elasticsearch配置

# elasticsearch  
spring.data.elasticsearch.cluster-name=elasticsearch  #节点名字,默认elasticsearch  
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300  #节点地址,多个节点用逗号隔开  
#spring.data.elasticsearch.local=false  
spring.data.elasticsearch.repositories.enable=true  

四、实体类(注意!id为String类型)

import org.springframework.data.annotation.Id;  
import org.springframework.data.elasticsearch.annotations.Document;  
import org.springframework.data.elasticsearch.annotations.Field;  

//indexName :索引名字(相当于mysql的数据库名字)  
//type:类型(相当于mysql的表名)
@Document(indexName = "megacorp",type = "employee", shards = 1,replicas = 0, refreshInterval = "-1")  
public class Employee {
    @Id
    private String id;
    @Field
    private String firstName;
    @Field
    private String lastName;
    @Field
    private Integer age = 0;
    @Field
    private String about;

    public String getId() {
        return id;
    }

    public void setId(final String id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(final String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(final String lastName) {
        this.lastName = lastName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(final Integer age) {
        this.age = age;
    }

    public String getAbout() {
        return about;
    }

    public void setAbout(final String about) {
        this.about = about;
    }
}

五、编写Dao

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;  
import org.springframework.stereotype.Component;  

import com.example.demo.elasticsearch.entity.Employee;  

@Component  
public interface EmployeeRepository extends ElasticsearchRepository<Employee,String>{  

    Employee queryEmployeeById(String id);  

}  

六、测试代码

import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RestController;  

import com.example.demo.elasticsearch.dao.EmployeeRepository;  
import com.example.demo.elasticsearch.entity.Employee;  
import com.google.gson.Gson;  

@RestController  
@RequestMapping("/es")  
public class ElasticSearchController {  

    @Autowired  
    private EmployeeRepository er;  

    //增加  
    @RequestMapping("/add")  
    public String add(){  

        Employee employee=new Employee();  
        employee.setId("1");  
        employee.setFirstName("xuxu");  
        employee.setLastName("zh");  
        employee.setAge(26);  
        employee.setAbout("i am in peking");  
        er.save(employee);  

        System.err.println("add a obj");  

        return "success";  
    }  

        //删除  
    @RequestMapping("/delete")  
    public String delete(){  

        er.delete("1");  

        return "success";  
    }  

        //局部更新  
    @RequestMapping("/update")  
    public String update(){  

        Employee employee=er.queryEmployeeById("1");  
        employee.setFirstName("哈哈");  
        er.save(employee);  

        System.err.println("update a obj");  

        return "success";  
    }  

        //查询  
    @RequestMapping("/query")  
    public Employee query(){  

        Employee accountInfo=er.queryEmployeeById("1");  
        System.err.println(new Gson().toJson(accountInfo));  

        return accountInfo;  
    }  

}  

非原创。文章转自:https://blog.csdn.net/zhaoyahui_666/article/details/78688688

你可能感兴趣的:(Java)