java 操作elasticsearch

java操作elasticsearch 端口是9300. elasticsearch和jdk版本一定要适配

因为elasticsearch是用java编写的,随着版本的升级,用的也是最新版的jdk,所以低版本的jdk就和最新elasticsearch版本不匹配。

但是,高版本的jdk可以向下兼容低版本的elasticsearch,因为jdk在升级的过程中,自身也要向下兼容。这一点很重要,否则项目是起不来的。

我用的是jdk1.8,elasticsearch-2.4.2

 

一、注入elasticsearch依赖

<!-- elasticsearch -->
    <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <!-- <version>6.0.0</version> -->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
         
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
        </dependency>
        <dependency>  
            <groupId>com.fasterxml.jackson.core</groupId>  
            <artifactId>jackson-databind</artifactId>  
            <!-- <version>2.1.3</version>   -->
         </dependency>
         <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna</artifactId>
            <!-- <version>3.0.9</version> -->
        </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

 

三、实体类

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

import cn.com.connext.utils.RandomUtil;


@Document(indexName = "syslog",shards = 1,replicas = 0, refreshInterval = "-1")
public class SystemLog {
    @Id
    private String id;
    @Field
    private String openId;
    @Field
    private String type;

    @Field
    private String detail;
    @Field
    private Date creatTime;

 

  1. // indexName :索引名字(对应mysql的数据库名字)
  2.  
    //type:类型(对应mysql的表名)

 

四、编写dao

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

import cn.com.connext.entity.SystemLog;
 
 
@Component
public interface SystemLogRepository extends ElasticsearchRepository <SystemLog,String>{
    
    SystemLog querySystemLogById(String id);

}

 

五、接口测试

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

import cn.com.connext.dao.SystemLogRepository;
import cn.com.connext.entity.SystemLog;

@RestController
@RequestMapping("/es")
public class ElasticSearchController {
    
    @Autowired
    private SystemLogRepository er;
    
    // 增加
    @RequestMapping("/add")
    public String add() {

        er.save(SystemLog.getSystemLog("123", "课程管理","购买课程"));

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

        return "success";
    }

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

        er.delete("1");

        return "success";
    }

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

        SystemLog employee = er.querySystemLogById("1");
        er.save(employee);

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

        return "success";
    }

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

        SystemLog accountInfo = er.querySystemLogById("1");

        return accountInfo;
    }

}

 

posted @ 2018-08-10 16:01  lyon♪♫  阅读(251)  评论(0编辑  收藏  举报