diff --git a/src/__tests__/__snapshots__/config.test.js.snap b/src/__tests__/__snapshots__/config.test.js.snap index 0c3799d49..a3a0f3775 100644 --- a/src/__tests__/__snapshots__/config.test.js.snap +++ b/src/__tests__/__snapshots__/config.test.js.snap @@ -7,6 +7,23 @@ Object { "bootstrapConcurrency": 100, "bootstrapIndexName": "npm-search-bootstrap", "indexName": "fake-index", + "indexRules": Array [ + Object { + "condition": Object { + "anchoring": "is", + "pattern": "{facet:concatenatedName}", + }, + "consequence": Object { + "params": Object { + "automaticOptionalFacetFilters": Array [ + "concatenatedName", + ], + }, + }, + "description": "promote exact matches", + "objectID": "promote-exact", + }, + ], "indexSettings": Object { "attributesForFaceting": Array [ "filterOnly(concatenatedName)", @@ -58,13 +75,17 @@ Object { "owners.name", ], "separatorsToIndex": "_", - "synonyms": Array [ - Array [ + }, + "indexSynonyms": Array [ + Object { + "objectID": "underscore", + "synonyms": Array [ "_", "underscore", ], - ], - }, + "type": "synonym", + }, + ], "maxObjSize": 450000, "npmDownloadsEndpoint": "https://api.npmjs.org/downloads", "npmRegistryEndpoint": "https://replicate.npmjs.com/registry", diff --git a/src/config.js b/src/config.js index 87f7dbfcb..5f98463ca 100644 --- a/src/config.js +++ b/src/config.js @@ -53,9 +53,30 @@ const defaultConfig = { ], optionalWords: ['js', 'javascript'], separatorsToIndex: '_', - synonyms: [['_', 'underscore']], replaceSynonymsInHighlight: false, }, + indexSynonyms: [ + { + type: 'synonym', + synonyms: ['_', 'underscore'], + objectID: 'underscore', + }, + ], + indexRules: [ + { + objectID: 'promote-exact', + description: 'promote exact matches', + condition: { + pattern: '{facet:concatenatedName}', + anchoring: 'is', + }, + consequence: { + params: { + automaticOptionalFacetFilters: ['concatenatedName'], + }, + }, + }, + ], }; export default Object.entries(defaultConfig).reduce( diff --git a/src/index.js b/src/index.js index e66fc924d..e914e6298 100644 --- a/src/index.js +++ b/src/index.js @@ -24,9 +24,8 @@ const { index: mainIndex, client } = createAlgoliaIndex(c.indexName); const { index: bootstrapIndex } = createAlgoliaIndex(c.bootstrapIndexName); const stateManager = createStateManager(mainIndex); -mainIndex - .setSettings(c.indexSettings) - .then(({ taskID }) => mainIndex.waitTask(taskID)) +setSettings(mainIndex) + .then(() => setSettings(bootstrapIndex)) .then(() => stateManager.check()) .then(bootstrap) .then(() => stateManager.get()) @@ -35,6 +34,18 @@ mainIndex .then(watch) .catch(error); +async function setSettings(index) { + await index.setSettings(c.indexSettings); + await index.batchSynonyms(c.indexSynonyms, { + replaceExistingSynonyms: true, + }); + const { taskID } = await index.batchRules(c.indexRules, { + replaceExistingRules: true, + }); + + return index.waitTask(taskID); +} + function infoChange(seq, nbChanges, emoji) { return npm.info().then(npmInfo => { const ratePerSecond = nbChanges / ((Date.now() - loopStart) / 1000);