npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

algolia-tiebreaking-sort

v1.0.3

Published

Post-processing sorting of search results coming from Algolia

Downloads

10

Readme

Algolia tie-breaking sort

npm node-lts JavaScript Style Guide tested with jest

What is it?

This package is a simple and fast post-processing sorting of search results coming from Algolia, with zero dependency. Original idea and implementation from mikaa123!

When should it be used?

The main use-case for this is to re-rank an Algolia result set after it has been returned by the search engine. Such need could happen if you want to merge results coming from different indices, or if you have a complex ranking strategy that needs 2 layers of sorting (Algolia has advanced result-tweaking solutions, but can only apply one global ranking strategy).

For the most performance-conscious people, a quick benchmark with Array.prototype.sort() and Node.js v16.2.0 showed that the function can sort 5000 results in less than 50 milliseconds.

How to use it?

Regular install through npm or yarn:

npm i algolia-tiebreaking-sort
# or
yarn add algolia-tiebreaking-sort

Then the most straightforward way to use it is the following:

const algoliasearch = require('algoliasearch')
const tieBreakingSort = require('algolia-tiebreaking-sort')

const client = algoliasearch('APPID', 'APIKEY')
const index1 = client.initIndex('index_name_1')
const index2 = client.initIndex('index_name_2')

// Only requirement is to have `_rankingInfo` and `objectID` for each hit
const search1 = index1.search('my query', { getRankingInfo: true })
const search2 = index2.search('my query', { getRankingInfo: true })

Promise.all([search1, search2]).then(results => {
  const hits = [...results[0].hits, ...results[1].hits]
  console.log(hits.sort(tieBreakingSort))
})

What are the next steps?

Potential improvements include:

  • Handle sort-by criteria
  • Make the ranking formula configurable, allowing custom ones
  • Optimize speed
  • Rewrite in Typescript

How to run tests?

Just clone this repository and run:

npm test
# or
yarn test

Disclaimer

This package is not officially supported by Algolia, so it cannot be held responsible for any use in production. If you need support, use GitHub issues or the community forum, but not Algolia email support.