狐狸情 发表于 2017-5-21 10:41:08

ElasticSearch (3) Java API -- put mapping to index

  // make sure the index existing before putting the mapping

Map map = client.admin().cluster()
.health(new ClusterHealthRequest(indexName)).actionGet()
.getIndices();
// map is always empty!!??
boolean exists = map.containsKey(indexName);
if (!exists) {
client.admin().indices()
.create(new CreateIndexRequest(indexName)).actionGet();
// waitForYellow
client.admin()
.cluster()
.health(new ClusterHealthRequest(indexName)
.waitForYellowStatus()).actionGet();
}
XContentBuilder mapping = XContentFactory.jsonBuilder().startObject()
.startObject("settings").startObject("_source")
.field("enabled", false).endObject()
.startObject("properties")
.startObject("hostname").field("type", "string").field("store", "no").field("index","not_analyzed").endObject()
.startObject("logtype").field("type", "string").field("store", "no").field("index","not_analyzed").endObject()
.startObject("env").field("type", "string").field("store", "no").field("index","not_analyzed").endObject()
.startObject("logpath").field("type", "string").field("store", "no").field("index","not_analyzed").endObject()
.startObject("logfilename").field("type", "string").field("store", "no").field("index","not_analyzed").endObject()
.startObject("timestamp").field("type", "long").field("store", "no").field("index","not_analyzed").endObject()
.startObject("eventbody").field("type", "string").field("store", "no").endObject()
.endObject()
.endObject()
.endObject();
PutMappingRequest putMappingRequest = new PutMappingRequest(
indexName);
putMappingRequest.type(indexName);
putMappingRequest.source(mapping);
PutMappingResponse putMappingResponse = client.admin().indices()
.putMapping(putMappingRequest).actionGet();
putMappingResponse.acknowledged();
PutMappingRequestBuilder pmrb = client.admin().indices()
.preparePutMapping(indexName).setType(indexName);
pmrb.setSource(XContentFactory.jsonBuilder());
long startTime = System.currentTimeMillis();
while ((sCurrentLine = br.readLine()) != null) {
rowkey = indexName + ":" + timeStamp + ":" + timeStamp + ":"
+ ESserver;
bulkRequest.add(client.prepareIndex(indexName, indexName, rowkey)
.setSource(
jsonBuilder().startObject()
.field("hostname", testCaseMetaInfo.getHostName())
.field("logtype", testCaseMetaInfo.getLogType())
.field("env", testCaseMetaInfo.getEnv())
.field("logpath", testCaseMetaInfo.getEnv())
.field("logfilename", testCaseMetaInfo.getLogFileName())
.field("timestamp", timeStamp)
.field("eventbody", sCurrentLine)
.endObject()));
if (i % 100 == 0) {
bulkRequest.execute().actionGet();
bulkRequest = client.prepareBulk();
System.out.println(i);
}
i++;
timeStamp++;
}
页: [1]
查看完整版本: ElasticSearch (3) Java API -- put mapping to index