1.// 创建client
Settings settings = Settings.builder().put("cluster.name", "my_cluster").build();
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.10"), 9300));
2.// 判断索引是否存在
// 2.1 方式一
IndicesExistsResponse response1 = this.client.admin().indices()
.exists(Requests.indicesExistsRequest("my_index1", "my_index2")).actionGet();
boolean exist1 = response1.isExists();
// 2.1 方式二
IndicesAdminClient indicesAdminClient = this.client.admin().indices();
IndicesExistsResponse response2 = indicesAdminClient.prepareExists("my_index1", "my_index2").get();
boolean exist2 = response2.isExists();
// 2.3 方式三
IndicesExistsRequest indicesExistsRequest = new IndicesExistsRequest("my_index1", "my_index2");
IndicesExistsResponse response3 = this.client.admin().indices()
.exists(indicesExistsRequest).actionGet();
boolean exist3 = response3.isExists();
3.// 创建索引 _setting
// 3.1 创建默认索引 分片个数为5,副本个数为1
public static boolean createSimpleIndex(Client client, String index) {
IndicesAdminClient indicesAdminClient = client.admin().indices();
CreateIndexResponse response = indicesAdminClient.prepareCreate("my_index1").get();
return response.isAcknowledged();
}
// 3.1 创建索引指定setting
public static boolean createIndex(Client client, String index) {
//settings 指定分片个数为3,副本个数为2
Settings settings = Settings.builder()
.put("index.number_of_shards", 3)
.put("index.number_of_replicas", 2).build();
IndicesAdminClient indicesAdminClient = client.admin().indices();
CreateIndexResponse response = indicesAdminClient
.prepareCreate("my_index1")
.setSettings(settings)
.get();
return response.isAcknowledged();
}
4.// 判断文档类型是否存在
// 判断文档类型是否存在
TypesExistsResponse typesExists = this.client.getTransportClient()
.admin().indices()
.prepareTypesExists("my_index1")
.setTypes("my_type1", "my_type2")
.get();
boolean exist = typesExists.isExists();
5.// 创建文档类型 _mapping
/**
* 创建mapping
* @param client
* @throws IOException
*/
public boolean createMapping(TransportClient client, String indexName, String typeName) {
//创建mapping约束字段
PutMappingResponse response = null;
try {
XContentBuilder mapping = XContentFactory.jsonBuilder()
.startObject()
.startObject("properties")
.startObject("title") // 文档字段title
.field("type","text")
.field("analyzer", "ik_max_word")
.field("search_analyzer", "ik_max_word")
.endObject()
.startObject("publishDate") // 文档字段publishDate
.field("type", "date")
.field("format", "yyyy-MM-dd HH:mm:ss")
.endObject()
.startObject("content") // 文档字段content
.field("type","text")
.field("analyzer", "ik_max_word")
.field("search_analyzer", "ik_smart")
.endObject()
.startObject("director") // 文档字段director
.field("type", "keyword")
.endObject()
.startObject("price") // 文档字段price
.field("type", "float")
.endObject()
.endObject()
.endObject();
//添加mapping 绑定到 index
PutMappingRequest putMappingRequest = Requests.putMappingRequest(indexName).type(typeName).source(mapping);
response = client.admin().indices().putMapping(putMappingRequest).actionGet();
} catch (IOException e) {
return false;
}
return response.isAcknowledged();
}