【ES】Elasticsearch-7.4.1-RestHighLevelClient

目录

          • 依赖
          • 初始化连接
          • 总览
          • 查询
          • 创建索引,并新增一条
          • 批量插入数据
          • 查询多个ID的数据
          • 根据id修改数据:
          • 跟据id查询一条

千万级数据在PostgresSQL数据库中,查询有点慢,换了Elasticsearch,搜索速度很快

SpringCloud版本固定,只能适配,所以采用RestHighLevelClient

关闭连接:

restHighLevelClient.close();
依赖
<dependency>
    <groupId>org.elasticsearch.clientgroupId>
    <artifactId>elasticsearch-rest-high-level-clientartifactId>
    <version>7.4.1version>
dependency>

<properties>
	<elasticsearch.version>7.4.1elasticsearch.version>
properties>
初始化连接
@Configuration
public class EsConfig {
   

    public static final RequestOptions COMMON_OPTIONS;

    static {
   
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
        COMMON_OPTIONS = builder.build();
    }

    @Bean
    public RestHighLevelClient esClient() {
   
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(
                new HttpHost("ip1", 9200, "http"),
                // 集群
                new HttpHost("ip1", 9201, "http")
        ));
        return restHighLevelClient;
    }

    @Bean
    public RestHighLevelClient esClient2() {
   
        RestClientBuilder builder = RestClient.builder(
                new HttpHost("ip1", 9200, "http"),
                new HttpHost("ip2", 9200, "http"),
                new HttpHost("ip3", 9200, "http"));
        // 这里就是不一样的
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("username", "password"));
        // 用户名和密码传入的地方
        builder.setHttpClientConfigCallback(f -> f.setDefaultCredentialsProvider(credentialsProvider));
        return new RestHighLevelClient(builder);
    }

}
总览
//增, source 里对象创建方式可以是JSON字符串,或者Map,或者XContentBuilder 对象
IndexRequest indexRequest = new 

你可能感兴趣的:(Elasticsearch,java,数据库,elasticsearch)