ElasticSearch学习 - (六)TransportClient的使用

前提

    /**
     * ElasticSearch客户端
     */
    @Autowired
    private TransportClient client;

索引

判断索引是否存在

public boolean isIndexExist(String index) {
    return client.admin().indices().prepareExists(index).execute().actionGet().isExists();
}

删除索引

public boolean deleteIndex(String index) {
    return isIndexExist(index)
        ? client.admin().indices().prepareDelete(index).execute().actionGet().isAcknowledged() 
        : false;
}

新增索引

public boolean addIndex(String index) {
    return isIndexExist(index) 
        ? false
        : client.admin().indices().prepareCreate(index).execute().actionGet().isAcknowledged();
}

类型

判断inde下指定type是否存在

public boolean isTypeExist(String index, String type) {
    return isIndexExist(index)
        ? client.admin().indices().prepareTypesExists(index).setTypes(type).execute().actionGet().isExists()
        : false;
}

新增类型

public boolean addIndexAndType() throws IOException {
    String index = "ahut";
    String type = "goods";
    // 创建索引映射,相当于创建数据库中的表操作
    CreateIndexRequestBuilder cib = client.admin().indices().prepareCreate(index);
    XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("properties") // 设置自定义字段
        .startObject("goodsName").field("type", "string").endObject() // 商品名称
        .startObject("goodsPrice").field("type", "double").endObject()// 商品价格
        .startObject("goodsUser").field("type", "string").endObject()// 商品主人
        .startObject("goodsTime").field("type", "date").field("format", "yyyy-MM-dd HH:mm:ss").endObject() // 商品上架时间
        .endObject().endObject();
    cib.addMapping(type, mapping);
    return cib.execute().actionGet().isAcknowledged();
}

文档

新增文档

public long addDocument() throws IOException {
    String index = "ahut";
    String type = "goods";
    // 自定义主键id,这个id也可以不要,让es自动为我们生成id
    String id = UUID.randomUUID().toString().replace("-", "");
    // 创建文档,相当于往表里面insert一行数据
    IndexResponse response = client.prepareIndex(index, type, id)
        .setSource(XContentFactory.jsonBuilder().startObject()// 开始
            .field("goodsName", "大学英语")// 商品名称
            .field("goodsPrice", 22.33)// 商品价格
            .field("goodsUser", "大拿")// 商品主人
            .field("goodsTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()))// 商品上架时间
        .endObject())
        .get();
    return response.getVersion();
}

删除文档

public String deleteDocument() {
    String index = "ahut";
    String type = "goods";
    String id = "5b1c93212c2f4d8e88e6bc91de22d08d";
    return client.prepareDelete(index, type, id).get().getId();
}
  •  

查询文档

依据id查询

@Test
public void testSearchById() {
    String index = "ahut";
    String type = "goods";
    String id = "9d3bd69ce77f41ab8b12b165483452f6";
    GetResponse response = client.prepareGet(index, type, id).execute().actionGet();
    String jsonStr = response.getSourceAsString();
    if (jsonStr != null) {
        System.out.println(jsonStr);
    } else {
        System.out.println("没有查到");
    }
}

查询索引下所有数据

@Test
public void matchAllQuery() {
    String index = "ahut";
    QueryBuilder query = QueryBuilders.matchAllQuery();
    SearchResponse response = client.prepareSearch(index).setQuery(query).execute().actionGet();
    for (SearchHit searchHit : response.getHits()) {
        String jsonStr = searchHit.getSourceAsString();
        System.out.println(jsonStr);
    }
}
  •  

查询类型下所有数据

@Test   
public void matchAllQueryInType() {
    String index = "ahut";
    String type = "goods";
    SearchResponse response = client.prepareSearch(index).setTypes(type).execute().actionGet();
    for (SearchHit searchHit : response.getHits()) {
        String jsonStr = searchHit.getSourceAsString();
        System.out.println(jsonStr);
    }
}
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值