spring boot集成elasticsearch并实现简单的增删改查

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

java操作elasticsearch是作为一个无数据节点与其他节点之间通信,因此使用的是tcp端口,elasticsearch默认的节点间通信的tcp端口是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#节点名字,默认elasticsearchspring.data.elasticsearch.cluster-name=elasticsearch#节点地址,多个节点用逗号隔开spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300#spring.data.elasticsearch.local=falsespring.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; // 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;    //get、set...}

四、编写dao

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;import org.springframework.stereotype.Component; import com.example.demo.elasticsearch.entity.Employee; @Componentpublic interface EmployeeRepository extends ElasticsearchRepository<Employee,String>{  Employee queryEmployeeById(String id); }

五、由于咱们就是入门测试,service我就省略了,直接书写接口了

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; } }

至此,elasticsearch就算是入门了。

参考文献:https://blog.csdn.net/zhaoyahui_666/article/details/78688688

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值