Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugins): introduce Query Suggestions plugin #360

Merged
merged 2 commits into from
Nov 9, 2020

Conversation

francoischalifour
Copy link
Member

This introduces a new Autocomplete plugin: the Query Suggestions plugin.

Usage

Basic

import algoliasearch from 'algoliasearch/lite';
import { autocomplete } from '@algolia/autocomplete-js';
import { createQuerySuggestionsPlugin } from '@algolia/autocomplete-plugin-query-suggestions';

const searchClient = algoliasearch(
  'latency',
  '6be0576ff61c053d5f9a3225e2a90f76'
);
const querySuggestions = createQuerySuggestionsPlugin({
  searchClient,
  indexName: 'instant_search_demo_query_suggestions',
});

autocomplete({
  container: '#autocomplete',
  plugins: [querySuggestions],
});

Using with Recent Searches

import algoliasearch from 'algoliasearch/lite';
import { autocomplete } from '@algolia/autocomplete-js';
import { createLocalStorageRecentSearchesPlugin } from '@algolia/autocomplete-plugin-recent-searches';
import { createQuerySuggestionsPlugin } from '@algolia/autocomplete-plugin-query-suggestions';

const searchClient = algoliasearch(
  'latency',
  '6be0576ff61c053d5f9a3225e2a90f76'
);

const recentSearches = createLocalStorageRecentSearchesPlugin({
  key: 'recent',
  limit: 3,
});

const querySuggestions = createQuerySuggestionsPlugin({
  searchClient,
  indexName: 'instant_search_demo_query_suggestions',
  getSearchParams() {
    // `getAlgoliaSearchParams` adds `facetFilters` to dedupe items from Recent Searches,
    // as well as gets the right `hitsPerPage` after deduping.
    return recentSearches.data.getAlgoliaSearchParams({
      hitsPerPage: 5
    });
  },
});

autocomplete({
  container: '#autocomplete',
  openOnFocus: true,
  plugins: [recentSearches, querySuggestions],
});

Changes

  • Since it now makes sense to entirely rely on plugins, the getSources function becomes optional
  • The two data function from Recent Searches are now gathered in getAlgoliaSearchParams (see example above)

Next steps

  • Think about a Requester API that allows to batch all Algolia queries from plugins in a single network request
  • Gather use cases with AlexC to see if we should support Query Suggestions Categories in our plugin

@codesandbox-ci
Copy link

codesandbox-ci bot commented Nov 6, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 706245b:

Sandbox Source
@algolia/js-example Configuration

Copy link
Contributor

@Haroenv Haroenv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super cool wrapping!

autocomplete({
container: '#autocomplete',
openOnFocus: true,
plugins: [recentSearches, querySuggestions],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the main example should probably still show results as well as query suggestions, to show more of the full api, wdyt?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah eventually we'll have examples in other places, that's used for dev only for now—so not too much of a deal!

# @algolia/autocomplete-plugin-query-suggestions

A plugin to add Query Suggestions to Algolia Autocomplete.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add some info here to link to the query suggestions feature?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, we'll work on an exhaustive doc with Maria so we'll wait for this content before changing this doc multiple times if you don't mind.

@Haroenv
Copy link
Contributor

Haroenv commented Nov 6, 2020

How do you deduplicate searches between multiple query suggestion sources / regular hits?

@francoischalifour
Copy link
Member Author

@Haroenv Right now we only duplicate RS/QS when provided explicitly with getAlgoliaSearchParams. AlexC and I are starting to think about the concept of grouping/combining to dedup between sources—so that'll be a future API! (And I'm open to ideas!)

Copy link
Contributor

@Haroenv Haroenv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it would be better to have a more complete readme that can be changed later; the code sample would most likely stay the same, and in the mean time it wouldn't be as straightforward to use this in an app, but I understand not wanting to rewrite I guess

Copy link
Contributor

@eunjae-lee eunjae-lee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. A good addition!

@francoischalifour francoischalifour merged commit 7d19396 into next Nov 9, 2020
@francoischalifour francoischalifour deleted the feat/autocomplete-plugin-query-suggestions branch November 9, 2020 10:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants