Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.