Skip to content

Commit

Permalink
v1.3.6 fix: RediSearch Repository [FtField(name)] 指定名称时读不到内容;
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Dec 20, 2024
1 parent 8b73a01 commit 391131c
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<AssemblyName>FreeRedis.DistributedCache</AssemblyName>
<PackageId>FreeRedis.DistributedCache</PackageId>
<RootNamespace>FreeRedis.DistributedCache</RootNamespace>
<Version>1.3.5</Version>
<Version>1.3.6</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageProjectUrl>https://github.com/2881099/FreeRedis</PackageProjectUrl>
<Description>分布式缓存 FreeRedis 实现 Microsoft.Extensions.Caching</Description>
Expand Down
2 changes: 1 addition & 1 deletion src/FreeRedis.OpenTelemetry/FreeRedis.OpenTelemetry.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<AssemblyName>FreeRedis.OpenTelemetry</AssemblyName>
<PackageId>FreeRedis.OpenTelemetry</PackageId>
<RootNamespace>FreeRedis.OpenTelemetry</RootNamespace>
<Version>1.3.5</Version>
<Version>1.3.6</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageProjectUrl>https://github.com/2881099/FreeRedis</PackageProjectUrl>
<RepositoryUrl>https://github.com/2881099/FreeRedis</RepositoryUrl>
Expand Down
2 changes: 1 addition & 1 deletion src/FreeRedis/FreeRedis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<AssemblyName>FreeRedis</AssemblyName>
<PackageId>FreeRedis</PackageId>
<RootNamespace>FreeRedis</RootNamespace>
<Version>1.3.5</Version>
<Version>1.3.6</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageProjectUrl>https://github.com/2881099/FreeRedis</PackageProjectUrl>
<Description>FreeRedis is .NET redis client, supports cluster, sentinel, master-slave, pipeline, transaction and connection pool.</Description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ internal protected class DocumentSchemaInfo
public Type DocumentType { get; set; }
public FtDocumentAttribute DocumentAttribute { get; set; }
public PropertyInfo KeyProperty { get; set; }
public List<DocumentSchemaFieldInfo> Fields { get; set; }
public Dictionary<string, DocumentSchemaFieldInfo> FieldsMap { get; set; }
public List<DocumentSchemaFieldInfo> Fields { get; set; } = new List<DocumentSchemaFieldInfo>();
public Dictionary<string, DocumentSchemaFieldInfo> FieldsMap { get; set; } = new Dictionary<string, DocumentSchemaFieldInfo>();
public Dictionary<string, DocumentSchemaFieldInfo> FieldsMapRead { get; set; } = new Dictionary<string, DocumentSchemaFieldInfo>();
}
internal protected class DocumentSchemaFieldInfo
{
Expand All @@ -41,23 +42,27 @@ public FtDocumentRepository(RedisClient client)
{
attribute = p.GetCustomAttributes(false).FirstOrDefault(a => a is FtFieldAttribute) as FtFieldAttribute,
property = p
}).Where(a => a.attribute != null).ToList();
if (fieldProprties.Any() == false) throw new Exception($"Not found: [FtFieldAttribute]");
}).Where(a => a.attribute != null);
var schema = new DocumentSchemaInfo
{
DocumentType = type,
DocumentAttribute = type.GetCustomAttributes(false).FirstOrDefault(a => a is FtDocumentAttribute) as FtDocumentAttribute,
KeyProperty = type.GetProperties().FirstOrDefault(p => p.GetCustomAttributes(false).FirstOrDefault(a => a is FtKeyAttribute) != null),
};
var fields = fieldProprties.Select(a => new DocumentSchemaFieldInfo
foreach (var fieldProperty in fieldProprties)
{
DocumentSchema = schema,
Property = a.property,
FieldAttribute = a.attribute,
FieldType = GetMapFieldType(a.property, a.attribute)
}).ToList();
schema.Fields = fields;
schema.FieldsMap = fields.ToDictionary(a => a.Property.Name, a => a);
var field = new DocumentSchemaFieldInfo
{
DocumentSchema = schema,
Property = fieldProperty.property,
FieldAttribute = fieldProperty.attribute,
FieldType = GetMapFieldType(fieldProperty.property, fieldProperty.attribute)
};
schema.Fields.Add(field);
schema.FieldsMap[field.Property.Name] = field;
schema.FieldsMapRead[field.FieldAttribute.Name] = field;
}
if (schema.Fields.Any() == false) throw new Exception($"Not found: [FtFieldAttribute]");
schema.DocumentAttribute = type.GetCustomAttributes(false).FirstOrDefault(a => a is FtDocumentAttribute) as FtDocumentAttribute;
schema.KeyProperty = type.GetProperties().FirstOrDefault(p => p.GetCustomAttributes(false).FirstOrDefault(a => a is FtKeyAttribute) != null);
return schema;
});
}
Expand Down Expand Up @@ -250,28 +255,41 @@ internal protected List<KeyValuePair<string, string>> ParseSelectorExpression(Ex
{
var fieldValues = new List<KeyValuePair<string, string>>();
if (selector is LambdaExpression lambdaExp) selector = lambdaExp.Body;

if (selector.NodeType == ExpressionType.New)
if (selector is UnaryExpression unaryExp) selector = unaryExp.Operand;
switch (selector.NodeType)
{
var newExp = selector as NewExpression;
for (var a = 0; a < newExp?.Members?.Count; a++)
{
var left = newExp.Members[a].Name;
var right = ParseQueryExpression(newExp.Arguments[a], new ParseQueryExpressionOptions { IsQuoteFieldName = isQuoteFieldName });
fieldValues.Add(new KeyValuePair<string, string>(left, right));
}
}
else if (selector.NodeType == ExpressionType.MemberInit)
{
var initExp = selector as MemberInitExpression;
for (var a = 0; a < initExp?.Bindings.Count; a++)
{
var initAssignExp = (initExp.Bindings[a] as MemberAssignment);
if (initAssignExp == null) continue;
var left = initAssignExp.Member.Name;
var right = ParseQueryExpression(initAssignExp.Expression, new ParseQueryExpressionOptions { IsQuoteFieldName = isQuoteFieldName });
fieldValues.Add(new KeyValuePair<string, string>(left, right));
}
case ExpressionType.MemberAccess:
{
var memExp = selector as MemberExpression;
var left = memExp.Member.Name;
var right = ParseQueryExpression(memExp, new ParseQueryExpressionOptions { IsQuoteFieldName = isQuoteFieldName });
fieldValues.Add(new KeyValuePair<string, string>(left, right));
}
break;
case ExpressionType.New:
{
var newExp = selector as NewExpression;
for (var a = 0; a < newExp?.Members?.Count; a++)
{
var left = newExp.Members[a].Name;
var right = ParseQueryExpression(newExp.Arguments[a], new ParseQueryExpressionOptions { IsQuoteFieldName = isQuoteFieldName });
fieldValues.Add(new KeyValuePair<string, string>(left, right));
}
}
break;
case ExpressionType.MemberInit:
{
var initExp = selector as MemberInitExpression;
for (var a = 0; a < initExp?.Bindings.Count; a++)
{
var initAssignExp = (initExp.Bindings[a] as MemberAssignment);
if (initAssignExp == null) continue;
var left = initAssignExp.Member.Name;
var right = ParseQueryExpression(initAssignExp.Expression, new ParseQueryExpressionOptions { IsQuoteFieldName = isQuoteFieldName });
fieldValues.Add(new KeyValuePair<string, string>(left, right));
}
}
break;
}
return fieldValues;
}
Expand Down Expand Up @@ -736,16 +754,21 @@ List<T> FetchResult(SearchResult result)
foreach (var kv in doc.Body)
{
var name = kv.Key.Replace("-", "_");
var prop = ttype.GetPropertyOrFieldIgnoreCase(name);
if (prop == null) continue;
FtDocumentRepository<T>.DocumentSchemaFieldInfo field = null;
if (_searchBuilder._return.Any()) //属性匹配
{
var prop = ttype.GetPropertyOrFieldIgnoreCase(name);
if (prop == null || !_repository._schema.FieldsMap.TryGetValue(prop.Name, out field)) continue;
}
else if (!_repository._schema.FieldsMapRead.TryGetValue(name, out field)) continue;
if (kv.Value == null) continue;
if (kv.Value is string valstr && _repository._schema.FieldsMap.TryGetValue(prop.Name, out var field) && field.FieldType == FieldType.Tag)
ttype.SetPropertyOrFieldValue(item, prop.Name,
if (kv.Value is string valstr && field.FieldType == FieldType.Tag)
ttype.SetPropertyOrFieldValue(item, field.Property.Name,
field.Property.PropertyType.IsArrayOrList() ?
field.Property.PropertyType.FromObject(valstr.Split(new[] { (field.FieldAttribute as FtTagFieldAttribute).Separator ?? "," }, StringSplitOptions.None)) : valstr
);
else
ttype.SetPropertyOrFieldValue(item, prop.Name, prop.GetPropertyOrFieldType().FromObject(kv.Value));
ttype.SetPropertyOrFieldValue(item, field.Property.Name, field.Property.PropertyType.FromObject(kv.Value));
}
var itemId = doc.Id;
if (!string.IsNullOrEmpty(prefix))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,32 +169,32 @@ public Task<SearchResult> ExecuteAsync()
}
#endif

private bool _noContent, _verbatim, _noStopwords, _withScores, _withPayloads, _withSortKeys;
private List<object> _filters = new List<object>();
private List<object> _geoFilter = new List<object>();
private List<object> _inKeys = new List<object>();
private List<object> _inFields = new List<object>();
private List<string[]> _return = new List<string[]>();
private bool _sumarize;
private List<object> _sumarizeFields = new List<object>();
private long _sumarizeFrags = -1;
private long _sumarizeLen = -1;
private string _sumarizeSeparator;
private bool _highLight;
private List<object> _highLightFields = new List<object>();
private object[] _highLightTags;
private decimal _slop = -1;
private long _timeout = -1;
private bool _inOrder;
private string _language;
private string _expander, _scorer;
private bool _explainScore;
private string _payLoad;
private string _sortBy;
private bool _sortByDesc;
private long _limitOffset, _limitNum = 10;
private List<object> _params = new List<object>();
private int _dialect;
internal bool _noContent, _verbatim, _noStopwords, _withScores, _withPayloads, _withSortKeys;
internal List<object> _filters = new List<object>();
internal List<object> _geoFilter = new List<object>();
internal List<object> _inKeys = new List<object>();
internal List<object> _inFields = new List<object>();
internal List<string[]> _return = new List<string[]>();
internal bool _sumarize;
internal List<object> _sumarizeFields = new List<object>();
internal long _sumarizeFrags = -1;
internal long _sumarizeLen = -1;
internal string _sumarizeSeparator;
internal bool _highLight;
internal List<object> _highLightFields = new List<object>();
internal object[] _highLightTags;
internal decimal _slop = -1;
internal long _timeout = -1;
internal bool _inOrder;
internal string _language;
internal string _expander, _scorer;
internal bool _explainScore;
internal string _payLoad;
internal string _sortBy;
internal bool _sortByDesc;
internal long _limitOffset, _limitNum = 10;
internal List<object> _params = new List<object>();
internal int _dialect;

public SearchBuilder NoContent(bool value = true)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class RediSearchTests : TestBase

protected static ConnectionStringBuilder Connection = new ConnectionStringBuilder()
{
Host = "8.154.26.119",
Host = "8.154.26.119:63791",
MaxPoolSize = 10,
Protocol = RedisProtocol.RESP2,
ClientName = "FreeRedis",
Expand All @@ -36,16 +36,16 @@ class TestDoc
[FtKey]
public int Id { get; set; }

[FtTextField("title", Weight = 5.0)]
[FtTextField("title22", Weight = 5.0)]
public string Title { get; set; }

[FtTextField("category")]
[FtTextField("category22")]
public string Category { get; set; }

[FtTextField("content", Weight = 1.0, NoIndex = true)]
[FtTextField("content22", Weight = 1.0, NoIndex = true)]
public string Content { get; set; }

[FtTagField("tags")]
[FtTagField("tags22")]
public string Tags { get; set; }

[FtNumericField("views")]
Expand Down Expand Up @@ -111,16 +111,16 @@ class TagMapListIndex
[FtKey]
public int Id { get; set; }

[FtTextField("title", Weight = 5.0)]
[FtTextField("title1", Weight = 5.0)]
public string Title { get; set; }

[FtTextField("category")]
[FtTextField("category1")]
public string Category { get; set; }

[FtTextField("content", Weight = 1.0, NoIndex = true)]
[FtTextField("content2", Weight = 1.0, NoIndex = true)]
public string Content { get; set; }

[FtTagField("tags")]
[FtTagField("tags11")]
public List<string> Tags { get; set; }

[FtNumericField("views")]
Expand Down

0 comments on commit 391131c

Please sign in to comment.