简体   繁体   English

创建映射和索引时如何在Elastic Search PHP中创建批量父子关系

[英]How to create bulk parent child relationship(join) in Elastic Search PHP while creating mapping and indexing

I want to create documents with parent child relationship. 我想创建具有父子关系的文档。

I have data like below, 我有如下数据

Parent Document data with parent_id = null parent_id = null的父文档数据

{
            "id": 1,                
            "workflow_name": "Diwali",
            "list_name": "number",
            "list_id": "798",
            "msgType": "Promotional - National",
            "sender_id": "MANISH",
            "submit_date": "2017-11-06 14:09:56",
            "dlrdatetime": "2017-11-06 14:10:06",
            "split_count": 1,
            "error_code": "Waiting",
            "error_text": "-",
            "currency_used": "0.2000",
            "text_type": "text",
            "error_code_status": null,
            "origin_type": "1",
            "api_response_id": 1,
            "response": null,
            "parent_id": null,
            "is_test": 0,
            "link": null,
            "type": 2,
            "message_text": "Hi This is text message",
            "status": null,
            "winner_branch": null,
            "instance_id": "724e540394481746",
            "created_at": "2017-11-06 14:10:06",
            "updated_at": "2017-11-06 14:10:06",
            "branch_id": 0
        }

Child Document data with parent_id = 1 parent_id = 1的子文档数据

 {
            "id": 1,                
            "workflow_name": "Diwali",
            "list_name": "number",
            "list_id": "798",
            "msgType": "Promotional - National",
            "sender_id": "MANISH",
            "submit_date": "2017-11-06 14:09:56",
            "dlrdatetime": "2017-11-06 14:10:06",
            "split_count": 1,
            "error_code": "Waiting",
            "error_text": "-",
            "currency_used": "0.2000",
            "text_type": "text",
            "error_code_status": null,
            "origin_type": "1",
            "api_response_id": 1,
            "response": null,
            "parent_id": 1,
            "is_test": 0,
            "link": null,
            "type": 2,
            "message_text": "Hi This is text message",
            "status": null,
            "winner_branch": null,
            "instance_id": "724e540394481746",
            "created_at": "2017-11-06 14:10:06",
            "updated_at": "2017-11-06 14:10:06",
            "branch_id": 0
        }

So i have one to many relationship, one parent have many child. 所以我有一对多的关系,一位父母有很多孩子。

Sample code snippet of for bulk mapping: 批量映射的示例代码段:

$mapping['index'] = 'response_packets_index_v5';
    $mapping['body'] = array(
        'mappings' => array(
            'response_packets_v5' => array(
                'properties' => [
                    'id' => [
                        'type' => 'integer'
                    ],
                    'workflow_id' => [
                        'type' => 'integer'
                    ],                                            
                    'parent_id' => [
                        'type' => 'integer',                        
                    ],                       
                    'instance_id' => [
                        'type' => 'text'
                    ],
                    'created_at' => [
                        'type' => 'date',
                        'format' => 'yyyy-MM-dd HH:mm:ss'
                    ],
                    'updated_at' => [
                        'type' => 'date',
                        'format' => 'yyyy-MM-dd HH:mm:ss'
                    ],
                    'branch_id' => [
                        'type' => 'integer'
                    ]
                ]
            )
        )
    );

    $client->indices()->create($mapping);

Code snippet of for bulk indexing: 批量索引编制的代码段:

for ($i = 0; $i <= $count; $i++) {
        $params['body'][] = [
            'index' => [
                '_index' => 'response_packets_index_v5',
                '_type' => response_packets_v5',                    
                'routing' => 'company',
            ]
        ];

        $params['body'][] = $documentData[$i];
 }
return $client->bulk($params);   

I already read this article but not helpful for me : https://www.elastic.co/guide/en/elasticsearch/reference/current/parent-join.html 我已经阅读了这篇文章,但对我没有帮助: https : //www.elastic.co/guide/en/elasticsearch/reference/current/parent-join.html

Can any one help me for encounter this problem, Help highly appreciable. 有人能帮助我遇到这个问题吗,高度帮助。

System details: 系统细节:

Operating System ubuntu 16.04, 操作系统ubuntu 16.04,

PHP Version 7.1, PHP版本7.1,

ES-PHP client version 6.0 ES-PHP客户端版本6.0

One of the problems that I see in your specification is that you are not defining a parent-child relationship. 我在规范中看到的问题之一是您没有定义父子关系。 In order to do so, you need to use the "join" datatype. 为此,您需要使用“ join”数据类型。 That is, in your mapping you need to create a field that specifies whether your document is a parent type or child type. 也就是说,在映射中,您需要创建一个字段,该字段指定文档是父类型还是子类型。 Check below: 检查以下内容:

"properties": {
            "doc_identifier" : {
                "type": "join",
                "relations": {
                    "parent_doc": "child_doc"
                }
            },
            "workflow_id" : {"type" : "keyword"},
            other fields...
}

Furthermore, instead of creating a "parent_id" field, you need to specify de parent_id in the child as following: 此外,除了创建“ parent_id”字段外,还需要在子级中指定de parent_id,如下所示:

{"doc_identifier" : {"name" : "child_doc", "parent":"parent_id_value"}, "workflow_id": "foo", ..., other fields,...}

Finally, when indexing a parent you don't need to add information from the child. 最后,在索引父级时,您不需要添加子级的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM