Skip to content

stereobooster/pagefind-instantsearch

Repository files navigation

InstantSearch adapter for pagefind monorepo

It is the monorepo repository:

Idea

The idea is to create an InstantSearch adapter for pagefind and build the same demo site as I did for facets. This way we can compare "apples to apples".

To do so I wanted to use the same Schema as in facets:

I started implementation with hardcoded conversion. I managed to create a working demo. However, I realized that pagefind can't be used as a general faceted search engine.

Type conversion

pagefind supports only string values, so all values need to be converted to strings before indexing and back to the initial type on the client side.

Indexing:

brand: [item.brand],
categories: item.categories,
price: [`${item.price}`],
rating: [`${item.rating}`],
free_shipping: [`${item.free_shipping}`],

Client-side:

{
  objectID: item.id,
  ...data.meta,
  categories: data.filters.categories,
  price: parseFloat(data.filters.price[0]),
  rating: parseFloat(data.filters.rating[0]),
}

Sorting

See:

Stats for numerical facets

Because there is no support for numbers to get stats we need to convert strings to numbers on the client side and calculate stats:

if (schema[field]?.type === "number") {
  const values: number[] = [];
  entries.forEach((a) => values.push(parseFloat(a[0])));
  facetsStats[field] = {
    min: Math.min(...values),
    max: Math.max(...values),
  };
}

Filter for numeric fields

I didn't implement a filter for numeric fields, like price >= 40 AND price <= 100. The price slider doesn't work in the deme

Status

After I realized that it was not practical to use a general faceted search engine I gave up and didn't finish code for schema transformation.

This code is not recomended for prodcution use.

Development

pnpm i
pnpm run dev

TODO

  • CLI to covert JSON to pagefind index
    • maybe cosmiconfig to pass configuration
      • it can use the same schema as facets
      • input
      • output
  • adapter for instantsearch
    • filtering for numeric facets