<![CDATA[ simpleSearch fieldPotency not sorting the results FIXED - My Forums]]> https://forums.modx.com/thread/?thread=102291 <![CDATA[simpleSearch fieldPotency not sorting the results FIXED]]> https://forums.modx.com/thread/102291/simplesearch-fieldpotency-not-sorting-the-results-fixed#dis-post-551205
I had some issues with &fieldPotency. The simpleSearch call I used was:

[[!SimpleSearch?
&searchStyle=`partial`
&docFields=`pagetitle,longtitle,description,introtext`
&perPage=`50`
&fieldPotency=`pagetitle:10,longtitle:1,description:1,introtext:1`
]]

No matter what values for pagetitle I set the sorted results seemed to be randomly ordered. Resourses which had the search term in their title were listed after those where the term was used only in the description tag even when pagetitle fieldPotency values were many times higher than those set for the description.

The problem

When the search term is used to count the number of matches for each &docField the comparison seems to be CASE SENSITIVE. Since the titles of my resources all start with a capital letter when compared to search term the result was no match. Thus the potency values for pagetitle were simply ingroned. To understand this behavior better check lines 126-132 of simplesearchdriver.php located in model/simplesearch/driver/

foreach ($this->search->searchArray as $term) {
$queryTerm = preg_quote($term,'/');
$regex = ($searchStyle == 'partial') ? "/{$queryTerm}/i" : "/\b{$queryTerm}\b/i";
$numberOfMatches = preg_match_all($regex, $resource->{$field}, $matches);
if (empty($this->searchScores[$resourceId])) $this->searchScores[$resourceId] = 0;
$this->searchScores[$resourceId] += $numberOfMatches * $potency;
}

In my particular case the problem was 'solved' by simply changing the first letter of $term to be capital before checking for matches by adding those 3 lines of code:

foreach ($this->search->searchArray as $term) {

$first_letter = mb_strtoupper(mb_substr($term, 0, 1, "UTF-8"), "UTF-8");
$term_end = mb_substr($term, 1, mb_strlen($term, "UTF-8"), "UTF-8");
$term = $first_letter . $str_end;

$queryTerm = preg_quote($term,'/');
$regex = ($searchStyle == 'partial') ? "/{$queryTerm}/i" : "/\b{$queryTerm}\b/i";
$numberOfMatches = preg_match_all($regex, $resource->{$field}, $matches);
if (empty($this->searchScores[$resourceId])) $this->searchScores[$resourceId] = 0;
$this->searchScores[$resourceId] += $numberOfMatches * $potency;
}

I am not a programmer so this 'solution' may turned out to be ineffective or even wrong. If you have better ideas how this issue could be avoided I would greatly appreciate if you share you knowledge. Thanks in advance.]]>
yossarian May 25, 2017, 05:30 AM https://forums.modx.com/thread/102291/simplesearch-fieldpotency-not-sorting-the-results-fixed#dis-post-551205
<![CDATA[Re: simpleSearch fieldPotency not sorting the results FIXED]]> https://forums.modx.com/thread/102291/simplesearch-fieldpotency-not-sorting-the-results-fixed#dis-post-551247
]]>
BobRay May 26, 2017, 04:35 PM https://forums.modx.com/thread/102291/simplesearch-fieldpotency-not-sorting-the-results-fixed#dis-post-551247
<![CDATA[Re: simpleSearch fieldPotency not sorting the results FIXED (Best Answer)]]> https://forums.modx.com/thread/102291/simplesearch-fieldpotency-not-sorting-the-results-fixed#dis-post-551226 yossarian May 26, 2017, 05:23 AM https://forums.modx.com/thread/102291/simplesearch-fieldpotency-not-sorting-the-results-fixed#dis-post-551226 <![CDATA[Re: simpleSearch fieldPotency not sorting the results FIXED]]> https://forums.modx.com/thread/102291/simplesearch-fieldpotency-not-sorting-the-results-fixed#dis-post-551225 https://github.com/modxcms/SimpleSearch/issues]]> BobRay May 26, 2017, 05:17 AM https://forums.modx.com/thread/102291/simplesearch-fieldpotency-not-sorting-the-results-fixed#dis-post-551225