Search tool that search sentences

Hello!

I made a search box to search words but I would like that when I search more than one words will only show that specific words not every single word.
For example if I write:

House
it work and my search find all houses
but if I search The House
It will find all the words that are The and House and not only “The house”

Is there a way to have a selective search?

Here is my code:

<?php

$query   = get('q');
$results = page('notes')->search($query, 'title|text');
$results = $results->paginate(20);

?>
<div class="searchcontainer">
<form class="search1" action="<?= $page->url() ?>" method="GET">
  <input id=search2 type="search" name="q" value="<?php echo esc($query) ?>">
  <input id=search2 type="submit" value="Search">
</form>
</div>

<?php if ($results->isNotEmpty()) : ?>
<ul id=search3>
  <?php foreach($results as $result) : ?>
  <li>
    <a href="<?php echo $result->url() ?>">
      <?php echo $result->title()->html() ?>
    </a>
  </li>
  <?php endforeach ?>
</ul>
<?php endif ?>

Thanks!

You would have to create your own search method, there is an example somewhere here for Kirby 2 that you could adapt to Kirby 3.

Install this plugin:

Then use $pages->bettersearch($options) instead of $pages->search($options).

1 Like

I installed this plugin and set $site->bettersearch($query, 'title|text'). I get an error in my results template saying $result->title() or $result->url() are null.
When I dump $result, I get :

Kirby\Cms\Field Object
(
    [bettersearch] => 
)

This is my site controller

<?php

return function ($site) {

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

  return [
    'query'   => $query,
    'results' => $results,
  ];

};

And my search.php template

 <?php foreach ($results as $result): ?>
  <li>
    <a href="<?= $result->url() ?>">
      <?= $result->title() ?>
    </a>
  </li>
  <?php endforeach ?>

Running on Kirby 3.3.5

There is no $site->bettersearch()method, but a $pages->bettersearch() method. So if you replace that, it should work. For a site wide search, you probably have to use $site->index()->bettersearch().

1 Like

Hi, @texnixe is right. The $site method is not implemented, but there’s an easy workaround as she explained above.

If you’ld like it to be added to the plugin, can you please add a request on github? Thx!