【问题标题】:How to index parent and child documents using java API in Elasticsearch version 6.2.1如何在 Elasticsearch 6.2.1 版中使用 java API 索引父子文档
【发布时间】:2018-06-09 16:23:50
【问题描述】:

根据我下面的实现,我无法从 ES 中检索父/子文档。

第 1 步:关系

{
  "versionjoin": {
    "properties": {
      "my_join_field": {
        "type": "join",
        "relations": {
          "version": "accountversion"
        }
      }
    }
  }
}

第 2 步:这就是我索引父文档的方式。

{
    String pattern = YYYY_MM_DD_HH_MM_SS;
                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
                            String date = simpleDateFormat.format(index_timestamp);
                            JSONObject obj = new JSONObject();
                            JSONObject joinObject = new JSONObject();
                            obj.put(VERSIONNUMBER, versionNumber);
                            obj.put(VERSIONDATE, date);
                            obj.put("my_join_field","version");

                                            client.prepareIndex(
                                                    versionIndexName,
                                                    versionJoinType)
                                                    .setId(versionNumber)
                                                    .setSource(obj.toString(),XContentType.JSON)
                                                    .execute()
                                                    .actionGet();
                                    }

第 3 步:这就是我尝试索引子文档的方式。

JsonNode node = serializeMapper.readTree(json);
ObjectNode addedNode = ((ObjectNode) node).putObject("my_join_field");
addedNode
        .put("name", "accountversion")
        .put("parent", association.getVersion());
String modifiedJson = serializeMapper.writeValueAsString(addedNode);

indexResponse = client.prepareIndex(versionIndexName, versionJoinType)
                        .setId(String.valueOf(association.getId() + ":" +
                                                association.getVersion()))
                        .setRouting(association.getVersion())
                        .setSource(modifiedJson,XContentType.JSON).get();

当我尝试使用文档 ID 和路由检索文档时,我在结果中看不到任何文档。我最近将 ES 从 2.4 迁移到 6.2,并将上述实现更改为从同一索引读取父文档和子文档。由于类型支持在 ES 6.2 中已被弃用。任何人都可以建议我是否以不正确的方式做任何事情。

【问题讨论】:

    标签: elasticsearch elasticsearch-5


    【解决方案1】:

    父子节点必须存储在同一个分片上,因此请确保调用setRouting使用父节点的相同路由。

    【讨论】:

    • 我将子文档的路由设置为父文档的相同路由。
    猜你喜欢
    • 1970-01-01
    • 2012-12-12
    • 2018-08-02
    • 2019-01-22
    • 1970-01-01
    • 2021-07-19
    • 2015-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多