Skip to content

Commit af8e709

Browse files
Haroenvvvo
authored andcommitted
fix(settings): put synonyms and rules in the configure file (#128)
* refactor(settings): put synonyms and rules in the configure file fixes #123
1 parent 59deb78 commit af8e709

File tree

3 files changed

+61
-8
lines changed

3 files changed

+61
-8
lines changed

src/__tests__/__snapshots__/config.test.js.snap

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@ Object {
77
"bootstrapConcurrency": 100,
88
"bootstrapIndexName": "npm-search-bootstrap",
99
"indexName": "fake-index",
10+
"indexRules": Array [
11+
Object {
12+
"condition": Object {
13+
"anchoring": "is",
14+
"pattern": "{facet:concatenatedName}",
15+
},
16+
"consequence": Object {
17+
"params": Object {
18+
"automaticOptionalFacetFilters": Array [
19+
"concatenatedName",
20+
],
21+
},
22+
},
23+
"description": "promote exact matches",
24+
"objectID": "promote-exact",
25+
},
26+
],
1027
"indexSettings": Object {
1128
"attributesForFaceting": Array [
1229
"filterOnly(concatenatedName)",
@@ -58,13 +75,17 @@ Object {
5875
"owners.name",
5976
],
6077
"separatorsToIndex": "_",
61-
"synonyms": Array [
62-
Array [
78+
},
79+
"indexSynonyms": Array [
80+
Object {
81+
"objectID": "underscore",
82+
"synonyms": Array [
6383
"_",
6484
"underscore",
6585
],
66-
],
67-
},
86+
"type": "synonym",
87+
},
88+
],
6889
"maxObjSize": 450000,
6990
"npmDownloadsEndpoint": "https://api.npmjs.org/downloads",
7091
"npmRegistryEndpoint": "https://replicate.npmjs.com/registry",

src/config.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,30 @@ const defaultConfig = {
5353
],
5454
optionalWords: ['js', 'javascript'],
5555
separatorsToIndex: '_',
56-
synonyms: [['_', 'underscore']],
5756
replaceSynonymsInHighlight: false,
5857
},
58+
indexSynonyms: [
59+
{
60+
type: 'synonym',
61+
synonyms: ['_', 'underscore'],
62+
objectID: 'underscore',
63+
},
64+
],
65+
indexRules: [
66+
{
67+
objectID: 'promote-exact',
68+
description: 'promote exact matches',
69+
condition: {
70+
pattern: '{facet:concatenatedName}',
71+
anchoring: 'is',
72+
},
73+
consequence: {
74+
params: {
75+
automaticOptionalFacetFilters: ['concatenatedName'],
76+
},
77+
},
78+
},
79+
],
5980
};
6081

6182
export default Object.entries(defaultConfig).reduce(

src/index.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ const { index: mainIndex, client } = createAlgoliaIndex(c.indexName);
2424
const { index: bootstrapIndex } = createAlgoliaIndex(c.bootstrapIndexName);
2525
const stateManager = createStateManager(mainIndex);
2626

27-
mainIndex
28-
.setSettings(c.indexSettings)
29-
.then(({ taskID }) => mainIndex.waitTask(taskID))
27+
setSettings(mainIndex)
28+
.then(() => setSettings(bootstrapIndex))
3029
.then(() => stateManager.check())
3130
.then(bootstrap)
3231
.then(() => stateManager.get())
@@ -35,6 +34,18 @@ mainIndex
3534
.then(watch)
3635
.catch(error);
3736

37+
async function setSettings(index) {
38+
await index.setSettings(c.indexSettings);
39+
await index.batchSynonyms(c.indexSynonyms, {
40+
replaceExistingSynonyms: true,
41+
});
42+
const { taskID } = await index.batchRules(c.indexRules, {
43+
replaceExistingRules: true,
44+
});
45+
46+
return index.waitTask(taskID);
47+
}
48+
3849
function infoChange(seq, nbChanges, emoji) {
3950
return npm.info().then(npmInfo => {
4051
const ratePerSecond = nbChanges / ((Date.now() - loopStart) / 1000);

0 commit comments

Comments
 (0)