我可以在Elasticsearch的父子映射中限制子文档的数量吗? [英] Is their any way using which i can restrict the number of children doc in my parent-child mapping in elasticsearch?

查看:293
本文介绍了我可以在Elasticsearch的父子映射中限制子文档的数量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ES v7.3,并且正在使用父子映射,因此我想知道可以通过什么方式限制父文档的子文档数量。假设我有一个父级 p1,那么我希望该父级不能有超过100个关联的子文档,并且如果对更多文档进行了索引,则旧的子文档将被删除而新的文档将被索引,但限制应为100个子文档。

I am using ES v7.3 and I am using parent-child mapping, I wanted to know is thier any way that i can restrict the number of child doc's for a parent document. suppose i have a parent 'p1' then i want that this parent must not have more than 100 child doc's associated with it and if some more doc's are indexed then the old child docs get deleted and new ones are indexed but the limit should be of 100 child docs.

PUT test/
{
  "mappings": {
    "properties": {
        "data": {
              "type": "join",
              "relations": {
                  "parent": ["child1", "child2", "child3"]
              }
        }
    }
}
}


推荐答案

我不知道您可以设置有关最大大小和通过映射自动删除的特定信息。

I am not aware that you could set such specific information about the maximum size and automatic delete via the mappings.

但是,您可以执行一个logstash过滤器,该过滤器检查当前子文档数量的大小,如果数量已经等于100,则对集群执行一些REST调用。

What you could do however is to implement a logstash filter that checks the size of the current number of child-documents and execute some REST-calls to the cluster if the number is already equal to 100.

我从没面对过d这样的用例,但我想为您提供一些解决方法:

I've never faced such an use case but I want to give you some possibilities for that workaround:

1。)通过Logstash的elasticsearch过滤器插件执行parent_id-query

parent_id文档,此查询返回已加入特定父文档的子文档。

As stated in the parent_id documentation, this query "Returns child documents joined to a specific parent document".

因此,使用父文档的ID,您应该能够在过滤器实现中获取所有子文档。请参阅 elasticsearch过滤器插件文档如何使用它。这样,您就可以通过ruby代码插件确定子文档的数量。

So with the id of the parent document you should be able to get all child documents in your filter implementation. Refer to the elasticsearch filter plugin documentation on how to use it. With that, you can surely determine the number of child-documents via a ruby code plugin.

2。)检查当前子文档的数量是否为等于100

3。)如果2)计算结果为true,请调用delete_by_query REST API

要为新的子文档建立索引而又不超过100个子文档的最大阈值,则必须删除以前建立索引的子文档。因此,您可以使用logstash的 http过滤器插件调用 delete_by_query API 使用确切的查询将删除先前索引的文档。

To index new child-documents without stepping over that maximum threshold of 100 child documents you have to delete previous indexed child documents. You could therefore use logstash's http filter plugin to call the delete_by_query API with the exact query that will delete previous indexed documents.

4。)通过elasticsearch输出插件为新文档建立索引

请参阅 Elasticsearch输出插件有关如何从logstash编制事件索引。

Refer to the Elasticsearch output plugin on how to index events from logstash.

因此,正如我在开始时所说,我并不完全知道这种方法是否会导致是否达到理想的结果。但我会尝试一下,因为我提到的Logstash插件能够执行工作流中的特定步骤。

So as I stated at the beginning I am not fully aware whether this approach will lead to the desired result or not. But I would give it a try since the Logstash plugins I mentioned are able to do the particular steps in the workflow.

这篇关于我可以在Elasticsearch的父子映射中限制子文档的数量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆