Skip to content

Commit

Permalink
add 查询Edges 的Source和Target 重复数据
Browse files Browse the repository at this point in the history
  • Loading branch information
xuzeyu91 committed Aug 13, 2024
1 parent 5816593 commit af028f3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<!-- See https://aka.ms/dotnet/msbuild/customize for more details on customizing your build -->
<PropertyGroup>
<Version>0.1.14</Version>
<Version>0.1.15</Version>
<SKVersion>1.17.1</SKVersion>
</PropertyGroup>
</Project>
28 changes: 28 additions & 0 deletions src/GraphRag.Net/Domain/Service/GraphService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ public async Task InsertGraphDataAsync(string index, string input)
}
if (!_edges_Repositories.IsAny(p => p.Target == relationShip.Edge.Target && p.Source == relationShip.Edge.Source))
{
relationShip.Edge.Id=Guid.NewGuid().ToString();
relationShip.Edge.Index = index;
_edges_Repositories.Insert(relationShip.Edge);
}
Expand Down Expand Up @@ -233,13 +234,40 @@ public async Task InsertGraphDataAsync(string index, string input)
{
Edges edge = new Edges()
{
Id=Guid.NewGuid().ToString(),
Index = index,
Source = nodeDic[e.Source],
Target = nodeDic[e.Target],
Relationship = e.Relationship
};
_edges_Repositories.Insert(edge);
}

//查询Edges 的Source和Target 重复数据
var repeatEdges = _edges_Repositories.GetDB().Queryable<Edges>()
.GroupBy(p => new { p.Source, p.Target })
.Select(p => new { p.Source, p.Target, Count = SqlFunc.AggregateCount(p.Source) })
.ToList().Where(p => p.Count > 1).ToList();
//合并查询Edges 的Source和Target 重复数据
foreach (var edge in repeatEdges)
{
var edges = _edges_Repositories.GetList(p => p.Source == edge.Source && p.Target == edge.Target);
var firstEdge=edges.First();

for (int i = 1; i < edges.Count(); i++)
{
if (firstEdge.Relationship == edges[i].Relationship)
{
//相同的边进行合并
_edges_Repositories.Delete(edges[i]);
continue;
}
var newDesc=await _semanticService.MergeDesc(firstEdge.Relationship, edges[i].Relationship);
firstEdge.Relationship= newDesc;
_edges_Repositories.Update(firstEdge);
_edges_Repositories.Delete(edges[i]);
}
}
}
catch (Exception ex)
{
Expand Down
5 changes: 5 additions & 0 deletions src/GraphRag.Net/Repositories/Graph/Edges/Edges.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ namespace GraphRag.Net.Repositories
[SugarIndex("i_edges_target", nameof(Edges.Target), OrderByType.Asc)]
public class Edges
{
/// <summary>
/// 主键
/// </summary>
[SugarColumn(IsPrimaryKey = true)]
public string Id { get; set; }
/// <summary>
/// 索引
/// </summary>
Expand Down

0 comments on commit af028f3

Please sign in to comment.