• Stars
    star
    262
  • Rank 156,136 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A JS adapter library to build rich search interfaces with Typesense and InstantSearch.js

Typesense Instantsearch Adapter

An adapter to use the awesome Instantsearch.js library with a Typesense Search Server, to build rich search interfaces.

NPM version downloads

Here is an example of UI you can build with this adapater: songs-search.typesense.org

Note: If your search interface is built on a custom autocomplete component, or is based on @algolia/autocomplete-js, then you don't need this adapter to use it with Typesense, as typesense-js library already supports client-side fetching data from any async data sources. Read more here.

Quick Links

Background

The good folks over at Algolia have built and open-sourced Instantsearch.js which is a collection of out-of-the-box components that you can use to build interactive search experiences swiftly.

With the adapter in this repository, you'll be able to use Instantsearch (and its React, Vue and Angular cousins) with data indexed in a Typesense search server.

If you haven't used Instantsearch before, we recommend going through their Getting Started guide here. Once you go through the guide, follow the instructions below to plug the Typesense adapter into Instantsearch.

Quick Start

Here's a guide on building a quick search interface with Typesense and InstantSearch.js: https://typesense.org/docs/0.20.0/guide/search-ui-components.html

Starter App

Here's a demo starter app that shows you how to use the adapter: https://github.com/typesense/typesense-instantsearch-demo

Installation

$ npm install --save typesense-instantsearch-adapter @babel/runtime

or

$ yarn add typesense-instantsearch-adapter @babel/runtime

or, you can also directly include the adapter via a script tag in your HTML:

<script src="https://cdn.jsdelivr.net/npm/typesense-instantsearch-adapter@2/dist/typesense-instantsearch-adapter.min.js"></script>

<!-- You might want to pin the version of the adapter used if you don't want to always receive the latest minor version -->

Since this is an adapter, it will not install the Instantsearch library automatically for you. You need to install one of the following in your application directly:

You'll find information on how to get started with each of the above libraries in their respective repos.

We'd also recommend checking out create-instantsearch-app to create your Search UI from a starter template.

Usage

With instantsearch.js

import instantsearch from "instantsearch.js";
import { searchBox, hits } from "instantsearch.js/es/widgets";
import TypesenseInstantSearchAdapter from "typesense-instantsearch-adapter";

const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
  server: {
    apiKey: "abcd", // Be sure to use an API key that only allows search operations
    nodes: [
      {
        host: "localhost",
        path: "", // Optional. Example: If you have your typesense mounted in localhost:8108/typesense, path should be equal to '/typesense'
        port: "8108",
        protocol: "http",
      },
    ],
    cacheSearchResultsForSeconds: 2 * 60, // Cache search results from server. Defaults to 2 minutes. Set to 0 to disable caching.
  },
  // The following parameters are directly passed to Typesense's search API endpoint.
  //  So you can pass any parameters supported by the search endpoint below.
  //  query_by is required.
  additionalSearchParameters: {
    query_by: "name,description,categories",
  },
});
const searchClient = typesenseInstantsearchAdapter.searchClient;

const search = instantsearch({
  searchClient,
  indexName: "products",
});
search.addWidgets([
  searchBox({
    container: "#searchbox",
  }),
  hits({
    container: "#hits",
    templates: {
      item: `
        <div class="hit-name">
          {{#helpers.highlight}}{ "attribute": "name" }{{/helpers.highlight}}
        </div>
      `,
    },
  }),
]);

search.start();

You can add any of the Instantsearch widgets here that are supported by the adapter.

You'll also find a working example in test/support/testground. To run it, run npm run testground from the project root folder.

With react-instantsearch

import React from "react";
import ReactDOM from "react-dom";
import { SearchBox } from "react-instantsearch-dom";
import TypesenseInstantSearchAdapter from "typesense-instantsearch-adapter";

const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
  server: {
    apiKey: "abcd", // Be sure to use an API key that only allows search operations
    nodes: [
      {
        host: "localhost",
        port: "8108",
        path: "", // Optional. Example: If you have your typesense mounted in localhost:8108/typesense, path should be equal to '/typesense'
        protocol: "http",
      },
    ],
    cacheSearchResultsForSeconds: 2 * 60, // Cache search results from server. Defaults to 2 minutes. Set to 0 to disable caching.
  },
  // The following parameters are directly passed to Typesense's search API endpoint.
  //  So you can pass any parameters supported by the search endpoint below.
  //  query_by is required.
  additionalSearchParameters: {
    query_by: "name,description,categories",
  },
});
const searchClient = typesenseInstantsearchAdapter.searchClient;

const App = () => (
  <InstantSearch indexName="products" searchClient={searchClient}>
    <SearchBox />
    <Hits />
  </InstantSearch>
);

You can then add any of the Instantsearch-React widgets here that are supported by the adapter.

The instructions above also apply to React Native.

With vue-instantsearch

App.vue:

<template>
  <ais-instant-search :search-client="searchClient" index-name="products">
    <ais-search-box />
    <ais-hits>
      <div slot="item" slot-scope="{ item }">
        <h2>{{ item.name }}</h2>
      </div>
    </ais-hits>
  </ais-instant-search>
</template>

<script>
import TypesenseInstantSearchAdapter from "typesense-instantsearch-adapter";

const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
  server: {
    apiKey: "abcd", // Be sure to use an API key that only allows search operations
    nodes: [
      {
        host: "localhost",
        path: "", // Optional. Example: If you have your typesense mounted in localhost:8108/typesense, path should be equal to '/typesense'
        port: "8108",
        protocol: "http",
      },
    ],
    cacheSearchResultsForSeconds: 2 * 60, // Cache search results from server. Defaults to 2 minutes. Set to 0 to disable caching.
  },
  // The following parameters are directly passed to Typesense's search API endpoint.
  //  So you can pass any parameters supported by the search endpoint below.
  //  query_by is required.
  additionalSearchParameters: {
    query_by: "name,description,categories",
  },
});
const searchClient = typesenseInstantsearchAdapter.searchClient;

export default {
  data() {
    return {
      searchClient,
    };
  },
};
</script>

You can then add any of the Instantsearch widgets here that are supported by the adapter.

With angular-instantsearch

// app.component.ts
import { Component } from "@angular/core";
import TypesenseInstantSearchAdapter from "typesense-instantsearch-adapter";

const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
  server: {
    apiKey: "abcd", // Be sure to use an API key that only allows search operations
    nodes: [
      {
        host: "localhost",
        path: "", // Optional. Example: If you have your typesense mounted in localhost:8108/typesense, path should be equal to '/typesense'
        port: "8108",
        protocol: "http",
      },
    ],
    cacheSearchResultsForSeconds: 2 * 60, // Cache search results from server. Defaults to 2 minutes. Set to 0 to disable caching.
  },
  // The following parameters are directly passed to Typesense's search API endpoint.
  //  So you can pass any parameters supported by the search endpoint below.
  //  query_by is required.
  additionalSearchParameters: {
    query_by: "name,description,categories",
  },
});
const searchClient = typesenseInstantsearchAdapter.searchClient;

@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"],
})
export class AppComponent {
  config = {
    indexName: "products",
    searchClient,
  };
}

You can then add any of the Instantsearch widgets here that are supported by the adapter.

Widget Specific Instructions

hierarchicalMenu

For this widget, you want to create independent fields in the collection's schema with this specific naming convention:

  • field.lvl0
  • field.lvl1
  • field.lvl2

for a nested hierarchy of field.lvl0 > field.lvl1 > field.lvl2

Each of these fields can also hold an array of values. This is useful for handling multiple hierarchies.

sortBy

When instantiating this widget, you want to set the value of the index name to this particular format:

search.addWidgets([
  sortBy({
    container: "#sort-by",
    items: [
      { label: "Default", value: "products" },
      { label: "Price (asc)", value: "products/sort/price:asc" },
      { label: "Price (desc)", value: "products/sort/price:desc" },
    ],
  }),
]);

The generalized pattern for the value attribute is: <index_name>[/sort/<sort_by>]. The adapter will use the value in <sort_by> as the value for the sort_by search parameter.

configure

If you need to specify a filter_by search parameter for Typesense, you want to use the configure InstantSearch widget, along with facetFilters, numericFilters or filters.

The format for facetFilters and numericFilters is the same as Algolia's as described here. But filters needs to be in Typesense's filter_by format as described in this table here.

Setting filter_by inside the additionalQueryParameters config only works when the widgets are loaded initially, because InstantSearch internally overrides the filter_by field subsequently. Read more here.

index

For Federated / Multi-Index Search, you'd need to use the index widget. To then be able to specify different search parameters for each index/collection, you can specify them using the collectionSpecificSearchParameters configuration:

const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
  server: {
    apiKey: "abcd", // Be sure to use an API key that only allows search operations
    nodes: [{ host: "localhost", path: "/", port: "8108", protocol: "http" }],
  },
  // Search parameters that are common to all collections/indices go here:
  additionalSearchParameters: {
    numTypos: 3,
  },
  // Search parameters that need to be *overridden* on a per-collection-basis go here:
  collectionSpecificSearchParameters: {
    products: {
      query_by: "name,description,categories",
    },
    brands: {
      query_by: "name",
    },
  },
});
const searchClient = typesenseInstantsearchAdapter.searchClient;

Essentially, any parameters set in collectionSpecificSearchParameters will be merged with the values in additionalSearchParameters when querying Typesense, effectively overriding values in additionalSearchParameters on a per-collection-basis.

geoSearch

Algolia uses _geoloc by default for the name of the field that stores the lat long values for a record. In Typesense, you can name the geo location field anything. If you use a name other than _geoloc, you need to specify it when initializing the adapter like below, so InstantSearch can access it:

const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
  server: {
    apiKey: "xyz",
    nodes: [
      {
        host: "localhost",
        port: "8108",
        path: "/",
        protocol: "http",
      },
    ],
  },
  geoLocationField: "lat_lng_field", // <<======
  additionalSearchParameters,
});

dynamicWidgets

Available as of Typesense Server v0.25.0.rc12

This dynamicWidgets widget works out of the box with no additional changes, but if you want to control the order in which these facets are displayed in the UI Instantsearch expects a parameter called renderingContent to be set.

const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
  server: {
    apiKey: "xyz",
    nodes: [
      {
        host: "localhost",
        port: "8108",
        path: "/",
        protocol: "http",
      },
    ],
  },
  renderingContent: {
    // <<===== Add this, only if you want to control the order of the widgets displayed by dynamicWidgets
    facetOrdering: {
      facets: {
        order: ["size", "brand"], // <<===== Change this as needed
      },
    },
  },
  additionalSearchParameters,
});

Read more about all available options for renderingContent in Algolia's documentation here.

Special characters in field names / values

Available as of typesense-instantsearch-adapter 2.7.0-2

  • If any string fields in your documents have a colon : in their values (for eg, let's say there's a field called { brand: "a:b" }, then you would need to add a parameter like below when instantiating the adapter:

    const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
      server: {
        apiKey: "xyz",
        nodes: [
          {
            host: "localhost",
            port: "8108",
            path: "/",
            protocol: "http",
          },
        ],
      },
      facetableFieldsWithSpecialCharacters: ["brand"], // <======= Add string fields that have colons in their values here, to aid in parsing
      additionalSearchParameters,
    });
  • If any numeric field names in your documents have special characters like >, <, = (for eg, let's say there's a field called { price>discount: 3.0 }) then you would need to add a parameter like below when instantiating the adapter:

    const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
      server: {
        apiKey: "xyz",
        nodes: [
          {
            host: "localhost",
            port: "8108",
            path: "/",
            protocol: "http",
          },
        ],
      },
      facetableFieldsWithSpecialCharacters: ["price>discount"], // // <======= Add numeric fields that have >, < or = in their names, to aid in parsing
      additionalSearchParameters,
    });

Grouped Hits

Available as of typesense-instantsearch-adapter 2.7.1-4

By default, when group_by is used as a search parameters, the adapter flattens the results across all groups into a single list of sequential hits.

If you'd like to preserve the groups, you want to set flattenGroupedHits: false when instantiating the adapter.

This will place the first hit in a group as the primary hit, and then add all hits in the group inside a _grouped_hits key inside each hit.

const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
  server: {
    apiKey: "xyz",
    nodes: [
      {
        host: "localhost",
        port: "8108",
        path: "/",
        protocol: "http",
      },
    ],
  },
  flattenGroupedHits: false, // <=======
  additionalSearchParameters,
});

Vector Search

Available as of typesense-instantsearch-adapter 2.7.0-3

The general idea is to first hook into the query life-cycle of Instantsearch, intercept the typed query and send it to an embedding API, fetch the embeddings and then send the vectors to Typesense to do a nearest neighbor vector search.

Here's a demo that you can run locally to see this in action: https://github.com/typesense/typesense-instantsearch-semantic-search-demo.

Here's how to do this in Instantsearch.js:

const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
  server: {
    apiKey: "xyz",
    nodes: [
      {
        host: "localhost",
        port: "8108",
        path: "/",
        protocol: "http",
      },
    ],
  },
  additionalSearchParameters,
});
const searchClient = typesenseInstantsearchAdapter.searchClient;
const search = instantsearch({
  searchClient,
  indexName: "products",
  routing: true,
  async searchFunction(helper) {
    const query = helper.getQuery().query;
    const page = helper.getPage(); // Retrieve the current page
    const totalNearestNeighborsToFetch = 1000;

    if (query !== "") {
      // Get embedding for the query
      let response = await fetch(
        "http://localhost:8000/embedding?" + new URLSearchParams({ q: query }) // <=== Embedding API
      );

      let parsedResponse = await response.json();

      console.log(parsedResponse);

      // Send the embedding to Typesense to do a nearest neighbor search
      helper
        .setQueryParameter(
          "typesenseVectorQuery", // <=== Special parameter that only works in [email protected] and above
          `vectors:([${parsedResponse["embedding"].join(",")}], k:${totalNearestNeighborsToFetch})`
        )
        .setPage(page)
        .search();
    } else {
      helper.setQueryParameter("typesenseVectorQuery", null).setPage(page).search();
    }
  },
});

Compatibility

Typesense Server typesense-instantsearch-adapter instantsearch.js react-instantsearch vue-instantsearch angular-instantsearch
>= v0.25.0.rc14 >= v2.7.0-1 >= 4.51 >= 6.39 >= 4.8 >= 4.4
>= v0.25.0.rc12 >= v2.6.0 >= 4.51 >= 6.39 >= 4.8 >= 4.4
>= v0.24 >= v2.5.0 >= 4.2.0 >= 6.0.0 >= 2.2.1 >= 3.0.0
>= v0.21 >= v2.0.0 >= 4.2.0 >= 6.0.0 >= 2.2.1 >= 3.0.0
>= v0.19 >= v1.0.0 >= 4.2.0 >= 6.0.0 >= 2.2.1 >= 3.0.0
>= v0.15 >= v0.3.0 >= 4.2.0 >= 6.0.0 >= 2.2.1 >= 3.0.0
>= v0.14 >= v0.2.0 >= 4.2.0 >= 6.0.0 >= 2.2.1 >= 3.0.0
>= v0.13 >= v0.1.0 >= 4.2.0 >= 6.0.0 >= 2.2.1 >= 3.0.0
>= v0.12 >= v0.0.4 >= 4.2.0 >= 6.0.0 >= 2.2.1 >= 3.0.0

If a particular version of the above libraries don't work with the adapter, please open a Github issue with details.

Widget Compatibility

This adapter works with all widgets in this list, except for the following:

  • queryRuleCustomData
  • queryRuleContext

Development

$ npm install
$ npm run typesenseServer
$ FORCE_REINDEX=true npm run indexTestData

$ npm link typesense-instantsearch-adapter
$ npm run testground

$ npm test

To release a new version, we use the np package:

$ npm install --global np
$ np

# Follow instructions that np shows you

Help

If you have any questions or run into any problems, please create a Github issue and we'll try our best to help.

Β© 2020-present Typesense, Inc.

More Repositories

1

typesense

Open Source alternative to Algolia and an Easier-to-Use alternative to ElasticSearch ⚑ πŸ” ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences
C++
12,770
star
2

showcase-recipe-search

Instantly search 2M cooking recipes using Typesense Search (an open source alternative to Algolia / ElasticSearch) ⚑ πŸ₯˜ πŸ”
JavaScript
437
star
3

typesense-js

JavaScript / TypeScript client for Typesense
JavaScript
266
star
4

typesense-php

PHP client for Typesense: https://github.com/typesense/typesense
PHP
181
star
5

showcase-songs-search

A site to instantly search 32M songs from the MusicBrainz songs database, using Typesense Search (an open source alternative to Algolia / ElasticSearch) ⚑ 🎡 πŸ”
JavaScript
135
star
6

laravel-scout-typesense-driver

Laravel Scout Driver for Typesense
PHP
131
star
7

showcase-books-search

A site to instantly search 28M books from OpenLibrary using Typesense Search (an open source alternative to Algolia / ElasticSearch) ⚑ πŸ“š πŸ”
JavaScript
130
star
8

typesense-go

Go client for Typesense: https://github.com/typesense/typesense
Go
124
star
9

firestore-typesense-search

Firebase Extension to automatically push Firestore documents to Typesense for full-text search with typo tolerance, faceting, and more
JavaScript
122
star
10

typesense-python

Python client for Typesense: https://github.com/typesense/typesense
Python
104
star
11

typesense-docsearch-scraper

A fork of Algolia's awesome DocSearch Scraper, customized to index data in Typesense (an open source alternative to Algolia)
Python
70
star
12

typesense-dart

Dart client for Typesense
Dart
65
star
13

docusaurus-theme-search-typesense

A fork of the awesome @docusaurus/theme-search-algolia library customized to work with Typesense
TypeScript
48
star
14

typesense-ruby

Ruby client for Typesense: https://github.com/typesense/typesense
Ruby
43
star
15

typesense-instantsearch-demo

A demo app that shows how to use the Typesense InstantSearch adapter, to build rich search interfaces.
JavaScript
41
star
16

typesense-swift

Swift Client for Typesense βš‘οΈπŸ”Ž
Swift
40
star
17

typesense-website

Typesense website and documentation | An open source search engine alternative to Algolia, Elasticsearch and Pinecone
Vue
38
star
18

showcase-nextjs-typesense-ecommerce-store

An app showing how you can use Typesense & Next.js / React to build a full-fledged ecommerce browsing and searching experience
JavaScript
36
star
19

typesense-java

Java client for Typesense
Java
31
star
20

showcase-ecommerce-store

An app showing how you can use Typesense to build a full-fledged ecommerce browsing and searching experience
JavaScript
31
star
21

gatsby-plugin-typesense

A Gatsby plugin to automatically index content to Typesense post-build
JavaScript
30
star
22

showcase-linux-commits-search

Instantly search 1M Linux Kernel Commit Messages using Typesense Search (an open source alternative to Algolia / ElasticSearch) ⚑ πŸ’» πŸ”
JavaScript
29
star
23

typesense-kubernetes

Typesense Kubernetes
24
star
24

postman

Postman collection for Typesense
21
star
25

typesense-rust

Rust client for Typesense | Work In Progress & Help Wanted
Rust
21
star
26

typesense-mongodb

A Node.js CLI to sync documents from a MongoDB collection to Typesense.
TypeScript
21
star
27

showcase-hn-comments-semantic-search

Semantic Search + Keyword Search + Hybrid Search + Filtering + Faceting on 300K HN Comments
JavaScript
20
star
28

typesense-docsearch.js

A fork of Algolia's awesome DocSearch.js, customized to use data from Typesense.
TypeScript
20
star
29

typesense-api-spec

Contains the API specs for the Typesense HTTP API
14
star
30

typesense-autocomplete-demo

A demo app that shows you how to use Algolia's autocomplete.js library with Typesense
HTML
13
star
31

typesense-instantsearch-semantic-search-demo

A demo that shows how to build a semantic search experience with Typesense's vector search feature and Instantsearch.js
JavaScript
13
star
32

showcase-xkcd-search

Search & Browse xkcd by topic & keywords | Using Typesense, an open source Algolia alternative and an easier-to-use alternative to Elasticsearch
JavaScript
12
star
33

typesense-rails

Rails Integration for Typesense | Work in Progress | Help Needed
Ruby
10
star
34

showcase-spellcheck

Use Typesense to build a type-ahead spellchecker
JavaScript
8
star
35

typesense-instantsearch-demo-no-npm-yarn

A demo that shows how to use typesense-instantsearch-adapter without NPM or YARN
HTML
8
star
36

showcase-airbnb-geosearch

Browse & GeoSearch 1 Million AirBnB listings with Typesense βš‘πŸ” ️🌎
JavaScript
8
star
37

typesense-wordpress-plugin

Deprecated in favor of https://wordpress.org/plugins/search-with-typesense/
JavaScript
7
star
38

typesense-vue-instantsearch-demo

A demo app that shows you how to use Vue & the Typesense InstantSearch adapter, to build rich search interfaces.
Vue
6
star
39

homebrew-tap

Homebrew Formulae for Typesense
Ruby
4
star
40

user-admin-search-laravel-demo

A demo Laravel app that integrates with Typesense
PHP
4
star
41

showcase-federated-search

An app that showcases federated search in Typesense (Open source alternative to Algolia)
JavaScript
3
star
42

typesense-lando-plugin

A Lando plugin for Typesense
JavaScript
2
star
43

typesense-flutter-demo

1
star
44

.github

Org-wide shared github configs
1
star
45

solr-xml-to-jsonl

A CLI utility to convert a solr data file to a simple JSONL file
JavaScript
1
star