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);
    }
}

你可能感兴趣的:(ElasticSearch)