DS-1241 Elastic Search Statistics - Put Mapping/Schema

This commit is contained in:
Peter Dietz
2012-08-10 14:53:03 -04:00
committed by Peter Dietz
parent 59d1970f1c
commit ee6753fcfc

View File

@@ -17,8 +17,9 @@ import org.dspace.statistics.util.SpiderDetector;
import org.elasticsearch.action.ActionFuture; import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.action.admin.indices.exists.IndicesExistsRequest; import org.elasticsearch.action.admin.indices.exists.IndicesExistsRequest;
import org.elasticsearch.action.admin.indices.exists.IndicesExistsResponse; import org.elasticsearch.action.admin.indices.exists.IndicesExistsResponse;
import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.client.action.admin.indices.mapping.put.PutMappingRequestBuilder;
import org.elasticsearch.client.action.index.IndexRequestBuilder; import org.elasticsearch.client.action.index.IndexRequestBuilder;
import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.ImmutableSettings;
@@ -146,10 +147,7 @@ public class ElasticSearchLogger {
.put("cluster.name", clusterName) .put("cluster.name", clusterName)
.build(); .build();
String stringMappingJSON = "{\""+indexType+"\" : { \"properties\" : {\n" +
String stringMappingJSON = "{\n" +
" \"userAgent\":{\n" + " \"userAgent\":{\n" +
" \"type\":\"string\"\n" + " \"type\":\"string\"\n" +
" },\n" + " },\n" +
@@ -228,11 +226,9 @@ public class ElasticSearchLogger {
" \"type\":\"string\",\n" + " \"type\":\"string\",\n" +
" \"index\":\"not_analyzed\"\n" + " \"index\":\"not_analyzed\"\n" +
" }\n" + " }\n" +
"}"; "} } }";
log.info("Mapping for ["+indexName+"]/["+indexType+"]="+stringMappingJSON); client.prepareIndex(indexName, indexType, "1")
IndexResponse response = client.prepareIndex(indexName, indexType, "1")
.setSource(jsonBuilder() .setSource(jsonBuilder()
.startObject() .startObject()
.field("user", "kimchy") .field("user", "kimchy")
@@ -242,24 +238,23 @@ public class ElasticSearchLogger {
) )
.execute() .execute()
.actionGet(); .actionGet();
//.setSettings(settings)
//client.admin().indices().prepareCreate(indexName).addMapping(indexType, stringMappingJSON).execute().actionGet();
log.info("Create INDEX ["+indexName+"]/["+indexType+"]"); log.info("Create INDEX ["+indexName+"]/["+indexType+"]");
// Wait for create to be finished.
client.admin().indices().prepareRefresh(indexName).execute().actionGet();
//PutMappingRequestBuilder putMappingRequestBuilder = client.admin().indices().preparePutMapping(indexName).setType(indexType); //Put the schema/mapping
//putMappingRequestBuilder.setSource(stringMappingJSON); log.info("Put Mapping for ["+indexName+"]/["+indexType+"]="+stringMappingJSON);
//PutMappingResponse response = putMappingRequestBuilder.execute().actionGet(); PutMappingRequestBuilder putMappingRequestBuilder = client.admin().indices().preparePutMapping(indexName).setType(indexType);
putMappingRequestBuilder.setSource(stringMappingJSON);
PutMappingResponse response = putMappingRequestBuilder.execute().actionGet();
//if(!response.getAcknowledged()) { if(!response.getAcknowledged()) {
// log.info("Could not define mapping for type ["+indexName+"]/["+indexType+"]"); log.info("Could not define mapping for type ["+indexName+"]/["+indexType+"]");
//} else { } else {
// log.info("Successfully put mapping for ["+indexName+"]/["+indexType+"]"); log.info("Successfully put mapping for ["+indexName+"]/["+indexType+"]");
//} }
//TODO, need to put the index mapping, i.e. country, city, id, type look at kb-stats-csv-import-elasticsearch/main.php
log.info("DS ES index didn't exist, but we created it."); log.info("DS ES index didn't exist, but we created it.");
} else { } else {
@@ -535,6 +530,11 @@ public class ElasticSearchLogger {
//createElasticClient(true); //createElasticClient(true);
} }
// Get the already available client, otherwise we will create a new client.
// TODO Allow for config to determine which architecture / topology to use.
// - Local Node, store Data
// - Node Client, must discover a master within ES cluster
// - Transport Client, specify IP address of server running ES.
public Client getClient() { public Client getClient() {
if(client == null) { if(client == null) {
log.error("getClient reports null client"); log.error("getClient reports null client");