RestHighLevelClient使用记录

RestHighLevelClient使用记录

1 SpringBoot整合

1.1 maven依赖

<dependency>
   <groupId>org.elasticsearch.client</groupId>
   <artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>

1.2 注册Bean

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

import java.util.ArrayList;
import java.util.List;

@Configuration
public class ElasticSearchConfig {
    private static final Logger logger = LoggerFactory.getLogger(ElasticSearchConfig.class);

    @Value("${elasticsearch.cloud.hosts}")
    private String cloudHosts;

    @Bean(name = "restHighLevelClient")
    public RestHighLevelClient restHighLevelClient() {
        logger.info(">> ElasticSearch # init ...");
        RestHighLevelClient restHighLevelClient = null;
        try {
            String[] hosts = cloudHosts.split(",");
            List<HttpHost> httpHosts = new ArrayList<>();
            for (String item : hosts){
                httpHosts.add(new HttpHost(item.split(":")[0], Integer.parseInt(item.split(":")[1]), "http"));
            }
            HttpHost[] clients = httpHosts.toArray(new HttpHost[httpHosts.size()]);
            // 配置信息
            RestClientBuilder restClientBuilder = RestClient.builder(clients);
            restHighLevelClient = new RestHighLevelClient(restClientBuilder);
        } catch (Exception e) {
            logger.error(">> ElasticSearch # TransportClient create error!", e);
            System.exit(1);
        }
        logger.info(">> ElasticSearch # init finished.");
        return restHighLevelClient;
    }
}

2 使用

2.1 新增

BulkRequest bulkRequest = new BulkRequest();
bulkRequest.add(new IndexRequest(index).id(id).source(JSON.toJSONString(author, serializeConfig), XContentType.JSON));
BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);

2.2 整体更新

UpdateRequest updateRequest = new UpdateRequest(index, id);
updateRequest.doc(JSON.toJSONString(author, serializeConfig), XContentType.JSON);
UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);

2.3 局部更新

// 构建更新参数
Map<String, Object> params = new HashMap<>();
params.put("analysis", JSON.toJSON(response.getResults()));

UpdateByQueryRequest request = new UpdateByQueryRequest(index);
// 更新条件
request.setQuery(new TermQueryBuilder("mid", mid));
// 最大更新文档树
request.setMaxDocs(1);
// 更新字段,多个用;隔开
request.setScript(new Script(ScriptType.INLINE, "painless", "ctx._source.analysis = params.analysis", params)));
BulkByScrollResponse response = restHighLevelClient.updateByQuery(request, RequestOptions.DEFAULT);
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值