In my article listing page, I have a search function. I define the article query like so:
{% set orderByRule = 'postDate DESC' %} {# Default order. #}
{% if searchQuery is not null %}
{% set orderByRule = 'score' %}
{% endif %}
{% set articleEntries = craft.entries()
.section('articles')
.search(search|trim)
.orderBy(orderByRule)
.relatedTo(categoryQuery)
.limit(21)
%}
The articles have a custom field called articleBody
instead of the regular body
.
Now, the searchScore
I get on each article seems weird to me, because it doesn't seem to care about the body text at all. However I recently realized this is probably because searchScore
is calculated on title, slug and body, only. In other words, searchScore doesn't care about my articleBody
field.
In case it matters, my config/general.php
file has this in terms of customization for the search functionality:
'defaultSearchTermOptions' => [
'attribute' => 'title',
'subLeft' => true,
'subRight' => true,
],
I can find very little documentation on the subject in general to be honest. I did find this article, but it doesn't talk about customizing the parameters with which searchScore is calculated.
So how do I customize what pieces of content searchScore is calculated on? Can it even be done?
articleBody
? Make sure in the field settings that "use this field's values as search keywords" is ticked. Then rebuild the search index if necessary.searchScore
.