ElasticSearch添加映射


调用client.admin().indices().putMapping(mapping).get()来完成映射的添加。


@Test
// 映射操作
public void demo5() throws IOException, InterruptedException,
ExecutionException {
// 创建连接搜索服务器对象
Client client = TransportClient
.builder()
.build()
.addTransportAddress(
new InetSocketTransportAddress(InetAddress
.getByName("127.0.0.1"), 9300));

// 添加映射
XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
.startObject("article").startObject("properties")
.startObject("id").field("type", "integer")
.field("store", "yes").endObject().startObject("title")
.field("type", "string").field("store", "yes")
.field("analyzer", "ik").endObject().startObject("content")
.field("type", "string").field("store", "yes")
.field("analyzer", "ik").endObject().endObject().endObject()
.endObject();

PutMappingRequest mapping = Requests.putMappingRequest("blog2")
.type("article").source(builder);
client.admin().indices().putMapping(mapping).get();

// 关闭连接
client.close();
}