Skip to content

Commit

Permalink
Removed support for Elastic 5 and 6. Lots of changes and cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Øyvind Tanum committed Jan 17, 2024
1 parent 85d2d45 commit 3066524
Show file tree
Hide file tree
Showing 20 changed files with 153 additions and 284 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ public ActionResult Index(string query, string index)

if(!String.IsNullOrWhiteSpace(index) && indices.Contains(index) && runQuery)
{
string uri = $"{_settings.Host}/{index}/_search";
if(_serverInfoService.GetInfo().Version >= Constants.TotalHitsAsIntAddedVersion)
uri += "?rest_total_hits_as_int=true";
string uri = $"{_settings.Host}/{index}/_search?rest_total_hits_as_int=true";

byte[] data = Encoding.UTF8.GetBytes(query);
byte[] returnData = _httpClientHelper.Post(new Uri(uri), data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ internal static string[] GetSuggestions<T>(this IElasticSearchService<T> service
var serverInfo = ServiceLocator.Current.GetInstance<IServerInfoService>();
var repository = ServiceLocator.Current.GetInstance<IAutoSuggestRepository>();

var skipDuplicates = serverInfo.GetInfo().Version >= Constants.SkipDuplicatesFieldVersion;

var request = new SuggestRequest(searchText, service.SizeValue, skipDuplicates);
var request = new SuggestRequest(searchText, service.SizeValue);

var elasticSuggestions = engine.GetSuggestions(request, service.SearchLanguage, service.IndexName);

Expand Down
5 changes: 2 additions & 3 deletions src/Epinova.ElasticSearch.Core.EPiServer/Indexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,9 @@ public BulkBatchResult BulkUpdate(IEnumerable<IContent> contents, Action<string>
string indexName = _elasticSearchSettings.GetCustomIndexName(index, language);
dynamic indexItem = content.AsIndexItem();
string id = content.ContentLink.ToReferenceWithoutVersion().ToString();
bool isSingleType = _serverInfo.Version >= Constants.SingleTypeMappingVersion;
int id = content.ContentLink.ToReferenceWithoutVersion().ID;
return new BulkOperation(indexName, indexItem, isSingleType, Operation.Index, typeof(IndexItem), id);
return new BulkOperation(indexName, indexItem, Operation.Index, typeof(IndexItem), id);
}
)
.Where(b => b.Data != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,7 @@ public List<InspectItem> Search(string searchText, bool analyzed, string indexNa
else
{
string query = CreateSearchQuery(searchText, type);
uri += $"?q={query}&size={size}";
if(_serverInfo.Version >= Constants.TotalHitsAsIntAddedVersion)
{
uri += "&rest_total_hits_as_int=true";
}
uri += $"?q={query}&size={size}&rest_total_hits_as_int=true";

response = _httpClientHelper.GetString(new Uri(uri));
}
Expand All @@ -95,22 +91,14 @@ public List<InspectItem> Search(string searchText, bool analyzed, string indexNa

private string[] GetMappedFields(string indexName)
{
ElasticSearchSection config = ElasticSearchSection.GetConfiguration();
string nameWithoutLanguage = _elasticSearchSettings.GetIndexNameWithoutLanguage(indexName);
IndexConfiguration index = config.IndicesParsed.Single(i => i.Name == nameWithoutLanguage);
Type mappingType = String.IsNullOrEmpty(index.Type) ? typeof(IndexItem) : Type.GetType(index.Type);
IndexMapping mapping = _mapping.GetIndexMapping(mappingType, indexName);
IndexMapping mapping = _mapping.GetIndexMapping(indexName);
return mapping.Properties.Select(p => p.Key).ToArray();
}

public Dictionary<string, List<TypeCount>> GetTypes(string searchText, string indexName)
{
string uri = $"{_elasticSearchSettings.Host}/{indexName}/_search";
if(_serverInfo.Version >= Constants.TotalHitsAsIntAddedVersion)
{
uri += "?rest_total_hits_as_int=true";
}

string uri = $"{_elasticSearchSettings.Host}/{indexName}/_search?rest_total_hits_as_int=true";

object query = CreateTypeQuery(searchText);
string json = JsonConvert.SerializeObject(query, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
byte[] data = Encoding.UTF8.GetBytes(json);
Expand Down
44 changes: 11 additions & 33 deletions src/Epinova.ElasticSearch.Core/Admin/Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Linq;
using System.Text;
using Epinova.ElasticSearch.Core.Contracts;
using Epinova.ElasticSearch.Core.Extensions;
using Epinova.ElasticSearch.Core.Models;
using Epinova.ElasticSearch.Core.Models.Admin;
using Epinova.ElasticSearch.Core.Models.Serialization;
Expand All @@ -22,20 +21,16 @@ public class Index
private readonly string _nameWithoutLanguage;
private readonly string _language;
private readonly Indexing _indexing;
private readonly ServerInfo _serverInfo;
private static readonly ILogger _logger = LogManager.GetLogger(typeof(Index));
private readonly IElasticSearchSettings _settings;
private readonly IHttpClientHelper _httpClientHelper;
private readonly Mapping _mapping;


public Index(IServerInfoService serverInfoService, IElasticSearchSettings settings, IHttpClientHelper httpClientHelper, string name)
{

_httpClientHelper = httpClientHelper;
_settings = settings;
_indexing = new Indexing(serverInfoService, settings, httpClientHelper);
_serverInfo = serverInfoService.GetInfo();
_mapping = new Mapping(serverInfoService, settings, httpClientHelper);

if(!string.IsNullOrWhiteSpace(name))
Expand Down Expand Up @@ -78,13 +73,12 @@ internal string GetTokenizer(string name)
return String.Empty;
}

var json = _httpClientHelper.GetString(_indexing.GetUri(name, "_settings"));
Uri uri = _indexing.GetUri(name, "_settings");
var json = _httpClientHelper.GetString(uri);
var languageAnalyzer = Language.GetLanguageAnalyzer(_settings.GetLanguageFromIndexName(name));

if(String.IsNullOrWhiteSpace(languageAnalyzer))
{
if(string.IsNullOrWhiteSpace(json) || string.IsNullOrWhiteSpace(languageAnalyzer))
return String.Empty;
}

var jpath = $"{name}.settings.index.analysis.analyzer.{languageAnalyzer}.tokenizer";

Expand Down Expand Up @@ -151,26 +145,11 @@ internal void Initialize(Type type)
}
else
{
CreateCustomMappings(type);
CreateCustomMappings();
}
}
}

internal void DisableDynamicMapping(Type indexType)
{
var typeName = indexType.GetTypeName();
var json = MappingPatterns.GetDisableDynamicMapping(typeName);
byte[] data = Encoding.UTF8.GetBytes(json);

var uri = _mapping.GetMappingUri(_name, typeName);

_logger.Information($"Disable dynamic mapping for {typeName}");
_logger.Information($"PUT: {uri}");
_logger.Information(JToken.Parse(json).ToString(Formatting.Indented));

_httpClientHelper.Put(uri, data);
}

internal void ChangeTokenizer(string tokenizer)
{
dynamic body = MappingPatterns.GetTokenizerTemplate(_settings.GetLanguageFromIndexName(_name), tokenizer);
Expand All @@ -182,12 +161,12 @@ internal void ChangeTokenizer(string tokenizer)
_logger.Information($"Adding tri-gram tokenizer:\n{json}");
}

private void CreateCustomMappings(Type type)
private void CreateCustomMappings()
{
string json = Serialization.Serialize(MappingPatterns.GetCustomIndexMapping(Language.GetLanguageAnalyzer(_language)));
byte[] data = Encoding.UTF8.GetBytes(json);
var uri = _mapping.GetMappingUri(_name, type.GetTypeName());

Uri uri = _mapping.GetMappingUri(_name);

_logger.Information($"Creating custom mappings. Language: {_language}");
_logger.Information($"PUT: {uri}");
Expand All @@ -198,9 +177,10 @@ private void CreateCustomMappings(Type type)

private void CreateStandardMappings()
{
string json = Serialization.Serialize(MappingPatterns.GetStandardIndexMapping(UseSingleType(), Language.GetLanguageAnalyzer(_language)));
string json = Serialization.Serialize(MappingPatterns.GetStandardIndexMapping(Language.GetLanguageAnalyzer(_language)));
var data = Encoding.UTF8.GetBytes(json);
var uri = _mapping.GetMappingUri(_name, typeof(IndexItem).GetTypeName());

Uri uri = _mapping.GetMappingUri(_name);

_logger.Information($"Creating standard mappings. Language: {_language}");
_logger.Information($"PUT: {uri}");
Expand Down Expand Up @@ -260,8 +240,6 @@ private void EnableClosing()
_logger.Information($"Enabling cluster index closing:\n{json}");
}

private bool MatchName(IndexInformation i) => _settings.Indices.Any(indexName => i.Index.StartsWith(indexName, StringComparison.OrdinalIgnoreCase) && i.Index.Contains("¤"));

private bool UseSingleType() => _serverInfo.Version >= Constants.SingleTypeMappingVersion;
private bool MatchName(IndexInformation i) => _settings.Indices.Any(index => i.Index.StartsWith(index, StringComparison.OrdinalIgnoreCase) && i.Index.Contains("¤"));
}
}
17 changes: 1 addition & 16 deletions src/Epinova.ElasticSearch.Core/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,7 @@ public static class Constants
public const string IndexEPiServerContentDisplayName = "Elasticsearch: Index CMS content";
public const string DefaultSynonym = "example_from,example_to";

public static readonly Version MinimumSupportedVersion = new Version(5, 0);

// There was a breaking change in v5.6 renaming the "inline" field to "source" in scripts
public static readonly Version InlineVsSourceVersion = new Version(5, 6);

// Flag "skip_duplicates" was added in v6.1 for suggestions
public static readonly Version SkipDuplicatesFieldVersion = new Version(6, 1);

// Param "rest_total_hits_as_int" was added in v7.0
public static readonly Version TotalHitsAsIntAddedVersion = new Version(7, 0);

// Param "include_type_name" was added in v7.0
public static readonly Version IncludeTypeNameAddedVersion = new Version(7, 0);

// Single Type mapping required in v8.0
public static readonly Version SingleTypeMappingVersion = new Version(8, 0);
public static readonly Version MinimumSupportedVersion = new Version(7, 0);

// Plugin 'ingest-attachment' is included in Elastic for v8.4.
public static readonly Version IngestAttachmentProcessorIncludedVersion = new Version(8, 4);
Expand Down
8 changes: 4 additions & 4 deletions src/Epinova.ElasticSearch.Core/CoreIndexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public void CreateAnalyzedMappingsIfNeeded(Type type, string language, string in
}

// Get mappings from server
mapping = _mapping.GetIndexMapping(typeof(IndexItem), indexName);
mapping = _mapping.GetIndexMapping(indexName);

// Ignore special mappings
mapping.Properties.Remove(DefaultFields.AttachmentData);
Expand Down Expand Up @@ -254,7 +254,7 @@ public void CreateAnalyzedMappingsIfNeeded(Type type, string language, string in

json = JsonConvert.SerializeObject(mapping, jsonSettings);
var data = Encoding.UTF8.GetBytes(json);
var uri = _mapping.GetMappingUri(indexName, typeof(IndexItem).GetTypeName());
var uri = _mapping.GetMappingUri(indexName);

_logger.Debug("Update mapping:\n" + JToken.Parse(json).ToString(Formatting.Indented));

Expand Down Expand Up @@ -323,7 +323,7 @@ public void UpdateMapping(Type type, Type indexType, string index, string langua
_logger.Information("IndexableProperties for " + typeName + ": " + String.Join(", ", indexableProperties.Select(p => p.Name)));

// Get existing mapping
IndexMapping mapping = _mapping.GetIndexMapping(indexType, index);
IndexMapping mapping = _mapping.GetIndexMapping(index);

// Ignore special mappings
mapping.Properties.Remove(DefaultFields.AttachmentData);
Expand Down Expand Up @@ -359,7 +359,7 @@ public void UpdateMapping(Type type, Type indexType, string index, string langua
var jsonSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
var json = JsonConvert.SerializeObject(mapping, jsonSettings);
var data = Encoding.UTF8.GetBytes(json);
var uri = _mapping.GetMappingUri(index, indexType.GetTypeName());
var uri = _mapping.GetMappingUri(index);

_logger.Information("Update mapping:\n" + JToken.Parse(json).ToString(Formatting.Indented));

Expand Down
16 changes: 3 additions & 13 deletions src/Epinova.ElasticSearch.Core/ElasticSearchService{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,25 +405,15 @@ private ScriptScore CreateScriptScore(Version version)
return null;
}

var scriptScore = new ScriptScore
return new ScriptScore
{
Script = new ScriptScore.ScriptScoreInner
{
Language = _customScriptScoreLanguage,
Parameters = _customScriptScoreParams
Parameters = _customScriptScoreParams,
Source = _customScriptScoreSource
}
};

if(version >= Constants.InlineVsSourceVersion)
{
scriptScore.Script.Source = _customScriptScoreSource;
}
else
{
scriptScore.Script.Inline = _customScriptScoreSource;
}

return scriptScore;
}

public IElasticSearchService<T> MoreLikeThis<T>(string id, int minimumTermFrequency = 1, int maxQueryTerms = 25, int minimumDocFrequency = 3, int minimumWordLength = 3)
Expand Down
6 changes: 3 additions & 3 deletions src/Epinova.ElasticSearch.Core/Engine/QueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ public QueryBuilder(IServerInfoService serverInfoService, IElasticSearchSettings
internal void SetMappedFields(string[] fields) =>
_mappedFields = fields;

private string[] GetMappedFields(string index, Type type)
private string[] GetMappedFields(string index)
{
_logger.Debug("Get mapped fields");

if(_mappedFields?.Any() != true)
{
_logger.Debug("No mapped fields found, lookup with Mapping.GetIndexMapping");

_mappedFields = _mapping.GetIndexMapping(type, index)
_mappedFields = _mapping.GetIndexMapping( index)
.Properties
.Where(m => _searchableFieldTypes.Contains(m.Value.Type)
&& !m.Key.EndsWith(Models.Constants.KeywordSuffix))
Expand Down Expand Up @@ -105,7 +105,7 @@ private RequestBase SearchInternal(QuerySetup setup)

if(setup.SearchFields.Count == 0)
{
setup.SearchFields.AddRange(GetMappedFields(setup.IndexName, setup.SearchType));
setup.SearchFields.AddRange(GetMappedFields(setup.IndexName));
}

if(_logger.IsDebugEnabled())
Expand Down
8 changes: 2 additions & 6 deletions src/Epinova.ElasticSearch.Core/Engine/SearchEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -395,12 +395,8 @@ private string GetSearchEndpoint(string indexName, string extraParam = null)
{
url += extraParam;
}

if(_serverInfo.Version >= Constants.TotalHitsAsIntAddedVersion)
{
url += (url.Contains("?") ? "&" : "?") + "rest_total_hits_as_int=true";
}


url += (url.Contains("?") ? "&" : "?") + "rest_total_hits_as_int=true";
return url;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@ public class BulkMetadataBase
[JsonProperty(JsonNames.Error)]
public Error Error { get; set; }

[JsonProperty(JsonNames.HitType)]
public string Type { get; set; }

[JsonIgnore]
public Type DataType { get; set; }

[JsonProperty(JsonNames.Id)]
public string Id { get; set; }
public long Id { get; set; }
}
}
Loading

0 comments on commit 3066524

Please sign in to comment.