Loosen

Integrations

Loosen needs two things from your site: the visitor's current filters, and your items. It does not replace your search engine: it looks at the filters and explains when they find nothing.

We have not tested Loosen on live Shopify or WooCommerce themes, nor against live Typesense, Meilisearch or Algolia instances: what follows is what the architecture allows, with code that is syntax-checked but not run against those products. The integration service exists for exactly this.

Runnable examples of the filter recipes below (facet state, URL filters, server mode) on a real catalogue: live demo and github.com/sicena-com/loosen-examples.

Shopify

  • Where to mount. Put <div id="loosen"></div> in the template shown when a collection or search has no products (in your collection section, the branch where collection.products.size == 0).
  • Reading the filters. Storefront filtering keeps the filters in the URL, so use the URL recipe of the install guide (parameter names such as filter.v.price.lte and filter.v.option.<name>; adapt the table to your theme).
  • Data. Browser mode needs your products as JSON: a theme asset or a JSON template with the attributes you filter on, fine for a few thousand products. For more, or when you cannot add a feed, use server mode: loosend loads your catalogue from a feed URL on a schedule (for example a scheduled export of your products).
  • Applying a relaxation navigates to a URL, so the widget is mounted again on the next page. A theme that refreshes its grid with AJAX can use the form or state recipes and call widget.update() after the grid refreshes.

WooCommerce

  • Where to mount. In the “no products found” template (loop/no-products-found.php) or with the woocommerce_no_products_found hook.
  • Reading the filters. The URL recipe of the install guide, with the parameters your theme or filter plugin uses (max_price, filter_<attribute>).
  • Data. A compact JSON feed of the attributes you filter on (a small endpoint or a scheduled export), or server mode with loosend reading that feed.

Typesense

Keep your search as it is. Map the same constraints the widget reads to a filter_by string, and call widget.update() after each search; the widget decides whether there is anything to explain. Load the same documents into the widget (browser mode) or into loosend (feed or API).

// Typesense: build the filter_by string from the same constraints the widget reads, and let the widget
// look at them after every search. The widget analyses its own copy of the data (client mode) or your loosend.
const q = v => (typeof v === "number" ? String(v) : "`" + v + "`");     // Typesense quotes strings with backticks

const toFilterBy = constraints => constraints.map(c => {
  switch (c.op) {
    case "eq": return `${c.attr}:=${q(c.value)}`;
    case "le": return `${c.attr}:<=${c.value}`;
    case "ge": return `${c.attr}:>=${c.value}`;
    case "in": return `${c.attr}:=[${c.values.map(q).join(",")}]`;
    case "allOf": return c.values.map(v => `${c.attr}:=${q(v)}`).join(" && ");
    default: throw new Error(`no mapping for ${c.op}`);
  }
}).join(" && ");

async function search(client, constraints) {
  const result = await client.collections("products").documents().search({q: "*", filter_by: toFilterBy(constraints)});
  widget.update();          // the widget decides itself whether there is anything to explain
  return result;
}

// Undo (optional): give mount() a restoreConstraints that stores the constraints it receives (the ones from before the click)
// and searches again, e.g.   restoreConstraints: constraints => { currentConstraints = constraints; return search(client, constraints); }

Meilisearch

// Meilisearch: the same idea with its filter syntax.
const toFilter = constraints => constraints.map(c => {
  switch (c.op) {
    case "eq": return `${c.attr} = ${JSON.stringify(c.value)}`;
    case "le": return `${c.attr} <= ${c.value}`;
    case "ge": return `${c.attr} >= ${c.value}`;
    case "in": return `${c.attr} IN [${c.values.map(v => JSON.stringify(v)).join(", ")}]`;
    case "allOf": return c.values.map(v => `${c.attr} = ${JSON.stringify(v)}`).join(" AND ");
    default: throw new Error(`no mapping for ${c.op}`);
  }
});

async function search(index, constraints) {
  const result = await index.search("", {filter: toFilter(constraints)});
  widget.update();
  return result;
}

// Undo (optional): give mount() a restoreConstraints that stores the constraints it receives (the ones from before the click)
// and searches again, e.g.   restoreConstraints: constraints => { currentConstraints = constraints; return search(index, constraints); }

Algolia

Algolia's remove-words fallback relaxes the text of a query; Loosen explains the filters. They complement each other.

// Algolia: numericFilters and facetFilters. In facetFilters, entries of the outer array are ANDed and the
// entries of an inner array are ORed.
const toAlgolia = constraints => ({
  numericFilters: constraints.filter(c => c.op === "le" || c.op === "ge").map(c => `${c.attr}${c.op === "le" ? "<=" : ">="}${c.value}`),
  facetFilters: constraints.flatMap(c => {
    if (c.op === "eq") return [[`${c.attr}:${c.value}`]];
    if (c.op === "in") return [c.values.map(v => `${c.attr}:${v}`)];
    if (c.op === "allOf") return c.values.map(v => [`${c.attr}:${v}`]);
    return [];
  }),
});

async function search(index, constraints) {
  const result = await index.search("", toAlgolia(constraints));
  widget.update();
  return result;
}

// Undo (optional): give mount() a restoreConstraints that stores the constraints it receives (the ones from before the click)
// and searches again, e.g.   restoreConstraints: constraints => { currentConstraints = constraints; return search(index, constraints); }

For state-based UIs (InstantSearch and similar) see the facet state recipe: convert your UI state to constraints with toConstraints, and the relaxed constraints back with relaxedConstraints.