-
Notifications
You must be signed in to change notification settings - Fork 66
Add cache system #533
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
Merged
Merged
Add cache system #533
Changes from 9 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3705a90
Add cache system
bidoubiwa 258e488
Add tests to avoid failing test
bidoubiwa 0ff0835
Use cached results even on pagination changes
bidoubiwa 8b91813
Add component architecture
bidoubiwa 5ec2991
Fix linting
bidoubiwa 57ad7d7
Add tests on search cache
bidoubiwa 44022d3
Add tests on search resolver
bidoubiwa f7c1fb0
Fix linting
bidoubiwa cb8a5c1
Create complete search context
bidoubiwa 1c81573
Update src/client/instant-meilisearch-client.ts
bidoubiwa c394d6f
Update tests/search-resolver.tests.ts
bidoubiwa 51e9081
Update tests/search-resolver.tests.ts
bidoubiwa 6857ba5
Update src/adapter/search-request-adapter/search-resolver.ts
bidoubiwa 63a8d64
Update src/client/instant-meilisearch-client.ts
bidoubiwa 88ea5e6
Update src/client/instant-meilisearch-client.ts
bidoubiwa fb7c192
Improve filters implementation
bidoubiwa 7593b70
Fix linting
bidoubiwa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,2 @@ | ||
| export * from './search-request-adapter' | ||
| export * from './search-response-adapter' | ||
| export * from './hits-adapter' | ||
| export * from './highlight-adapter' | ||
| export * from './pagination-adapter' | ||
| export * from './facets-distribution-adapter' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/cache/__tests__/cache.tests.ts → ...t-adapter/__tests__/filter-cache.tests.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import { cacheFilters } from '..' | ||
| import { cacheFilters } from '../filters' | ||
|
|
||
| const facetCacheData = [ | ||
| { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import { | ||
| Filter, | ||
| ParsedFilter, | ||
| FacetsDistribution, | ||
| FilterCache, | ||
| } from '../../types' | ||
| import { removeUndefined } from '../../utils' | ||
|
|
||
| /** | ||
| * @param {string} filter | ||
| */ | ||
| const adaptFilterSyntax = (filter: string) => { | ||
| const matches = filter.match(/([^=]*)="?([^\\"]*)"?$/) | ||
| if (matches) { | ||
| const [_, filterName, value] = matches | ||
| return [{ filterName, value }] | ||
| } | ||
| return [undefined] | ||
| } | ||
|
|
||
| /** | ||
| * @param {Filter} filters? | ||
| * @returns {Array} | ||
| */ | ||
| function extractFilters(filters?: Filter): Array<ParsedFilter | undefined> { | ||
| if (typeof filters === 'string') { | ||
| return adaptFilterSyntax(filters) | ||
| } else if (Array.isArray(filters)) { | ||
| return filters | ||
| .map((nestedFilter) => { | ||
| if (Array.isArray(nestedFilter)) { | ||
| return nestedFilter.map((filter) => adaptFilterSyntax(filter)) | ||
| } | ||
| return adaptFilterSyntax(nestedFilter) | ||
| }) | ||
| .flat(2) | ||
| } | ||
| return [undefined] | ||
bidoubiwa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * @param {Filter} filters? | ||
| * @returns {FilterCache} | ||
| */ | ||
| export function cacheFilters(filters?: Filter): FilterCache { | ||
| const extractedFilters = extractFilters(filters) | ||
| const cleanFilters = removeUndefined(extractedFilters) | ||
| return cleanFilters.reduce<FilterCache>( | ||
| (cache, parsedFilter: ParsedFilter) => { | ||
| const { filterName, value } = parsedFilter | ||
| const prevFields = cache[filterName] || [] | ||
| cache = { | ||
| ...cache, | ||
| [filterName]: [...prevFields, value], | ||
| } | ||
| return cache | ||
| }, | ||
| {} as FilterCache | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Assign missing filters to facetsDistribution. | ||
| * All facet passed as filter should appear in the facetsDistribution. | ||
| * If not present, the facet is added with 0 as value. | ||
| * | ||
| * | ||
| * @param {FilterCache} cache? | ||
| * @param {FacetsDistribution} distribution? | ||
| * @returns {FacetsDistribution} | ||
| */ | ||
| export function assignMissingFilters( | ||
| cache?: FilterCache, | ||
| distribution?: FacetsDistribution | ||
| ): FacetsDistribution { | ||
| distribution = distribution || {} | ||
| if (cache && Object.keys(cache).length > 0) { | ||
| for (const cachedFacet in cache) { | ||
| for (const cachedField of cache[cachedFacet]) { | ||
| // if cached field is not present in the returned distribution | ||
|
|
||
| if ( | ||
| !distribution[cachedFacet] || | ||
| !Object.keys(distribution[cachedFacet]).includes(cachedField) | ||
| ) { | ||
| // add 0 value | ||
| distribution[cachedFacet] = distribution[cachedFacet] || {} | ||
| distribution[cachedFacet][cachedField] = 0 | ||
| } | ||
| } | ||
| } | ||
bidoubiwa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| return distribution | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export * from './search-resolver' | ||
| export * from './search-params-adapter' |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.