Skip to content
Merged
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 @@ -404,12 +404,10 @@ private static IReadOnlyList<ComponentWeight> ExtractComponentWeights(HybridSear
for (int index = 0; index < hybridSearchQueryInfo.ComponentQueryInfos.Count; ++index)
{
QueryInfo queryInfo = hybridSearchQueryInfo.ComponentQueryInfos[index];
Debug.Assert(queryInfo.HasOrderBy, "The component query should have an order by");
Debug.Assert(queryInfo.HasNonStreamingOrderBy, "The component query is a non streaming order by");
Debug.Assert(queryInfo.OrderBy.Count == 1, "The component query should have exactly one order by expression");
SortOrder sortOrder = queryInfo.HasOrderBy ? queryInfo.OrderBy[0] : SortOrder.Descending;

double componentWeight = useDefaultComponentWeight ? 1.0 : hybridSearchQueryInfo.ComponentWeights[index];
result.Add(new ComponentWeight(componentWeight, queryInfo.OrderBy[0]));
result.Add(new ComponentWeight(componentWeight, sortOrder));
}

return result;
Expand Down Expand Up @@ -635,14 +633,19 @@ private static void ComputeRrfScores(

private static QueryInfo RewriteOrderByQueryInfo(QueryInfo queryInfo, GlobalFullTextSearchStatistics statistics, int componentCount)
{
Debug.Assert(queryInfo.HasOrderBy, "The component query should have an order by");
Debug.Assert(queryInfo.HasNonStreamingOrderBy, "The component query is a non streaming order by");
IReadOnlyList<string> rewrittenOrderByExpressions = queryInfo.OrderByExpressions;

List<string> rewrittenOrderByExpressions = new List<string>(queryInfo.OrderByExpressions.Count);
foreach (string orderByExpression in queryInfo.OrderByExpressions)
if (queryInfo.HasOrderBy)
{
string rewrittenOrderByExpression = FormatComponentQueryTextWorkaround(orderByExpression, statistics, componentCount);
rewrittenOrderByExpressions.Add(rewrittenOrderByExpression);
Debug.Assert(queryInfo.HasNonStreamingOrderBy, "The component query is a non streaming order by");
List<string> orderByExpressions = new List<string>(queryInfo.OrderByExpressions.Count);
foreach (string orderByExpression in queryInfo.OrderByExpressions)
{
string rewrittenOrderByExpression = FormatComponentQueryTextWorkaround(orderByExpression, statistics, componentCount);
orderByExpressions.Add(rewrittenOrderByExpression);
}

rewrittenOrderByExpressions = orderByExpressions;
}

string rewrittenQuery = FormatComponentQueryTextWorkaround(queryInfo.RewrittenQuery, statistics, componentCount);
Expand Down Expand Up @@ -777,18 +780,15 @@ private static string FormatComponentQueryTextWorkaround(string format, GlobalFu

private class ComponentWeight
{
public SortOrder SortOrder { get; }

public double Weight { get; }

public Comparison<double> Comparison { get; }

public ComponentWeight(double weight, SortOrder sortOrder)
{
this.Weight = weight;
this.SortOrder = sortOrder;

int comparisonFactor = (this.SortOrder == SortOrder.Ascending) ? 1 : -1;
int comparisonFactor = (sortOrder == SortOrder.Ascending) ? 1 : -1;
this.Comparison = (x, y) => comparisonFactor * x.CompareTo(y);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,33 @@ public static HybridSearchQueryResult Create(CosmosElement document)
throw new ArgumentException($"{FieldNames.Rid} must exist.");
}

if (!cosmosObject.TryGetValue(FieldNames.Payload, out CosmosObject outerPayload))
{
throw new ArgumentException($"{FieldNames.Payload} must exist.");
}
bool outerPayloadExists = cosmosObject.TryGetValue(FieldNames.Payload, out CosmosObject outerPayload);

if (!outerPayload.TryGetValue(FieldNames.Payload, out CosmosElement innerPayload))
HybridSearchQueryResult result;
if (outerPayloadExists && outerPayload.TryGetValue(FieldNames.ComponentScores, out CosmosArray componentScores))
{
innerPayload = CosmosUndefined.Create();
}
// Using the older format where the payload is nested.
if (!outerPayload.TryGetValue(FieldNames.Payload, out CosmosElement innerPayload))
{
innerPayload = CosmosUndefined.Create();
}

if (!outerPayload.TryGetValue(FieldNames.ComponentScores, out CosmosArray componentScores))
result = new HybridSearchQueryResult(rid, componentScores, innerPayload);
}
else
{
throw new ArgumentException($"{FieldNames.ComponentScores} must exist.");
// Using the newer format where the payload is not nested.
if (!cosmosObject.TryGetValue(FieldNames.ComponentScores, out componentScores))
{
throw new ArgumentException($"{FieldNames.ComponentScores} must exist.");
}

CosmosElement payload = outerPayloadExists ? outerPayload : CosmosUndefined.Create();

result = new HybridSearchQueryResult(rid, componentScores, payload);
}

return new HybridSearchQueryResult(rid, componentScores, innerPayload);
return result;
}

private static class FieldNames
Expand Down
Loading
Loading