Skip to content

Instantly share code, notes, and snippets.

@aaronzirbes
Created July 13, 2017 10:05
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 aaronzirbes/37175a1e88f642796b4cd2d0f4315bf5 to your computer and use it in GitHub Desktop.
Save aaronzirbes/37175a1e88f642796b4cd2d0f4315bf5 to your computer and use it in GitHub Desktop.
Testing Retrofit API for Elasticsearch
@Grapes([
@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()
.setEndpoint('http://localhost:9200')
.build()
.create(ElasticApi)
static void main(String ... args) {
ElasticTests rubberband = new ElasticTests()
rubberband.dothings()
}
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 ${it.id}"
elasticApi.create(INDEX, it.name, it.id.toString(), it)
}
}
}
class Thing {
UUID id = UUID.randomUUID()
String name
String color
Integer weight
}
protected interface ElasticApi {
@Headers(['Accept: application/json'])
@PUT('/{index}')
Map createIndex(@Path('index') String index)
@Headers(['Accept: application/json'])
@PUT('/{index}/{type}/{key}')
Map create(@Path('index') String index,
@Path('type') String type,
@Path('key') String key,
@Body Thing eventDTO)
@Headers(['Accept: application/json'])
@GET('/{index}/{type}/{key}')
Map get(@Path('index') String index,
@Path('type') String type,
@Path('key') String key)
@Headers(['Accept: application/json'])
@GET('/{index}/_search')
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