Is there any solution with elasticsearch parent-child join












0















I have an es settings like following:



PUT /test
{
"mappings": {
"doc": {
"properties": {
"status": {
"type": "keyword"
},
"counting": {
"type": "integer"
},
"join": {
"type": "join",
"relations": {
"vsim": ["pool", "package"]
}
},
"poolId": {
"type": "keyword"
},
"packageId": {
"type": "keyword"
},
"countries": {
"type": "keyword"
},
"vId": {
"type": "keyword"
}
}
}
}}


Then add data:



// add vsim
PUT /test/doc/doc1
{"counting":6, "join": {"name": "vsim"}, "content": "1", "status": "disabled"}

PUT /test/doc/doc2
{"counting":5,"join": {"name": "vsim"}, "content": "2", "status": "disabled"}

PUT /test/doc/doc3
{"counting":5,"join": {"name": "vsim"}, "content": "2", "status": "enabled"}

// add package
PUT /test/doc/ner2?routing=doc2
{"join": {"name": "package", "parent": "doc2"}, "countries":["CN", "UK"]}

PUT test/doc/ner12?routing=doc1
{"join": {"name": "package", "parent": "doc1"}, "countries":["CN", "US"]}

PUT /test/doc/ner11?routing=doc1
{"join":{"name": "package", "parent": "doc1"}, "countries":["US", "KR"]}

PUT /test/doc/ner13?routing=doc3
{"join":{"name": "package", "parent": "doc3"}, "countries":["UK", "AU"]}


// add pool
PUT /test/doc/ner21?routing=doc1
{"join": {"name": "pool", "parent": "doc1"}, "poolId": "MER"}

PUT /test/doc/ner22?routing=doc2
{"join": {"name": "pool", "parent": "doc2"}, "poolId": "MER"}

PUT /test/doc/ner23?routing=doc2
{"join": {"name": "pool", "parent": "doc2"}, "poolId": "NER"}


and then I want to count the counting group by the status(vsim), poolId(pool) and countries(package), the expect result like:



disabled-MER-CN: 3



disabled-MER-US: 3



enabled-MR-CN: 1
... and so on.
I'm a new player for elasticsearch, and I have learnt the document like



https://www.elastic.co/guide/en/elasticsearch/reference/current/joining-queries.html
and
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-children-aggregation.html



but still have no idea to implement this aggregation query, PLEASE give me some suggestion, thanks!










share|improve this question





























    0















    I have an es settings like following:



    PUT /test
    {
    "mappings": {
    "doc": {
    "properties": {
    "status": {
    "type": "keyword"
    },
    "counting": {
    "type": "integer"
    },
    "join": {
    "type": "join",
    "relations": {
    "vsim": ["pool", "package"]
    }
    },
    "poolId": {
    "type": "keyword"
    },
    "packageId": {
    "type": "keyword"
    },
    "countries": {
    "type": "keyword"
    },
    "vId": {
    "type": "keyword"
    }
    }
    }
    }}


    Then add data:



    // add vsim
    PUT /test/doc/doc1
    {"counting":6, "join": {"name": "vsim"}, "content": "1", "status": "disabled"}

    PUT /test/doc/doc2
    {"counting":5,"join": {"name": "vsim"}, "content": "2", "status": "disabled"}

    PUT /test/doc/doc3
    {"counting":5,"join": {"name": "vsim"}, "content": "2", "status": "enabled"}

    // add package
    PUT /test/doc/ner2?routing=doc2
    {"join": {"name": "package", "parent": "doc2"}, "countries":["CN", "UK"]}

    PUT test/doc/ner12?routing=doc1
    {"join": {"name": "package", "parent": "doc1"}, "countries":["CN", "US"]}

    PUT /test/doc/ner11?routing=doc1
    {"join":{"name": "package", "parent": "doc1"}, "countries":["US", "KR"]}

    PUT /test/doc/ner13?routing=doc3
    {"join":{"name": "package", "parent": "doc3"}, "countries":["UK", "AU"]}


    // add pool
    PUT /test/doc/ner21?routing=doc1
    {"join": {"name": "pool", "parent": "doc1"}, "poolId": "MER"}

    PUT /test/doc/ner22?routing=doc2
    {"join": {"name": "pool", "parent": "doc2"}, "poolId": "MER"}

    PUT /test/doc/ner23?routing=doc2
    {"join": {"name": "pool", "parent": "doc2"}, "poolId": "NER"}


    and then I want to count the counting group by the status(vsim), poolId(pool) and countries(package), the expect result like:



    disabled-MER-CN: 3



    disabled-MER-US: 3



    enabled-MR-CN: 1
    ... and so on.
    I'm a new player for elasticsearch, and I have learnt the document like



    https://www.elastic.co/guide/en/elasticsearch/reference/current/joining-queries.html
    and
    https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-children-aggregation.html



    but still have no idea to implement this aggregation query, PLEASE give me some suggestion, thanks!










    share|improve this question



























      0












      0








      0








      I have an es settings like following:



      PUT /test
      {
      "mappings": {
      "doc": {
      "properties": {
      "status": {
      "type": "keyword"
      },
      "counting": {
      "type": "integer"
      },
      "join": {
      "type": "join",
      "relations": {
      "vsim": ["pool", "package"]
      }
      },
      "poolId": {
      "type": "keyword"
      },
      "packageId": {
      "type": "keyword"
      },
      "countries": {
      "type": "keyword"
      },
      "vId": {
      "type": "keyword"
      }
      }
      }
      }}


      Then add data:



      // add vsim
      PUT /test/doc/doc1
      {"counting":6, "join": {"name": "vsim"}, "content": "1", "status": "disabled"}

      PUT /test/doc/doc2
      {"counting":5,"join": {"name": "vsim"}, "content": "2", "status": "disabled"}

      PUT /test/doc/doc3
      {"counting":5,"join": {"name": "vsim"}, "content": "2", "status": "enabled"}

      // add package
      PUT /test/doc/ner2?routing=doc2
      {"join": {"name": "package", "parent": "doc2"}, "countries":["CN", "UK"]}

      PUT test/doc/ner12?routing=doc1
      {"join": {"name": "package", "parent": "doc1"}, "countries":["CN", "US"]}

      PUT /test/doc/ner11?routing=doc1
      {"join":{"name": "package", "parent": "doc1"}, "countries":["US", "KR"]}

      PUT /test/doc/ner13?routing=doc3
      {"join":{"name": "package", "parent": "doc3"}, "countries":["UK", "AU"]}


      // add pool
      PUT /test/doc/ner21?routing=doc1
      {"join": {"name": "pool", "parent": "doc1"}, "poolId": "MER"}

      PUT /test/doc/ner22?routing=doc2
      {"join": {"name": "pool", "parent": "doc2"}, "poolId": "MER"}

      PUT /test/doc/ner23?routing=doc2
      {"join": {"name": "pool", "parent": "doc2"}, "poolId": "NER"}


      and then I want to count the counting group by the status(vsim), poolId(pool) and countries(package), the expect result like:



      disabled-MER-CN: 3



      disabled-MER-US: 3



      enabled-MR-CN: 1
      ... and so on.
      I'm a new player for elasticsearch, and I have learnt the document like



      https://www.elastic.co/guide/en/elasticsearch/reference/current/joining-queries.html
      and
      https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-children-aggregation.html



      but still have no idea to implement this aggregation query, PLEASE give me some suggestion, thanks!










      share|improve this question
















      I have an es settings like following:



      PUT /test
      {
      "mappings": {
      "doc": {
      "properties": {
      "status": {
      "type": "keyword"
      },
      "counting": {
      "type": "integer"
      },
      "join": {
      "type": "join",
      "relations": {
      "vsim": ["pool", "package"]
      }
      },
      "poolId": {
      "type": "keyword"
      },
      "packageId": {
      "type": "keyword"
      },
      "countries": {
      "type": "keyword"
      },
      "vId": {
      "type": "keyword"
      }
      }
      }
      }}


      Then add data:



      // add vsim
      PUT /test/doc/doc1
      {"counting":6, "join": {"name": "vsim"}, "content": "1", "status": "disabled"}

      PUT /test/doc/doc2
      {"counting":5,"join": {"name": "vsim"}, "content": "2", "status": "disabled"}

      PUT /test/doc/doc3
      {"counting":5,"join": {"name": "vsim"}, "content": "2", "status": "enabled"}

      // add package
      PUT /test/doc/ner2?routing=doc2
      {"join": {"name": "package", "parent": "doc2"}, "countries":["CN", "UK"]}

      PUT test/doc/ner12?routing=doc1
      {"join": {"name": "package", "parent": "doc1"}, "countries":["CN", "US"]}

      PUT /test/doc/ner11?routing=doc1
      {"join":{"name": "package", "parent": "doc1"}, "countries":["US", "KR"]}

      PUT /test/doc/ner13?routing=doc3
      {"join":{"name": "package", "parent": "doc3"}, "countries":["UK", "AU"]}


      // add pool
      PUT /test/doc/ner21?routing=doc1
      {"join": {"name": "pool", "parent": "doc1"}, "poolId": "MER"}

      PUT /test/doc/ner22?routing=doc2
      {"join": {"name": "pool", "parent": "doc2"}, "poolId": "MER"}

      PUT /test/doc/ner23?routing=doc2
      {"join": {"name": "pool", "parent": "doc2"}, "poolId": "NER"}


      and then I want to count the counting group by the status(vsim), poolId(pool) and countries(package), the expect result like:



      disabled-MER-CN: 3



      disabled-MER-US: 3



      enabled-MR-CN: 1
      ... and so on.
      I'm a new player for elasticsearch, and I have learnt the document like



      https://www.elastic.co/guide/en/elasticsearch/reference/current/joining-queries.html
      and
      https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-children-aggregation.html



      but still have no idea to implement this aggregation query, PLEASE give me some suggestion, thanks!







      elasticsearch parent-child aggregation






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 3 at 12:54









      Mysterion

      6,89122044




      6,89122044










      asked Jan 2 at 23:25









      WalleWalle

      11




      11
























          1 Answer
          1






          active

          oldest

          votes


















          0














          If I followed your structure of the documents - you have types pool and package on the same level (they are siblings) - I wasn't able to achieve exactly your expected results. I also highly doubt that it's possible with those types being siblings.



          However, it's still possible to slice per one field in your doc (status) and later separately slice both by poolId and countries with a query like this:



          {
          "aggs": {
          "status-aggs": {
          "terms": {
          "field": "status",
          "size": 10
          },
          "aggs": {
          "to-pool": {
          "children": {
          "type": "pool"
          },
          "aggs": {
          "top-poolid": {
          "terms": {
          "field": "poolId",
          "size": 10
          }
          }
          }
          },
          "to-package": {
          "children": {
          "type": "package"
          },
          "aggs": {
          "top-countries": {
          "terms": {
          "field": "countries",
          "size": 10
          }
          }
          }
          }
          }
          }
          }
          }


          with a response from Elasticsearch like this (I've omitted some part of json for readability):



          {
          "status-aggs": {
          "buckets": [
          {
          "key": "disabled",
          "doc_count": 2,
          "to-pool": {
          "doc_count": 3,
          "top-poolid": {
          "buckets": [
          {
          "key": "MER",
          "doc_count": 2
          },
          {
          "key": "NER",
          "doc_count": 1
          }
          ]
          }
          },
          "to-package": {
          "doc_count": 3,
          "top-countries": {
          "buckets": [
          {
          "key": "CN",
          "doc_count": 2
          },
          {
          "key": "US",
          "doc_count": 2
          },
          {
          "key": "KR",
          "doc_count": 1
          },
          {
          "key": "UK",
          "doc_count": 1
          }
          ]
          }
          }
          },
          {
          "key": "enabled",
          "doc_count": 1,
          "to-pool": {
          "doc_count": 0,
          "top-poolid": {
          "buckets":
          }
          },
          "to-package": {
          "doc_count": 1,
          "top-countries": {
          "buckets": [
          {
          "key": "AU",
          "doc_count": 1
          },
          {
          "key": "UK",
          "doc_count": 1
          }
          ]
          }
          }
          }
          ]
          }
          }





          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014498%2fis-there-any-solution-with-elasticsearch-parent-child-join%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            If I followed your structure of the documents - you have types pool and package on the same level (they are siblings) - I wasn't able to achieve exactly your expected results. I also highly doubt that it's possible with those types being siblings.



            However, it's still possible to slice per one field in your doc (status) and later separately slice both by poolId and countries with a query like this:



            {
            "aggs": {
            "status-aggs": {
            "terms": {
            "field": "status",
            "size": 10
            },
            "aggs": {
            "to-pool": {
            "children": {
            "type": "pool"
            },
            "aggs": {
            "top-poolid": {
            "terms": {
            "field": "poolId",
            "size": 10
            }
            }
            }
            },
            "to-package": {
            "children": {
            "type": "package"
            },
            "aggs": {
            "top-countries": {
            "terms": {
            "field": "countries",
            "size": 10
            }
            }
            }
            }
            }
            }
            }
            }


            with a response from Elasticsearch like this (I've omitted some part of json for readability):



            {
            "status-aggs": {
            "buckets": [
            {
            "key": "disabled",
            "doc_count": 2,
            "to-pool": {
            "doc_count": 3,
            "top-poolid": {
            "buckets": [
            {
            "key": "MER",
            "doc_count": 2
            },
            {
            "key": "NER",
            "doc_count": 1
            }
            ]
            }
            },
            "to-package": {
            "doc_count": 3,
            "top-countries": {
            "buckets": [
            {
            "key": "CN",
            "doc_count": 2
            },
            {
            "key": "US",
            "doc_count": 2
            },
            {
            "key": "KR",
            "doc_count": 1
            },
            {
            "key": "UK",
            "doc_count": 1
            }
            ]
            }
            }
            },
            {
            "key": "enabled",
            "doc_count": 1,
            "to-pool": {
            "doc_count": 0,
            "top-poolid": {
            "buckets":
            }
            },
            "to-package": {
            "doc_count": 1,
            "top-countries": {
            "buckets": [
            {
            "key": "AU",
            "doc_count": 1
            },
            {
            "key": "UK",
            "doc_count": 1
            }
            ]
            }
            }
            }
            ]
            }
            }





            share|improve this answer




























              0














              If I followed your structure of the documents - you have types pool and package on the same level (they are siblings) - I wasn't able to achieve exactly your expected results. I also highly doubt that it's possible with those types being siblings.



              However, it's still possible to slice per one field in your doc (status) and later separately slice both by poolId and countries with a query like this:



              {
              "aggs": {
              "status-aggs": {
              "terms": {
              "field": "status",
              "size": 10
              },
              "aggs": {
              "to-pool": {
              "children": {
              "type": "pool"
              },
              "aggs": {
              "top-poolid": {
              "terms": {
              "field": "poolId",
              "size": 10
              }
              }
              }
              },
              "to-package": {
              "children": {
              "type": "package"
              },
              "aggs": {
              "top-countries": {
              "terms": {
              "field": "countries",
              "size": 10
              }
              }
              }
              }
              }
              }
              }
              }


              with a response from Elasticsearch like this (I've omitted some part of json for readability):



              {
              "status-aggs": {
              "buckets": [
              {
              "key": "disabled",
              "doc_count": 2,
              "to-pool": {
              "doc_count": 3,
              "top-poolid": {
              "buckets": [
              {
              "key": "MER",
              "doc_count": 2
              },
              {
              "key": "NER",
              "doc_count": 1
              }
              ]
              }
              },
              "to-package": {
              "doc_count": 3,
              "top-countries": {
              "buckets": [
              {
              "key": "CN",
              "doc_count": 2
              },
              {
              "key": "US",
              "doc_count": 2
              },
              {
              "key": "KR",
              "doc_count": 1
              },
              {
              "key": "UK",
              "doc_count": 1
              }
              ]
              }
              }
              },
              {
              "key": "enabled",
              "doc_count": 1,
              "to-pool": {
              "doc_count": 0,
              "top-poolid": {
              "buckets":
              }
              },
              "to-package": {
              "doc_count": 1,
              "top-countries": {
              "buckets": [
              {
              "key": "AU",
              "doc_count": 1
              },
              {
              "key": "UK",
              "doc_count": 1
              }
              ]
              }
              }
              }
              ]
              }
              }





              share|improve this answer


























                0












                0








                0







                If I followed your structure of the documents - you have types pool and package on the same level (they are siblings) - I wasn't able to achieve exactly your expected results. I also highly doubt that it's possible with those types being siblings.



                However, it's still possible to slice per one field in your doc (status) and later separately slice both by poolId and countries with a query like this:



                {
                "aggs": {
                "status-aggs": {
                "terms": {
                "field": "status",
                "size": 10
                },
                "aggs": {
                "to-pool": {
                "children": {
                "type": "pool"
                },
                "aggs": {
                "top-poolid": {
                "terms": {
                "field": "poolId",
                "size": 10
                }
                }
                }
                },
                "to-package": {
                "children": {
                "type": "package"
                },
                "aggs": {
                "top-countries": {
                "terms": {
                "field": "countries",
                "size": 10
                }
                }
                }
                }
                }
                }
                }
                }


                with a response from Elasticsearch like this (I've omitted some part of json for readability):



                {
                "status-aggs": {
                "buckets": [
                {
                "key": "disabled",
                "doc_count": 2,
                "to-pool": {
                "doc_count": 3,
                "top-poolid": {
                "buckets": [
                {
                "key": "MER",
                "doc_count": 2
                },
                {
                "key": "NER",
                "doc_count": 1
                }
                ]
                }
                },
                "to-package": {
                "doc_count": 3,
                "top-countries": {
                "buckets": [
                {
                "key": "CN",
                "doc_count": 2
                },
                {
                "key": "US",
                "doc_count": 2
                },
                {
                "key": "KR",
                "doc_count": 1
                },
                {
                "key": "UK",
                "doc_count": 1
                }
                ]
                }
                }
                },
                {
                "key": "enabled",
                "doc_count": 1,
                "to-pool": {
                "doc_count": 0,
                "top-poolid": {
                "buckets":
                }
                },
                "to-package": {
                "doc_count": 1,
                "top-countries": {
                "buckets": [
                {
                "key": "AU",
                "doc_count": 1
                },
                {
                "key": "UK",
                "doc_count": 1
                }
                ]
                }
                }
                }
                ]
                }
                }





                share|improve this answer













                If I followed your structure of the documents - you have types pool and package on the same level (they are siblings) - I wasn't able to achieve exactly your expected results. I also highly doubt that it's possible with those types being siblings.



                However, it's still possible to slice per one field in your doc (status) and later separately slice both by poolId and countries with a query like this:



                {
                "aggs": {
                "status-aggs": {
                "terms": {
                "field": "status",
                "size": 10
                },
                "aggs": {
                "to-pool": {
                "children": {
                "type": "pool"
                },
                "aggs": {
                "top-poolid": {
                "terms": {
                "field": "poolId",
                "size": 10
                }
                }
                }
                },
                "to-package": {
                "children": {
                "type": "package"
                },
                "aggs": {
                "top-countries": {
                "terms": {
                "field": "countries",
                "size": 10
                }
                }
                }
                }
                }
                }
                }
                }


                with a response from Elasticsearch like this (I've omitted some part of json for readability):



                {
                "status-aggs": {
                "buckets": [
                {
                "key": "disabled",
                "doc_count": 2,
                "to-pool": {
                "doc_count": 3,
                "top-poolid": {
                "buckets": [
                {
                "key": "MER",
                "doc_count": 2
                },
                {
                "key": "NER",
                "doc_count": 1
                }
                ]
                }
                },
                "to-package": {
                "doc_count": 3,
                "top-countries": {
                "buckets": [
                {
                "key": "CN",
                "doc_count": 2
                },
                {
                "key": "US",
                "doc_count": 2
                },
                {
                "key": "KR",
                "doc_count": 1
                },
                {
                "key": "UK",
                "doc_count": 1
                }
                ]
                }
                }
                },
                {
                "key": "enabled",
                "doc_count": 1,
                "to-pool": {
                "doc_count": 0,
                "top-poolid": {
                "buckets":
                }
                },
                "to-package": {
                "doc_count": 1,
                "top-countries": {
                "buckets": [
                {
                "key": "AU",
                "doc_count": 1
                },
                {
                "key": "UK",
                "doc_count": 1
                }
                ]
                }
                }
                }
                ]
                }
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 3 at 13:29









                MysterionMysterion

                6,89122044




                6,89122044
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014498%2fis-there-any-solution-with-elasticsearch-parent-child-join%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

                    Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

                    A Topological Invariant for $pi_3(U(n))$