Skip to content

Commit f528101

Browse files
author
Luis Cabrera
committed
Added support for multi-facet graphs.
1 parent 0362623 commit f528101

File tree

4 files changed

+41
-23
lines changed

4 files changed

+41
-23
lines changed

02 - Web UI Template/CognitiveSearch.UI/Controllers/HomeController.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -290,14 +290,16 @@ private void GetContainerSasUris()
290290
[HttpPost]
291291
public JObject GetGraphData(string query)
292292
{
293-
string facetName = _configuration.GetSection("GraphFacet")?.Value;
293+
string facetsList = _configuration.GetSection("GraphFacet")?.Value;
294+
295+
string[] facetNames = facetsList.Split(new char[] {',',' '}, StringSplitOptions.RemoveEmptyEntries);
294296

295297
if (query == null)
296298
{
297299
query = "*";
298300
}
299301
FacetGraphGenerator graphGenerator = new FacetGraphGenerator(_docSearch);
300-
var graphJson = graphGenerator.GetFacetGraphNodes(query, facetName);
302+
var graphJson = graphGenerator.GetFacetGraphNodes(query, facetNames.ToList<string>());
301303

302304
return graphJson;
303305
}

02 - Web UI Template/CognitiveSearch.UI/Search/DocumentSearchClient.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,15 @@ public string GetSearchId()
214214
return string.Empty;
215215
}
216216

217-
public DocumentSearchResult<Document> GetFacets(string searchText, string facetName, int maxCount = 30)
217+
public DocumentSearchResult<Document> GetFacets(string searchText, List<string> facetNames, int maxCount = 30)
218218
{
219+
var facets = new List<String>();
220+
221+
foreach (var facet in facetNames)
222+
{
223+
facets.Add($"{facet}, count:{maxCount}");
224+
}
225+
219226
// Execute search based on query string
220227
try
221228
{
@@ -224,7 +231,7 @@ public DocumentSearchResult<Document> GetFacets(string searchText, string facetN
224231
SearchMode = SearchMode.Any,
225232
Top = 10,
226233
Select = new List<String>() { idField },
227-
Facets = new List<String>() { $"{facetName}, count:{maxCount}" },
234+
Facets = facets,
228235
QueryType = QueryType.Full
229236
};
230237

02 - Web UI Template/CognitiveSearch.UI/Search/FacetGraphGenerator.cs

+24-17
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public FacetGraphGenerator(DocumentSearchClient searchClient)
1616
_searchHelper = searchClient;
1717
}
1818

19-
public JObject GetFacetGraphNodes(string q, string facetName)
19+
public JObject GetFacetGraphNodes(string q, List<string> facetNames)
2020
{
2121
// Calculate nodes for 3 levels
2222
JObject dataset = new JObject();
@@ -48,30 +48,37 @@ public JObject GetFacetGraphNodes(string q, string facetName)
4848
{
4949
CurrentLevel++;
5050
}
51-
DocumentSearchResult<Document> response = _searchHelper.GetFacets(q, facetName, 10);
51+
DocumentSearchResult<Document> response = _searchHelper.GetFacets(q, facetNames, 10);
5252
if (response != null)
5353
{
54-
IList<FacetResult> facetVals = (response.Facets)[facetName];
55-
foreach (FacetResult facet in facetVals)
54+
//var facetName = facetNames[0]; // TODO: Fix this in a bit.
55+
foreach (var facetName in facetNames)
5656
{
57-
int node = -1;
58-
if (NodeMap.TryGetValue(facet.Value.ToString(), out node) == false)
59-
{
60-
// This is a new node
61-
CurrentNodes++;
62-
node = CurrentNodes;
63-
NodeMap[facet.Value.ToString()] = node;
64-
}
65-
// Add this facet to the fd list
66-
if (NodeMap[q] != NodeMap[facet.Value.ToString()])
57+
IList<FacetResult> facetVals = (response.Facets)[facetName];
58+
59+
foreach (FacetResult facet in facetVals)
6760
{
68-
FDEdgeList.Add(new FDGraphEdges { source = NodeMap[q], target = NodeMap[facet.Value.ToString()] });
69-
if (CurrentLevel < MaxLevels)
61+
int node = -1;
62+
if (NodeMap.TryGetValue(facet.Value.ToString(), out node) == false)
7063
{
71-
NextLevelTerms.Add(facet.Value.ToString());
64+
// This is a new node
65+
CurrentNodes++;
66+
node = CurrentNodes;
67+
NodeMap[facet.Value.ToString()] = node;
68+
}
69+
// Add this facet to the fd list
70+
if (NodeMap[q] != NodeMap[facet.Value.ToString()])
71+
{
72+
FDEdgeList.Add(new FDGraphEdges { source = NodeMap[q], target = NodeMap[facet.Value.ToString()] });
73+
if (CurrentLevel < MaxLevels)
74+
{
75+
NextLevelTerms.Add(facet.Value.ToString());
76+
}
7277
}
7378
}
7479
}
80+
81+
7582
}
7683
}
7784

02 - Web UI Template/CognitiveSearch.UI/appsettings.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
// Optional key to an Azure Maps account if you would like to display the geoLocation field in a map
2727
"AzureMapsSubscriptionKey": "",
2828

29-
// Set to the name of a facetable field you would like to represent as a graph
30-
"GraphFacet": "keyPhrases"
29+
// Set to the name of a facetable field you would like to represent as a graph.
30+
// You may also set to a comma separated list of the facet names if you would like more than one facet type on the graph.
31+
"GraphFacet": "keyPhrases, locations"
32+
3133
}

0 commit comments

Comments
 (0)