Skip to content

Commit 0936857

Browse files
committed
feat(Discovery): use json sub types in query aggregation
1 parent b9ef1c6 commit 0936857

20 files changed

+110
-18
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: node_js
22
node_js:
3-
- "8"
3+
- "10"
44
os:
55
- osx
66
osx_image: xcode61

Examples/ExampleDiscoveryV2.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public IEnumerator CreateService()
4343
// Create credential and instantiate service using bearer token
4444
BearerTokenAuthenticator authenticator = new BearerTokenAuthenticator(bearerToken: bearerToken);
4545

46-
// Otions 2
46+
// Option 2
4747
// Create credential and instantiate service using username/password
4848
// var authenticator = new CloudPakForDataAuthenticator(
4949
// url: "https://{cpd_cluster_host}{:port}",

Scripts/Services/Assistant/V1/AssistantService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,16 +517,16 @@ private void OnGetWorkspaceResponse(RESTConnector.Request req, RESTConnector.Res
517517
/// (optional)</param>
518518
/// <param name="counterexamples">An array of objects defining input examples that have been marked as
519519
/// irrelevant input. (optional)</param>
520-
/// <param name="webhooks"> (optional)</param>
521520
/// <param name="append">Whether the new data is to be appended to the existing data in the workspace. If
522521
/// **append**=`false`, elements included in the new data completely replace the corresponding existing
523522
/// elements, including all subelements. For example, if the new data includes **entities** and
524523
/// **append**=`false`, all existing entities in the workspace are discarded and replaced with the new entities.
525524
///
526525
/// If **append**=`true`, existing elements are preserved, and the new elements are added. If any elements in
527526
/// the new data collide with existing elements, the update request fails. (optional, default to false)</param>
527+
/// <param name="webhooks"> (optional)</param>
528528
/// <returns><see cref="Workspace" />Workspace</returns>
529-
public bool UpdateWorkspace(Callback<Workspace> callback, string workspaceId, string name = null, string description = null, string language = null, Dictionary<string, object> metadata = null, bool? learningOptOut = null, WorkspaceSystemSettings systemSettings = null, List<CreateIntent> intents = null, List<CreateEntity> entities = null, List<DialogNode> dialogNodes = null, List<Counterexample> counterexamples = null, List<Webhook> webhooks = null, bool? append = null)
529+
public bool UpdateWorkspace(Callback<Workspace> callback, string workspaceId, string name = null, string description = null, string language = null, Dictionary<string, object> metadata = null, bool? learningOptOut = null, WorkspaceSystemSettings systemSettings = null, List<CreateIntent> intents = null, List<CreateEntity> entities = null, List<DialogNode> dialogNodes = null, List<Counterexample> counterexamples = null, bool? append = null, List<Webhook> webhooks = null)
530530
{
531531
if (callback == null)
532532
throw new ArgumentNullException("`callback` is required for `UpdateWorkspace`");

Scripts/Services/Discovery/V1/Model/Calculation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace IBM.Watson.Discovery.V1.Model
2323
/// <summary>
2424
/// Calculation.
2525
/// </summary>
26-
public class Calculation
26+
public class Calculation: QueryAggregation
2727
{
2828
/// <summary>
2929
/// The field where the aggregation is located in the document.

Scripts/Services/Discovery/V1/Model/Filter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace IBM.Watson.Discovery.V1.Model
2323
/// <summary>
2424
/// Filter.
2525
/// </summary>
26-
public class Filter
26+
public class Filter: QueryAggregation
2727
{
2828
/// <summary>
2929
/// The match the aggregated results queried for.

Scripts/Services/Discovery/V1/Model/Histogram.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace IBM.Watson.Discovery.V1.Model
2323
/// <summary>
2424
/// Histogram.
2525
/// </summary>
26-
public class Histogram
26+
public class Histogram: QueryAggregation
2727
{
2828
/// <summary>
2929
/// The field where the aggregation is located in the document.

Scripts/Services/Discovery/V1/Model/Nested.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace IBM.Watson.Discovery.V1.Model
2323
/// <summary>
2424
/// Nested.
2525
/// </summary>
26-
public class Nested
26+
public class Nested: QueryAggregation
2727
{
2828
/// <summary>
2929
/// The area of the results the aggregation was restricted to.

Scripts/Services/Discovery/V1/Model/QueryAggregation.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,26 @@
1616
*/
1717

1818
using System.Collections.Generic;
19+
using JsonSubTypes;
1920
using Newtonsoft.Json;
2021

2122
namespace IBM.Watson.Discovery.V1.Model
2223
{
2324
/// <summary>
2425
/// An aggregation produced by Discovery to analyze the input provided.
2526
/// </summary>
27+
[JsonConverter(typeof(JsonSubtypes), "Type")]
28+
[JsonSubtypes.KnownSubType(typeof(Histogram), "histogram")]
29+
[JsonSubtypes.KnownSubType(typeof(Calculation), "max")]
30+
[JsonSubtypes.KnownSubType(typeof(Calculation), "min")]
31+
[JsonSubtypes.KnownSubType(typeof(Calculation), "average")]
32+
[JsonSubtypes.KnownSubType(typeof(Calculation), "sum")]
33+
[JsonSubtypes.KnownSubType(typeof(Calculation), "unique_count")]
34+
[JsonSubtypes.KnownSubType(typeof(Term), "term")]
35+
[JsonSubtypes.KnownSubType(typeof(Filter), "filter")]
36+
[JsonSubtypes.KnownSubType(typeof(Nested), "nested")]
37+
[JsonSubtypes.KnownSubType(typeof(Timeslice), "timeslice")]
38+
[JsonSubtypes.KnownSubType(typeof(TopHits), "top_hits")]
2639
public class QueryAggregation
2740
{
2841
/// <summary>

Scripts/Services/Discovery/V1/Model/Term.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace IBM.Watson.Discovery.V1.Model
2323
/// <summary>
2424
/// Term.
2525
/// </summary>
26-
public class Term
26+
public class Term: QueryAggregation
2727
{
2828
/// <summary>
2929
/// The field where the aggregation is located in the document.

Scripts/Services/Discovery/V1/Model/Timeslice.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace IBM.Watson.Discovery.V1.Model
2323
/// <summary>
2424
/// Timeslice.
2525
/// </summary>
26-
public class Timeslice
26+
public class Timeslice: QueryAggregation
2727
{
2828
/// <summary>
2929
/// The field where the aggregation is located in the document.

Scripts/Services/Discovery/V1/Model/TopHits.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace IBM.Watson.Discovery.V1.Model
2323
/// <summary>
2424
/// TopHits.
2525
/// </summary>
26-
public class TopHits
26+
public class TopHits: QueryAggregation
2727
{
2828
/// <summary>
2929
/// Number of top hits returned by the aggregation.

Scripts/Services/Discovery/V2/Model/QueryAggregation.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,25 @@
1616
*/
1717

1818
using Newtonsoft.Json;
19+
using JsonSubTypes;
1920

2021
namespace IBM.Watson.Discovery.V2.Model
2122
{
2223
/// <summary>
2324
/// An abstract aggregation type produced by Discovery to analyze the input provided.
2425
/// </summary>
26+
[JsonConverter(typeof(JsonSubtypes), "Type")]
27+
[JsonSubtypes.KnownSubType(typeof(QueryHistogramAggregation), "histogram")]
28+
[JsonSubtypes.KnownSubType(typeof(QueryCalculationAggregation), "max")]
29+
[JsonSubtypes.KnownSubType(typeof(QueryCalculationAggregation), "min")]
30+
[JsonSubtypes.KnownSubType(typeof(QueryCalculationAggregation), "average")]
31+
[JsonSubtypes.KnownSubType(typeof(QueryCalculationAggregation), "sum")]
32+
[JsonSubtypes.KnownSubType(typeof(QueryCalculationAggregation), "unique_count")]
33+
[JsonSubtypes.KnownSubType(typeof(QueryTermAggregation), "term")]
34+
[JsonSubtypes.KnownSubType(typeof(QueryFilterAggregation), "filter")]
35+
[JsonSubtypes.KnownSubType(typeof(QueryNestedAggregation), "nested")]
36+
[JsonSubtypes.KnownSubType(typeof(QueryTimesliceAggregation), "timeslice")]
37+
[JsonSubtypes.KnownSubType(typeof(QueryTopHitsAggregation), "top_hits")]
2538
public class QueryAggregation
2639
{
2740
/// <summary>

Scripts/Services/Discovery/V2/Model/QueryCalculationAggregation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace IBM.Watson.Discovery.V2.Model
2323
/// Returns a scalar calculation across all documents for the field specified. Possible calculations include min,
2424
/// max, sum, average, and unique_count.
2525
/// </summary>
26-
public class QueryCalculationAggregation
26+
public class QueryCalculationAggregation: QueryAggregation
2727
{
2828
/// <summary>
2929
/// The field to perform the calculation on.

Scripts/Services/Discovery/V2/Model/QueryFilterAggregation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace IBM.Watson.Discovery.V2.Model
2323
/// <summary>
2424
/// A modifier that will narrow down the document set of the sub aggregations it precedes.
2525
/// </summary>
26-
public class QueryFilterAggregation
26+
public class QueryFilterAggregation: QueryAggregation
2727
{
2828
/// <summary>
2929
/// The filter written in Discovery Query Language syntax applied to the documents before sub aggregations are

Scripts/Services/Discovery/V2/Model/QueryHistogramAggregation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace IBM.Watson.Discovery.V2.Model
2424
/// Numeric interval segments to categorize documents by using field values from a single numeric field to describe
2525
/// the category.
2626
/// </summary>
27-
public class QueryHistogramAggregation
27+
public class QueryHistogramAggregation: QueryAggregation
2828
{
2929
/// <summary>
3030
/// The numeric field name used to create the histogram.

Scripts/Services/Discovery/V2/Model/QueryLargePassages.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace IBM.Watson.Discovery.V2.Model
2323
/// <summary>
2424
/// Configuration for passage retrieval.
2525
/// </summary>
26-
public class QueryLargePassages
26+
public class QueryLargePassages: QueryAggregation
2727
{
2828
/// <summary>
2929
/// A passages query that returns the most relevant passages from the results.

Scripts/Services/Discovery/V2/Model/QueryLargeSuggestedRefinements.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace IBM.Watson.Discovery.V2.Model
2222
/// <summary>
2323
/// Configuration for suggested refinements.
2424
/// </summary>
25-
public class QueryLargeSuggestedRefinements
25+
public class QueryLargeSuggestedRefinements: QueryAggregation
2626
{
2727
/// <summary>
2828
/// Whether to perform suggested refinements.

Scripts/Services/Discovery/V2/Model/QueryLargeTableResults.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace IBM.Watson.Discovery.V2.Model
2222
/// <summary>
2323
/// Configuration for table retrieval.
2424
/// </summary>
25-
public class QueryLargeTableResults
25+
public class QueryLargeTableResults: QueryAggregation
2626
{
2727
/// <summary>
2828
/// Whether to enable table retrieval.

Scripts/Services/Discovery/V2/Model/QueryNestedAggregation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace IBM.Watson.Discovery.V2.Model
2424
/// A restriction that alter the document set used for sub aggregations it precedes to nested documents found in the
2525
/// field specified.
2626
/// </summary>
27-
public class QueryNestedAggregation
27+
public class QueryNestedAggregation: QueryAggregation
2828
{
2929
/// <summary>
3030
/// The path to the document field to scope sub aggregations to.

Tests/DiscoveryV1IntegrationTests.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,72 @@ public IEnumerator TestQuery()
917917
}
918918
#endregion
919919

920+
#region Query Aggregation
921+
[UnityTest, Order(28)]
922+
public IEnumerator TestQueryAggregation()
923+
{
924+
Log.Debug("DiscoveryServiceV1IntegrationTests", "Attempting to Query...");
925+
string naturalLanguageQuery = "Who beat Ken Jennings in Jeopardy!";
926+
QueryResponse queryResultTimeslice = null;
927+
service.Query(
928+
callback: (DetailedResponse<QueryResponse> response, IBMError error) =>
929+
{
930+
Log.Debug("DiscoveryServiceV1IntegrationTests", "Query result: {0}", response.Response);
931+
queryResultTimeslice = response.Result;
932+
Assert.IsNotNull(queryResultTimeslice);
933+
Assert.IsNull(error);
934+
Assert.IsNotNull(queryResultTimeslice.Aggregations);
935+
Assert.IsTrue((queryResultTimeslice.Aggregations[0] as Timeslice).Field == "product.sales");
936+
Assert.IsTrue((queryResultTimeslice.Aggregations[0] as Timeslice).Interval == "2d");
937+
Assert.IsTrue((queryResultTimeslice.Aggregations[0] as Timeslice).Anomaly == true);
938+
},
939+
environmentId: "system",
940+
collectionId: "news-en",
941+
naturalLanguageQuery: naturalLanguageQuery,
942+
aggregation: "timeslice(product.sales,2day,anomaly:true)"
943+
);
944+
945+
QueryResponse queryResultTerm = null;
946+
service.Query(
947+
callback: (DetailedResponse<QueryResponse> response, IBMError error) =>
948+
{
949+
Log.Debug("DiscoveryServiceV1IntegrationTests", "Query result: {0}", response.Response);
950+
queryResultTerm = response.Result;
951+
Assert.IsNotNull(queryResultTerm);
952+
Assert.IsNull(error);
953+
Assert.IsNotNull(queryResultTerm.Aggregations);
954+
Assert.IsTrue((queryResultTerm.Aggregations[0] as Term).Field == "enriched_text.concepts.text");
955+
Assert.IsTrue((queryResultTerm.Aggregations[0] as Term).Count == 10);
956+
},
957+
environmentId: "system",
958+
collectionId: "news-en",
959+
naturalLanguageQuery: naturalLanguageQuery,
960+
aggregation: "term(enriched_text.concepts.text,count:10)"
961+
);
962+
963+
QueryResponse queryResultFilter = null;
964+
service.Query(
965+
callback: (DetailedResponse<QueryResponse> response, IBMError error) =>
966+
{
967+
Log.Debug("DiscoveryServiceV1IntegrationTests", "Query result: {0}", response.Response);
968+
queryResultFilter = response.Result;
969+
Assert.IsNotNull(queryResultFilter);
970+
Assert.IsNull(error);
971+
Assert.IsNotNull(queryResultFilter.Aggregations);
972+
Assert.IsTrue((queryResultFilter.Aggregations[0] as Filter).Match == "enriched_text.concepts.text:\"cloud computing\"");
973+
},
974+
environmentId: "system",
975+
collectionId: "news-en",
976+
naturalLanguageQuery: naturalLanguageQuery,
977+
aggregation: "filter(enriched_text.concepts.text:\"cloud computing\")"
978+
);
979+
980+
while (queryResultTimeslice == null || queryResultFilter == null || queryResultTerm == null)
981+
yield return null;
982+
983+
}
984+
#endregion
985+
920986
#region QueryNotices
921987
[UnityTest, Order(29)]
922988
public IEnumerator TestQueryNotices()

0 commit comments

Comments
 (0)