1督惰、Code -> GitHub
https://github.com/liufengji/elasticsearch_api.git
當(dāng)直接在ElasticSearch建立文檔對(duì)象時(shí)麸折,如果索引不存在的为流,默認(rèn)會(huì)自動(dòng)創(chuàng)建,映射采用默認(rèn)方式渗鬼。
ElasticSearch服務(wù)默認(rèn)端口9300
Web管理平臺(tái)端口9200
2、源代碼
@Test
public void createIndexByJson() throws UnknownHostException {
// 1 文檔數(shù)據(jù)準(zhǔn)備
String json = "{" + "\"id\":\"1\"," + "\"title\":\"基于Lucene的搜索服務(wù)器\","
+ "\"content\":\"它提供了一個(gè)分布式多用戶能力的全文搜索引擎,基于RESTful web接口\"" + "}";
// 2 創(chuàng)建文檔
IndexResponse indexResponse = client.prepareIndex("blog",
"article", "1")
.setSource(json)
.execute().actionGet();
// 3 打印返回的結(jié)果
System.out.println("index:" + indexResponse.getIndex());
System.out.println("type:" + indexResponse.getType());
System.out.println("id:" + indexResponse.getId());
System.out.println("version:" + indexResponse.getVersion());
System.out.println("result:" + indexResponse.getResult());
// 4 關(guān)閉連接
client.close();
}