Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Testing Retrofit API for Elasticsearch
@Grab(group='com.squareup.retrofit', module='retrofit', version='1.9.0')
import retrofit.http.*
import retrofit.RestAdapter
class ElasticTests {
static final String INDEX = 'zirbes'
ElasticApi elasticApi = new RestAdapter.Builder()
static void main(String ... args) {
ElasticTests rubberband = new ElasticTests()
void dothings() {
List<Thing> events = [
new Thing(name: 'firetruck', color: 'red', weight: 9999),
new Thing(name: 'bike', color: 'black', weight: 20),
new Thing(name: 'bike', color: 'purple', weight: 35),
new Thing(name: 'bike', color: 'greeen', weight: 25),
new Thing(name: 'bike', color: 'brown', weight: 18),
new Thing(name: 'skateboard', color: 'white', weight: 2)
events.each {
println "Writing document ${}"
elasticApi.create(INDEX,,, it)
class Thing {
UUID id = UUID.randomUUID()
String name
String color
Integer weight
protected interface ElasticApi {
@Headers(['Accept: application/json'])
Map createIndex(@Path('index') String index)
@Headers(['Accept: application/json'])
Map create(@Path('index') String index,
@Path('type') String type,
@Path('key') String key,
@Body Thing eventDTO)
@Headers(['Accept: application/json'])
Map get(@Path('index') String index,
@Path('type') String type,
@Path('key') String key)
@Headers(['Accept: application/json'])
Map search(@Path('index') String index,
@Query('q') String q)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.