Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Highlight and score #16908

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -432,14 +432,15 @@ public async Task<ActionResult> ForceDelete(ElasticIndexSettingsViewModel model)
return RedirectToAction(nameof(Index));
}

public async Task<IActionResult> Mappings(string indexName)
public async Task<IActionResult> IndexInfo(string indexName)
{
var mappings = await _elasticIndexManager.GetIndexMappings(indexName);
var formattedJson = JNode.Parse(mappings).ToJsonString(JOptions.Indented);
return View(new MappingsViewModel
var info = await _elasticIndexManager.GetIndexInfo(indexName);

var formattedJson = JNode.Parse(info).ToJsonString(JOptions.Indented);
return View(new IndexInfoViewModel
{
IndexName = _elasticIndexManager.GetFullIndexName(indexName),
Mappings = formattedJson
IndexInfo = formattedJson
});
}

Expand Down Expand Up @@ -514,7 +515,9 @@ public async Task<IActionResult> Query(AdminQueryViewModel model)
stopwatch.Start();

var parameters = JConvert.DeserializeObject<Dictionary<string, object>>(model.Parameters);
var tokenizedContent = await _liquidTemplateManager.RenderStringAsync(model.DecodedQuery, _javaScriptEncoder, parameters.Select(x => new KeyValuePair<string, FluidValue>(x.Key, FluidValue.Create(x.Value, _templateOptions.Value))));
var props = parameters.Select(x => new KeyValuePair<string, FluidValue>(x.Key, FluidValue.Create(x.Value, _templateOptions.Value)));

var tokenizedContent = await _liquidTemplateManager.RenderStringAsync(model.DecodedQuery, _javaScriptEncoder, props);

try
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using Microsoft.Extensions.Configuration;
using OrchardCore.Environment.Shell.Configuration;

namespace OrchardCore.Search.Elasticsearch.Extensions;
internal static class ElasticsearchOptionsExtensions
{
internal static ElasticsearchOptions AddAnalyzers(this ElasticsearchOptions options, IConfigurationSection configuration)
{
var jsonNode = configuration.GetSection(nameof(options.Analyzers)).AsJsonNode();
var jsonElement = JsonSerializer.Deserialize<JsonElement>(jsonNode);

var analyzersObject = JsonObject.Create(jsonElement, new JsonNodeOptions()
{
PropertyNameCaseInsensitive = true,
});

if (analyzersObject != null)
{
if (jsonNode is JsonObject jAnalyzers)
{
foreach (var analyzer in jAnalyzers)
{
if (analyzer.Value is not JsonObject jAnalyzer)
{
continue;
}

options.Analyzers.Add(analyzer.Key, jAnalyzer);
}
}
}

if (options.Analyzers.Count == 0)
{
// When no analyzers are configured, we'll define a default analyzer.
options.Analyzers.Add(ElasticsearchConstants.DefaultAnalyzer, new JsonObject
{
["type"] = "standard",
});
}

return options;
}

internal static ElasticsearchOptions AddFilter(this ElasticsearchOptions options, IConfigurationSection configuration)
{
var jsonNode = configuration.GetSection(nameof(options.Filter)).AsJsonNode();
var jsonElement = JsonSerializer.Deserialize<JsonElement>(jsonNode);

var filterObject = JsonObject.Create(jsonElement, new JsonNodeOptions()
{
PropertyNameCaseInsensitive = true,
});

if (filterObject != null)
{
if (jsonNode is JsonObject jFilters)
{
foreach (var filter in jFilters)
{
if (filter.Value is not JsonObject jFilter)
{
continue;
}

options.Filter.Add(filter.Key, jFilter);
}
}
}

return options;
}

internal static ElasticsearchOptions AddIndexPrefix(this ElasticsearchOptions options, IConfigurationSection configuration)
{
options.IndexPrefix = configuration.GetValue<string>(nameof(options.IndexPrefix));
return options;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using OrchardCore.Search.Elasticsearch.Core.Providers;
using OrchardCore.Search.Elasticsearch.Core.Services;
using OrchardCore.Search.Elasticsearch.Drivers;
using OrchardCore.Search.Elasticsearch.Extensions;
using OrchardCore.Search.Elasticsearch.Services;
using OrchardCore.Search.Lucene.Handler;
using OrchardCore.Security.Permissions;
Expand Down Expand Up @@ -54,42 +55,9 @@ public override void ConfigureServices(IServiceCollection services)
{
var configuration = _shellConfiguration.GetSection(ElasticConnectionOptionsConfigurations.ConfigSectionName);

o.IndexPrefix = configuration.GetValue<string>(nameof(o.IndexPrefix));

var jsonNode = configuration.GetSection(nameof(o.Analyzers)).AsJsonNode();
var jsonElement = JsonSerializer.Deserialize<JsonElement>(jsonNode);

var analyzersObject = JsonObject.Create(jsonElement, new JsonNodeOptions()
{
PropertyNameCaseInsensitive = true,
});

if (analyzersObject != null)
{
o.IndexPrefix = configuration.GetValue<string>(nameof(o.IndexPrefix));

if (jsonNode is JsonObject jAnalyzers)
{
foreach (var analyzer in jAnalyzers)
{
if (analyzer.Value is not JsonObject jAnalyzer)
{
continue;
}

o.Analyzers.Add(analyzer.Key, jAnalyzer);
}
}
}

if (o.Analyzers.Count == 0)
{
// When no analyzers are configured, we'll define a default analyzer.
o.Analyzers.Add(ElasticsearchConstants.DefaultAnalyzer, new JsonObject
{
["type"] = "standard",
});
}
o.AddIndexPrefix(configuration);
o.AddFilter(configuration);
o.AddAnalyzers(configuration);
});

services.AddElasticServices();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace OrchardCore.Search.Elasticsearch.ViewModels;

public class MappingsViewModel
public class IndexInfoViewModel
{
public string IndexName { get; set; }
public string Mappings { get; set; }
public string IndexInfo { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<p>@entry.AnalyzerName</p>
</div>
<div class="float-end">
<a asp-action="Mappings" asp-route-IndexName="@entry.Name" class="btn btn-primary btn-sm">@T["Mappings"]</a>
<a asp-action="IndexInfo" asp-route-IndexName="@entry.Name" class="btn btn-primary btn-sm">@T["Index Info"]</a>
<a asp-action="Query" asp-route-IndexName="@entry.Name" class="btn btn-success btn-sm">@T["Query"]</a>
<a asp-action="Reset" asp-route-id="@entry.Name" class="btn btn-primary btn-sm" data-title="@T["Reset Index"]" data-message="@T["This will restart the indexing of all content items. Continue?"]" data-ok-text="@T["Yes"]" data-cancel-text="@T["No"]" data-ok-class="btn-primary" data-url-af="UnsafeUrl">@T["Reset"]</a>
<a asp-action="Rebuild" asp-route-id="@entry.Name" class="btn btn-warning btn-sm" data-title="@T["Rebuild Index"]" data-message="@T["Your index will be rebuilt, which might alter some services during the process. Continue?"]" data-ok-text="@T["Yes"]" data-cancel-text="@T["No"]" data-url-af="UnsafeUrl">@T["Rebuild"]</a>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
@model MappingsViewModel
@model IndexInfoViewModel

<h2>@T["Elasticsearch index mappings"]</h2>
<h2>@T["Elasticsearch index information"]</h2>

<div class="row">
<div class="col">
<div class="mb-4">
<label asp-for="Mappings" class="form-label">@T["Mappings for: "]@Model.IndexName</label>
<label asp-for="IndexInfo" class="form-label">@T["Info for: "]@Model.IndexName</label>
<div class="form-control" style="min-height: 400px;">
<div id="@Html.IdFor(x => x.Mappings)_editor" style="min-height: 385px;" dir="@Orchard.CultureDir()" data-schema='@Model.Mappings'></div>
<div id="@Html.IdFor(x => x.IndexInfo)_editor" style="min-height: 385px;" dir="@Orchard.CultureDir()" data-schema='@Model.IndexInfo'></div>
</div>
<textarea asp-for="Mappings" hidden></textarea>
<span class="hint">@T["The Elasticsearch index mapping. For reference only."]</span>
<textarea asp-for="IndexInfo" hidden></textarea>
<span class="hint">@T["The Elasticsearch index information. For reference only."]</span>
</div>
</div>
</div>
Expand All @@ -35,8 +35,8 @@
setTheme();

var modelUri = monaco.Uri.parse("x://orchardcore.search.elastic.mappings.json");
var editor = document.getElementById('@Html.IdFor(x => x.Mappings)_editor');
var textArea = document.getElementById('@Html.IdFor(x => x.Mappings)');
var editor = document.getElementById('@Html.IdFor(x => x.IndexInfo)_editor');
var textArea = document.getElementById('@Html.IdFor(x => x.IndexInfo)');
var schema = JSON.parse(editor.dataset.schema)
var model = monaco.editor.createModel(textArea.value, "json", modelUri);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using Nest;

namespace OrchardCore.Search.Elasticsearch;

public class ElasticTopDocs
{
public List<Dictionary<string, object>> TopDocs { get; set; }
public List<Dictionary<string, object>> Fields { get; set; }
public IReadOnlyCollection<IHit<Dictionary<string, object>>> Hits { get; set; }
public long Count { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ public class ElasticsearchOptions
public string IndexPrefix { get; set; }

public Dictionary<string, JsonObject> Analyzers { get; } = [];

public Dictionary<string, JsonObject> Filter { get; } = [];
}
Loading