Skip to content

Commit

Permalink
using JsonObject as a value
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek committed Nov 22, 2024
1 parent e78a6d5 commit 40d1100
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,8 @@ public async Task<IActionResult> Query(AdminQueryViewModel model)

if (results != null)
{
model.Documents = results.TopDocs?.Where(x => x != null).Select(doc => doc.Data);
model.Fields = results.Fields?.Where(x => x != null).Select(field => field.Data);
model.Documents = results.TopDocs?.Where(x => x != null).Select(doc => doc.Value.Deserialize<Dictionary<string, object>>());
model.Fields = results.Fields?.Where(x => x != null).Select(field => field.Value.Deserialize<Dictionary<string, object>>());
model.Count = results.Count;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,93 +1,28 @@
using System.Text.Json.Nodes;

namespace OrchardCore.Search.Elasticsearch;

public class ElasticsearchResult
{
public List<ElasticsearchRecord> TopDocs { get; set; }
public IList<ElasticsearchRecord> TopDocs { get; set; }

public List<ElasticsearchRecord> Fields { get; set; }
public IList<ElasticsearchRecord> Fields { get; set; }

public long Count { get; set; }
}

public class ElasticsearchRecord
{
public Dictionary<string, object> Data { get; set; }
public JsonObject Value { get; }

public IReadOnlyDictionary<string, IReadOnlyCollection<string>> Highlights { get; set; }

public double? Score { get; set; }

public ElasticsearchRecord()
{
}

public ElasticsearchRecord(Dictionary<string, object> data)
{
Data = data;
}

public bool TryGetDataValue(string key, out object value)
{
if (Data == null)
{
value = null;

return false;
}

return Data.TryGetValue(key, out value);
}

public bool TryGetDataValue<T>(string key, out T value)
public ElasticsearchRecord(JsonObject value)
{
if (Data == null)
{
value = default;

return false;
}

if (Data.TryGetValue(key, out var obj))
{
if (obj is T typedValue)
{
value = typedValue;

return true;
}

if (typeof(T) == typeof(string))
{
value = (T)(object)obj?.ToString();

return true;
}

// Handle nullable types (e.g., Nullable<T>).
if (Nullable.GetUnderlyingType(typeof(T)) != null)
{
// If the object is null, assign the default value for the nullable type.
if (obj == null)
{
value = default;

return true; // Return true for null values if T is nullable.
}
}

try
{
value = (T)Convert.ChangeType(obj, typeof(T));

return true;
}
catch
{
}
}

value = default;
ArgumentNullException.ThrowIfNull(value);

return false;
Value = value;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Text.Json.Nodes;
using Elastic.Clients.Elasticsearch;
using Elastic.Clients.Elasticsearch.QueryDsl;
using Json.Path;
using Microsoft.Extensions.Options;
using OrchardCore.ContentManagement;
using OrchardCore.Search.Elasticsearch.Core.Models;
Expand Down Expand Up @@ -47,14 +49,14 @@ public async Task<IEnumerable<ContentPickerResult>> Search(ContentPickerSearchCo

await _elasticIndexManager.SearchAsync(indexName, async elasticClient =>
{
SearchResponse<Dictionary<string, object>> searchResponse = null;
SearchResponse<JsonObject> searchResponse = null;
var elasticTopDocs = new ElasticsearchResult();

var valuesQuery = new TermsQueryField(searchContext.ContentTypes.Select(contentType => FieldValue.String(contentType)).ToArray());

if (string.IsNullOrWhiteSpace(searchContext.Query))
{
searchResponse = await elasticClient.SearchAsync<Dictionary<string, object>>(s => s
searchResponse = await elasticClient.SearchAsync<JsonObject>(s => s
.Index(_elasticIndexManager.GetFullIndexName(indexName))
.Query(q => q
.Bool(b => b
Expand All @@ -70,7 +72,7 @@ await _elasticIndexManager.SearchAsync(indexName, async elasticClient =>
}
else
{
searchResponse = await elasticClient.SearchAsync<Dictionary<string, object>>(s => s
searchResponse = await elasticClient.SearchAsync<JsonObject>(s => s
.Index(_elasticIndexManager.GetFullIndexName(indexName))
.Query(q => q
.Bool(b => b
Expand Down Expand Up @@ -102,19 +104,19 @@ await _elasticIndexManager.SearchAsync(indexName, async elasticClient =>
{
var result = new ContentPickerResult();

if (doc.TryGetDataValue<string>(nameof(ContentItem.ContentItemId), out var contentItemId))
if (doc.Value.TryGetPropertyValue(nameof(ContentItem.ContentItemId), out var contentItemId))
{
result.ContentItemId = contentItemId;
result.ContentItemId = contentItemId.GetValue<string>();
}

if (doc.TryGetDataValue<string>("Content.ContentItem.DisplayText.keyword", out var keyword))
if (doc.Value.TryGetPropertyValue("Content.ContentItem.DisplayText.keyword", out var keyword))
{
result.DisplayText = keyword;
result.DisplayText = keyword.GetValue<string>();
}

if (doc.TryGetDataValue<bool>("Content.ContentItem.Published", out var published))
if (doc.Value.TryGetPropertyValue("Content.ContentItem.Published", out var published) && published.TryGetValue<bool>(out var hasPublished))
{
result.HasPublished = published;
result.HasPublished = hasPublished;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ public async Task<ElasticsearchResult> SearchAsync(ElasticsearchSearchContext co
Highlight = context.Highlight,
};

var searchResponse = await _elasticClient.SearchAsync<Dictionary<string, object>>(searchRequest);
var searchResponse = await _elasticClient.SearchAsync<JsonObject>(searchRequest);

if (searchResponse.IsValidResponse)
{
Expand All @@ -551,7 +551,7 @@ public async Task<ElasticsearchResult> SearchAsync(ElasticsearchSearchContext co
continue;
}

var topDoc = new Dictionary<string, object>
var topDoc = new JsonObject
{
{ nameof(ContentItem.ContentItemId), hit.Id },
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ public async Task<IList<string>> GetContentItemIdsAsync(ElasticsearchSearchConte

foreach (var item in results.TopDocs)
{
if (!item.TryGetDataValue<string>(nameof(ContentItem.ContentItemId), out var contentItemId))
if (!item.Value.TryGetPropertyValue(nameof(ContentItem.ContentItemId), out var contentItemId))
{
continue;
}

contentItemIds.Add(contentItemId);
contentItemIds.Add(contentItemId.GetValue<string>());
}

return contentItemIds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ public async Task<IQueryResults> ExecuteQueryAsync(Query query, IDictionary<stri

foreach (var topDoc in topDocs)
{
if (!topDoc.TryGetDataValue<string>(nameof(ContentItem.ContentItemVersionId), out var versionId))
if (!topDoc.Value.TryGetPropertyValue(nameof(ContentItem.ContentItemVersionId), out var versionId))
{
continue;
}

indexedContentItemVersionIds.Add(versionId);
indexedContentItemVersionIds.Add(versionId.GetValue<string>());
}

var dbContentItems = await _session.Query<ContentItem, ContentItemIndex>(x => x.ContentItemVersionId.IsIn(indexedContentItemVersionIds)).ListAsync();
Expand All @@ -96,7 +96,7 @@ public async Task<IQueryResults> ExecuteQueryAsync(Query query, IDictionary<stri

foreach (var document in docs.TopDocs)
{
results.Add(new JsonObject(document.Data.Select(x =>
results.Add(new JsonObject(document.Value.Select(x =>
KeyValuePair.Create(x.Key, (JsonNode)JsonValue.Create(x.Value)))));
}

Expand Down

0 comments on commit 40d1100

Please sign in to comment.