2016-06-25 21 views
0

我正在使用以下函数来获取某些ID集的术语向量。Elasticsearch Java中的MultiTermVectors

public static void builtTermVectorRequest(Client client, String index, Map<String, String> postIDs) { 
    TermVectorsRequest termVectorsRequest = new TermVectorsRequest(); 
    termVectorsRequest.index(index).type("post"); 
    for (Map.Entry<String, String> 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) {} 
    } 

在这里我有一些文件ID连同他们的父母ID作为文件是子文件。

我得知即使它们存在,文档也没有找到。

为了证实我尝试使用Python中同样的事情:

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 
) 

,我得到返回结果。

Java代码有什么问题?

+0

看起来有些'routing'问题是'X [“_路由”]'一样ParentId如果是的话,你可以明确地尝试'.routing(currentParentID)'? – keety

+0

做了这项工作? – keety

+0

@ keety对不起,没跟进。即使在我尝试了更多组合并最终实现它的工作之后,我也遇到了同样的问题。你可以查看我的答案。 –

回答

3

我尝试了关于如何使用TermVectorsRequestMultiTermVectorsRequestBuilder更多的组合,终于来到了以下解决方案,它的工作原理:

/** 
* Prints term-vectors for child documents given their parent ids 
* 
* @param client Es client 
* @param index  Index name 
* @param postIDs Map of child document ID to its _parent/_routing ID 
*/ 
public static void builtTermVectorRequest(Client client, String index, Map<String, String> 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<String, String> 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) { 
    } 
}