1

I currently have a search box which returns results, but only seems to return results if the search term is an exact match. For example, if i search hospital I will get 5 results back with the name hospital in the title, but if i search hospitals i will get no matches found. I would like the search to return results that are close to the search term as well. This is how i have it set up.

This is my search box. It returns to the same page as my index page has an {% include "components/search" %} to display on the homepage.

   <form class="navbar-search" action="{{ url('') }}">
      <div class="input-group add-on">
        <input class="form-control" placeholder="Search" name="q" id="srch-term" type="search">
        <div class="input-group-btn">
        <button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
        </div>
      </div>
  </form>

Then my search component is this:

{% set query = craft.request.getParam('q') %}

{% paginate craft.entries.section('Organizations').search('title:' ~ query).order('score').limit(10) as entries %}
    {% if entries|length < 1 %}
        {% paginate craft.entries.section('Organizations').search('organizationSummary:' ~ query).order('score').limit(10) as entries %}

If the title returns no search results I wanted it to search through a summary section instead. Currently it works very well except for only returning exact terms. I'm a bit stuck as I'm ordering by score as shown in the Craft documentation so not sure what I am missing here. Any ideas?

Thanks

1

Lots going on here, but I'll take a stab at it (in the future, it's helpful if you have multiple questions/issues to make separate posts for them).

Your questions of "why isn't this search term matching" probably comes down to the fact that by default, Craft doesn't do fuzzy matching. You can use wildcards (*) to change that behavior on a per-search basis. You can see that syntax here along with all of the other search syntax that Craft supports: https://craftcms.com/docs/searching

Also of note from that page is the defaultSearchTerms config setting, where you can change that behavior.

Then my search component is this:

{% set query = craft.request.getParam('q') %}

{% paginate craft.entries.section('Organizations').search('title:' ~ query).order('score').limit(10) as entries %}
    {% if entries|length < 1 %}
        {% paginate craft.entries.section('Organizations').search('organizationSummary:' ~ query).order('score').limit(10) as entries %}

That's probably not working as expected because not only can you not nest paginate tags, you can technically only have one paginate tag per request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.