Skip to content

Commit 3e73f52

Browse files
committed
Notes on searching and nested entries
1 parent 8d97c0b commit 3e73f52

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

docs/5.x/system/searching.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,28 @@ Any time an indexable attribute or field on an element is updated (as indicated
287287

288288
The [eligible properties](#searching-for-specific-element-attributes) differ for each element type, the field layout a given element uses, and which of the underlying fields are flagged as searchable.
289289

290+
## Searching Nested Elements
291+
292+
In Craft 5, [entry queries](../reference/element-types/entries.md#querying-entries) return nested elements, by default. To search only for entries that belong to a section (those that are not owned by another entry), apply the `.section()` param:
293+
294+
```twig
295+
{% set results = craft.entries()
296+
.section(['news', 'resources'])
297+
.search(terms)
298+
.orderBy('score')
299+
.all() %}
300+
```
301+
302+
If you want entries in any section, and would prefer to not maintain a list of specific sections (or _excluded_ sections, with `.section(['not', 'home', 'suppliers'])`) in your template, pass an asterisk <Since ver="5.2.0" feature="Querying for non-nested entries with the special “section wildcard”" /> (`*`):
303+
304+
```twig{2}
305+
{% set results = craft.entries()
306+
.section('*')
307+
.search(terms)
308+
.orderBy('score')
309+
.all() %}
310+
```
311+
290312
## Rebuilding Your Search Indexes
291313

292314
Craft does its best to keep its search indexes as up-to-date as possible, but there are a couple things that might render portions of them inaccurate. If you suspect your search indexes are out of date, you can have Craft rebuild them by bulk-resaving entries with the [`resave/entries`](../reference/cli.md#resave) command and including the `--update-search-index` flag:

0 commit comments

Comments
 (0)