Skip to content

Commit

Permalink
feat(qs): rename category to categoryAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Feb 26, 2021
1 parent 396dcb7 commit 5d8c5d4
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 18 deletions.
7 changes: 6 additions & 1 deletion examples/js/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ const querySuggestionsPlugin = createQuerySuggestionsPlugin({
hitsPerPage: state.query ? 5 : 10,
});
},
categoryAttribute: 'categories',
categoryAttribute: [
'instant_search',
'facets',
'exact_matches',
'categories',
],
});
const categoriesPlugin = createCategoriesPlugin({ searchClient });

Expand Down
7 changes: 6 additions & 1 deletion examples/query-suggestions-with-inline-categories/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ const querySuggestionsPlugin = createQuerySuggestionsPlugin({
hitsPerPage: 6,
};
},
categoryAttribute: 'categories',
categoryAttribute: [
'instant_search',
'facets',
'exact_matches',
'categories',
],
categoriesPerItem: 1,
categoriesLimit: 2,
transformSource: ({ source, onTapAhead }) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/autocomplete-plugin-query-suggestions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"dependencies": {
"@algolia/autocomplete-core": "1.0.0-alpha.43",
"@algolia/autocomplete-js": "1.0.0-alpha.43",
"@algolia/autocomplete-preset-algolia": "1.0.0-alpha.43"
"@algolia/autocomplete-preset-algolia": "1.0.0-alpha.43",
"@algolia/autocomplete-shared": "1.0.0-alpha.43"
},
"devDependencies": {
"@algolia/client-search": "4.8.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
AutocompleteState,
} from '@algolia/autocomplete-core';
import { AutocompleteSource, getAlgoliaHits } from '@algolia/autocomplete-js';
import { getAttributeValueByPath } from '@algolia/autocomplete-shared';
import { SearchOptions } from '@algolia/client-search';
import { SearchClient } from 'algoliasearch/lite';

Expand All @@ -21,8 +22,10 @@ export type CreateQuerySuggestionsPluginParams<
}): AutocompleteSource<TItem>;
/**
* The attribute to display categories.
* @example "instant_search.facets.exact_matches.categories"
* @example "instant_search.facets.exact_matches['hierarchicalCategories.lvl0']"
*/
categoryAttribute?: string;
categoryAttribute?: string | string[];
/**
* The number of items to display categories for.
* @default 1
Expand Down Expand Up @@ -86,9 +89,12 @@ export function createQuerySuggestionsPlugin<
> = [current];

if (i <= categoriesPerItem - 1) {
const categories = current.instant_search.facets.exact_matches[
categoryAttribute
]
const categories = getAttributeValueByPath(
current,
Array.isArray(categoryAttribute)
? categoryAttribute
: [categoryAttribute]
)
.map((x) => x.value)
.slice(0, categoriesLimit);

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { warn } from '@algolia/autocomplete-shared';
import { getAttributeValueByPath, warn } from '@algolia/autocomplete-shared';

import { getAttributeValueByPath } from './getAttributeValueByPath';
import { HighlightedHit } from './HighlightedHit';
import { ParseAlgoliaHitParams } from './ParseAlgoliaHitParams';
import { parseAttribute } from './parseAttribute';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { warn } from '@algolia/autocomplete-shared';
import { getAttributeValueByPath, warn } from '@algolia/autocomplete-shared';

import { getAttributeValueByPath } from './getAttributeValueByPath';
import { ParseAlgoliaHitParams } from './ParseAlgoliaHitParams';
import { parseAttribute } from './parseAttribute';
import { ParsedAttribute } from './ParsedAttribute';
Expand Down
6 changes: 6 additions & 0 deletions packages/autocomplete-shared/src/getAttributeValueByPath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function getAttributeValueByPath<TRecord>(
record: TRecord,
path: string[]
): any {
return path.reduce((current, key) => current && current[key], record);
}
1 change: 1 addition & 0 deletions packages/autocomplete-shared/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './createRef';
export * from './debounce';
export * from './getAttributeValueByPath';
export * from './getItemsCount';
export * from './invariant';
export * from './isEqual';
Expand Down
16 changes: 13 additions & 3 deletions packages/website/docs/adding-suggested-searches.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ const querySuggestionsPlugin = createQuerySuggestionsPlugin({
const querySuggestionsPluginWithCategories = createQuerySuggestionsPlugin({
searchClient,
indexName: 'instant_search_demo_query_suggestions',
categoryAttribute: 'categories',
categoryAttribute: [
'instant_search',
'facets',
'exact_matches',
'categories',
],
categoriesLimit: 2,
categoriesPerItem: 2,
});
Expand Down Expand Up @@ -141,7 +146,7 @@ With [some configuration](https://www.algolia.com/doc/guides/getting-insights-an

To display categories with the suggestions, you need to define the attribute to retrieve category information from, using the `categoryAttribute` option when instantiating the plugin.

In this example, the category data is stored in the nested attribute `instant_search.facets.exact_matches.categories`. With this structure, you only need to provide `"categories"` as the `categoryAttribute`.
In this example, the category data is stored in the nested attribute `instant_search.facets.exact_matches.categories`. With this structure, you need to provide the path `["instant_search", "facets", "exact_matches", "categories"]` as the `categoryAttribute`.

You can also set the number of items to display categories for using `categoriesLimit` and the maximum number of categories to display per item using `categoriesPerItem`. Both default to `1`.

Expand All @@ -159,7 +164,12 @@ const querySuggestionsPlugin = createQuerySuggestionsPlugin({
getSearchParams({ state }) {
return { hitsPerPage: state.query ? 5 : 10 };
},
categoryAttribute: 'categories',
categoryAttribute: [
'instant_search',
'facets',
'exact_matches',
'categories',
],
categoriesLimit: 2,
categoriesPerItem: 2,
});
Expand Down
7 changes: 6 additions & 1 deletion packages/website/docs/introduction.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ const querySuggestionsPlugin = createQuerySuggestionsPlugin({
hitsPerPage: 3,
};
},
categoryAttribute: 'categories',
categoryAttribute: [
'instant_search',
'facets',
'exact_matches',
'categories',
],
categoriesLimit: 2,
});
const productsPlugin = createProductsPlugin();
Expand Down

0 comments on commit 5d8c5d4

Please sign in to comment.