Created
April 27, 2012 21:20
-
-
Save kimchy/2513321 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment