springboot 链接elasticsearch

链接方式参考官网

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-getting-started.html

传统的方式是JEST。但是我建议用官网的API比较方便

建立一个congif类

package com.test.es.yang.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: 杨丰源
 * @date: 2021-06-29 14:48
 * @description:
 */
@Configuration
public class ESConfiguration {
    @Value("${elasticsearch.hostname}")
    String hostname;
    @Value("${elasticsearch.port}")
    int port;
    @Bean
    public RestHighLevelClient restHighLevelClient() {
        // 如果有多个从节点可以持续在内部new多个HttpHost,参数1是IP,参数2是端口,参数3是通信协议
        return new RestHighLevelClient(RestClient.builder(new HttpHost(hostname, port, "http")));
    }

}

这里配置文件的配置如下

elasticsearch:
  hostname: localhost
  port: 9200

建立接口:

IRestHighLevelClientService 实现创建删除索引
package com.test.es.yang.service;


import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.indices.CreateIndexResponse;

import java.io.IOException;

/**
 * @author: 杨丰源
 * @date: 2021-06-29 16:00
 * @description:
 */
public interface IRestHighLevelClientService {
     CreateIndexResponse createIndex(String indexName) throws IOException;
     AcknowledgedResponse deleteIndex(String indexName) throws IOException;
}

建立service实现类实现接口

package com.test.es.yang.service.impl;

import com.test.es.yang.service.IRestHighLevelClientService;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;

import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author: 杨丰源
 * @date: 2021-06-29 16:05
 * @description:
 */

@Service
public class RestHighLevelClientServiceImpl implements IRestHighLevelClientService {
    @Autowired
    private RestHighLevelClient restHighLevelClient;
    @Override
    public CreateIndexResponse createIndex(String indexName) throws IOException {
        CreateIndexRequest request = new CreateIndexRequest(indexName.toLowerCase());
        request.settings(Settings.builder()
                .put("index.number_of_shards", 5)
                .put("index.number_of_replicas", 0)
        );
        CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
        return createIndexResponse;
    }

    @Override
    public AcknowledgedResponse deleteIndex(String indexName) throws IOException {
        DeleteIndexRequest indexRequest = new DeleteIndexRequest(indexName);
        AcknowledgedResponse delete = restHighLevelClient.indices().delete(indexRequest, RequestOptions.DEFAULT);
        return delete;

    }
}

建立controller

package com.test.es.yang.controller;

import com.test.es.yang.service.IRestHighLevelClientService;
import common.CommonResult;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @author: 杨丰源
 * @date: 2021-06-29 15:58
 * @description:
 */
@RestController
@RequestMapping("/es")
public class ESController {
    @Autowired
    private IRestHighLevelClientService restHighLevelClientService;
    /**
     * 创建索引
     * @return
     * @throws IOException
     */
    @PostMapping("/createIndex")
    public CommonResult createIndex(@RequestParam String indexName) throws IOException {

        CreateIndexResponse index = restHighLevelClientService.createIndex(indexName);
        String success="1";
        return new CommonResult(200,"success",index);
    }

    /**
     * 删除索引
     * @return
     * @throws IOException
     */
    @PostMapping("/deleteIndex")
    public CommonResult deleteIndex(@RequestParam String indexName) throws IOException {

        AcknowledgedResponse acknowledgedResponse = restHighLevelClientService.deleteIndex(indexName);
        return new CommonResult(200,"success",acknowledgedResponse);
    }

}

最简单的建立索引搞定

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值