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

TNO-2973 Fixed ES search query #2280

Merged
merged 1 commit into from
Sep 12, 2024
Merged
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
39 changes: 22 additions & 17 deletions libs/net/dal/Extensions/JsonDocumentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,48 +59,53 @@ public static JsonDocument IncludeOnlyLatestPostedAndContentIds(this JsonDocumen
{
var json = JsonNode.Parse(query.ToJson())?.AsObject();
if (json == null) return query;
JsonNode jMustTerms = null!;
JsonNode jIncludeOnlyLatestPosted = null!;
JsonNode jShouldQuery = null!;
JsonNode? jShouldQuery = null;

if (contentIds != null && contentIds.Any())
{
jMustTerms = JsonNode.Parse($"{{ \"terms\": {{ \"id\": [{String.Join(',', contentIds)}] }}}}")?.AsObject() ?? throw new InvalidOperationException("Failed to parse JSON");
jShouldQuery = JsonNode.Parse($"{jMustTerms.ToJsonString()}")!;
jShouldQuery = JsonNode.Parse($"{{ \"terms\": {{ \"id\": [{String.Join(',', contentIds)}] }}}}")?.AsObject() ?? throw new InvalidOperationException("Failed to parse JSON");
}
if (previousPublishedOn != null)
{
jIncludeOnlyLatestPosted = JsonNode.Parse($"{{ \"range\": {{ \"postedOn\": {{ \"gte\": \"{previousPublishedOn.Value:yyyy-MM-ddTHH:mm:ss}\", \"time_zone\": \"US/Pacific\" }} }} }}")!;
var jIncludeOnlyLatestPosted = JsonNode.Parse($"{{ \"range\": {{ \"postedOn\": {{ \"gte\": \"{previousPublishedOn.Value:yyyy-MM-ddTHH:mm:ss}\", \"time_zone\": \"US/Pacific\" }} }} }}")?.AsObject();
if (jIncludeOnlyLatestPosted != null)
{
if (jShouldQuery != null)
{
jShouldQuery = JsonNode.Parse($"[ {jShouldQuery.ToJsonString()}, {jIncludeOnlyLatestPosted.ToJsonString()} ]")!;
jShouldQuery = JsonNode.Parse($"{{ \"bool\": {{ \"should\": [ {jShouldQuery.ToJsonString()}, {jIncludeOnlyLatestPosted.ToJsonString()} ]}}}}")?.AsObject();
}
else
{
jShouldQuery = JsonNode.Parse($"[ {jIncludeOnlyLatestPosted.ToJsonString()} ]")!;
jShouldQuery = JsonNode.Parse($"{{ \"bool\": {{ \"should\": [ {jIncludeOnlyLatestPosted.ToJsonString()} ]}}}}")?.AsObject();
}
}
}
if (json.TryGetPropertyValue("query", out JsonNode? jQuery))
if (jShouldQuery != null)
{
if (jQuery?.AsObject().TryGetPropertyValue("bool", out JsonNode? jQueryBool) == true)
if (json.TryGetPropertyValue("query", out JsonNode? jQuery))
{
if (jShouldQuery != null)
if (jQuery?.AsObject().TryGetPropertyValue("bool", out JsonNode? jQueryBool) == true)
{
jQueryBool?.AsObject().Add("should", JsonNode.Parse($"{jShouldQuery.ToJsonString()}"));
if (jQueryBool?.AsObject().TryGetPropertyValue("must", out JsonNode? jQueryBoolMust) == true)
{
jQueryBoolMust?.AsArray().Add(jShouldQuery);
}
else
{
jQueryBool?.AsObject().Add("must", JsonNode.Parse($"[ {jShouldQuery.ToJsonString()} ]"));
}
}
else
{
jQuery?.AsObject().Add("bool", JsonNode.Parse($"{{ \"must\": [ {jShouldQuery.ToJsonString()} ]}}"));
}
}
else
{
jQuery?.AsObject().Add("bool", JsonNode.Parse($"{{ \"should\": [ {jShouldQuery.ToJsonString()} ]}}"));

json.Add("query", JsonNode.Parse($"{{ \"bool\": {{ \"must\": [ {jShouldQuery.ToJsonString()} ] }}}}"));
}
}
else
{
json.Add("query", JsonNode.Parse($"{{ \"bool\": {{ \"should\": [ {jShouldQuery.ToJsonString()} ] }}}}"));
}
return JsonDocument.Parse(json.ToJsonString());
}

Expand Down
Loading