Skip to content

Commit

Permalink
feat: recover code
Browse files Browse the repository at this point in the history
  • Loading branch information
AElfBourneShi committed Oct 19, 2024
1 parent 402b22d commit 88b35d3
Showing 1 changed file with 63 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,22 @@ protected override Expression VisitMethodCall(MethodCallExpression expression)

// private string GetFullNameKey(MemberExpression memberExpression)
// {
// var key = _propertyNameInferrerParser.Parser(memberExpression.Member.Name);
// while (memberExpression.Expression != null)
// {
// memberExpression = memberExpression.Expression as MemberExpression;
// if (memberExpression == null)
// {
// break;
// }
//
// key = _propertyNameInferrerParser.Parser(memberExpression.Member.Name + "." + key);
// return key;
// }
//
// return key;
// var key = _propertyNameInferrerParser.Parser(memberExpression.Member.Name);
// while (memberExpression.Expression != null)
// {
// memberExpression = memberExpression.Expression as MemberExpression;
// if (memberExpression == null)
// {
// break;
// }
//
// key = _propertyNameInferrerParser.Parser(memberExpression.Member.Name + "." + key);
// return key;
// }

//
// return key;
// }

private string GetFullPropertyPath(Expression expression)
{
switch (expression)
Expand All @@ -166,6 +166,7 @@ private string GetFullPropertyPath(Expression expression)
var collectionPath = GetFullPropertyPath(methodCallExpression.Object);
return collectionPath; // Returns the path of the collection directly, without adding an index
}

break;
}

Expand Down Expand Up @@ -256,7 +257,7 @@ protected override Expression VisitSubQuery(SubQueryExpression expression)
VisitBinarySetSubQuery((BinaryExpression)predicate.Right, from, fullPath, true);
}
}

}

break;
Expand Down Expand Up @@ -299,14 +300,14 @@ private void HandleNestedContains(SubQueryExpression subQueryExpression, Express
{
if (subQueryExpression == null || expression == null)
throw new ArgumentNullException("SubQueryExpression or expression cannot be null.");

//Check if the number of items in the Terms query array within the Contains clause is too large.
if (subQueryExpression.QueryModel.MainFromClause
.FromExpression is ConstantExpression constantExpression)
{
CheckTermsArrayLength(constantExpression);
}

foreach (var resultOperator in subQueryExpression.QueryModel.ResultOperators)
{
switch (resultOperator)
Expand All @@ -320,29 +321,48 @@ private void HandleNestedContains(SubQueryExpression subQueryExpression, Express

Node query;
query = GetDifferentTypesTermsQueryNode();

QueryMap[expression] = ParseQuery(query);
QueryMap[expression].IsSubQuery = true;
QueryMap[expression].SubQueryPath = subQueryPath; //from.ToLower();
QueryMap[expression].SubQueryFullPath = subQueryFullPath;
}

private static readonly HashSet<Type> SupportedTermsTypes = new HashSet<Type>
{
typeof(Guid), typeof(Guid?),
typeof(int), typeof(int?),
typeof(long), typeof(long?),
typeof(double), typeof(double?),
typeof(bool), typeof(bool?)
};

private Node GetDifferentTypesTermsQueryNode()
{
Node query;
if (SupportedTermsTypes.Contains(PropertyType))
if (PropertyType == typeof(Guid))
{
query = new TermsNode(PropertyName, ((IEnumerable<Guid>)Value).Select(x => x.ToString()));
}
else if (PropertyType == typeof(Guid?))
{
query = new TermsNode(PropertyName, ((IEnumerable<Guid?>)Value).Select(x => x.ToString()));
}
else if (PropertyType == typeof(int))
{
query = new TermsNode(PropertyName, ((IEnumerable<int>)Value).Select(x => x.ToString()));
}
else if (PropertyType == typeof(int?))
{
query = new TermsNode(PropertyName, ((IEnumerable<int?>)Value).Select(x => x.ToString()));
}
else if (PropertyType == typeof(long))
{
query = new TermsNode(PropertyName, ((IEnumerable<long>)Value).Select(x => x.ToString()));
}
else if (PropertyType == typeof(long?))
{
query = new TermsNode(PropertyName, ((IEnumerable<long?>)Value).Select(x => x.ToString()));
}
else if (PropertyType == typeof(double))
{
query = new TermsNode(PropertyName, ((IEnumerable<double>)Value).Select(x => x.ToString()));
}
else if (PropertyType == typeof(double?))
{
query = new TermsNode(PropertyName, ((IEnumerable<double?>)Value).Select(x => x.ToString()));
}
else if (PropertyType == typeof(DateTime))
{
query = new TermsNode(PropertyName,
Expand All @@ -353,6 +373,14 @@ private Node GetDifferentTypesTermsQueryNode()
query = new TermsNode(PropertyName,
((IEnumerable<DateTime?>)Value).Select(x => x.ToString()));
}
else if (PropertyType == typeof(bool))
{
query = new TermsNode(PropertyName, ((IEnumerable<bool>)Value).Select(x => x.ToString()));
}
else if (PropertyType == typeof(bool?))
{
query = new TermsNode(PropertyName, ((IEnumerable<bool?>)Value).Select(x => x.ToString()));
}
else if (PropertyType == typeof(string))
{
query = new TermsNode(PropertyName, (IEnumerable<string>)Value);
Expand All @@ -365,14 +393,17 @@ private Node GetDifferentTypesTermsQueryNode()
return query;
}



private void CheckTermsArrayLength(ConstantExpression constantExpression)
{
if (constantExpression.Value is System.Collections.IEnumerable objectList)
{
var count = objectList.Cast<object>().Count();
if (count > _elasticsearchOptions.TermsArrayMaxLength)
{
throw new Exception($"The array input for Terms query is too large, exceeding {_elasticsearchOptions.TermsArrayMaxLength} items.");
throw new Exception(
$"The array input for Terms query is too large, exceeding {_elasticsearchOptions.TermsArrayMaxLength} items.");
}
}
}
Expand Down Expand Up @@ -693,7 +724,8 @@ private object ConvertEnumValue(Type entityType, string propertyName, object val
return (int)enumValue;
}

protected void VisitBinarySetSubQuery(BinaryExpression expression, string path, string fullPath, bool parentIsSubQuery)
protected void VisitBinarySetSubQuery(BinaryExpression expression, string path, string fullPath,
bool parentIsSubQuery)
{
if (expression.Left is BinaryExpression && expression.Right is ConstantExpression)
{
Expand Down

0 comments on commit 88b35d3

Please sign in to comment.