lucaong / minisearch

Tiny and powerful JavaScript full-text search engine for browser and Node
https://lucaong.github.io/minisearch/
MIT License
4.64k stars 133 forks source link

Any way to search across multiple vitepress sites? #242

Closed dunstan closed 9 months ago

dunstan commented 9 months ago

Hi there,

Would you have any suggestions for how one might search across multiple vitepress sites, so that no matter which of the sites you're looking at you could get search results from all of them? Could minisearch combine several index files if they were copied over from each site?

Thanks for any help you can offer.

Dunstan

lucaong commented 9 months ago

Hi @dunstan , I am not an expert on Vitepress specifically, but it is fine to index in MiniSearch documents coming from different sources, as long as they have the same structure, and each of them has a unique ID. If you can export the document lists for multiple sites as arrays of JSON-like objects, you could devise a scheme to make the IDs from different sites unique (e.g. concatenate the site name with the ID, like site1-123, site2-321, etc.), and merge them all into a single array. Then, you can index it in MiniSearch with the addAll or addAllAsync methods.

Alternatively, if you already have pre-built indexes, you could create one MiniSearch instance per site, and write some logic to dispatch searches to each of them, as well as merge the results. Something like:

const site1 = MiniSearch.loadJSON(site1Index)
const site2 = MiniSearch.loadJSON(site2Index)
const site3 = MiniSearch.loadJSON(site3Index)
const allSites = [site1, site2, site3]

const searchAllSites = (query, searchOptions) => {
  // Get results from all sites into a single list
  const allResults = allSites.flatMap((site) => site.search(query, searchOptions))

  // Re-sort by descending score
  allResults.sort((a, b) => b.score - a.score)

  return allResults
}

// Search all sites:
searchAllSites('something', { prefix: true, fuzzy: true })

Would this work for your use-case?

dunstan commented 9 months ago

I think that's more than enough to get us started, Luca. Thanks so much for the kind reply, and for building and sharing such a helpful tool.

dunstan commented 9 months ago

I'll close this to keep things tidy.

lucaong commented 9 months ago

Happy that my answer was helpful :)