Site Search with Multiple Keywords

Really pleased to see Site Search working with Multiple Keywords.

I was surprised when I tested that Site Search returns all Pages that have any of the Keywords, not ALL of the Keywords. Not at all what I need.

After this change Site Search returned too many results, most fairly meaningless.

The only way I could see to produce what I need was to update the Site Search List in the Page Layouts.

There might be a better way to handle it but the following code will ensure all the keywords are in the Page Title or the Description. It does just what I need. It also supports a variable number of keywords.

{% assign keyWords = request.request_url.params.searchkeyword | split: " " %}
{% for item in this['items'] %}
    {% assign searchTitle = "" %}
    {% assign searchDesc = "" %}
    {% for keyWord in keyWords %}
        {% if item['Name'] | downcase contains keyword | downcase %}
            {% assign searchTitle = searchTitle | append: "Y" %}
        {% endif %}
        {% if item['Description'] | downcase contains keyword | downcase %}
            {% assign searchDesc = searchDesc | append: "Y" %}
        {% endif %}
    {% endfor %}
    {% if searchTitle | size == keyWords | size  or searchDesc | size == keyWords | size %}
        {% if item['Description'] -%}
                <div class="clearfix search-result">
                    <h4><a href="{{item['Url']}}">{{item['Name']}}</a></h4>
                    <p>{{item['Description'] | strip_html | truncatewords: 50 }}</p>
                </div>
        {% endif -%}  
    {% endif -%}
{% endfor %}

If there is a better way to do it I’d love to hear about it.

I think filtering the results with Liquid, like you’ve done, would be the only solution at present.
Thanks for sharing your code too @peter.medbury - it’s pretty neat!

@A3CS maybe a good one for Treehouse snippets?