Example #1
0
        /// <summary>
        /// Gets a page of videos for a search query.
        /// </summary>
        public override async Task <SearchVideosResponse> SearchVideos(SearchVideosRequest request, ServerCallContext context)
        {
            // Do a Solr query against DSE search to find videos using Solr's ExtendedDisMax query parser. Query the
            // name, tags, and description fields in the videos table giving a boost to matches in the name and tags
            // fields as opposed to the description field
            // More info on ExtendedDisMax: http://wiki.apache.org/solr/ExtendedDisMax
            string solrQuery = "{ \"q\": \"{!edismax qf=\\\"name^2 tags^1 description\\\"}" + request.Query + "\" }";

            PreparedStatement prepared = await _statementCache.GetOrAddAsync(
                "SELECT videoid, userid, name, preview_image_location, added_date FROM videos WHERE solr_query=?");

            // The driver's built-in paging feature just works with DSE Search Solr paging which is pretty cool
            IStatement bound = prepared.Bind(solrQuery)
                               .SetConsistencyLevel(ConsistencyLevel.LocalOne)              // Search queries only support One / LocalOne
                               .SetAutoPage(false)
                               .SetPageSize(request.PageSize);

            // The initial query won't have a paging state, but subsequent calls should if there are more pages
            if (string.IsNullOrEmpty(request.PagingState) == false)
            {
                bound.SetPagingState(Convert.FromBase64String(request.PagingState));
            }

            RowSet rows = await _session.ExecuteAsync(bound).ConfigureAwait(false);

            var response = new SearchVideosResponse
            {
                Query       = request.Query,
                PagingState = rows.PagingState != null && rows.PagingState.Length > 0 ? Convert.ToBase64String(rows.PagingState) : ""
            };

            response.Videos.Add(rows.Select(MapRowToVideoPreview));
            return(response);
        }
        /// <summary>
        /// Gets a page of videos for a search query (looks for videos with that tag).
        /// </summary>
        public override async Task <SearchVideosResponse> SearchVideos(SearchVideosRequest request, ServerCallContext context)
        {
            // Use the driver's built-in paging feature to get only a page of rows
            PreparedStatement preparedStatement = await _statementCache.GetOrAddAsync("SELECT * FROM videos_by_tag WHERE tag = ?");

            IStatement boundStatement = preparedStatement.Bind(request.Query)
                                        .SetAutoPage(false)
                                        .SetPageSize(request.PageSize);

            // The initial query won't have a paging state, but subsequent calls should if there are more pages
            if (string.IsNullOrEmpty(request.PagingState) == false)
            {
                boundStatement.SetPagingState(Convert.FromBase64String(request.PagingState));
            }

            RowSet rows = await _session.ExecuteAsync(boundStatement).ConfigureAwait(false);

            var response = new SearchVideosResponse
            {
                Query       = request.Query,
                PagingState = rows.PagingState != null && rows.PagingState.Length > 0 ? Convert.ToBase64String(rows.PagingState) : ""
            };

            response.Videos.Add(rows.Select(MapRowToVideoPreview));
            return(response);
        }