Skip to content

Instantly share code, notes, and snippets.

@focampo
Created September 26, 2011 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save focampo/1242136 to your computer and use it in GitHub Desktop.
Save focampo/1242136 to your computer and use it in GitHub Desktop.
Populating and immediately searching ES Test
@BeforeClass
public static void uniqueSetUp() throws Exception {
startAndPopulateES()
System.sleep(5000)
}
private static void startAndPopulateES() {
def fileStream = new FileInputStream(SETTINGS_PATH)
Settings settings = ImmutableSettings.settingsBuilder().loadFromStream(SETTINGS_PATH, fileStream).build()
localEsNode = org.elasticsearch.node.NodeBuilder.nodeBuilder().local(LOCAL).settings(settings).node()
Client client = localEsNode.client()
client.admin().cluster().prepareHealth(INDEX).setWaitForGreenStatus().execute().actionGet()
fillESServerUpWithItems(client)
}
private static void fillESServerUpWithItems(client) {
populateItemsBase()
def bulkIndexBuilder = client.prepareBulk()
itemsBase.each { id, item ->
def indexReq = client.prepareIndex(INDEX, TYPE, id as String).setSource(item as Map)
bulkIndexBuilder.add(indexReq)
}
def response = bulkIndexBuilder.execute().actionGet()
//assert !response.hasFailures()
//Espera que el Cluster levante y refresca los datos indexados dejandolos disponibles para la busqueda
client.admin().indices().prepareRefresh(INDEX).execute().actionGet()
assert client.admin().indices().prepareExists(INDEX).execute().actionGet().exists()
}
@Test
public void "search for all items"() {
def expectedIds = itemsBase.keySet()
def results = esService.search(null, [from:0, size:SIZE], null, null)
Assert.assertNotNull(results)
Assert.assertEquals(expectedIds.size(), results.getHits().totalHits())
//Verifica los ids de los items
def resultIds = results.hits().collect { it.getSource().id }
Assert.assertTrue(resultIds.containsAll(expectedIds))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment