Skip to content

Instantly share code, notes, and snippets.

@kimchy
Created April 27, 2012 21:20

Revisions

  1. kimchy created this gist Apr 27, 2012.
    93 changes: 93 additions & 0 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,93 @@
    import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
    import org.elasticsearch.action.bulk.BulkRequestBuilder;
    import org.elasticsearch.action.bulk.BulkResponse;
    import org.elasticsearch.action.search.SearchRequestBuilder;
    import org.elasticsearch.action.search.SearchResponse;
    import org.elasticsearch.client.Requests;
    import org.elasticsearch.client.transport.TransportClient;
    import org.elasticsearch.common.settings.ImmutableSettings;
    import org.elasticsearch.common.settings.Settings;
    import org.elasticsearch.common.transport.InetSocketTransportAddress;
    import org.elasticsearch.common.xcontent.XContentBuilder;
    import org.elasticsearch.common.xcontent.XContentFactory;
    import org.elasticsearch.index.query.QueryBuilders;
    import org.elasticsearch.index.query.QueryStringQueryBuilder;
    import org.elasticsearch.node.Node;
    import org.elasticsearch.node.NodeBuilder;
    import org.elasticsearch.search.SearchHits;

    import java.io.IOException;

    public class EsTestCase {

    public static final String INDEX_NAME = "slowindex";

    public static void main(String[] args) {
    Node node = NodeBuilder.nodeBuilder().node();
    Settings settings = ImmutableSettings
    .settingsBuilder()
    // setting the timeout to a higher number works.
    //.put("client.transport.ping_timeout", "100s")
    .build();

    TransportClient transportClient =
    new TransportClient(settings)
    .addTransportAddress(new InetSocketTransportAddress("localhost", 9300));

    for (int i = 0; i < 10; i++) {
    indexABigDocument(transportClient);
    }

    transportClient.admin().indices().refresh(new RefreshRequest(INDEX_NAME)).actionGet();


    SearchRequestBuilder builder = transportClient.prepareSearch(INDEX_NAME);
    QueryStringQueryBuilder value = QueryBuilders.queryString("John");

    long start = System.currentTimeMillis();

    System.out.println("Searching...");
    // This query times out, and the node gets disconnected
    SearchResponse searchResponse = builder.setQuery(value).setSize(100).execute().actionGet();

    SearchHits hits = searchResponse.getHits();
    System.out.println("searchResponse.getHits().getTotalHits() = " + hits.getTotalHits());
    long end = System.currentTimeMillis();
    System.out.println("request took: " + (end - start) + " ms");
    }

    private static void indexABigDocument(TransportClient client) {
    try {
    BulkRequestBuilder request = client.prepareBulk();

    for (int i = 0; i < 1; i++) {

    XContentBuilder builder = XContentFactory.jsonBuilder().startObject();
    builder.field("firstname", "John");
    builder.field("lastname", "Lee");
    builder.field("data", aReallyLongString());
    builder.endObject();

    request.add(Requests.indexRequest(INDEX_NAME).type("my_type")
    .source(builder));
    }

    BulkResponse response = request.execute().actionGet();
    if (response.hasFailures()) {
    System.out.println("Bulk request failed");
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }


    private static String aReallyLongString() throws IOException {
    StringBuilder stringBuilder = new StringBuilder();
    for (int j = 0; j < 1000000; j++) {
    stringBuilder.append("some more stuff");
    }
    System.out.println("stringBuilder.toString().getBytes().length = " + stringBuilder.toString().getBytes().length);
    return stringBuilder.toString();
    }
    }