public void Query_Success() { IClient client = Substitute.For <IClient>(); IRequest request = Substitute.For <IRequest>(); client.PostAsync(Arg.Any <string>()) .Returns(request); DiscoveryService service = new DiscoveryService(client); var versionDate = "versionDate"; service.VersionDate = versionDate; var projectId = "projectId"; var collectionIds = new List <string>(); var filter = "filter"; var query = "query"; var naturalLanguageQuery = "naturalLanguageQuery"; var aggregation = "aggregation"; var count = 1; var _return = new List <string>(); var offset = 1; var sort = "sort"; var highlight = false; var spellingSuggestions = false; var tableResults = new QueryLargeTableResults(); var suggestedRefinements = new QueryLargeSuggestedRefinements(); var passages = new QueryLargePassages(); var result = service.Query(projectId: projectId, collectionIds: collectionIds, filter: filter, query: query, naturalLanguageQuery: naturalLanguageQuery, aggregation: aggregation, count: count, _return: _return, offset: offset, sort: sort, highlight: highlight, spellingSuggestions: spellingSuggestions, tableResults: tableResults, suggestedRefinements: suggestedRefinements, passages: passages); JObject bodyObject = new JObject(); if (collectionIds != null && collectionIds.Count > 0) { bodyObject["collection_ids"] = JToken.FromObject(collectionIds); } if (!string.IsNullOrEmpty(filter)) { bodyObject["filter"] = JToken.FromObject(filter); } if (!string.IsNullOrEmpty(query)) { bodyObject["query"] = JToken.FromObject(query); } if (!string.IsNullOrEmpty(naturalLanguageQuery)) { bodyObject["natural_language_query"] = JToken.FromObject(naturalLanguageQuery); } if (!string.IsNullOrEmpty(aggregation)) { bodyObject["aggregation"] = JToken.FromObject(aggregation); } if (count != null) { bodyObject["count"] = JToken.FromObject(count); } if (_return != null && _return.Count > 0) { bodyObject["return"] = JToken.FromObject(_return); } if (offset != null) { bodyObject["offset"] = JToken.FromObject(offset); } if (!string.IsNullOrEmpty(sort)) { bodyObject["sort"] = JToken.FromObject(sort); } bodyObject["highlight"] = JToken.FromObject(highlight); bodyObject["spelling_suggestions"] = JToken.FromObject(spellingSuggestions); if (tableResults != null) { bodyObject["table_results"] = JToken.FromObject(tableResults); } if (suggestedRefinements != null) { bodyObject["suggested_refinements"] = JToken.FromObject(suggestedRefinements); } if (passages != null) { bodyObject["passages"] = JToken.FromObject(passages); } var json = JsonConvert.SerializeObject(bodyObject); request.Received().WithArgument("version", versionDate); request.Received().WithBodyContent(Arg.Is <StringContent>(x => x.ReadAsStringAsync().Result.Equals(json))); client.Received().PostAsync($"{service.ServiceUrl}/v2/projects/{projectId}/query"); }
/// <summary> /// Query a project. /// /// By using this method, you can construct queries. For details, see the [Discovery /// documentation](https://cloud.ibm.com/docs/services/discovery-data?topic=discovery-data-query-concepts). /// </summary> /// <param name="projectId">The ID of the project. This information can be found from the deploy page of the /// Discovery administrative tooling.</param> /// <param name="queryLong">An object that represents the query to be submitted. (optional)</param> /// <returns><see cref="QueryResponse" />QueryResponse</returns> public DetailedResponse <QueryResponse> Query(string projectId, List <string> collectionIds = null, string filter = null, string query = null, string naturalLanguageQuery = null, string aggregation = null, long?count = null, List <string> _return = null, long?offset = null, string sort = null, bool?highlight = null, bool?spellingSuggestions = null, QueryLargeTableResults tableResults = null, QueryLargeSuggestedRefinements suggestedRefinements = null, QueryLargePassages passages = null) { if (string.IsNullOrEmpty(projectId)) { throw new ArgumentNullException("`projectId` is required for `Query`"); } else { projectId = Uri.EscapeDataString(projectId); } if (string.IsNullOrEmpty(VersionDate)) { throw new ArgumentNullException("versionDate cannot be null."); } DetailedResponse <QueryResponse> result = null; try { IClient client = this.Client; SetAuthentication(); var restRequest = client.PostAsync($"{this.Endpoint}/v2/projects/{projectId}/query"); restRequest.WithArgument("version", VersionDate); restRequest.WithHeader("Accept", "application/json"); restRequest.WithHeader("Content-Type", "application/json"); JObject bodyObject = new JObject(); if (collectionIds != null && collectionIds.Count > 0) { bodyObject["collection_ids"] = JToken.FromObject(collectionIds); } if (!string.IsNullOrEmpty(filter)) { bodyObject["filter"] = filter; } if (!string.IsNullOrEmpty(query)) { bodyObject["query"] = query; } if (!string.IsNullOrEmpty(naturalLanguageQuery)) { bodyObject["natural_language_query"] = naturalLanguageQuery; } if (!string.IsNullOrEmpty(aggregation)) { bodyObject["aggregation"] = aggregation; } if (count != null) { bodyObject["count"] = JToken.FromObject(count); } if (_return != null && _return.Count > 0) { bodyObject["return"] = JToken.FromObject(_return); } if (offset != null) { bodyObject["offset"] = JToken.FromObject(offset); } if (!string.IsNullOrEmpty(sort)) { bodyObject["sort"] = sort; } if (highlight != null) { bodyObject["highlight"] = JToken.FromObject(highlight); } if (spellingSuggestions != null) { bodyObject["spelling_suggestions"] = JToken.FromObject(spellingSuggestions); } if (tableResults != null) { bodyObject["table_results"] = JToken.FromObject(tableResults); } if (suggestedRefinements != null) { bodyObject["suggested_refinements"] = JToken.FromObject(suggestedRefinements); } if (passages != null) { bodyObject["passages"] = JToken.FromObject(passages); } var httpContent = new StringContent(JsonConvert.SerializeObject(bodyObject), Encoding.UTF8, HttpMediaType.APPLICATION_JSON); restRequest.WithBodyContent(httpContent); restRequest.WithHeaders(Common.GetSdkHeaders("discovery", "v2", "Query")); restRequest.WithHeaders(customRequestHeaders); ClearCustomRequestHeaders(); result = restRequest.As <QueryResponse>().Result; if (result == null) { result = new DetailedResponse <QueryResponse>(); } } catch (AggregateException ae) { throw ae.Flatten(); } return(result); }
public void TestQuery() { string filter = "entities.text:IBM"; string query = "relations.action.lemmatized:acquire"; string aggregation = "filter(enriched_text.concepts.text:cloud computing)"; long count = 5; List <string> _return = new List <string>() { "title", "url" }; long offset = 1; string sort = "sort=enriched_text.sentiment.document.score"; bool highlight = true; bool spellingSuggestions = true; QueryLargeTableResults tableResults = new QueryLargeTableResults() { Enabled = true, Count = 3 }; QueryLargeSuggestedRefinements suggestedRefinements = new QueryLargeSuggestedRefinements() { Enabled = true, Count = 3 }; QueryLargePassages passages = new QueryLargePassages() { Enabled = true, PerDocument = true, MaxPerDocument = 3, Fields = new List <string>() { "text", "abstract", "conclusion" }, Count = 3, Characters = 100 }; service.WithHeader("X-Watson-Test", "1"); var queryResult = service.Query( projectId: projectId, collectionIds: new List <string>() { collectionId }, filter: filter, query: query, aggregation: aggregation, count: count, _return: _return, offset: offset, sort: sort, highlight: highlight, spellingSuggestions: spellingSuggestions, tableResults: tableResults, suggestedRefinements: suggestedRefinements, passages: passages ); Assert.IsNotNull(queryResult.Result); Assert.IsTrue(queryResult.Result.Aggregations[0].Type == "filter"); Assert.IsTrue((queryResult.Result.Aggregations[0] as QueryFilterAggregation).Match == "enriched_text.concepts.text:cloud computing"); }