@@ -13,9 +13,14 @@ public NodeInfo(int index, int colorId)
13
13
{
14
14
Index = index ;
15
15
ColorId = colorId ;
16
+ LayerCornerStone = - 1 ;
16
17
}
17
18
public int Index { get ; set ; }
18
19
public int ColorId { get ; set ; }
20
+ public int Layer { get ; set ; }
21
+ public int Distance { get ; set ; }
22
+ public int ChildCount { get ; set ; }
23
+ public int LayerCornerStone { get ; set ; }
19
24
}
20
25
21
26
public class FacetGraphGenerator
@@ -27,89 +32,135 @@ public FacetGraphGenerator(DocumentSearchClient searchClient)
27
32
_searchHelper = searchClient ;
28
33
}
29
34
30
- public JObject GetFacetGraphNodes ( string q , List < string > facetNames )
35
+ public JObject GetFacetGraphNodes ( string q , List < string > facetNames , int maxLevels , int maxNodes )
31
36
{
32
- // Calculate nodes for 3 levels
37
+ // Calculate nodes for N levels
33
38
JObject dataset = new JObject ( ) ;
34
- int MaxEdges = 50 ;
35
- int MaxLevels = 3 ;
36
- int CurrentLevel = 1 ;
37
39
int CurrentNodes = 0 ;
40
+ int originalDistance = 100 ;
38
41
39
42
List < FDGraphEdges > FDEdgeList = new List < FDGraphEdges > ( ) ;
40
43
// Create a node map that will map a facet to a node - nodemap[0] always equals the q term
41
44
42
- Dictionary < string , NodeInfo > NodeMap = new Dictionary < string , NodeInfo > ( ) ;
43
-
44
- NodeMap [ q ] = new NodeInfo ( CurrentNodes , 0 ) ;
45
+ var NodeMap = new Dictionary < string , NodeInfo > ( ) ;
46
+
47
+ NodeMap [ q ] = new NodeInfo ( CurrentNodes , 0 )
48
+ {
49
+ Distance = originalDistance ,
50
+ Layer = 0
51
+ } ;
45
52
46
53
// If blank search, assume they want to search everything
47
54
if ( string . IsNullOrWhiteSpace ( q ) )
48
55
{
49
56
q = "*" ;
50
57
}
51
58
59
+ List < string > currentLevelTerms = new List < string > ( ) ;
60
+
52
61
List < string > NextLevelTerms = new List < string > ( ) ;
53
62
NextLevelTerms . Add ( q ) ;
54
63
55
- // Iterate through the nodes up to 3 levels deep to build the nodes or when I hit max number of nodes
56
- while ( ( NextLevelTerms . Count ( ) > 0 ) && ( CurrentLevel <= MaxLevels ) && ( FDEdgeList . Count ( ) < MaxEdges ) )
64
+ // Iterate through the nodes up to MaxLevels deep to build the nodes or when I hit max number of nodes
65
+ for ( var CurrentLevel = 0 ; CurrentLevel < maxLevels && maxNodes > 0 ; ++ CurrentLevel , maxNodes /= 2 )
57
66
{
58
- q = NextLevelTerms . First ( ) ;
59
- NextLevelTerms . Remove ( q ) ;
60
- if ( NextLevelTerms . Count ( ) == 0 )
61
- {
62
- CurrentLevel ++ ;
63
- }
64
- DocumentSearchResult < Document > response = _searchHelper . GetFacets ( q , facetNames , 10 ) ;
65
- if ( response != null )
67
+ currentLevelTerms = NextLevelTerms . ToList ( ) ;
68
+ NextLevelTerms . Clear ( ) ;
69
+ var levelNodeCount = 0 ;
70
+
71
+ NodeInfo densestNodeThisLayer = null ;
72
+ var density = 0 ;
73
+
74
+ foreach ( var k in NodeMap )
75
+ k . Value . Distance += originalDistance ;
76
+
77
+ foreach ( var t in currentLevelTerms )
66
78
{
67
- int facetColor = 0 ;
68
-
69
- foreach ( var facetName in facetNames )
79
+ if ( levelNodeCount >= maxNodes )
80
+ break ;
81
+
82
+ int facetsToGrab = 10 ;
83
+ if ( maxNodes < 10 )
70
84
{
71
- IList < FacetResult > facetVals = ( response . Facets ) [ facetName ] ;
72
- facetColor ++ ;
85
+ facetsToGrab = maxNodes ;
86
+ }
87
+ DocumentSearchResult < Document > response = _searchHelper . GetFacets ( t , facetNames , facetsToGrab ) ;
88
+ if ( response != null )
89
+ {
90
+ int facetColor = 0 ;
73
91
74
- foreach ( FacetResult facet in facetVals )
92
+ foreach ( var facetName in facetNames )
75
93
{
76
- NodeInfo nodeInfo = new NodeInfo ( - 1 , - 1 ) ;
77
- if ( NodeMap . TryGetValue ( facet . Value . ToString ( ) , out nodeInfo ) == false )
78
- {
79
- // This is a new node
80
- CurrentNodes ++ ;
81
- int node = CurrentNodes ;
82
- NodeMap [ facet . Value . ToString ( ) ] = new NodeInfo ( node , facetColor ) ;
83
- }
94
+ var facetVals = ( response . Facets ) [ facetName ] ;
95
+ facetColor ++ ;
84
96
85
- // Add this facet to the fd list
86
- if ( NodeMap [ q ] != NodeMap [ facet . Value . ToString ( ) ] )
97
+ foreach ( FacetResult facet in facetVals )
87
98
{
88
- FDEdgeList . Add ( new FDGraphEdges { source = NodeMap [ q ] . Index , target = NodeMap [ facet . Value . ToString ( ) ] . Index } ) ;
89
- if ( CurrentLevel < MaxLevels )
99
+ var facetValue = facet . Value . ToString ( ) ;
100
+ NodeInfo nodeInfo = new NodeInfo ( - 1 , - 1 ) ;
101
+ if ( NodeMap . TryGetValue ( facetValue , out nodeInfo ) == false )
90
102
{
91
- NextLevelTerms . Add ( facet . Value . ToString ( ) ) ;
103
+ // This is a new node
104
+ ++ levelNodeCount ;
105
+ NodeMap [ facetValue ] = new NodeInfo ( ++ CurrentNodes , facetColor )
106
+ {
107
+ Distance = originalDistance ,
108
+ Layer = CurrentLevel + 1
109
+ } ;
110
+
111
+ if ( CurrentLevel < maxLevels )
112
+ {
113
+ NextLevelTerms . Add ( facetValue ) ;
114
+ }
115
+ }
116
+
117
+ // Add this facet to the fd list
118
+ var newNode = NodeMap [ facetValue ] ;
119
+ var oldNode = NodeMap [ t ] ;
120
+ if ( oldNode != newNode )
121
+ {
122
+ oldNode . ChildCount += 1 ;
123
+ if ( densestNodeThisLayer == null || oldNode . ChildCount > density )
124
+ {
125
+ density = oldNode . ChildCount ;
126
+ densestNodeThisLayer = oldNode ;
127
+ }
128
+
129
+ FDEdgeList . Add ( new FDGraphEdges
130
+ {
131
+ source = oldNode . Index ,
132
+ target = newNode . Index ,
133
+ distance = newNode . Distance
134
+ } ) ;
92
135
}
93
136
}
94
137
}
95
138
}
96
139
}
140
+
141
+ if ( densestNodeThisLayer != null )
142
+ densestNodeThisLayer . LayerCornerStone = CurrentLevel ;
97
143
}
98
144
99
145
// Create nodes
100
146
JArray nodes = new JArray ( ) ;
101
- int nodeNumber = 0 ;
102
147
foreach ( KeyValuePair < string , NodeInfo > entry in NodeMap )
103
148
{
104
- nodes . Add ( JObject . Parse ( "{name: \" " + entry . Key . Replace ( "\" " , "" ) + "\" " + ", id: " + entry . Value . Index + ", color: " + entry . Value . ColorId + "}" ) ) ;
105
- nodeNumber ++ ;
149
+ nodes . Add ( JObject . FromObject ( new
150
+ {
151
+ name = entry . Key ,
152
+ id = entry . Value . Index ,
153
+ color = entry . Value . ColorId ,
154
+ layer = entry . Value . Layer ,
155
+ cornerStone = entry . Value . LayerCornerStone
156
+ } ) ) ;
106
157
}
107
158
108
159
// Create edges
109
160
JArray edges = new JArray ( ) ;
110
161
foreach ( FDGraphEdges entry in FDEdgeList )
111
162
{
112
- edges . Add ( JObject . Parse ( "{source: " + entry . source + ", target: " + entry . target + "}" ) ) ;
163
+ edges . Add ( JObject . FromObject ( entry ) ) ;
113
164
}
114
165
115
166
dataset . Add ( new JProperty ( "links" , edges ) ) ;
@@ -122,6 +173,7 @@ public class FDGraphEdges
122
173
{
123
174
public int source { get ; set ; }
124
175
public int target { get ; set ; }
176
+ public int distance { get ; set ; }
125
177
}
126
178
}
127
179
}
0 commit comments