首页 > 其他分享 >Elasticsearch SpringBoot 整合 ES 测试保存

Elasticsearch SpringBoot 整合 ES 测试保存

时间:2022-10-18 23:14:20浏览次数:95  
标签:index SpringBoot client elasticsearch org import RestHighLevelClient Elasticsear

一、修改Config

官方文档:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.4/java-rest-low-usage-requests.html#java-rest-low-usage-request-options

package com.mangoubiubiu.search.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.HttpAsyncResponseConsumerFactory;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class SearchConfig {

    /**
     * 通用设置项 基于默认规则加的设置
     */
    public static final RequestOptions COMMON_OPTIONS;
    static {
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
//        builder.addHeader("Authorization", "Bearer " + TOKEN);
//        builder.setHttpAsyncResponseConsumerFactory(
//                new HttpAsyncResponseConsumerFactory
//                        .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));
        COMMON_OPTIONS = builder.build();
    }

    @Bean
    public RestHighLevelClient esRestClient(){
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("192.168.56.10", 9200, "http")));

        return client;
    }

}

二、Junit 测试保存

官方文档:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.4/java-rest-high-document-index.html

package com.atguigu.gulimall.search;

import com.alibaba.fastjson.JSON;
import com.atguigu.gulimall.search.config.SearchConfig;
import lombok.Data;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;


@RunWith(SpringRunner.class)
@SpringBootTest
public class GulimallSearchApplicationTests {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Test
   public void contextLoads() {
        System.out.println(restHighLevelClient);
    }

    /**
     * 测试存储数据到ES
     * 保存更新
     */
    @Test
    public void indexData() throws IOException {
        IndexRequest request = new IndexRequest("users");
        request.id("1"); //数据id
//        request.source("userName","zhangsan","age",18,"gender","男");
        User user = new User();
        user.setUserName("张三");
        user.setAge(18);
        user.setGender("男");
        String jsonString = JSON.toJSONString(user);
        request.source(jsonString, XContentType.JSON); //要保存的内容

        //执行保存操作
        IndexResponse index = restHighLevelClient.index(request, SearchConfig.COMMON_OPTIONS);

        //提前有用的响应的数据

        System.out.println(index);
    }

    @Data
    class User{
        private String userName;
        private String gender;
        private Integer age;


    }


}

有数据,保存成功

 

 

 

 

 

标签:index,SpringBoot,client,elasticsearch,org,import,RestHighLevelClient,Elasticsear
From: https://www.cnblogs.com/mangoubiubiu/p/16804554.html

相关文章

  • Divide by Zero 2021 and Codeforces Round #714 C
    C.AddOne显然对于每一位单独分析我们经过一次进位只能变成10这样该怎么做呢我们显然可以dp设dp[i][j]表示i(0-9)经过j次变换有几位显然我们初始化i+j<10就是1elsed......
  • Module not found: Error: Can't resolve 'pubsub-js'
    包未安装或者包版本过新,再者安装位置有误。我安装各种工具库或者其他包时有个 -g 到全局的习惯,觉得装到全局时在文件夹中何时何处都可以用。当文件夹嵌套较多,......
  • SpringBoot
    了解SpringBoot:https://blog.csdn.net/Delia_theme/article/details/88681722学习文档:http://c.biancheng.net/spring_boot/example.htmlspringboot狂神笔记:https://blog......
  • Elasticsearch SpringBoot 整合 ES
    一.ES的JavaAPI两种方式  Elasticsearch的API分为RESTClientAPI(http请求形式)以及transportClientAPI两种。相比来说transportClientAPI效率更高,transportCli......
  • Elasticsearch 缓存深入详解
    手敲脑图串讲Elasticsearch核心知识点1、Elasticsearch缓存引出Elasticsearch查询的响应需要占用CPU、内存资源,在复杂业务场景,会出现慢查询,需要花费大量的时间。如......
  • 基于儿童积木玩具图解 Elasticsearch 聚合
    手敲脑图串讲Elasticsearch核心知识点故事得从这一筐积木说起......周末带孩子正准备玩积木的时候,手机响了,死磕Elasticsearch技术群里在探讨Elastic认证中聚合考点......
  • 大事!!Elasticsearch 和 Kibana 换开源协议了......
      Elastic社区的一大事件,必须以官方文档公布为准:​​https://www.elastic.co/cn/blog/licensing-change​​核心点翻译梳理如下:1、作者是:ShayBanon,ElasticCEO。标题......
  • Elasticsearch 中为什么会有大量文档插入后变成 deleted?
    Elasticsearch最少必要知识实战教程直播回放1、从两个实战问题开始....问题1:elasticsearch-head插件索引文档数显示结果不一致?一个:3429,一个:5291,什么意思?问题2:批量写入......
  • Elasticsearch 聚合性能优化六大猛招
    Elasticsearch最少必要知识实战教程直播回放1、问题引出默认情况下,Elasticsearch已针对大多数用例进行了优化,确保在写入性能和查询性能之间取得平衡。我们将介绍一些聚......
  • Elasticsearch 如何实现类主流搜索引擎广告置顶显示效果?
    1、需求私信问题:Elasticsearch如何实现类似百度广告置顶显示给定商品数据的效果?置顶显示某特定数据就是:搜索某关键词,出现关联广告置顶显示的效果。举例:百度搜索“电动汽车......