<p>To get this working I submitted a patch to 'SortByField' - <a href="https://github.com/dannynimmo/craftcms-sortbyfield" rel="nofollow">https://github.com/dannynimmo/craftcms-sortbyfield</a> - to get this plugin to work with Commerce products - and I now do this:</p>

<pre><code>    {% set product_results = craft.commerce.products.search(query).order('score') %}  
    {% set article_results = craft.entries.search(query).order('score') %}
    {% set all_results = [] %}

    {% if product_results|length %}
        {% set all_results = all_results|merge(product_results) %}
    {% endif %}
    {% if article_results|length %}
        {% set all_results = all_results|merge(article_results) %}
    {% endif %}

    {% if all_results|length %}
        {% set all_results = all_results|sortByField('searchScore', 'desc') %}
    etc.
</code></pre>

<p>Which is working well and the combined results make sense to me.</p>

<p>I am just wondering if their is a better/recommended and ideally plugin free way of doing this - i.e. sorting the combined result set by score?</p>

CC BY-SA 3.0
2015-11-18T10:11:24.747Z
Another plugin solution, but it should work without a patch: Supersort (which I hope gets included into Craft core at some point. It basically just brings PHPs sort functions to Twig!).
carlcs
CC BY-SA 3.0
2015-11-19T01:08:30.370Z
@carlcs - It doesn't seem obvious that Supersort can do a sort specifically on the searchScore field though?  I did check it out intially but maybe missed something?
Jeremy Daalder
CC BY-SA 3.0
2015-11-19T08:14:38.907Z
Jeremy, [Supersort's](http://michaelrog.com/craft/lagniappe/supersort) `sortAs` methods work like so: the second param can be a Twig "microtemplate", passed in as a string. Each row is available as a object named `object`, so `{{ object.searchScore }}` should return what you need and is then used as the value to sort by.
carlcs
CC BY-SA 3.0
2015-11-19T22:04:12.363Z
Thanks Carl, very helpful once again!

I accepted my own answer here because in terms of the question it's the best method I can find, and with your recipe for self pagination it should work well.  Thanks for your input!
Jeremy Daalder
CC BY-SA 3.0
2015-11-17T06:38:30.213Z
2015-11-17T06:38:30.213Z
Jeremy Daalder
<p>You could do all of this in PHP. Build your criteria models, merge the results and sort them with <code>usort</code>.</p>

CC BY-SA 3.0
2015-11-18T22:01:02.423Z
Yep for sure, PHP seems the way to go here currently.
Jeremy Daalder
CC BY-SA 3.0
2015-11-19T01:11:51.043Z
The problem beyond a PHP solution, though, is that one can't then do things like paginate the result set.  As pagination relies on an ElementCriteriaModel rather than an array of found results.  

I prefer my solution that uses less custom PHP and an existing, more tested plugin to be honest.

In terms of building my own PHP, I'd certianly do it if I could work out a way to return an ECM that could cope with both products and entries, but I don't think this is possible.

In all, I still think my solution above is the best currently.   Might just have to live without pagination for now...
Jeremy Daalder
CC BY-SA 3.0
2015-11-19T08:19:07.837Z
I think you are right, and you probably can't build a criteria model including mulitple element types, unfortunatelly this is also the case for both plugin solutions we listed in the other answer. But what you could do is to build your own pagination logic, I also had to do this for a project (I even had a criteria model, but there was a bug with the paginate tag!). Will post the code below.
carlcs
CC BY-SA 3.0
2015-11-19T08:37:41.910Z
Posted it as a new question: [Paginate entries when I do not have a criteria model?](http://craftcms.stackexchange.com/q/12455/125)
carlcs
CC BY-SA 3.0
2015-11-18T10:15:40.577Z
2015-11-18T10:15:40.577Z
carlcs
<p>So I have created a combined search for products and entries using the new Craft 2.5 search score.  It's working well, but...I was hoping to be able to do this <strong>without a plugin</strong> if possible ... but it doesn't seem immediately possible to do unless I am missing something.</p>

<p>If anyone knows a way, it would be much appreciated.  In the meantime I'll post the solution I have come up with as an answer below.</p>

<search><sort>
Method (plugin free?) to combine search results across multiple searches (and element types)
# Method (plugin free?) to combine search results across multiple searches (and element types)

(Asked by: username_0 on 2015-11-17)

So I have created a combined search for products and entries using the new Craft 2.5 search score. It's working well, but...I was hoping to be able to do this **without a plugin** if possible ... but it doesn't seem immediately possible to do unless I am missing something.

If anyone knows a way, it would be much appreciated. In the meantime I'll post the solution I have come up with as an answer below.

**Tags:** search, sort


---

## 2 answers

### Answer by username_0 on 2015-11-17. Score: 4 | Accepted answer

To get this working I submitted a patch to 'SortByField' - https://github.com/dannynimmo/craftcms-sortbyfield - to get this plugin to work with Commerce products - and I now do this:
    {% set product_results = craft.commerce.products.search(query).order('score') %}  
    {% set article_results = craft.entries.search(query).order('score') %}
    {% set all_results = [] %}

    {% if product_results|length %}
        {% set all_results = all_results|merge(product_results) %}
    {% endif %}
    {% if article_results|length %}
        {% set all_results = all_results|merge(article_results) %}
    {% endif %}

    {% if all_results|length %}
        {% set all_results = all_results|sortByField('searchScore', 'desc') %}
    etc.

Which is working well and the combined results make sense to me.
I am just wondering if their is a better/recommended and ideally plugin free way of doing this - i.e. sorting the combined result set by score?

> **Comments:**
> **username_1**: Another plugin solution, but it should work without a patch: Supersort (which I hope gets included into Craft core at some point. It basically just brings PHPs sort functions to Twig!).  
> **username_0**: @username_1 - It doesn't seem obvious that Supersort can do a sort specifically on the searchScore field though?  I did check it out intially but maybe missed something?  
> **username_1**: Jeremy, [Supersort's](http://michaelrog.com/craft/lagniappe/supersort) `sortAs` methods work like so: the second param can be a Twig "microtemplate", passed in as a string. Each row is available as a object named `object`, so `{{ object.searchScore }}` should return what you need and is then used as the value to sort by.  
> **username_0**: Thanks Carl, very helpful once again!  I accepted my own answer here because in terms of the question it's the best method I can find, and with your recipe for self pagination it should work well.  Thanks for your input!  

---

### Answer by username_1 on 2015-11-18. Score: 1

You could do all of this in PHP. Build your criteria models, merge the results and sort them with `usort`.

> **Comments:**
> **username_0**: Yep for sure, PHP seems the way to go here currently.  
> **username_0**: The problem beyond a PHP solution, though, is that one can't then do things like paginate the result set.  As pagination relies on an ElementCriteriaModel rather than an array of found results.    I prefer my solution that uses less custom PHP and an existing, more tested plugin to be honest.  In terms of building my own PHP, I'd certianly do it if I could work out a way to return an ECM that could cope with both products and entries, but I don't think this is possible.  In all, I still think my solution above is the best currently.   Might just have to live without pagination for now...  
> **username_1**: I think you are right, and you probably can't build a criteria model including mulitple element types, unfortunatelly this is also the case for both plugin solutions we listed in the other answer. But what you could do is to build your own pagination logic, I also had to do this for a project (I even had a criteria model, but there was a bug with the paginate tag!). Will post the code below.  
> **username_1**: Posted it as a new question: [Paginate entries when I do not have a criteria model?](http://craftcms.stackexchange.com/q/12455/125)  

---

