<p>I have a site that currently uses a structure where we have a parent entry and children under it. On the parent page we have entries that have been created with matrix blocks. The structure is as follows below in the screenshot:</p>

<p><a href="https://i.sstatic.net/Vvk3v.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Vvk3v.png" alt="enter image description here"></a></p>

<p>On each parent page we want the ability to filter by the child entries under it. So far we have got it working well with a little fiddling but the only issue is that its showing entries for other nested children not related to the parent entry.</p>

<p>So for instance the result we want is:</p>

<p><strong>Bedroom furniture(parent page)
 - Beds
 - Bedroom storage
 - etc
 - etc</strong></p>

<p>But what we are seeing is:</p>

<p><strong>Bedroom furniture(parent page)
 - Bed
 - Armchairs</strong></p>

<p>The filters on the page should only be the child entries that belong under that parent.</p>

<p>Now here is the code we have got in the algolia plugin when indexing the commerce product:</p>

<pre><code>[
            'indexName' =&gt; 'allProducts',
            'elementType' =&gt; 'Commerce_Product',
            'transformer' =&gt; function(Commerce_ProductModel $product) {

                $typesCriteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry, [
                    'section' =&gt; 'products',
                    'relatedTo' =&gt; [
                        'targetElement' =&gt; $product,
                        'field' =&gt; 'productBlock.product',
                    ]
                ]);

                $types = array_map(function($el) {
                  return [
                    'title' =&gt; $el-&gt;title,
                    'id' =&gt; $el-&gt;id,
                    'level' =&gt; $el-&gt;level,
                  ];
                }, $typesCriteria-&gt;find());
                $typesByLevel = [];  

                foreach ($types as $type) {
                  $typesByLevel['types.level' . $type['level']][] = $type;                   
                }                

                $variantAmount = count($variants);

                return array_merge([
                    'title' =&gt; $title,
                    'id' =&gt; $product-&gt;id,
                    'frontImage' =&gt; $fm,
                    'type' =&gt; $product-&gt;type-&gt;name,
                    'types' =&gt; $types,
                    'designer' =&gt; $designer,
                    'brand' =&gt; $brand,
                    'price' =&gt; $totalPrice,
                    'productPrice' =&gt; $product-&gt;defaultVariant-&gt;price,
                    'material' =&gt; $product-&gt;variantMaterials[0]-&gt;title,
                    'url' =&gt; $url,
                    'variants' =&gt; $variantAmount,
                    'isInWishlist' =&gt; craft()-&gt;like-&gt;isLike($product-&gt;id),
                    'quickViewJson' =&gt; $mergedProductData,
                ], $typesByLevel);
            }
        ],
</code></pre>

<p>This code indexes the product and creates the levels in Algolia and we have this result in algolia below:</p>

<p><a href="https://i.sstatic.net/qjnuB.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/qjnuB.png" alt="enter image description here"></a>
<a href="https://i.sstatic.net/YZwNL.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/YZwNL.png" alt="enter image description here"></a></p>

<p>This gets the level but we need the level that is associated with the parent page only not from all other areas of the site.</p>

<p>On the parent page we then get the parent structure Id and do disjunctive faceting with js on that page the get the filters that belong to that page like so:</p>

<pre><code>if( $('#product-search-config').length ){
 // type gets the entry.id
        const type = $('#product-search-config').data('type');

            searchProducts.addWidget({
              init: function(data) {
                console.log(data)
              },
              getConfiguration: function() {
                return {
                  disjunctiveFacets: ['types'],
                  disjunctiveFacetsRefinements: {
                    'types.level1.id': [type],
                  }
                };
              }
            });

    };
</code></pre>

<p>This needs to only find the filter relating to that parent as you see in the menu above.</p>

<p>Can this be done?</p>

<p>Thanks</p>

CC BY-SA 3.0
2018-02-28T03:50:32.757Z
Your question is really hard to understanding. Let's sum it up: you have a structured section called products. What do you mean with "filtering children under those entries" you can get all children with the `children` function. There is usually no need to filter them somehow. With your current structure it should be impossible to get `armchair` when you are fetching the children for `bedroom`. And you can't create entries with matrix blocks. You can relate entries with a field inside a matrix but I don't have any idea what you mean with "entries that have been created with matrix blocks"
Robin Schambach
CC BY-SA 3.0
2018-02-28T12:54:50.467Z
Basically the children under each parent structure Entry need to be the filters when you land on the parent structure Entry page. These filters need to be added to Algolia with indexing and the children must only show on the parent structure entry that belong to that parent only. The issue is that in Algolia that relationship is not working.
M dunbavan
CC BY-SA 3.0
2018-02-28T12:58:13.003Z
Then again, one child will only belong to one parent. So your `entry.children` will return an array of entries that are totally unique. there is no entry that will return the same child like another one
Robin Schambach
CC BY-SA 3.0
2018-02-28T13:00:34.587Z
Hmm yes I know that but we aren’t using the native craft templates we are using a plugin to index that in Algolia
M dunbavan
CC BY-SA 3.0
2018-02-28T13:01:59.763Z
What does this has to do with your question? There is no difference between calling functions in twig or php
Robin Schambach
CC BY-SA 3.0
2018-02-28T13:04:32.687Z
No it doesn’t I just wanted to clarify :) I think the real hard thing to do is get the correct filters for the parent they belong to
M dunbavan
CC BY-SA 3.0
2018-03-03T12:57:41.920Z
Were you able to solve your problem? Otherwise we can talk in the chat because I didn't quite understand what you want exactly but it still sounds trivial for me
Robin Schambach
CC BY-SA 3.0
2018-03-03T13:30:28.007Z
I can come on here and chat or we could go onto the craft cms Slack and chat on there. My username is mdunbavan
M dunbavan
CC BY-SA 3.0
2018-03-03T13:32:06.497Z
We can use this room https://chat.stackexchange.com/rooms/70444/plugin-developement I never used slack, don't know what that is
Robin Schambach
<entries><structure><php><filtering>
How can I filter with facets in algolia with nested structures?
# How can I filter with facets in algolia with nested structures?

(Asked by: username_0 on 2018-02-27)

I have a site that currently uses a structure where we have a parent entry and children under it. On the parent page we have entries that have been created with matrix blocks. The structure is as follows below in the screenshot:

On each parent page we want the ability to filter by the child entries under it. So far we have got it working well with a little fiddling but the only issue is that its showing entries for other nested children not related to the parent entry.
So for instance the result we want is:
Bedroom furniture(parent page)
 - Beds
 - Bedroom storage
 - etc
 - etc
But what we are seeing is:
Bedroom furniture(parent page)
 - Bed
 - Armchairs
The filters on the page should only be the child entries that belong under that parent.
Now here is the code we have got in the algolia plugin when indexing the commerce product:
[
            'indexName' => 'allProducts',
            'elementType' => 'Commerce_Product',
            'transformer' => function(Commerce_ProductModel $product) {

                $typesCriteria = craft()->elements->getCriteria(ElementType::Entry, [
                    'section' => 'products',
                    'relatedTo' => [
                        'targetElement' => $product,
                        'field' => 'productBlock.product',
                    ]
                ]);

                $types = array_map(function($el) {
                  return [
                    'title' => $el->title,
                    'id' => $el->id,
                    'level' => $el->level,
                  ];
                }, $typesCriteria->find());
                $typesByLevel = [];  

                foreach ($types as $type) {
                  $typesByLevel['types.level' . $type['level']][] = $type;                   
                }                

                $variantAmount = count($variants);

                return array_merge([
                    'title' => $title,
                    'id' => $product->id,
                    'frontImage' => $fm,
                    'type' => $product->type->name,
                    'types' => $types,
                    'designer' => $designer,
                    'brand' => $brand,
                    'price' => $totalPrice,
                    'productPrice' => $product->defaultVariant->price,
                    'material' => $product->variantMaterials[0]->title,
                    'url' => $url,
                    'variants' => $variantAmount,
                    'isInWishlist' => craft()->like->isLike($product->id),
                    'quickViewJson' => $mergedProductData,
                ], $typesByLevel);
            }
        ],

This code indexes the product and creates the levels in Algolia and we have this result in algolia below:


This gets the level but we need the level that is associated with the parent page only not from all other areas of the site.
On the parent page we then get the parent structure Id and do disjunctive faceting with js on that page the get the filters that belong to that page like so:
if( $('#product-search-config').length ){
 // type gets the entry.id
        const type = $('#product-search-config').data('type');

            searchProducts.addWidget({
              init: function(data) {
                console.log(data)
              },
              getConfiguration: function() {
                return {
                  disjunctiveFacets: ['types'],
                  disjunctiveFacetsRefinements: {
                    'types.level1.id': [type],
                  }
                };
              }
            });

    };

This needs to only find the filter relating to that parent as you see in the menu above.
Can this be done?
Thanks

**Tags:** entries, structure, php, filtering
> **Comments:**
> **username_1**: Your question is really hard to understanding. Let's sum it up: you have a structured section called products. What do you mean with "filtering children under those entries" you can get all children with the `children` function. There is usually no need to filter them somehow. With your current structure it should be impossible to get `armchair` when you are fetching the children for `bedroom`. And you can't create entries with matrix blocks. You can relate entries with a field inside a matrix but I don't have any idea what you mean with "entries that have been created with matrix blocks"  
> **username_0**: Basically the children under each parent structure Entry need to be the filters when you land on the parent structure Entry page. These filters need to be added to Algolia with indexing and the children must only show on the parent structure entry that belong to that parent only. The issue is that in Algolia that relationship is not working.  
> **username_1**: Then again, one child will only belong to one parent. So your `entry.children` will return an array of entries that are totally unique. there is no entry that will return the same child like another one  
> **username_0**: Hmm yes I know that but we aren’t using the native craft templates we are using a plugin to index that in Algolia  
> **username_1**: What does this has to do with your question? There is no difference between calling functions in twig or php  
> **username_0**: No it doesn’t I just wanted to clarify :) I think the real hard thing to do is get the correct filters for the parent they belong to  
> **username_1**: Were you able to solve your problem? Otherwise we can talk in the chat because I didn't quite understand what you want exactly but it still sounds trivial for me  
> **username_0**: I can come on here and chat or we could go onto the craft cms Slack and chat on there. My username is mdunbavan  
> **username_1**: We can use this room https://chat.stackexchange.com/rooms/70444/plugin-developement I never used slack, don't know what that is  

---

## 0 answers

