Page search returns unexpected result

Hello,

I have a search function to search articles in my blog. This works in general. I just noticed that a specific 2 word search term results in 2 results where I would expect just 1 result.

Search function:

return function ($site) {

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

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

};

I checked the article.de.txt file that gets found and I see that the metadata field id of the blocks field text contains the search string by chance. From a technical perspective it makes sense that the search function finds this. From an user perspective it doesn’t make sense because the article doesn’t contain the 2 chars long search phrase so this can’t be what the user actually searches for.

Can I fix/prevent this somehow? I would expect that only the actual content of the blocks field is searched without going into some metadata reading.

Best Regards,

For performance reasons, the search component does not load the blueprint for every page - this would cause quite some delay. But because of this, the search function cannot know what type a certain field is and thus only treat every field with its raw content.

I do understand that that can be tricky with blocks or layout fields that add their own “markup” as well. One could consider this a bug, I probably would, though it’ll be hard to fix I fear. But please feel free to add a bug tracker issue.

How could you deal with this for now?
You could write your own version of the search component that tackles your issue specifically. The search component can be replace as an core component extension in a plugin

1 Like

Thank you for your workaround and the explaination @distantnative! I created a bug report on GitHub to make sure it’s not lost for later on: Page search returns irrelevant pages due to `id` metadata field of block field containing the searched term by chance · Issue #5490 · getkirby/kirby · GitHub

It’s no critical issue in my case. The search term isn’t too uncommon but it’s more or less a tiny issue with the search function. If it becomes a bigger issue later on, I will see what I can do to avoid it.