BulkRequestBuilder request = client.prepareBulk();
for (int j = 0; j < BATCH; j++) {
request.add(Requests.indexRequest("test").type("type1").id(Integer.toString(idCounter++)).source(source("prefix" + character + termCounter++)));
}
character++;
BulkResponse response = request.execute().actionGet();
if (response.hasFailures()) {
System.err.println("failures...");
}
}
System.out.println("Indexing took " + stopWatch.totalTime());
client.admin().indices().prepareRefresh().execute().actionGet();
System.out.println("Count: " + client.prepareCount().setQuery(matchAllQuery()).execute().actionGet().getCount());
} catch (Exception e) {
System.out.println("--> Index already exists, ignoring indexing phase, waiting for green");
ClusterHealthResponse clusterHealthResponse = client.admin().cluster().prepareHealth().setWaitForGreenStatus().setTimeout("10m").execute().actionGet();
if (clusterHealthResponse.isTimedOut()) {
System.err.println("--> Timed out waiting for cluster health");
}
client.admin().indices().prepareRefresh().execute().actionGet();
System.out.println("Count: " + client.prepareCount().setQuery(matchAllQuery()).execute().actionGet().getCount());
}
System.out.println("Warming up...");
char startChar = 'a';
for (int i = 0; i <= 20; i++) {
String term = "prefix" + startChar;
SearchResponse response = client.prepareSearch()
.setQuery(prefixQuery("field", term))
.addSuggestion(SuggestBuilders.termSuggestion("field").field("field").text(term).suggestMode("always"))
.execute().actionGet();
if (response.getHits().totalHits() == 0) {
System.err.println("No hits");
continue;
}
startChar++;
}
System.out.println("Starting benchmarking suggestions.");
startChar = 'a';
long timeTaken = 0;
for (int i = 0; i <= SEARCH_ITERS; i++) {
String term = "prefix" + startChar;
SearchResponse response = client.prepareSearch()
.setQuery(matchQuery("field", term))
.addSuggestion(SuggestBuilders.termSuggestion("field").text(term).field("field").suggestMode("always"))
.execute().actionGet();
timeTaken += response.getTookInMillis();
if (response.getSuggest() == null) {
System.err.println("No suggestions");
continue;
}
List<? extends Option> options = response.getSuggest().getSuggestion("field").getEntries().get(0).getOptions();
if (options == null || options.isEmpty()) {
System.err.println("No suggestions");
}
startChar++;
}