/// <summary> /// Gets a list of query suggestions for providing typeahead support. /// </summary> public async Task <SuggestedQueries> GetQuerySuggestions(GetQuerySuggestions getSuggestions) { string firstLetter = getSuggestions.Query.Substring(0, 1); PreparedStatement preparedStatement = await _statementCache.NoContext.GetOrAddAsync("SELECT tag FROM tags_by_letter WHERE first_letter = ? AND tag >= ? LIMIT ?"); BoundStatement boundStatement = preparedStatement.Bind(firstLetter, getSuggestions.Query, getSuggestions.PageSize); RowSet rows = await _session.ExecuteAsync(boundStatement).ConfigureAwait(false); return(new SuggestedQueries { Query = getSuggestions.Query, Suggestions = rows.Select(row => row.GetValue <string>("tag")).ToList() }); }
/// <summary> /// Gets a list of query suggestions for providing typeahead support. /// </summary> public async Task <SuggestedQueries> GetQuerySuggestions(GetQuerySuggestions getSuggestions) { // Set the base URL of the REST client to use the first node in the Cassandra cluster string nodeIp = _session.Cluster.AllHosts().First().Address.Address.ToString(); _restClient.BaseUrl = new Uri(string.Format("http://{0}:8983/solr", nodeIp)); var request = new RestRequest("killrvideo.videos/suggest"); request.Method = Method.POST; request.AddParameter("wt", "json"); // Requires a build after new names are added, added on a safe side. request.AddParameter("spellcheck.build", "true"); request.AddParameter("spellcheck.q", getSuggestions.Query); IRestResponse <SearchSuggestionResult> response = await _restClient.ExecuteTaskAsync <SearchSuggestionResult>(request).ConfigureAwait(false); // Check for network/timeout errors if (response.ResponseStatus != ResponseStatus.Completed) { //Logger.Error(response.ErrorException, "Error while querying Solr search suggestions from {host} for {query}", nodeIp, getSuggestions.Query); return(new SuggestedQueries { Query = getSuggestions.Query, Suggestions = Enumerable.Empty <string>() }); } // Check for HTTP error codes if (response.StatusCode != HttpStatusCode.OK) { //Logger.Error("HTTP status code {code} while querying Solr video suggestions from {host} for {query}", (int)response.StatusCode, nodeIp, getSuggestions.Query); return(new SuggestedQueries { Query = getSuggestions.Query, Suggestions = Enumerable.Empty <string>() }); } // Success // Json embeds another json object within an array. // Ensures we receive data if (response.Data.Spellcheck.Suggestions.Count >= 2) { // Deserialize the embedded object var suggestions = JsonConvert.DeserializeObject <SearchSpellcheckSuggestions>(response.Data.Spellcheck.Suggestions.Last()); // Ensure the object deserialized correctly if (suggestions.Suggestion != null) { return new SuggestedQueries { Query = getSuggestions.Query, Suggestions = suggestions.Suggestion } } ; } return(new SuggestedQueries { Query = getSuggestions.Query, Suggestions = Enumerable.Empty <string>() }); }