How to use search to include multiple "words"


#21

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


#22

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() 
  );

};

?>


#23

Great :slight_smile: !

(and some more useless chars)


#24

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!


#25

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


#26

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?


#27

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


#28

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


#29

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.


#30

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


#31

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.