Search results contain searched term as a string, but not as its own word!

I have been having issues with the Kirby search since I implemented it:

If I search for “hi” when none of my content has “hi” as a word within it, the search still returns several results, because each has the word “which”, as it contains “hi” inside of it: which.

However, I really would prefer a more standard search functionality: one that would return 0 results in the above case because no content has the word “hi”.

Here is the code within my search controller:

 <?php 

return function($site, $pages, $page) {

  $query   = get('q');
  $results = search($pages->index(), $query, 'title|text');

  return array(
'query'   => $query,
'results' => $results,
  );

};

?>

What is the expected behavior of Kirby’s search? How can I fix this??? Thanks so much for any help!

The search method has different options, including search for words, see the documentation examples: https://getkirby.com/docs/cheatsheet/site/search

Thanks, however, the problem is that I can only seem to apply one of those possible parameters/defaults in the array list in the link at a time. And I already need to use the one that filters ‘fields’.

I also need to know how to apply the 'words' => true parameter to the code shown in my controller (code above), which uses a slightly different code than shown in the search documentation examples. This is because I am calling the plugin that allows for “and” searches instead of just “or” searches when entering more than one word.

I tried adding to the “return array” part of my code, but it didn’t work:
return array(
‘query’ => $query,
‘results’ => $results,
‘words’ => true
);

Either way, with my controller code and with trying the method in the documentation examples, I would need to know how to call more than one “possible parameters/defaults”? Does anyone know how to make this happen? Thanks!!!

No, the option has to be part of the options array of the search method, not the return array of the controller. The syntax is like this:

search($pages, $query, $params = array())

So, that makes it:

$results = search($pages->index(), $query, array('words' => true, 'fields' => ['title', 'text'] ));

I just tested that exact line of code, but it’s not working…now both the ‘words’ isn’t working, and also the filtering by just ‘text’ and ‘title’ is also not working.

Does it work for you? Any ideas? Thanks so much for the suggestions!

Well, yes, the words option does not work with the plugin, because the plugin has been modified to work with exact matches only, no matter if these are within word boundaries or not. You would have to find a regex pattern that does not ignore word boundaries.

If you change line 30 in the plugin file from

if($matches = preg_match_all('!' . preg_quote($query) . '!i', $data[$key], $r)) {

to

if($matches = preg_match_all('!\b' . preg_quote($query) . '\b!i', $data[$key], $r)) {

you should get the desired results (I hope).

If you want to make that optional, you can use the word option and put in some if statement.

Thank you @texnixe! Now it is working perfectly as I want and expect it to.