CloudCannon / pagefind

Static low-bandwidth search at scale
https://pagefind.app
MIT License
3.45k stars 111 forks source link

Multiple index bundles? #484

Closed MartinMalinda closed 11 months ago

MartinMalinda commented 11 months ago

I'm testing filters and while they are great, I'm not sure if they can fix my usecase.

My site can potentially grow very big. I'm thinking about publishing whole books on my site. Or very long stories. Each book / story page is a separate web page.

I'd like to provide a search, but scope it only to just opened story / book. Most likely, people don't want to search in multiple books, they wan't to search only in the book they are just reading.

Since I basically never want to search globally, are there any possible optimizations for this usecase during buildtime?

Filters don't seem ideal for me, unless I'm misunderstanding them - it seems like the search happens as usual, and the filter takes an effect in the last moment, after different index files are already loaded. For me, the ideal solution would be to only load specific index files relevant to the opened book.

Does this make sense?

Would I need to run pagefind separately for each book somehow, each time with different files? Or is there a more elegant solution?

Thanks!

bglw commented 11 months ago

Hey @MartinMalinda 👋

Would I need to run pagefind separately for each book somehow, each time with different files?

To fully separate the indexes from each other, yes this would be required.

is there a more elegant solution?

I think so! Fundamentally the same action, but performed via the NodeJS API.

Psuedo-coding something that might work:

import * as pagefind from "pagefind";

const books = [
  { directory: "content/books/book-1", "id": "book-1" },
  /* ... more books ... */
];

for (const book of books) {
  const { index } = await pagefind.createIndex();
  await index.addDirectory({
    path: book.directory
  });
  await index.writeFiles({
    outputPath: `public/pagefind/${book.id}`
  });
}

That would output a fully independent /pagefind/book-1/pagefind.js etc to import.

To reduce the duplication of the assets, you could instead use index.getFiles and write the files to disk yourself, skipping the js/css assets and writing those to a common directory instead, then loading Pagefind with a custom bundlePath pointing to the relevant directory.

Hopefully that makes sense! Let me know if you think that might work for you 🙂

MartinMalinda commented 11 months ago

At the moment I'm using https://github.com/shishkin/astro-pagefind so I'll need to "eject" from that and roll my own solution. Because of that I can't test it real quick but this looks very realistic to me. I'll see how I can integrate pagefind to Astro in a custom way.

Thanks a lot!

bglw commented 11 months ago

Grand!

I'll convert this Issue into a Discussion so we can keep tabs on how you go 🙂