弹性搜索中的多项向量

2024-10-06 10:21:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用下面的函数来获取一些id集合的术语向量。在

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中使用了相同的方法:

^{2}$

结果又回来了。在

Java代码有什么问题?在


Tags: 文档clientidmapstringindexresponsebuilder
1条回答
网友
1楼 · 发布于 2024-10-06 10:21:13

我尝试了更多关于如何将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) {
  }
}

相关问题 更多 >