使用的是maven项目

对es已经创建过的index,通过updateSettingsRequest修改setting,这个例子是修改

max_result_window的值

pom文件,导入以下依赖:

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.3.0</version>
</dependency>
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-client</artifactId>
    <version>7.3.0</version>
</dependency>
<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>7.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.7.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.12</version>
    <scope>provided</scope>
</dependency>
<!-- elasticsearch依赖2.x的log4j -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.8.2</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.8.2</version>
</dependency>
<!-- junit单元测试 -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>

<!--将对象的json格式转化为字符串-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.62</version>
</dependency>

 代码如下:

public static void main(String[] args) throws Exception {

        //index的名字
        String index = "indexName";

        //获取链接
        HttpHost[] hosts = new HttpHost[3];
        hosts[0] = new HttpHost("host1", 9200, "http");
        hosts[1] = new HttpHost("host2", 9200, "http");
        hosts[2] = new HttpHost("host3", 9200, "http");

        RestHighLevelClient restHighLevelClient = null;

        try {
            //创建restHighLevelClient 对象
            RestClientBuilder builder = RestClient.builder(hosts);
            restHighLevelClient = new RestHighLevelClient(builder);

            //创建Setting对象
            Settings settings = Settings.builder()
                    .put("max_result_window", 20000)//es默认是1000
                    .build();

            //创建UpdateSettingsRequest 
            UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest(settings, index);

            //执行put
            AcknowledgedResponse settingsResult = restHighLevelClient.indices().putSettings(updateSettingsRequest, RequestOptions.DEFAULT);

            //打印返回结果   成功的话,返回结果是true
            System.out.println(settingsResult.isAcknowledged());

        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            restHighLevelClient.close();
        }

    }

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐