如何使用ES的Java API来创建一个新类型的索引

问题描述 投票:0回答:2

我已经成功创建索引的使用客户,这样的代码:

public static boolean addIndex(Client client,String index) throws Exception {
    if(client == null){
        client = getSettingClient();
    }
    CreateIndexRequestBuilder requestBuilder = client.admin().indices().prepareCreate(index);
    CreateIndexResponse response = requestBuilder.execute().actionGet();
    return response.isAcknowledged();  
    //client.close();
}

public static boolean addIndexType(Client client, String index, String type) throws Exception {
    if (client == null) {
        client = getSettingClient();
    }
    TypesExistsAction action = TypesExistsAction.INSTANCE;
    TypesExistsRequestBuilder requestBuilder = new TypesExistsRequestBuilder(client, action, index);
    requestBuilder.setTypes(type);
    TypesExistsResponse response = requestBuilder.get();
    return response.isExists();
}

然而,addIndex类型的方法不受影响,类型不创建。

我不知道如何创建类型?

elasticsearch
2个回答
0
投票

当你通过提供适当的映射配置创建索引您可以创建类型。另外一种被当指数形成一定类型的文档创建。然而,第一个建议是最好的一个,因为这样你可以控制的,而不是依靠动态映射这种类型的完整映射。


0
投票

您可以在下面的方法类型:

// JSON schema is the JSON mapping which you want to give for the index.

JSONObject builder = new JSONObject().put(type, JSONSchema);

// And then just fire the below command

client.admin().indices().preparePutMapping(indexName)
                        .setType(type)
                        .setSource(builder.toString(), XContentType.JSON)
                        .execute()
                        .actionGet();
© www.soinside.com 2019 - 2024. All rights reserved.