【ElasticSearch教程】--- Elasticsearch JavaApi 索引操作(十七)

JavaApi 索引操作

RESTful api类似, java api 是采用代码的形式操作索引,也有相同的索引操作。下面我们对api 一一进行测试。

创建索引

步骤:

  1. 创建HttpHost对象,该对象主要用于链接ES服务器的描述,包含了, host, port, schema
  2. 创建RestClientBuilder对象,该对象由RestClient对象的静态方法build创建,需要带入HttpHost。集群应该还可以带入多个HttpHost
  3. 创建关键对象RestHighLevelClient,该对象才是真正链接ES服务发起操作的对象。
  4. 创建CreateIndexRequest对象,该对象是创建索引的请求对象,在7.8.0的版本上需要用org.elasticsearch.client.indices包下的,另外有一个同名的类,应该是过期了。
  5. 发起创建请求,client.indices().create(req, RequestOptions.DEFAULT); ,其中client就是RestHighLevelClient的实例对象。
  6. 请求会响应一个CreateIndexResponse对象。这个时候其实已经完成了创建操作了。
  7. 最后记得关闭链接,client.close();
    代码如下:
package com.maomao.elastic.search.index;

import org.apache.http.HttpHost;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;

import java.io.IOException;

/**
 * @author mao.kaiyin
 */
public class IndexCreate {

    public static void main(String[] args) throws IOException {
        HttpHost host = new HttpHost("127.0.0.1", 9200, "http");
        RestClientBuilder builder = RestClient.builder(host);
        RestHighLevelClient client = new RestHighLevelClient(builder);
        //创建索引
        CreateIndexRequest req = new CreateIndexRequest("teacher");
        CreateIndexResponse res = client.indices().create(req, RequestOptions.DEFAULT);

        boolean acknowledged = res.isAcknowledged();
        System.out.println("索引操作: " + acknowledged);
        client.close();
    }

}

查询索引

查询索引的整体步骤其实和创建类似,差异就是第4,5步。

  1. 第4步创建的对象是org.elasticsearch.client.indices.GetIndexRequest类的对象.
  2. 第5步 不能在是create方法了而是get方法。
  3. 最后请求响应的对象也是GetIndexResponse对象。
    代码如下:
package com.maomao.elastic.search.index;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;

import java.io.IOException;

public class IndexSearch {

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

        HttpHost host = new HttpHost("127.0.0.1", 9200, "http");
        RestClientBuilder builder = RestClient.builder(host);
        RestHighLevelClient client = new RestHighLevelClient(builder);

        GetIndexRequest query = new GetIndexRequest("teacher");
        GetIndexResponse response = client.indices().get(query, RequestOptions.DEFAULT);

        System.out.println(response.getAliases());
        System.out.println(response.getMappings());
        System.out.println(response.getSettings());

        client.close();
    }

}

删除索引

与两者类似,差异也是在请求对象不一样,响应对象不一样,调用方法不一样。核心差异也就是第4,5步。

  1. 第4步创建的对象是org.elasticsearch.client.indices.DeleteIndexRequest类的对象.
  2. 第5步 不能在是create方法了而是get方法。
  3. 最后请求响应的对象也是AcknowledgedResponse对象,响应不是DeleteIndexResponse,那说明应该是后续有一堆的操作响应的内容体是一致的,所以用了一个通用的响应对象AcknowledgedResponse
    代码如下:
package com.maomao.elastic.search.index;

import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;

import java.io.IOException;

public class IndexDelete {

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

        HttpHost host = new HttpHost("127.0.0.1", 9200, "http");
        RestClientBuilder builder = RestClient.builder(host);
        RestHighLevelClient client = new RestHighLevelClient(builder);

        DeleteIndexRequest query = new DeleteIndexRequest("teacher");
        AcknowledgedResponse delete = client.indices().delete(query, RequestOptions.DEFAULT);

        System.out.println("删除索引:" + delete.isAcknowledged());

        client.close();
    }

}

总结

通过上面的操作,可以总结整体操作的内容:

  1. java api的操作流程应该大致都是一致的,需要一个核心的对象RestHighLevelClient
  2. 对索引的操作都是IndicesClient, 它是可以通过RestHighLevelClient对象的indices()方法获取到的。
  3. 请求对象有差异,但都是XXXIndexRequest,除了delete以外,尽量用包org.elasticsearch.client.indices下的。
  4. 响应的内容与restful api一致,都是对应内容的数据结构。所以要获取什么内容可以从json内容中整体查看。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值