Skip to content

Instantly share code, notes, and snippets.

@mchv
Created May 30, 2014 08:16
Show Gist options
  • Save mchv/492efc68e06dc4f2a18c to your computer and use it in GitHub Desktop.
Save mchv/492efc68e06dc4f2a18c to your computer and use it in GitHub Desktop.
Elasticsearch Terms Vector API example
import org.elasticsearch.action.ActionListener
import org.elasticsearch.action.termvector.TermVectorResponse
import org.elasticsearch.action.termvector.TermVectorRequestBuilder
import org.elasticsearch.common.xcontent.ToXContent
import org.elasticsearch.common.xcontent.XContentBuilder
import org.elasticsearch.common.xcontent.XContentFactory
import org.elasticsearch.common.xcontent.XContentType
def getTermsVector(`type`: String, id: String, field: String) {
val builder = new TermVectorRequestBuilder(client, index, `type`, id).setSelectedFields(field)
builder.setTermStatistics(true).setFieldStatistics(true)
builder.execute(new ActionListener[TermVectorResponse] {
def onResponse(response: TermVectorResponse) {
val builder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint()
response.toXContent(builder, ToXContent.EMPTY_PARAMS)
println("TermsVector: " + builder.string)
}
def onFailure(e: Throwable) {e.printStackTrace}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment