distantnative / search-for-kirby

Kirby 3 plugin for adding a search index (sqlite or Algolia).
43 stars 3 forks source link

Error when search term contains special character #43

Closed tobiasfabian closed 3 years ago

tobiasfabian commented 3 years ago

Describe the bug

I’m getting an error message Undefined index: hits when the search term contains a special character (e.g. -, !, ?).

Steps to reproduce

<?php
$site->search('tree-house');

or

<?php
$site->search('who?');

Expected behavior

Should return search results for “tree-house” or “who?”.

Additional context

Whoops\Exception\ErrorException thrown with message "Undefined index: hits"

Stacktrace:
#15 Whoops\Exception\ErrorException in /…/site/plugins/search/src/models/Index.php:129
#14 Whoops\Run:handleError in /…/site/plugins/search/src/models/Index.php:129
#13 Kirby\Search\Index:search in /…/site/plugins/search/src/helpers.php:8
#12 search in /…/site/plugins/search/src/config/component.php:17
#11 Kirby\Toolkit\F:{closure} in /…/kirby/src/Cms/Search.php:40
#10 Kirby\Cms\Search:collection in /…/kirby/src/Cms/Collection.php:323
#9 Kirby\Cms\Collection:search in /…/kirby/src/Cms/Site.php:501
#8 Kirby\Cms\Site:search in /…/site/templates/home.php:12
#7 include in /…/kirby/src/Toolkit/F.php:403
#6 Kirby\Toolkit\F:loadIsolated in /…/kirby/src/Toolkit/F.php:380
#5 Kirby\Toolkit\F:load in /…/kirby/src/Toolkit/Tpl.php:35
#4 Kirby\Toolkit\Tpl:load in /…/kirby/src/Cms/Template.php:167
#3 Kirby\Cms\Template:render in /…/kirby/src/Cms/Page.php:1172
#2 Kirby\Cms\Page:render in /…/kirby/src/Cms/App.php:686
#1 Kirby\Cms\App:io in /…/kirby/src/Cms/App.php:1039
#0 Kirby\Cms\App:render in /…/public/index.php:12

Specifications

// site/config/search.php
<?php
return [
  'entries' => [
    'pages' => 'site.index',
    'files' => false,
    'users' => false,
  ],
  'fields' => [
    'pages' => [
      'title',
      'searchTerms',
      'name',
      'subtitle',
      'text',
      // ...
    ],
  ],
];

By the way, thank your for the awesome plugin! ❤️

distantnative commented 3 years ago

Ok, I think these are two issues to tackle:

distantnative commented 3 years ago

Ok, first I can fix easily. Punctuation seems to be rather difficult to support with the Sqlite provider (with the FTS5 module underlying).

One way would be to strip punctuation from the query before processing. But then it would also match anything without the character.

tobiasfabian commented 3 years ago

For my current use case it would be fine to strip some special characters (e.g. ? or !).

E.g. $site->search('who?'); searches for who (without ?).

On the other hand it would be nice to have the ability to search for slugs. e.g. $site->search('notes/exploring-the-universe'). So / and - shouldn’t be ignored. If it’s not possible to search for slugs it would be also fine.

distantnative commented 3 years ago

I tried, but I don't manage to make it work with those characters. So I will make the search ignore them.