<p>You can search each section separately, count the totals of each and then merge them back together to form your full results.</p>

<pre><code>{# Search each section separately #}
{% set query = craft.request.getParam('query') %}
{% set mapMarkerResults = craft.weightedSearch.substringSearch(query, ['mapMarker']) %}
{% set blogResults = craft.weightedSearch.substringSearch(query, ['blog']) %}

{# Merge the two sections' results together #}
{% set fullResults = mapMarkerResults|merge(blogResults) %}
</code></pre>

<p>Because Twig's <code>|merge</code> filter will add all the <code>blogResults</code> after <code>mapMarkerResults</code> the sort order will be wrong. Using the <a href="https://github.com/TopShelfCraft/SuperSort" rel="nofollow noreferrer">SuperSort plugin</a> (version 1.2.0 supports Craft 2) you can fix that.</p>

<pre><code>{# Use SuperSort's rsortAs method to sort the results by Weighted Search's score in descending order #}
{% set fullResults = fullResults|supersort('rsortAs', '{score}') %}
</code></pre>

<p>Now you can handle the output.</p>

<pre><code>There's {{ blogResults|length }} results from the Blog section.
There's {{ mapMarkerResults|length }} results from the Map Marker section.

{% for searchResult in fullResults %}
    {{ searchResult.title }}
{% endfor %}
</code></pre>

<p><strong>Note:</strong> SuperSort might not sort exactly the same as Weighted Search. They will correctly be sorted by score in descending order, but it might not break ties the same way (e.g. two entries with a score of 1). If it's important for the search results to be in <strong>exactly</strong> the same order as Weighted Search you should run the search again. This is obviously more server intensive and I wouldn't recommend it as it'll slow down your page loads.</p>

<pre><code>{% set mapMarkerResultsCount = craft.weightedSearch.substringSearch(query, ['mapMarker'])|length %}
{% set blogResultsCount = craft.weightedSearch.substringSearch(query, ['blog'])|length %}
{% set fullResults = craft.weightedSearch.substringSearch(query, ['people', 'blog']) %}
</code></pre>

CC BY-SA 4.0
2018-10-28T22:55:54.370Z
2018-10-29T23:30:39.047Z
2018-10-29T23:30:39.047Z
Simon Kuran
<p>Can't you just use the same filter for your <code>typesToSearch</code> parameter?</p>

<pre><code>typesToSearch|length
</code></pre>

CC BY-SA 4.0
2018-09-28T07:43:08.740Z
This will output `2` and I want an output like : `Blog: 3, mapMarker: 6`. Is this not clear in my question? Because then I will edit it.
mike
CC BY-SA 4.0
2018-09-27T21:32:52.813Z
2018-09-27T21:32:52.813Z
Robin Schambach
<p>I am using the <a href="https://github.com/wja-no/craft-weighted-search" rel="nofollow noreferrer">weighted search</a> plugin to inplement a search functionality in my site. It works as it should but I am looking for a way to tell how much results were found of each type. Where type for example is <code>blog</code> and <code>mapMarker</code>.</p>

<p>This is how the search bar gets its results:</p>

<pre><code>{% set query = craft.request.getParam('query') %}
{% set typesToSearch = ['mapMarker', 'blog'] %} &lt;- This is what I want to count
{% set results = craft.weightedSearch.substringSearch(query, typesToSearch) %}
</code></pre>

<p>And then I have a block of code that shows all my results, and this is the place where I want to tell the end user how much results is from each <code>typesToSearch</code>.</p>

<p>How do I implement such feature in this code? :</p>

<pre><code>{% for searchResult in results %}
    {{ searchResult.entry.type|length }} &lt;- I tried this but this doesn't work.
{% endfor %}
</code></pre>

<templating><plugins><database><craft2>
Counting section handle in TWIG with weighted search plugin
# Counting section handle in TWIG with weighted search plugin

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

I am using the weighted search plugin to inplement a search functionality in my site. It works as it should but I am looking for a way to tell how much results were found of each type. Where type for example is blog and mapMarker.
This is how the search bar gets its results:
{% set query = craft.request.getParam('query') %}
{% set typesToSearch = ['mapMarker', 'blog'] %} <- This is what I want to count
{% set results = craft.weightedSearch.substringSearch(query, typesToSearch) %}

And then I have a block of code that shows all my results, and this is the place where I want to tell the end user how much results is from each typesToSearch.
How do I implement such feature in this code? :
{% for searchResult in results %}
    {{ searchResult.entry.type|length }} <- I tried this but this doesn't work.
{% endfor %}

**Tags:** templating, plugins, database, craft2

---

## 2 answers

### Answer by username_1 on 2018-10-28. Score: 1

You can search each section separately, count the totals of each and then merge them back together to form your full results.
{# Search each section separately #}
{% set query = craft.request.getParam('query') %}
{% set mapMarkerResults = craft.weightedSearch.substringSearch(query, ['mapMarker']) %}
{% set blogResults = craft.weightedSearch.substringSearch(query, ['blog']) %}

{# Merge the two sections' results together #}
{% set fullResults = mapMarkerResults|merge(blogResults) %}

Because Twig's |merge filter will add all the blogResults after mapMarkerResults the sort order will be wrong. Using the SuperSort plugin (version 1.2.0 supports Craft 2) you can fix that.
{# Use SuperSort's rsortAs method to sort the results by Weighted Search's score in descending order #}
{% set fullResults = fullResults|supersort('rsortAs', '{score}') %}

Now you can handle the output.
There's {{ blogResults|length }} results from the Blog section.
There's {{ mapMarkerResults|length }} results from the Map Marker section.

{% for searchResult in fullResults %}
    {{ searchResult.title }}
{% endfor %}

Note: SuperSort might not sort exactly the same as Weighted Search. They will correctly be sorted by score in descending order, but it might not break ties the same way (e.g. two entries with a score of 1). If it's important for the search results to be in exactly the same order as Weighted Search you should run the search again. This is obviously more server intensive and I wouldn't recommend it as it'll slow down your page loads.
{% set mapMarkerResultsCount = craft.weightedSearch.substringSearch(query, ['mapMarker'])|length %}
{% set blogResultsCount = craft.weightedSearch.substringSearch(query, ['blog'])|length %}
{% set fullResults = craft.weightedSearch.substringSearch(query, ['people', 'blog']) %}


---

### Answer by username_2 on 2018-09-27. Score: 0

Can't you just use the same filter for your typesToSearch parameter?
typesToSearch|length

> **Comments:**
> **username_0**: This will output `2` and I want an output like : `Blog: 3, mapMarker: 6`. Is this not clear in my question? Because then I will edit it.  

---

