java vector search,Elasticsearch Java TermVectorRequest(_termvector)

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();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值