Elastic Search Java Api 创建索引结构,添加索引

本文详细介绍了如何使用Java创建一个Elasticsearch客户端,创建索引、定义索引结构,并添加索引数据的过程。
创建TCP客户端
Client client = new TransportClient()
             .addTransportAddress(new InetSocketTransportAddress(
                     "localhost", 9300));
创建索引
	client.admin().indices().prepareCreate("pages").execute().actionGet();
创建索引结构
	XContentBuilder builder=XContentFactory
			.jsonBuilder()
			.startObject()
			    .startObject("sina")
				.startObject("properties")
					.startObject("article_title")
						.field("type", "string")
						.field("store", "yes")
						.field("analyzer","ik")
						.field("index","analyzed")
					.endObject()
					.startObject("article_content")
						.field("type", "string")
						.field("store", "no")
						.field("analyzer","ik")
						.field("index","analyzed")
					.endObject()
					.startObject("article_url")
						.field("type", "string")
						.field("store", "yes")
						.field("index","not_analyzed")
					.endObject()
				.endObject()
			.endObject()
		.endObject();
PutMappingRequest mapping = Requests.putMappingRequest("pages").type("sina").source(builder);
client.admin().indices().putMapping(mapping).actionGet();
添加索引数据		
IndexResponse response = client.prepareIndex("pages", "sina", null)
		.setSource(jsonBuilder()
			.startObject()
			      .field("article_title", Bytes.toString(r.getValue("article".getBytes(), "title".getBytes())))
			       .field("article_content", Bytes.toString(r.getValue("article".getBytes(), "content".getBytes())))
			        .field("article_url", Bytes.toString(r.getValue("article".getBytes(), "url".getBytes())))
			   .endObject()
			   )
		.execute()
		.actionGet();
			
client.close();



转载于:https://my.oschina.net/congqian/blog/383463

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值