-
Notifications
You must be signed in to change notification settings - Fork 64
Improve settings implementation feature #329
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6172075
update index settings when updating a collection (#328)
04030ae
Add section in README about settings usage
bidoubiwa 4c93a8c
Fix tests caused by strapi notifications
bidoubiwa 8b98055
Add resources with settings examples
bidoubiwa 2bdc99e
Add settings tests
bidoubiwa 4711231
Remove clear mocking
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 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,159 @@ | ||
| const { MeiliSearch } = require('meilisearch') | ||
| const createMeiliSearchConnector = require('../meilisearch') | ||
| const createStoreConnector = require('../store') | ||
| const createCollectionConnector = require('../collection') | ||
|
|
||
| jest.mock('meilisearch') | ||
|
|
||
| const addDocumentsMock = jest.fn(() => 10) | ||
| const updateSettingsMock = jest.fn(() => 10) | ||
|
|
||
| const mockIndex = jest.fn(() => ({ | ||
| addDocuments: addDocumentsMock, | ||
| updateSettings: updateSettingsMock, | ||
| })) | ||
|
|
||
| MeiliSearch.mockImplementation(() => { | ||
| return { | ||
| getOrCreateIndex: () => { | ||
| return mockIndex | ||
| }, | ||
| index: mockIndex, | ||
| } | ||
| }) | ||
|
|
||
| const storeClientMock = { | ||
| set: jest.fn(() => 'test'), | ||
| get: jest.fn(() => 'test'), | ||
| } | ||
|
|
||
| const servicesMock = { | ||
| restaurant: { | ||
| count: jest.fn(() => { | ||
| return 11 | ||
| }), | ||
| find: jest.fn(() => { | ||
| return [{ id: '1', collection: [{ name: 'one' }, { name: 'two' }] }] | ||
| }), | ||
| }, | ||
| } | ||
|
|
||
| const transformEntryMock = jest.fn(function ({ entry }) { | ||
| const transformedEntry = { | ||
| ...entry, | ||
| collection: entry.collection.map(cat => cat.name), | ||
| } | ||
| return transformedEntry | ||
| }) | ||
|
|
||
| const loggerMock = { | ||
| warn: jest.fn(() => 'test'), | ||
| } | ||
|
|
||
| describe('Test MeiliSearch settings', () => { | ||
| let storeConnector | ||
| beforeEach(async () => { | ||
| jest.clearAllMocks() | ||
| jest.restoreAllMocks() | ||
| storeConnector = createStoreConnector({ | ||
| storeClient: storeClientMock, | ||
| }) | ||
| }) | ||
|
|
||
| test('Test not settings field in configuration', async () => { | ||
| const modelMock = { | ||
| restaurant: { | ||
| meilisearch: { | ||
| indexName: 'my_restaurant', | ||
| transformEntry: transformEntryMock, | ||
| }, | ||
| }, | ||
| } | ||
| const collectionConnector = createCollectionConnector({ | ||
| logger: loggerMock, | ||
| models: modelMock, | ||
| services: servicesMock, | ||
| }) | ||
| const meilisearchConnector = await createMeiliSearchConnector({ | ||
| collectionConnector, | ||
| storeConnector, | ||
| }) | ||
| const getIndexNameSpy = jest.spyOn(collectionConnector, 'getIndexName') | ||
| const getSettingsSpy = jest.spyOn(collectionConnector, 'getSettings') | ||
|
|
||
| await meilisearchConnector.addCollectionInMeiliSearch('restaurant') | ||
|
|
||
| expect(servicesMock.restaurant.count).toHaveBeenCalledTimes(1) | ||
|
|
||
| expect(getIndexNameSpy).toHaveBeenCalledWith('restaurant') | ||
| expect(getIndexNameSpy).toHaveReturnedWith('my_restaurant') | ||
| expect(getSettingsSpy).toHaveBeenCalledWith('restaurant') | ||
| expect(getSettingsSpy).toHaveReturnedWith({}) | ||
| }) | ||
|
|
||
| test('Test a empty setting object in configuration', async () => { | ||
| const modelMock = { | ||
| restaurant: { | ||
| meilisearch: { | ||
| indexName: 'my_restaurant', | ||
| transformEntry: transformEntryMock, | ||
| settings: {}, | ||
| }, | ||
| }, | ||
| } | ||
| const collectionConnector = createCollectionConnector({ | ||
| logger: loggerMock, | ||
| models: modelMock, | ||
| services: servicesMock, | ||
| }) | ||
| const meilisearchConnector = await createMeiliSearchConnector({ | ||
| collectionConnector, | ||
| storeConnector, | ||
| }) | ||
| const getIndexNameSpy = jest.spyOn(collectionConnector, 'getIndexName') | ||
| const getSettingsSpy = jest.spyOn(collectionConnector, 'getSettings') | ||
|
|
||
| await meilisearchConnector.addCollectionInMeiliSearch('restaurant') | ||
|
|
||
| expect(servicesMock.restaurant.count).toHaveBeenCalledTimes(1) | ||
|
|
||
| expect(getIndexNameSpy).toHaveBeenCalledWith('restaurant') | ||
| expect(getIndexNameSpy).toHaveReturnedWith('my_restaurant') | ||
| expect(getSettingsSpy).toHaveBeenCalledWith('restaurant') | ||
| expect(getSettingsSpy).toHaveReturnedWith({}) | ||
| }) | ||
|
|
||
| test('Test a setting object with one field in configuration', async () => { | ||
| const modelMock = { | ||
| restaurant: { | ||
| meilisearch: { | ||
| indexName: 'my_restaurant', | ||
| transformEntry: transformEntryMock, | ||
| settings: { | ||
| searchableAttributes: ['*'], | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| const collectionConnector = createCollectionConnector({ | ||
| logger: loggerMock, | ||
| models: modelMock, | ||
| services: servicesMock, | ||
| }) | ||
| const meilisearchConnector = await createMeiliSearchConnector({ | ||
| collectionConnector, | ||
| storeConnector, | ||
| }) | ||
| const getIndexNameSpy = jest.spyOn(collectionConnector, 'getIndexName') | ||
| const getSettingsSpy = jest.spyOn(collectionConnector, 'getSettings') | ||
|
|
||
| await meilisearchConnector.addCollectionInMeiliSearch('restaurant') | ||
|
|
||
| expect(servicesMock.restaurant.count).toHaveBeenCalledTimes(1) | ||
|
|
||
| expect(getIndexNameSpy).toHaveBeenCalledWith('restaurant') | ||
| expect(getIndexNameSpy).toHaveReturnedWith('my_restaurant') | ||
| expect(getSettingsSpy).toHaveBeenCalledWith('restaurant') | ||
| expect(getSettingsSpy).toHaveReturnedWith({ searchableAttributes: ['*'] }) | ||
| }) | ||
| }) |
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 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 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 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,17 @@ | ||
| /** | ||
| * Adds the settings specific to this collection in MeiliSearch. | ||
| */ | ||
|
|
||
| module.exports = { | ||
| meilisearch: { | ||
| settings: { | ||
| filterableAttributes: ['genres'], | ||
| distinctAttribute: null, | ||
| searchableAttributes: ['title', 'description', 'genres'], | ||
| synonyms: { | ||
| wolverine: ['xmen', 'logan'], | ||
| logan: ['wolverine', 'xmen'], | ||
| }, | ||
| }, | ||
| }, | ||
| } |
37 changes: 37 additions & 0 deletions
37
resources/meilisearch-settings/make-relationship-filterable.js
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,37 @@ | ||
| /** | ||
| * Imagine if you have a collection restaurant with a category field. | ||
| * Category is a collection of its own. They have a many-to-many relationship. | ||
| * | ||
| * In MeiliSearch you can use filters to for example in our case, only find italian restaurants. | ||
| * To be able to do that, you need to provide a list of values and add in the settings the field in `filterableAttributes`. | ||
| * See guide: https://docs.meilisearch.com/reference/features/filtering_and_faceted_search.html#configuring-filters | ||
| * | ||
| * In Strapi, when fetching an entry the many-to-many relationships are inside an object: | ||
| * | ||
| * { | ||
| * id: 1, | ||
| * restaurant_name: "The squared pizza", | ||
| * category: [ | ||
| * { id: 1, name: "italian" }, | ||
| * * { id: 2, name: "French" } | ||
| * ] | ||
| * } | ||
| * | ||
| * Since MeiliSearch is expecting `category: ["Italian", "french"]` and | ||
| * also `category` to be in `filterableAttributes` we can use the model configuration file to provide all these informations. | ||
| */ | ||
|
|
||
| module.exports = { | ||
| meilisearch: { | ||
| settings: { | ||
| filterableAttributes: ['categories'], // add categories to filterable attributes. | ||
| }, | ||
| transformEntry({ entry }) { | ||
| const transformedEntry = { | ||
| ...entry, | ||
| categories: entry.categories.map(cat => cat.name), // map categories to only have categories name. | ||
| } | ||
| return transformedEntry | ||
| }, | ||
| }, | ||
| } | ||
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.
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.