6

Is there a way to control the weighting of certain fields in Craft when using search in the front-end?

I have a Products Channel which I am trying to search against and I want to be able to boost certain entries, ideally via specific keywords.

I have tried to do this via a tag field but this doesn't seem to have a noticeable impact on the results.

Take the following query:

{% set tmpEntries = craft.entries({
section: 'products',
search: q,
orderBy: 'score',
with: ['product_preview','productCategory','searchWeighting'],
}).all() %}

If I carry this out against the term LAS8001 I get the following results and scores:

  • LAS8001M (83.25)
  • LAS8001 si (46.4841269841)
  • LAS9010 (0.488017429194)

I'm struggling to work out why the weighting is so different for the first two results. Both entries have associated assets with the search term in file names. In trying to boost the second result I have added a tag field and included the search term in that field but with only a negligible boost in search score (from 46.4841269841 to 53.1507936508).

Is there any documentation on how the search algorithms are calculated or ways in which we can control the weighting of specific fields over others?

Cole

1
  • I'm getting odd results too, so would be good to know. I search for "food" and an entry with 2 instances scores double an entry with 4 references to the keyword (after search re-index). Found this: craftcms.stackexchange.com/questions/2376/… May 22, 2019 at 16:18

1 Answer 1

3

Rather than trying to explain it myself, I prefer to link to the following article: https://gregorterrill.com/posts/search-scores-in-craft-cms. Even though the article is from 2021, it does a great job of explaining how search scoring works in Craft CMS by analyzing its source code and giving examples.

That said the article closes with a few key findings such as:

  1. Titles are the most important field for search result scoring.

  2. Don't remove important keywords from slugs as this is another way to score highly.

  3. Titles, slugs, and field values with fewer words will always be stronger matches to individual search terms.

  4. Search keywords in body copy fields with high word counts will probably be rounded out of existence unless the keyword appears multiple times.

  5. You might consider adding a "Search Keywords" field to your entries that doesn't render on the front-end but gives you a place to put in some terms that you really want the entry to appear for when they're searched.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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