elasticsearch JAVA-API 解释说明

Posted 代码凯乐

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了elasticsearch JAVA-API 解释说明相关的知识,希望对你有一定的参考价值。

1.使用maven项目 在pom文件中添加pom依赖

<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>7.17.5</version>
</dependency>
<!-- elasticsearch的客户端 -->
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.17.5</version>
</dependency>
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-client</artifactId>
    <version>7.17.5</version>
</dependency>
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>x-pack-transport</artifactId>
    <version>7.17.5</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.9</version>
</dependency>

       

2.java连接elasticsearch有用户名、密码的客户端

private String[] url="http://127.0.0.1:9200";

private RestHighLevelClient client;

private String userName="elastic";

private String password="123456";

final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));

final HttpHost[] httpHosts = Arrays.stream(url).map(HttpHost::create).toArray(HttpHost[]::new);
client = new RestHighLevelClient(RestClient.builder(httpHosts)
        .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() 
            @Override
            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) 
                httpClientBuilder.disableAuthCaching();
                return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
            
        ));
System.out.println("client:" + client);

client.close();

httpClientBuilder.disableAuthCaching() 作用:— 禁用抢占式身份验证

3.创建索引前先判断要创建的索引是否存在

GetIndexRequest request = new GetIndexRequest("user");
Boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println("user索引是否存在 = " + exists);

4.创建索引以及对应的属性,这边的属性就是mapping、settings

// 创建Request对象, 准备创建的索引名为user
CreateIndexRequest request = new CreateIndexRequest("user");
// 设置Request参数
request.settings(Settings.builder()
        .put("auto_expand_replicas", "0-all")
        .put("max_result_window","1000000")
        .put("max_inner_result_window","1000000"));
// 通过JSON字符串的方式,设置ES索引结构的mapping
request.mapping(
        "\\n" +
                "  \\"properties\\": \\n" +
                "    \\"message\\": \\n" +
                "      \\"type\\": \\"text\\"\\n" +
                "    \\n" +
                "  \\n" +
                "", XContentType.JSON);
//返回Response对象
CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
// 响应状态
boolean acknowledged = createIndexResponse.isAcknowledged();
System.out.println("user索引操作 :" + acknowledged);

auto_expand_replicas的作用:根据集群的个数来自定义分片和副本分片数

max_result_window的作用:数据返回的最大窗口数

max_inner_result_window的作用:内联数据返回的最大窗口数

DSL语句:

PUT /index

  "settings": 
    "index": 
      "auto_expand_replicas": "0-all",
      "max_result_window": "1000000000",
      "max_inner_result_window": "1000000000"
    
  ,
  "mappings": 
    "properties": 
      "message": 
        "type": "text"
      
    
  

Kibana展示:

5.查询索引详细信息

// 查询索引User
GetIndexRequest request = new GetIndexRequest("user");
// 查询所有索引
//GetIndexRequest request = new GetIndexRequest("*");
GetIndexResponse getIndexResponse = client.indices().get(request, RequestOptions.DEFAULT);
// 响应状态
System.out.println(getIndexResponse.getAliases());//别名
System.out.println(getIndexResponse.getMappings());//mappings
System.out.println(getIndexResponse.getSettings());//settings

DSL语句:

GET /index

Kibana展示:

6.删除索引

//查询索引
DeleteIndexRequest request = new DeleteIndexRequest("user");
//返回Response对象
AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
// 响应状态
System.out.println("user索引操作 :"+response.isAcknowledged());

DSL语句:

DELETE /index

Kibana展示:

 

以上是关于elasticsearch JAVA-API 解释说明的主要内容,如果未能解决你的问题,请参考以下文章

ElasticSearch Java Api -创建索引

使用java如何操作elasticsearch?简单示例。

04-springboot整合elasticsearch初识-简单增删改查及复杂排序,分页,聚合操作

ElasticSearch04_elasticsearch-Rest-Client整合SpringBoot中使用保存数据利用JAVA代码操作es

ElasticSearch04_elasticsearch-Rest-Client整合SpringBoot中使用保存数据利用JAVA代码操作es

ElasticSearch04_elasticsearch-Rest-Client整合SpringBoot中使用保存数据利用JAVA代码操作es