Skip to content

Commit 7667fe4

Browse files
author
arnoson
committed
feat!: return raw search results
Before, we were returning a collection of found pages, with a `hit()` helper to retrieve the raw loupe result by the page's uuid. But this approach doesn't work for multiple hits with the same page. So for now we just return the loupe hits as a collection. Maybe we can think of an abstraction that feels more kirby-like in the future.
1 parent c9c3b67 commit 7667fe4

File tree

4 files changed

+19
-28
lines changed

4 files changed

+19
-28
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php return [
2+
"debug" => true,
3+
];

example/site/templates/default.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@
5555
<p><i>Search took <?= $searchTime ?>ms</i></p>
5656

5757
<!-- Results -->
58-
<?php foreach ($results as $result): ?>
58+
<?php foreach ($results as $result):
59+
$page = page($result["uuid"]); ?>
5960
<article>
60-
<h2><?= $result->title() ?></h2>
61-
<p><?= $result->text() ?></p>
62-
<p class="interests">Interests: <?= $result->interests() ?></p>
61+
<h2><?= $page->title() ?></h2>
62+
<p><?= $page->text() ?></p>
63+
<p class="interests">Interests: <?= $page->interests() ?></p>
6364
</article>
64-
<?php endforeach; ?>
65+
<?php
66+
endforeach; ?>
6567

6668
<!-- Pagination -->
6769
<?php $pagination = $results->pagination(); ?>

index.dev.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
try {
2+
await import("http://127.0.0.1:5177/src/index.ts");
3+
} catch (err) {
4+
console.error(
5+
"[kirbyup] Couldn't connect to the development server. Run `npm run serve` to start Vite or build the plugin with `npm run build` so Kirby uses the production version."
6+
);
7+
throw err;
8+
}

lib/KirbyLoupeCollection.php

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,18 @@
33
namespace arnoson;
44

55
use Kirby\Cms\Collection;
6-
use Kirby\Cms\Page;
76
use Kirby\Cms\Pagination;
87
use Loupe\Loupe\SearchResult;
98

109
class KirbyLoupeCollection extends Collection {
11-
private array $hitsByUuid = [];
12-
1310
public function __construct(protected SearchResult $searchResult) {
14-
parent::__construct();
15-
foreach ($searchResult->getHits() as $hit) {
16-
$uuid = $hit["uuid"];
17-
$this->hitsByUuid[$uuid] = $hit;
18-
$this->append(page($uuid));
19-
}
11+
parent::__construct($searchResult->getHits());
2012
}
2113

2214
public function searchResult() {
2315
return $this->searchResult;
2416
}
2517

26-
/**
27-
* Get the raw search hit from Loupe for the specified page.
28-
*/
29-
public function hit(Page $page) {
30-
return $this->hitsByUuid[$page->uuid()->toString()];
31-
}
32-
33-
/**
34-
* Get the formatted attributes from Loupe for the specified page.
35-
*/
36-
public function formatted(Page $page) {
37-
return $this->hit($page)["_formatted"];
38-
}
39-
4018
public function paginate(...$arguments): static {
4119
// The original Collection class uses Pagination::for() which will
4220
// automatically set the total parameter based on the collection length

0 commit comments

Comments
 (0)