Created
September 1, 2024 06:55
-
-
Save frueda1/9d9d477560fe714ec48517810faa9106 to your computer and use it in GitHub Desktop.
Creating Elasticsearch analyzers from Sitecore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
POST my_sitecore_index/_close | |
PUT my_sitecore_index/_settings | |
{ | |
"analysis": { | |
"filter": { | |
"my_synonyms_filter": { | |
"type": "synonym", | |
"synonyms": [ | |
"RPA,Robotic Process,Robot Process Automation,Automatic Processes", | |
"XP, EXP, Experience Services", | |
"PM, PMI, Project Management" | |
] | |
} | |
}, | |
"analyzer": { | |
"my_synonyms_analyzer": { | |
"type": "custom", | |
"tokenizer": "standard", | |
"filter": [ | |
"lowercase", | |
"my_synonyms_filter" | |
] | |
} | |
} | |
} | |
} | |
POST my_sitecore_index/_open |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Nest; | |
using Sitecore.Configuration; | |
using Sitecore.Diagnostics; | |
using Sitecore.Shell.Applications.ContentEditor; | |
using Sitecore.Shell.Framework.Commands; | |
using System; | |
using System.Collections.Generic; | |
using System.Net.PeerToPeer; | |
using System.Threading.Tasks; | |
namespace Workshop.Foundation.Elasticsearch.Commands | |
{ | |
public class SynonymsAnalyzer : Sitecore.Shell.Framework.Commands.Command | |
{ | |
private readonly ElasticClient _client; | |
private readonly ConnectionSettings _connectionSettings; | |
private readonly Uri _node; | |
public SynonymsAnalyzer() | |
{ | |
_node = new Uri("[ELASTIC_URL]"); | |
_connectionSettings = new ConnectionSettings(_node); | |
_connectionSettings | |
.BasicAuthentication("username", "password"); | |
_client = new ElasticClient(_connectionSettings); | |
} | |
public override void Execute(CommandContext context) | |
{ | |
Sitecore.Context.ClientPage.ClientResponse.Alert($"Running creation of the analyzer"); | |
CreateSynonymsAnalyzer(); | |
} | |
public async Task<Result> CreateSynonymsAnalyzer() | |
{ | |
var indexResult = Result.Created; | |
var synonymsFilter = "my_synonyms_filter"; | |
var synonymsSearchAnalyzer = "my_synonyms_analyzer"; | |
var indexName = "my_sitecore_index"; | |
var synonymsList = new List<string> | |
{ | |
"RPA, Robotic Process, Robot Process Automation, Automatic Processes", | |
"XP, EXP, Experience Services", | |
"PM, PMI, Project Management" | |
}; | |
try | |
{ | |
var closeIndexResponse = _client.Indices.Close(indexName); | |
if (!closeIndexResponse.IsValid) | |
{ | |
return Result.Error; | |
} | |
var updateSettingsResponse = _client.Indices.UpdateSettings(indexName, u => u | |
.IndexSettings(s => s | |
.Analysis(a => a | |
.TokenFilters(tf => tf | |
.Synonym(synonymsFilter, sf => sf | |
.Synonyms(synonymsList) | |
) | |
) | |
.Analyzers(an => an | |
.Custom(synonymsSearchAnalyzer, ca => ca | |
.Tokenizer("standard") | |
.Filters("lowercase", synonymsFilter) | |
) | |
) | |
) | |
) | |
); | |
if (!updateSettingsResponse.IsValid) | |
{ | |
Log.Info($"Elasticsearch response: {updateSettingsResponse.DebugInformation}", this); | |
return Result.Error; | |
} | |
} | |
catch (Exception ex) | |
{ | |
Log.Error($"Error in creating the analyzer in index {indexName}", ex, this); | |
} | |
finally | |
{ | |
var openIndexResponse = _client.Indices.Open(indexName); | |
} | |
return indexResult; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment