2016-06-25 4 views
0

Я использую следующую функцию, чтобы получить вектор-вектор для некоторого набора идентификаторов.MultiTermVectors в Elasticsearch Java

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) {} 
    } 

Здесь у меня есть идентификаторы документов вместе с их родительскими идентификаторами, поскольку документы являются дочерними документами.

Получаю, что документы не были найдены, даже если они существуют.

Для подтверждения я попробовал то же самое в 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' вопросе' х [ «_ маршрутизации»] 'так же, как ParentId, если бы вы могли явно попробовать '.routing (currentParentID)'? – keety

+0

сделал это работа? – keety

+0

@keety Извините, что не следил за ними. Я получал ту же проблему, даже после этого, я попробовал еще несколько комбинаций и, наконец, получил ее на работу. Вы можете просмотреть мой ответ. –

ответ

3

Я попробовал несколько комбинаций, как использовать TermVectorsRequest с MultiTermVectorsRequestBuilder и, наконец, пришли к следующему решению, которое работает:

/** 
* 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) { 
    } 
} 
Смежные вопросы