Tip: Updating Elasticsearch synonyms list on-the-fly

mikko.ropanen
mikko.ropanen Spryker Solution Partner Posts: 27 ✨ - Novice

So it came up that many Spryker customers wish there was a backoffice GUI for maintaining a synonyms list for the site search. Well there isn't, but technically it's a relatively simple operation:

// Get index
$index = $this->client->getIndex($this->indexName);

// Close index
$index->close();
// Update settings
$newSettings['analysis']['filter']['synonym_graph_filter']['synonyms'] = $synonyms;
// Persist new settings
$index->setSettings($newSettings);
// Open index
$index->open();

where $this→client is an \Elastica\Client and $synonyms is just an array.

Hope this is useful for someone!

Tagged: