Can anyone help identify why one on my entries is not being picked up in my search query?

The search terms are 'hampshire 10k' and the craft_searchindex table for this entry looks like this:

Search Index

The search string param is added as follows (along with a handful of other params) for any search

{ search: '*hampshire* *10k*' }

Unsure if it's down to bug or a templating issue? The keyword search forms part of quite complex entry search.


As Brad mentioned, you’re not getting any results because your search query requires both keywords to be present, but you don’t have any fields that have both keywords.

You can solve this with a hidden Preparse field that stores the content of all fields you want to be cross-searchable. Call it something like “Keywords”, and set its Twig template to:

{{ entry.myCustomFieldHandle }}
{{ entry.myOtherCustomFieldHandle }}

If you want to include any Matrix or relational fields in the keywords, remember the same templating rules apply here as on your front end templates:

{% for block in entry.myMatrixFieldHandle %}
    {{ block.myBlockFieldHandle }}
{% endfor %}

{% for relatedEntry in entry.myEntriesFieldHandel %}
    {{ relatedEntry.title }}
    {{ relatedEntry.myRelatedEntryFieldHandle }}
{% endfor %}

Once you’ve got that field set up and added to your section(s), you’ll need to re-save all of your existing entries before it takes effect. An easy way to do that is to go into the sections’ settings and just click the Save button, which will trigger a “Resaving Elements” background task for all entries in the section.

  • Nice! Going to have to try this out myself... Oct 16 '15 at 15:26
  • Great solution, search has been greatly improved! Nov 10 '15 at 14:39

It's not finding anything because there isn't a single row that has both *hampshire* and *10k* in it.

If you want to search across rows, then you'll want to use { search: '*hampshire* OR *10k*' }.

  • Ah, so would the keywords have to both feature in a specific field or the title to match together without the use of OR? Oct 15 '15 at 17:09
  • That sound about right.
    – Brad Bell
    Oct 15 '15 at 17:21
  • Ok, but how do you search for entries that must have both terms, no matter in what field?
    – carlcs
    Oct 15 '15 at 17:25
  • I think some content strategy can also help you here. (See my answer below). Oct 15 '15 at 19:39
  • Thanks @RitterKnight, completely agree, aware of this but with 9k entries that is a long term goal. @Brad Bell what happens with hampshire OR 10k vs hampshire AND 10k surely the later is the preferred behaviour in this case as the more relevant results should contain both hampshire and 10k and these could be store in different fields (a location field) and a distance category?? Oct 16 '15 at 13:56

Assuming you have a field for distance, since you have a ton of entries, Craft allows dynamic titles (and slugs) so you could generate your titles based on fields already present on your entry.

Something like: {raceLocation} {raceDistance} {whateverOtherField}

Another solution would be to do a little "sanitizing" on the user's search terms to grab the distance before you send the search to Craft. Seems like searching on distance is a pretty common query, so a little AI might be in order. Something like:

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

{# add the distance field to the search #}
{{ query|replace('10k','raceDistance:10k') }}

{% set entries = craft.entries.search(query).order('score') %}

{# rest of the search logic here #}

That should match the terms the user put in. It'll also match the race distance separately. You're probably going to want to use a regex on the distance (\\d+\\s+km?) to make it more flexible...

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.