How to use search to include multiple "words"

I was lazy and just dropped it into an existing project with search functionality.

This line worked. Thanks :slight_smile: @texnixe My controller looks like this now.

 <?php 

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

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

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

};

?>

Great :slight_smile: !

(and some more useless chars)

Hi @lukasbestle!

I’m trying to implement this to one of my websites but i’m a little confused with the links you provide it.
Maybe by adding new code to the files the links are moved and don’t reference to the same lines anymore?

Any help would be appreciated.

Thanks!

If you compare the plugin gist with the original code of the search method, you can see where @lukasbestle made the changes.

Hi everyone!

I use @lukasbestle plugin. It runs correctly but if I search “my search”, I want it searchs “my search”, “my (…) search”, “search my”.

Is this possible in some way?

That should actually be the behavior of the built-in $pages->search() method. Have you tried it without my plugin?

Yes but without it searchs “my search”, “my … search”, “search my”, “my” and “search”. I want to obviate the last two.

You are right, the current algorithm is an “OR” algorithm.

I have added the $pages->search() method to our list of methods to refactor. Maybe it makes sense to add support for literal strings (strings in quotes) and at the same time make it configurable whether to use AND or OR when filtering the collection.

1 Like

Thanks @lukasbestle. But you think I can solve with some way now?

The check whether a page matches the search query happens in this line.

What you’d need to do is to copy the code into a plugin like in my plugin above and then change the condition to check for all words in the $searchwords array separately. Also you should delete these lines from your plugin as these lines only make sense in the RegEx context.

Please let me know in case you get stuck.

This is an old post, but I’m wondering if this issue has been address with the ‘words’ parameter. For example…

// search for full words only
$results = $page->search('my awesome->search', array('words' => true));

What exactly does this do?

Without this option set to true, Kirby also returns search results that contain the search string as a substring.

Gotcha. Thank you greatly.

Hi! I am also searching for a solution to this, but unfortunately don’t know where to start (coding skills are limited at best). How do I change search to an “AND” algorithm? Thanks thanks thanks

What exactly do you need? @lukasbestle created this plugin.

I want basically what @jciaurrant was trying to solve for above:

Basically to not include every possible post that includes either search word, but only articles that have both search words, whether or not they are in a string. The plugin is for strings, or?

Thanks

Yes, the plugin is for complete strings, that is, it would find “my awesome search” instead of any of the individual strings. but not “my super awesome search”. You’d need a more complicated regex for this to work.

Right. Probably above my ability then. @lukasbestle seemed to hint that this may be reworked in the future, is there a way to get pinged if/when it does?

Changes like this will not make their way into Kirby 2 anymore. That means, if it makes it into a new version, we won’t see this before the advent of Kirby 3.