ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

springboot 集成 Elasticsearch7.2.0 ,head 插件

2019-07-12 18:36:09  阅读:565  来源: 互联网

标签:插件 return Elasticsearch7.2 head springframework client elasticsearch org import


1,下载 Elasticsearch

官网下载地址 https://www.elastic.co/cn/downloads/elasticsearch 作者这里下载的是7.2.0 ,如果想下载之前版本 可以点击如下图下载:
在这里插入图片描述
在这里插入图片描述

2,启动Elasticsearch

1>进入bin目录点击 elasticsearch.bat 启动 Elasticsearch
访问 http://127.0.0.1:9200/ 可以看到如下图信息:
在这里插入图片描述

3,在springboot pom.xm 中引入依赖包

    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>7.2.0</version>
    </dependency>
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>transport</artifactId>
        <version>7.2.0</version>
    </dependency>

4,在application.yml 配置属性文件

spring:
  	es:
      host: 127.0.0.1
      port: 9300

5,注入 加载配置文件 并且注入 TransportClient

import lombok.Data;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Data
@Component
@ConfigurationProperties(prefix = "spring.es")
public class ElasticsearchConfig {
    private String host;
    private Integer port;

    @Bean
    public TransportClient client() throws UnknownHostException {
        // 设置es节点的配置信息
        Settings settings = Settings.builder()
                .put("cluster.name", "myClusterName")
                .build();
        // 实例化es的客户端对象
        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName(host), port));

        return client;
    }
}

6,新增(index)查看接口实现(get)

  import io.swagger.annotations.Api;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.Date;
@RestController
@RequestMapping("/es")
public class ElasticsearchController {
    @Autowired
    private TransportClient client;
    /**
     * 添加书籍数据         
     * @param title       书籍标题
     * @param author      书籍作者
     * @param wordCount   书籍字数
     * @param publishDate 发行时间
     * @return
     */
    @PostMapping("/add/book/novel")
    public ResponseEntity add(@RequestParam("title") String title,
                              @RequestParam("author") String author,
                              @RequestParam("word_count") int wordCount,
                              @RequestParam("publish_date")
                              @DateTimeFormat(pattern = "yyy-MM-dd HH:mm:ss")
                                      Date publishDate)  {
        try {
            // 将参数build成一个json对象
            XContentBuilder content = XContentFactory.jsonBuilder()
                    .startObject()
                    .field("title", title)
                    .field("author", author)
                    .field("word_count", wordCount)
                    .field("publish_date", publishDate.getTime())
                    .endObject();
            IndexResponse response = client.prepareIndex("books", "book","1")
                    .setSource(content)
                    .get();
            return new ResponseEntity(response.getId(), HttpStatus.OK);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }finally {
            client.close();
        }
    }
     /**
     * 按id查询
     * @param id
     * @return
     */
    @GetMapping("/get/book/novel")
    public ResponseEntity searchById(@RequestParam("id") String id) {
        if (id.isEmpty()) {
            return new ResponseEntity(HttpStatus.NOT_FOUND);
        }
        // 通过索引、类型、id向es进行查询数据
        GetResponse response = client.prepareGet("books", "book", id).get();
        if (!response.isExists()) {
            return new ResponseEntity(HttpStatus.NOT_FOUND);
        }
        // 返回查询到的数据
        return new ResponseEntity(response.getSource(), HttpStatus.OK);
    }
}

作者这里集成了swagger 所以结果如下:
在这里插入图片描述
修改删除等:可以参考官方API: 官方Api地址:https://www.elastic.co/guide/en/elasticsearch/client/java-api/7.3/index.html
在这里插入图片描述

7,head插件

参考:https://www.cnblogs.com/hts-technology/p/8477258.html
最后作者成功集成附图:
在这里插入图片描述

标签:插件,return,Elasticsearch7.2,head,springframework,client,elasticsearch,org,import
来源: https://blog.csdn.net/qq_22696519/article/details/95639666

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有