I have executed the following query in my browser:
http://127.0.0.1:9200/index/type/id/_termvector
Which returns:
"_index" : "index",
"_type" : "type",
"_id" : "id",
"_version" : 1,
"found" : true,
"took" : 2,
"term_vectors" : {
"text" : {
"field_statistics" : {
"sum_doc_freq" : 100453,
"doc_count" : 3566,
"sum_ttf" : 126632
},
"terms" : {
"trust" : {
"term_freq" : 1
},
"believe" : {
"term_freq" : 1
}
}
}
}
I tried the Java API counterpart TermVectorRequest/Response, like so:
TermVectorResponse tvr = client.prepareTermVector().setIndex(index).setType(type).setId(id).execute().actionGet();
I have absolutely no clue how to retrieve and parse the result from the API call or if the function I use is correct.
Thank you in advance :)
解决方案
Solution:
I finally managed to parse the response into json.
TermVectorResponse resp = client.prepareTermVector().setIndex(index).setType(type).setId(id).execute().actionGet();
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
resp.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
System.out.println(builder.string());
Don't forget to use the builder.startObject() and builder.endObject(), otherwise it will not output correct json.
This gives the expected output.
Bonus:
To convert the json into a Map use
Map map = XContentHelper.convertToMap(builder.bytes(), false).v2();