java searchterm,在Elasticsearch Java中的MultiTermVectors

I am using the following function to get the term vector for some set of IDs.

public static void builtTermVectorRequest(Client client, String index, Map postIDs) {

TermVectorsRequest termVectorsRequest = new TermVectorsRequest();

termVectorsRequest.index(index).type("post");

for (Map.Entry entry : postIDs.entrySet()) {

String currentPostId = entry.getKey();

String currentParentID = entry.getValue();

termVectorsRequest

.id(currentPostId)

.parent(currentParentID)

.termStatistics(true)

.selectedFields("content");

}

MultiTermVectorsRequestBuilder mtbuilder = client.prepareMultiTermVectors();

mtbuilder.add(termVectorsRequest);

MultiTermVectorsResponse response = mtbuilder.execute().actionGet();

XContentBuilder builder;

try {

builder = XContentFactory.jsonBuilder().startObject();

response.toXContent(builder, ToXContent.EMPTY_PARAMS);

builder.endObject();

System.out.println(builder.prettyPrint().string());

} catch (IOException e) {}

}

Here I have some document IDs along with their parent IDs as the documents are child documents.

I get that the documents were not found even when they exist.

To confirm I tried the same thing in Python using:

body = dict(docs=map(lambda x:

{

"fields": ["content"],

"_id": x["_id"],

"_routing": x["_routing"],

"term_statistics": "true"

}, result["hits"]["hits"]))

es_client = elasticsearch.Elasticsearch([{'host': '192.168.111.12', 'port': 9200}])

all_term_vectors = es_client.mtermvectors(

index="prf_test",

doc_type="post",

body=body

)

and I get results back.

What is wrong with the Java code?

解决方案

I tried out more combinations on how to use TermVectorsRequest with MultiTermVectorsRequestBuilder and finally came to the following solution which works:

/**

* Prints term-vectors for child documents given their parent ids

*

* @param client Es client

* @param index Index name

* @param postIDs Map of child docuemnt ID to its _parent/_routing ID

* @return

*/

public static void builtTermVectorRequest(Client client, String index, Map postIDs) {

/**

* Initialize the MultiTermVectorsRequestBuilder first

*/

MultiTermVectorsRequestBuilder multiTermVectorsRequestBuilder = client.prepareMultiTermVectors();

/**

* For every document ID, create a different TermVectorsRequest and

* add it to the MultiTermVectorsRequestBuilder created above

*/

for (Map.Entry entry : postIDs.entrySet()) {

String currentPostId = entry.getKey();

String currentRoutingID = entry.getValue();

TermVectorsRequest termVectorsRequest = new TermVectorsRequest()

.index(index)

.type("doc_type")

.id(currentPostId)

.parent(currentRoutingID) // You can use .routing(currentRoutingID) also

.selectedFields("some_field")

.termStatistics(true);

multiTermVectorsRequestBuilder.add(termVectorsRequest);

}

/**

* Finally execute the MultiTermVectorsRequestBuilder

*/

MultiTermVectorsResponse response = multiTermVectorsRequestBuilder.execute().actionGet();

XContentBuilder builder;

try {

builder = XContentFactory.jsonBuilder().startObject();

response.toXContent(builder, ToXContent.EMPTY_PARAMS);

builder.endObject();

System.out.println(builder.prettyPrint().string());

} catch (IOException e) {

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值