Ndugu
October 20, 2020, 12:47am
1
I use the helpsection plugin and need to exclude that stuff from search results. The helpsection has its own page in the content folder but it is not listed. Nevertheless it is shown in search results because the subpages are listed, due to their leading number. So this does not work:
return function ($site) {
$query = get('q');
$results = $site->index()->listed()->search($query, 'title|text');
$results = $results->paginate(20);
return [
'query' => $query,
'results' => $results,
'pagination' => $results->pagination()
];
};
Then I tried this
$results = $site->index()->listed()->filterBy('template', '!=', 'helpsection')->search($query, 'title|text');
but it does not exclude the stuff.
I don’t know what to filter and how to do it.
Ndugu
October 20, 2020, 1:01am
2
also ‘not in’ seems not to be the one:
$results = $site->index()->listed()->filterBy('pages', 'not in', ['helpsection'])->search($query, 'title|text');
Try
$results = $site->index()->listed()->not([page('helpsection'), page('helpsection')->children()])->search($query, 'title|text');
I use this for the helpsection plugin:
$site->index()->listed()->filterBy('intendedTemplate', 'not in', ['docindex', 'doc'])
Ndugu:
No, this doesn’t work.
What does that mean? It doesn’t filter or you get an error? What’s your Kirby version?
Ndugu
October 20, 2020, 7:10am
7
This gives me a list of all pages, even without a doing search. I also can’t delete that list.
It looks like it is somehow cached.
Did you add to add ->search(...)
to that string @thguenther posted… ? And make sure the template names are correct.
Ndugu
October 20, 2020, 7:12am
9
It doesn’t exclude the helpsection from the search. No error. It is Version
3.4.3.
Ndugu:
It is Version
3.4.3.
Ok, array support as parameter for not()
was added in 3.4.3, so that should be ok. I wonder if you are overriding your filters again somewhere in your template. I tested my code in a Starterkit and it works.
Ndugu
October 20, 2020, 7:18am
11
I added $results =
but not the search()
It is now
$results = $site->index()->listed()->filterBy('intendedTemplate', 'not in', ['docindex', 'doc'])->search($query, 'title|text');
and now it does work.
Thats cool.
Ndugu
October 20, 2020, 7:22am
12
That is my template
<input class="search-input" type="search" name="q" value="<?= html($query) ?>">
<input type="submit" value="Suchen">
</form>
<ul class="search-result-list">
<?php foreach ($results as $result): ?>
<li>
<a class="search-link" href="<?= $result->url() ?>">
<?= $result->title() ?>
</a>
</li>
<?php endforeach ?>
</ul>
Could you please also post the complete controller as it looks now? And do you in fact use the templates docindex and doc for the helpsection pages?
Ndugu
October 20, 2020, 7:37am
14
sure
<?php
return function ($site) {
$query = get('q');
# $results = $site->index()->listed()->not([page('helpsection'), page('helpsection')->children()])->search($query, 'title|text');
$results = $site->index()->listed()->filterBy('intendedTemplate', 'not in', ['docindex', 'doc'])->search($query, 'title|text');
$results = $results->paginate(20);
return [
'query' => $query,
'results' => $results,
'pagination' => $results->pagination()
];
};
Yes, I kept docindex and doc.
I also try to bring in words=> true
in but I can’t figure it out. Also the search does not find in the text, only in headlines. Maybe because I use the Editor Plugin.
Ndugu
October 20, 2020, 7:44am
15
If I add this from the docs:
$results = $pages->search('my awesome->search', ['words' => true, 'minlength' => 4]);
then of cours “pages” is wrong. If I change the variable to site the search doesn’t find anything.
If I stuff ['words' => true, 'minlength' => 4]
into the search, like this
$results = $site->index()->listed()->filterBy('intendedTemplate', 'not in', ['docindex', 'doc'])->search($query, 'title|text', ['words' => true, 'minlength' => 4]);
it still uses letters as query, not words.
Ndugu
October 20, 2020, 7:58am
16
Is 'title|text'
somehow wrong? If I leave that out, the search does search in the text, not just in title.
That depends on whether your content has a field called text or not. If there is no text field, Kirby will only search in title
, of course.
Ndugu
October 20, 2020, 8:06am
18
I have text, textarea and editor fields.
Sure, but what are the names of these fields? text, textarea and editor are field types , unless you actually named your fields like that.