Skip to content

Commit

Permalink
fix(settings): put synonyms and rules in the configure file (#128)
Browse files Browse the repository at this point in the history
* refactor(settings): put synonyms and rules in the configure file

fixes #123
  • Loading branch information
Haroenv authored and vvo committed Feb 13, 2018
1 parent 59deb78 commit af8e709
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 8 deletions.
29 changes: 25 additions & 4 deletions src/__tests__/__snapshots__/config.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down Expand Up @@ -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",
Expand Down
23 changes: 22 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
17 changes: 14 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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);
Expand Down

0 comments on commit af8e709

Please sign in to comment.