Skip to content

Instantly share code, notes, and snippets.

@dgouyette
Created March 28, 2012 05:14
Show Gist options
  • Save dgouyette/2223825 to your computer and use it in GitHub Desktop.
Save dgouyette/2223825 to your computer and use it in GitHub Desktop.
insertElastic
@Grab('org.fluttercode.datafactory:datafactory:0.8')
@GrabConfig(systemClassLoader=true)
import org.fluttercode.datafactory.impl.DataFactory;
@Grab('org.elasticsearch:elasticsearch:0.19.0')
@GrabConfig(systemClassLoader=true)
import org.elasticsearch.client.Client;
import org.elasticsearch.node.Node;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
println "Insertion d'items dans elasticSearch"
df = new DataFactory()
Node node
Client client
node = nodeBuilder().node()
client = node.client()
start = System.currentTimeMillis()
(1..10000).each{
BulkRequestBuilder bulkRequest = client.prepareBulk();
(1..100).each{
bulkRequest.add(client.prepareIndex("personne", "personnephysique")
.setSource(jsonBuilder().startObject()
.field("id", it)
.field("nom", df.getLastName())
.field("prenom", df.getFirstName())
.field("codePostal", df.getNumberBetween(0, 99999))
.field("dateNaissance", df.getBirthDate())
.field("pays", "FRANCE")
.endObject()
));
}
BulkResponse bulkResponse = bulkRequest.execute().actionGet();
if (bulkResponse.hasFailures()) {
throw new RuntimeException("Des exceptions ont eu lieu : " + bulkResponse.buildFailureMessage());
}
}
println(System.currentTimeMillis() - start+" ms")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment