-
Notifications
You must be signed in to change notification settings - Fork 332
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,28 @@ | ||
import { Hit } from '@algolia/client-search'; | ||
import algoliasearch from 'algoliasearch/lite'; | ||
import { | ||
autocomplete, | ||
AutocompleteSource, | ||
getAlgoliaHits, | ||
reverseHighlightHit, | ||
} from '@algolia/autocomplete-js'; | ||
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' | ||
); | ||
|
||
type QuerySuggestionHit = { query: string }; | ||
|
||
const recentSearches = createLocalStorageRecentSearchesPlugin({ | ||
key: 'recent', | ||
limit: 3, | ||
}); | ||
|
||
autocomplete<Hit<QuerySuggestionHit>>({ | ||
container: '#autocomplete', | ||
debug: true, | ||
openOnFocus: true, | ||
// dropdownPlacement: 'start', | ||
plugins: [recentSearches], | ||
getSources({ query }) { | ||
return getAlgoliaHits<QuerySuggestionHit>({ | ||
searchClient, | ||
queries: [ | ||
{ | ||
indexName: 'instant_search_demo_query_suggestions', | ||
query, | ||
params: { | ||
hitsPerPage: recentSearches.data.getAlgoliaQuerySuggestionsHitsPerPage( | ||
10 | ||
), | ||
facetFilters: [ | ||
...recentSearches.data.getAlgoliaQuerySuggestionsFacetFilters(), | ||
], | ||
}, | ||
}, | ||
], | ||
}).then(([hits]) => { | ||
return [ | ||
{ | ||
getItemInputValue({ item }) { | ||
return item.query; | ||
}, | ||
getItems() { | ||
return hits; | ||
}, | ||
templates: { | ||
item({ item }) { | ||
return ` | ||
<div class="aa-ItemContent"> | ||
<div class="aa-ItemSourceIcon">${searchIcon}</div> | ||
<div class="aa-ItemTitle"> | ||
${reverseHighlightHit<Hit<QuerySuggestionHit>>({ | ||
hit: item, | ||
attribute: 'query', | ||
})} | ||
</div> | ||
</div> | ||
`; | ||
}, | ||
}, | ||
} as AutocompleteSource<Hit<QuerySuggestionHit>>, | ||
]; | ||
}); | ||
const querySuggestions = createQuerySuggestionsPlugin({ | ||
searchClient, | ||
indexName: 'instant_search_demo_query_suggestions', | ||
getSearchParams() { | ||
return recentSearches.data.getAlgoliaSearchParams(); | ||
}, | ||
}); | ||
|
||
const searchIcon = ` | ||
<svg width="20" height="20" viewBox="0 0 20 20"> | ||
<path | ||
d="M14.386 14.386l4.0877 4.0877-4.0877-4.0877c-2.9418 2.9419-7.7115 2.9419-10.6533 0-2.9419-2.9418-2.9419-7.7115 0-10.6533 2.9418-2.9419 7.7115-2.9419 10.6533 0 2.9419 2.9418 2.9419 7.7115 0 10.6533z" | ||
stroke="currentColor" | ||
fill="none" | ||
fillRule="evenodd" | ||
strokeLinecap="round" | ||
strokeLinejoin="round" | ||
/> | ||
</svg> | ||
`; | ||
autocomplete({ | ||
container: '#autocomplete', | ||
openOnFocus: true, | ||
plugins: [recentSearches, querySuggestions], | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# @algolia/autocomplete-plugin-query-suggestions | ||
|
||
A plugin to add Query Suggestions to Algolia Autocomplete. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add some info here to link to the query suggestions feature? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
## Installation | ||
|
||
```sh | ||
yarn add @algolia/autocomplete-plugin-query-suggestions@alpha | ||
# or | ||
npm install @algolia/autocomplete-plugin-query-suggestions@alpha | ||
``` | ||
francoischalifour marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"name": "@algolia/autocomplete-plugin-query-suggestions", | ||
"description": "A plugin to add query suggestions to Algolia Autocomplete.", | ||
"version": "1.0.0-alpha.34", | ||
"license": "MIT", | ||
"homepage": "https://github.com/algolia/autocomplete.js", | ||
"repository": "algolia/autocomplete.js", | ||
"author": { | ||
"name": "Algolia, Inc.", | ||
"url": "https://www.algolia.com" | ||
}, | ||
"source": "src/index.ts", | ||
"types": "dist/esm/index.d.ts", | ||
"module": "dist/esm/index.js", | ||
"main": "dist/umd/index.js", | ||
"umd:main": "dist/umd/index.js", | ||
"unpkg": "dist/umd/index.js", | ||
"jsdelivr": "dist/umd/index.js", | ||
"sideEffects": false, | ||
"files": [ | ||
"dist/" | ||
], | ||
"scripts": { | ||
"build:clean": "rm -rf ./dist", | ||
"build:esm": "babel src --root-mode upward --extensions '.ts,.tsx' --out-dir dist/esm --ignore '**/*/__tests__/'", | ||
"build:types": "tsc -p ./tsconfig.declaration.json --outDir ./dist/esm", | ||
"build:umd": "rollup --config", | ||
"build": "rm -rf ./dist && yarn build:umd && yarn build:esm && yarn build:types", | ||
"on:change": "concurrently \"yarn build:esm\" \"yarn build:types\"", | ||
"prepare": "yarn run build:esm", | ||
"watch": "watch \"yarn on:change\" --ignoreDirectoryPattern \"/dist/\"" | ||
}, | ||
"dependencies": { | ||
"@algolia/autocomplete-core": "1.0.0-alpha.34", | ||
"@algolia/autocomplete-js": "1.0.0-alpha.34" | ||
}, | ||
"devDependencies": { | ||
"@algolia/client-search": "4.5.1", | ||
"algoliasearch": "4.5.1" | ||
}, | ||
"peerDependencies": { | ||
"@algolia/client-search": "^4.5.1", | ||
"algoliasearch": "^4.5.1" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { plugins } from '../../rollup.base.config'; | ||
import { getBundleBanner } from '../../scripts/getBundleBanner'; | ||
|
||
import pkg from './package.json'; | ||
|
||
export default { | ||
input: 'src/index.ts', | ||
output: { | ||
file: 'dist/umd/index.js', | ||
format: 'umd', | ||
sourcemap: true, | ||
name: pkg.name, | ||
banner: getBundleBanner(pkg), | ||
}, | ||
plugins, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { AutocompletePlugin } from '@algolia/autocomplete-core'; | ||
import { getAlgoliaHits, SourceTemplates } from '@algolia/autocomplete-js'; | ||
import { SearchOptions } from '@algolia/client-search'; | ||
import { SearchClient } from 'algoliasearch/lite'; | ||
|
||
import { | ||
getTemplates as defaultGetTemplates, | ||
GetTemplatesParams, | ||
} from './getTemplates'; | ||
import { QuerySuggestionsHit } from './types'; | ||
|
||
export type CreateQuerySuggestionsPluginParams< | ||
TItem extends QuerySuggestionsHit | ||
> = { | ||
searchClient: SearchClient; | ||
indexName: string; | ||
getSearchParams?(): SearchOptions; | ||
getTemplates?( | ||
params: GetTemplatesParams<TItem> | ||
): SourceTemplates<TItem>['templates']; | ||
}; | ||
|
||
export function createQuerySuggestionsPlugin< | ||
TItem extends QuerySuggestionsHit | ||
>({ | ||
searchClient, | ||
indexName, | ||
getSearchParams = () => ({}), | ||
getTemplates = defaultGetTemplates, | ||
}: CreateQuerySuggestionsPluginParams<TItem>): AutocompletePlugin< | ||
TItem, | ||
undefined | ||
> { | ||
return { | ||
getSources({ query, setQuery, refresh }) { | ||
return [ | ||
{ | ||
getItemInputValue({ item }) { | ||
return item.query; | ||
}, | ||
getItems() { | ||
return getAlgoliaHits<TItem>({ | ||
searchClient, | ||
queries: [ | ||
{ | ||
indexName, | ||
query, | ||
params: getSearchParams(), | ||
}, | ||
], | ||
}); | ||
}, | ||
templates: getTemplates({ | ||
onTapAhead(item) { | ||
setQuery(item.query); | ||
refresh(); | ||
}, | ||
}), | ||
}, | ||
]; | ||
}, | ||
}; | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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!