栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Elasticsearch整合-续集-index

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Elasticsearch整合-续集-index

在配置类中继续添加配置
相关配置的官网地址:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.4/java-rest-low-usage-requests.html#java-rest-low-usage-request-options

当前配置类:

@Configuration
public class VmElasticsearchConfig {

    @Bean
    public RestHighLevelClient esRestClient() {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("192.168.23.145", 9200, "http")));
                        //没有集群不用指定多个
//                        new HttpHost("localhost", 9201, "http"))
        return client;
    }

    public static final RequestOptions COMMON_OPTIONS;
    static {
        RequestOptions.Builderbuilder=RequestOptions.DEFAULT.toBuilder();
//        builder.addHeader("Authorization", "Bearer " + TOKEN);
//        builder.setHttpAsyncResponseConsumerFactory(
//                new HttpAsyncResponseConsumerFactory
//                        .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));
        COMMON_OPTIONS = builder.build();
    }
}

测试添加:
文档地址:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.4/java-rest-high-document-index.html
有多种方式,详情见文档

index测试:

  @Test
    public void indexData() throws IOException {
      IndexRequest indexRequest = new IndexRequest("users");
      indexRequest.id("1");//数据的id
//      indexRequest.source("userName","zhangsan","age","18");  第一种方式
        User user = new User();
        user.setUserName("zhangsan");
        user.setAge(18);
        user.setGender("男");
        String jsonString = JSON.toJSONString(user);
        indexRequest.source(jsonString, XContentType.JSON);//数据的内容

        //执行操作
        IndexResponse index = client.index(indexRequest, VmElasticsearchConfig.COMMON_OPTIONS);

        //提取有用的响应数据
        System.out.println(index);
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/860159.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号