1

Is it possible to include plural variations? For example if I search for 'apple' but the entry includes 'apples' it appears.

Or would this need to be approached by getting these variations into the entries via tags or similar?

{% set query = craft.request.getParam('q') %}
{% set entries = craft.entries.search(query).order('score').section('newsEvents') %}


{% if entries|length %}
<h3>{{ entries|length }} result(s):</h3>

    {% for entry in entries %}
         ...
    {% endfor %}

    {% else %}
     <p>Your search for "{{ query }}" didn't return any results.</p>

{% endif %}             
3

You can certainly achieve a very rudimentary form of plural search by following the recommendations in AbbeyDesign's answer.

However, it comes with an important caveat: it won't work for plurals which do not include the singular form.

For example, baby / babies, knife / knives, or tooth / teeth.

If you need to accommodate this requirement, Craft's built-in search probably isn't going to cut it, and you'll need to go with something a bit more sophisticated.

I haven't personally tried it, but I think Search Plus can handle this. It's basically a convenient wrapper around Algolia, a very sophisticated search tool, which automatically handles pluralisation.

| improve this answer | |
  • Ok thank you. I've added subRight. If I needed to handle doing it the other way around (searching 'apples' and returning 'apple') is there a more manual way of doing it such as adding tags to cover these variations? I assume if they don't output these tags on the front-end they wouldn't appear in results, I'd have to do a display:none. – sarah3585 May 9 '17 at 8:51
  • 1
    You could, in theory, create a "keywords" field, in which you explicitly list the singular and plural form of each desired search term, and tell Craft to search against that field (as described in the documentation). However, this would require an enormous amount of patience, discipline, and indeed foresight on the part of the content author, and wouldn't scale at all. – Stephen Lewis May 9 '17 at 14:21
2

You'll want to enable fuzzy search as explained here in Craft Docs:

Enable Fuzzy Search

But there's a little caveat you'll run into when you search for "apples" and expect to return "apple"

Fuzzy Search and Plurals

| improve this answer | |
  • 1
    Important note: setting the subRight config to true will ensure that "apple" also matches "apples". Setting the subLeft config to true will result in "apple" also matching "grapple", which probably isn't what you want. – Stephen Lewis May 8 '17 at 16:15

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.