예제 #1
0
        /// <summary>
        /// Provide search result for table to be used by user's based on Azure Search service.
        /// </summary>
        /// <param name="searchScope">Scope of the search.</param>
        /// <param name="searchQuery">Query which the user had typed in Messaging Extension search field.</param>
        /// <param name="userObjectId">Azure Active Directory object id of the user.</param>
        /// <param name="count">Number of search results to return.</param>
        /// <param name="skip">Number of search results to skip.</param>
        /// <param name="sortBy">Represents sorting type like: Popularity or Newest.</param>
        /// <param name="filterQuery">Filter bar based query.</param>
        /// <returns>List of search results.</returns>
        public async Task <IEnumerable <PostEntity> > GetPostsAsync(
            PostSearchScope searchScope,
            string searchQuery,
            string userObjectId,
            int?count          = null,
            int?skip           = null,
            int?sortBy         = null,
            string filterQuery = null)
        {
            await this.EnsureInitializedAsync();

            var searchParameters = this.InitializeSearchParameters(searchScope, userObjectId, count, skip, sortBy, filterQuery);

            SearchContinuationToken continuationToken = null;
            var posts            = new List <PostEntity>();
            var postSearchResult = await this.searchIndexClient.Documents.SearchAsync <PostEntity>(searchQuery, searchParameters);

            if (postSearchResult?.Results != null)
            {
                posts.AddRange(postSearchResult.Results.Select(p => p.Document));
                continuationToken = postSearchResult.ContinuationToken;
            }

            if (continuationToken == null)
            {
                return(posts);
            }

            do
            {
                var searchResult = await this.searchIndexClient.Documents.ContinueSearchAsync <PostEntity>(continuationToken);

                if (searchResult?.Results != null)
                {
                    posts.AddRange(searchResult.Results.Select(p => p.Document));
                    continuationToken = searchResult.ContinuationToken;
                }
            }while (continuationToken != null);

            return(posts);
        }
예제 #2
0
        /// <summary>
        /// Initialization of search service parameters which will help in searching the documents.
        /// </summary>
        /// <param name="searchScope">Scope of the search.</param>
        /// <param name="userObjectId">Azure Active Directory object id of the user.</param>
        /// <param name="count">Number of search results to return.</param>
        /// <param name="skip">Number of search results to skip.</param>
        /// <param name="sortBy">Represents sorting type like: Popularity or Newest.</param>
        /// <param name="filterQuery">Filter bar based query.</param>
        /// <returns>Represents an search parameter object.</returns>
        private SearchParameters InitializeSearchParameters(
            PostSearchScope searchScope,
            string userObjectId,
            int?count          = null,
            int?skip           = null,
            int?sortBy         = null,
            string filterQuery = null)
        {
            SearchParameters searchParameters = new SearchParameters()
            {
                Top  = count ?? ApiSearchResultCount,
                Skip = skip ?? 0,
                IncludeTotalResultCount = false,
                Select = new[]
                {
                    nameof(PostEntity.PostId),
                    nameof(PostEntity.Type),
                    nameof(PostEntity.Title),
                    nameof(PostEntity.Description),
                    nameof(PostEntity.ContentUrl),
                    nameof(PostEntity.Tags),
                    nameof(PostEntity.CreatedDate),
                    nameof(PostEntity.CreatedByName),
                    nameof(PostEntity.UpdatedDate),
                    nameof(PostEntity.UserId),
                    nameof(PostEntity.TotalVotes),
                    nameof(PostEntity.IsRemoved),
                },
                SearchFields = new[] { nameof(PostEntity.Title) },
                Filter       = !string.IsNullOrEmpty(filterQuery) ? filterQuery : string.Empty,
            };

            switch (searchScope)
            {
            case PostSearchScope.AllItems:
                searchParameters.OrderBy = new[] { $"{nameof(PostEntity.UpdatedDate)} desc" };
                break;

            case PostSearchScope.PostedByMe:
                searchParameters.Filter  = $"{nameof(PostEntity.UserId)} eq '{userObjectId}' ";
                searchParameters.OrderBy = new[] { $"{nameof(PostEntity.UpdatedDate)} desc" };
                break;

            case PostSearchScope.Popular:
                searchParameters.OrderBy = new[] { $"{nameof(PostEntity.TotalVotes)} desc" };
                break;

            case PostSearchScope.TeamPreferenceTags:
                searchParameters.SearchFields = new[] { nameof(PostEntity.Tags) };
                searchParameters.Top          = 5000;
                searchParameters.Select       = new[] { nameof(PostEntity.Tags) };
                break;

            case PostSearchScope.FilterAsPerTeamTags:
                searchParameters.OrderBy      = new[] { $"{nameof(PostEntity.UpdatedDate)} desc" };
                searchParameters.SearchFields = new[] { nameof(PostEntity.Tags) };
                break;

            case PostSearchScope.FilterPostsAsPerDateRange:
                searchParameters.OrderBy = new[] { $"{nameof(PostEntity.UpdatedDate)} desc" };
                searchParameters.Top     = 200;
                break;

            case PostSearchScope.UniqueUserNames:
                searchParameters.OrderBy = new[] { $"{nameof(PostEntity.UpdatedDate)} desc" };
                searchParameters.Select  = new[] { nameof(PostEntity.CreatedByName), nameof(PostEntity.UserId) };
                break;

            case PostSearchScope.SearchTeamPostsForTitleText:
                searchParameters.OrderBy      = new[] { $"{nameof(PostEntity.UpdatedDate)} desc" };
                searchParameters.QueryType    = QueryType.Full;
                searchParameters.SearchFields = new[] { nameof(PostEntity.Title) };
                break;

            case PostSearchScope.FilterTeamPosts:
                if (sortBy != null)
                {
                    searchParameters.OrderBy = sortBy == SortByPopular ? new[] { $"{nameof(PostEntity.TotalVotes)} desc" } : new[] { $"{nameof(PostEntity.UpdatedDate)} desc" };
                }

                searchParameters.SearchFields = new[] { nameof(PostEntity.Tags) };
                break;
            }

            return(searchParameters);
        }