public
Last active

I spent a couple hours trying to figure out the client API for ElasticSearch, so I wrote this simple unit test to try out some of the features.

  • Download Gist
ElasticSearchTest.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
package com.typeahead.dropwizard.elasticsearch;
 
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
 
import java.util.Date;
import java.util.Map;
 
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
import org.elasticsearch.action.admin.cluster.node.info.NodeInfo;
import org.elasticsearch.action.admin.cluster.node.info.NodesInfoRequest;
import org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.count.CountResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.facet.FacetBuilders;
import org.elasticsearch.search.facet.terms.TermsFacet;
import org.junit.Before;
import org.junit.Test;
 
//import static org.elasticsearch.index.query.IndicesQueryBuilder.*;
 
public class ElasticSearchTest {
 
Client client = null;
 
@Before
public void setup() throws Exception {
client = new TransportClient()
.addTransportAddress(new InetSocketTransportAddress(
"localhost", 9300));
}
 
@Test
public void test1() throws Exception {
 
IndexResponse response = client
.prepareIndex("twitter", "tweet", "1")
.setSource(
jsonBuilder().startObject().field("user", "rick")
.field("postDate", new Date())
.field("message", "another test").endObject())
.execute().actionGet();
 
System.out.println("-test1---------------------");
System.out.println("id:" + response.getId());
System.out.println("version:" + response.getVersion());
System.out.println("----------------------");
 
Thread.sleep(1000);
 
}
 
@Test
public void test2() throws Exception {
NodesInfoResponse response = client.admin().cluster()
.nodesInfo(new NodesInfoRequest().all()).actionGet();
 
System.out.println("-test2---------------------");
System.out.println("cluster name: " + response.getClusterName());
for (NodeInfo n : response.getNodes()) {
System.out.println(" - hostname: " + n.getHostname());
Map<String, String> settings = n.getSettings().getAsMap();
for (String key : settings.keySet()) {
System.out.println(" - " + key + ": " + settings.get(key));
}
}
System.out.println("----------------------");
 
Thread.sleep(1000);
}
 
@Test
public void testClusterHealth() throws Exception {
ClusterHealthResponse response = client.admin().cluster().prepareHealth(new String[]{}).setTimeout(new TimeValue(50000)).execute().actionGet();
System.out.println("-testClusterHealth---------------------");
System.out.println("cluster name: " + response.getClusterName());
System.out.println("active shards: " + response.getActiveShards());
System.out.println("active primary shards: " + response.getActivePrimaryShards());
System.out.println("data nodes: " + response.getNumberOfDataNodes());
System.out.println("nodes: " + response.getNumberOfNodes());
System.out.println("reloc shards: " + response.getRelocatingShards());
System.out.println("unassigned shards: " + response.getUnassignedShards());
System.out.println("initializing shards: " + response.getInitializingShards());
ClusterHealthStatus status = response.status();
System.out.println("status: " + status);
System.out.println("----------------------");
 
Thread.sleep(1000);
}
 
@Test
public void test3() throws Exception {
BulkResponse response = client
.prepareBulk()
.add(new IndexRequest("twitter", "tweet", "2")
.source(jsonBuilder().startObject()
.field("user", "test1")
.field("postDate", new Date())
.field("message", "test2 message").endObject()))
.add(new IndexRequest("twitter", "tweet", "3")
.source(jsonBuilder().startObject()
.field("user", "test3")
.field("postDate", new Date())
.field("message", "test3 message").endObject()))
.add(new IndexRequest("twitter", "tweet", "4")
.source(jsonBuilder().startObject()
.field("user", "test4")
.field("postDate", new Date())
.field("message", "test4 message").endObject()))
.add(new IndexRequest("twitter", "tweet", "5")
.source(jsonBuilder().startObject()
.field("user", "test5")
.field("postDate", new Date())
.field("message", "test5 message").endObject()))
.add(new IndexRequest("twitter", "tweet", "6")
.source(jsonBuilder().startObject()
.field("user", "rick")
.field("postDate", new Date())
.field("message", "another rick message").endObject()))
.execute().actionGet();
System.out.println("-test3---------------------");
for (BulkItemResponse resp : response.items()) {
System.out.println(resp.id() + ":" + resp.version());
}
System.out.println("----------------------");
 
Thread.sleep(1000);
}
 
@Test
public void test4() throws Exception {
CountResponse response = client.prepareCount("twitter").execute()
.actionGet();
 
System.out.println("-test4---------------------");
System.out.println(response.count());
System.out.println("----------------------");
 
Thread.sleep(1000);
 
}
 
@Test
public void test5() throws Exception {
QueryBuilder query = QueryBuilders.termQuery("user", "rick");
SearchResponse response = client.prepareSearch("twitter").setQuery(query).execute().actionGet();
System.out.println("-test5---------------------");
SearchHits hits = response.getHits();
System.out.println("total: " + hits.getTotalHits());
for (SearchHit hit : hits) {
System.out.println(hit.getId() + ":" + hit.getSourceAsString() + " score:" + hit.getScore());
}
System.out.println("----------------------");
Thread.sleep(1000);
}
@Test
public void test6() throws Exception {
QueryBuilder query = QueryBuilders.disMaxQuery().tieBreaker(0.7f).boost(1.2f)
.add(QueryBuilders.matchAllQuery());
SearchResponse response = client.prepareSearch("twitter")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.addFacet(FacetBuilders.termsFacet("Users").allTerms(true).field("user"))
.setQuery(query)
.setFrom(0)
.setSize(3)
.setExplain(true)
.execute()
.actionGet();
 
System.out.println("-test6---------------------");
SearchHits hits = response.getHits();
System.out.println("total: " + hits.getTotalHits());
TermsFacet facet = response.getFacets().facet(TermsFacet.class, "Users");
System.out.println("facet: " + facet.getName() + ":" + facet.getTotalCount());
for (TermsFacet.Entry entry : facet.entries()) {
System.out.println(" - " + entry.getTerm() + ":" + entry.getCount());
}
for (SearchHit hit : hits) {
System.out.println(hit.getId() + ":" + hit.getSourceAsString() + " score:" + hit.getScore());
}
System.out.println("----------------------");
Thread.sleep(1000);
}
@Test
public void test7() throws Exception {
// QueryBuilder query = QueryBuilders.disMaxQuery().tieBreaker(0.7f).boost(1.2f)
// .add(QueryBuilders.matchAllQuery());
 
QueryBuilder query = QueryBuilders.matchAllQuery();
SearchResponse response = client.prepareSearch("twitter")
.setSearchType(SearchType.SCAN)
.setScroll(new TimeValue(60000))
.setQuery(query)
.setSize(2)
.setExplain(true)
.execute()
.actionGet();
 
System.out.println("-test7---------------------");
//Scroll until no hits are returned
while (true) {
response = client.prepareSearchScroll(response.getScrollId())
.setScroll(new TimeValue(600000))
.execute()
.actionGet();
for (SearchHit hit : response.getHits()) {
System.out.println(hit.getId() + ":" + hit.getSourceAsString() + " score:" + hit.getScore() + " explain:" + hit.explanation());
}
//Break condition: No hits are returned
if (response.hits().hits().length == 0) {
break;
}
System.out.println("!!");
}
System.out.println("----------------------");
Thread.sleep(1000);
}
@Test
public void test8() throws Exception {
DeleteResponse response = client.prepareDelete("twitter", "tweet", "1")
.execute().actionGet();
 
System.out.println("-test8---------------------");
System.out.println(response.getId());
System.out.println("----------------------");
 
Thread.sleep(1000);
}
@Test
public void test9() throws Exception {
DeleteIndexResponse response = client.admin().indices().delete(new DeleteIndexRequest("twitter")).actionGet();
 
System.out.println("-test9---------------------");
System.out.println(response.acknowledged());
System.out.println("----------------------");
 
Thread.sleep(1000);
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.