How to use search to include multiple "words"


#1

How can I have $site->search(‘my awesome search’); search for “my awesome search” instead of “my” || “awesome” || “search”?

I want to include only the pages which have the string “my awesome search” on them (including spaces), and exclude pages which only have “my” or “awesome” or “search” on them?

I hope I make myself clear enough?

Thanks!
Bart


Search results using "and" instead "or" option
#2

Unfortunately, that’s currently not possible.

You can however create your own implementation of $pages->search(). If you comment out this and the following line and add $page->searchHits += $matches; back in at line 219, it should do exactly what you want.

I have opened an issue over at GitHub, so maybe this will become a feature in a future version.


Search by string and not by separate words
Search() function: general questions
#3

Thank you for your replay @lukasbestle.

Is this something I can do as a plugin in the mean time (override the default search)?


#4

Sure! I have created a little plugin for you. It’s untested, so please let me know if it works.

Just drop it into your plugins directory and use search($pages, $query, $params) instead of $pages->search($query, $params).


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

I can confirm this works.

thank you very much @lukasbestle


#6

I am just trying now the plugin and if I search “telefunken 12ax7” returns me few results, but if I search “12ax7 telefunken” returns no results.

Is there something that could avoid this problem to happen?


#7

The plugin searches for complete string matches and not for the separate words individually, no matter in what order they appear in the text.


#8

This deserves to be in core, at least as an option. I think it’s a behaviour expected by lots of users.


#9

Thank you for the feedback, I have added your +1 to the GitHub issue.


#10

Where do you find this to change it out? $pages->search($query, $params)


#11

I’m currently using a controller

<?php 

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

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

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

};

?>


#12

@bbarclay Are you using Lukas’ plugin or do you want to make changes to the core?


#13

@texnixe i just want my search results to be able to filter whole search terms. For instance “airline seats” and not airline and seats.

So either way. Hopefully I don’t have to modify the core.


#14

You can use the Lukas little plugin linked above?


#15

Yeah, that’s what i was going to do. Although it says

Just drop it into your plugins directory and use search($pages, $query, $params) instead of $pages->search($query, $params).


#16

I dropped this in my plugins folder. But when i search for something like, airline seats. It still pulls airlines and seats.

Not just “airline seats”


#17

Hm, it works in my test installation … Have you changed your controller?

From

$results = $site->search($query, 'title|text');

to

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

#18

I wonder if this has something to do with that custom controller. I was looking for search solutions in the past and these files were all included. So i removed my custom controller and it broke my search functionality.


#19

I’m using the advice from here

https://getkirby.com/docs/cookbook/search

Telling me to create another controller in site/controller

Did your demo installation include that? @texnixe


#20

Let me try this. Then I will get back to you.