Skip to content

Commit 362d4a1

Browse files
c4ndybarHaroenv
authored andcommitted
feat(settings): allow keeping index settings using mergeSettings (#153)
* adding merge settings option * adding docs
1 parent f0b976f commit 362d4a1

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ const queries = [
6969
// Note: by supplying settings, you will overwrite all existing settings on the index
7070
},
7171
matchFields: ['slug', 'modified'], // Array<String> overrides main match fields, optional
72+
mergeSettings: false, // optional, defaults to false. See notes on mergeSettings below
7273
},
7374
];
7475

@@ -131,6 +132,14 @@ You can also specify `matchFields` per query to check for different fields based
131132

132133
You can set settings for each index individually (per query), or otherwise it will keep your existing settings.
133134

135+
### Merge Settings
136+
137+
`mergeSettings` allows you to preserve settings changes made on the Algolia website. The default behavior (`mergeSettings: false`) will wipe out your index settings and replace them with settings from the config on each build.
138+
139+
When set to true, the config index settings will be merged with the existing index settings in Algolia (with the config index settings taking precendence).
140+
141+
NOTE: When using `mergeSettings`, any **deleted** settings from the config settings will continue to be persisted since they will still exist in Algolia. If you want to remove a setting, be sure to remove it from both the config and on Algolia's website.
142+
134143
### Replicas
135144

136145
For replica settings, extra care is taken to make sure only apply replicas to non-temporary indices.

gatsby-node.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,11 @@ async function runIndexQueries(
318318

319319
// defer to first query for index settings
320320
// todo: maybe iterate over all settings and throw if they differ
321-
const { settings = mainSettings, forwardToReplicas } = queries[0] || {};
321+
const { settings = mainSettings, mergeSettings = false, forwardToReplicas } = queries[0] || {};
322322

323323
const settingsToApply = await getSettingsToApply({
324324
settings,
325+
mergeSettings,
325326
index,
326327
tempIndex,
327328
indexToUse,
@@ -399,6 +400,7 @@ async function getIndexToUse({ index, tempIndex, enablePartialUpdates }) {
399400
/**
400401
* @param {object} options
401402
* @param {import('@algolia/client-search').Settings} options.settings
403+
* @param {boolean} query.mergeSettings
402404
* @param {import('algoliasearch').SearchIndex} options.index
403405
* @param {import('algoliasearch').SearchIndex} options.tempIndex
404406
* @param {import('algoliasearch').SearchIndex} options.indexToUse
@@ -407,6 +409,7 @@ async function getIndexToUse({ index, tempIndex, enablePartialUpdates }) {
407409
*/
408410
async function getSettingsToApply({
409411
settings,
412+
mergeSettings,
410413
index,
411414
tempIndex,
412415
indexToUse,
@@ -427,6 +430,7 @@ async function getSettingsToApply({
427430
);
428431

429432
const { replicaUpdateMode, ...requestedSettings } = {
433+
...(mergeSettings ? existingSettings : {}),
430434
...settings,
431435
replicas: replicasToSet,
432436
};

0 commit comments

Comments
 (0)