Example #1
0
        /// <summary>
        /// Provides idea search results based on query details provided by the user.
        /// </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 <IdeaEntity> > GetTeamIdeasAsync(
            IdeaSearchScope searchScope,
            string searchQuery,
            string userObjectId,
            int?count          = null,
            int?skip           = null,
            string sortBy      = null,
            string filterQuery = null)
        {
            await this.EnsureInitializedAsync();

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

            SearchContinuationToken continuationToken = null;
            var ideas = new List <IdeaEntity>();

            if (searchScope == IdeaSearchScope.SearchTeamPostsForTitleText && !string.IsNullOrWhiteSpace(searchQuery))
            {
                searchQuery = searchQuery.EscapeCharactersInQuery();
            }

            var ideaSearchResult = await this.searchIndexClient.Documents.SearchAsync <IdeaEntity>(searchQuery, searchParameters);

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

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

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

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

            return(ideas);
        }
Example #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(
            IdeaSearchScope searchScope,
            string userObjectId,
            int?count          = null,
            int?skip           = null,
            string sortBy      = null,
            string filterQuery = null)
        {
            SearchParameters searchParameters = new SearchParameters()
            {
                Top  = count ?? ApiSearchResultCount,
                Skip = skip ?? 0,
                IncludeTotalResultCount = false,
                Select = new[]
                {
                    nameof(IdeaEntity.IdeaId),
                    nameof(IdeaEntity.CategoryId),
                    nameof(IdeaEntity.Category),
                    nameof(IdeaEntity.Title),
                    nameof(IdeaEntity.Description),
                    nameof(IdeaEntity.Tags),
                    nameof(IdeaEntity.CreatedDate),
                    nameof(IdeaEntity.CreatedByName),
                    nameof(IdeaEntity.UpdatedDate),
                    nameof(IdeaEntity.CreatedByObjectId),
                    nameof(IdeaEntity.TotalVotes),
                    nameof(IdeaEntity.Status),
                    nameof(IdeaEntity.CreatedByUserPrincipalName),
                },
                SearchFields = new[] { nameof(IdeaEntity.Title) },
                Filter       = string.IsNullOrEmpty(filterQuery) ? null : $"({filterQuery})",
            };

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

                break;

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

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

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

            case IdeaSearchScope.Categories:
                searchParameters.SearchFields = new[] { nameof(IdeaEntity.CategoryId) };
                searchParameters.Top          = 5000;
                searchParameters.Select       = new[] { nameof(IdeaEntity.CategoryId) };
                break;

            case IdeaSearchScope.CategoriesInUse:
                searchParameters.SearchFields = new[] { nameof(IdeaEntity.CategoryId) };
                searchParameters.Filter       = $"{nameof(IdeaEntity.Status)} eq {(int)IdeaStatus.Pending} or {nameof(IdeaEntity.Status)} eq {(int)IdeaStatus.Approved}";
                searchParameters.Select       = new[] { nameof(IdeaEntity.Category) };
                break;

            case IdeaSearchScope.FilterAsPerTeamTags:
                searchParameters.OrderBy      = new[] { $"{nameof(IdeaEntity.UpdatedDate)} desc" };
                searchParameters.SearchFields = new[] { nameof(IdeaEntity.CategoryId) };
                break;

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

            case IdeaSearchScope.UniqueUserNames:
                searchParameters.OrderBy = new[] { $"{nameof(IdeaEntity.UpdatedDate)} desc" };
                searchParameters.Select  = new[] { nameof(IdeaEntity.CreatedByName) };
                break;

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

            case IdeaSearchScope.Pending:
                searchParameters.Filter  = $"{nameof(IdeaEntity.Status)} eq {(int)IdeaStatus.Pending} ";
                searchParameters.OrderBy = new[] { $"{nameof(IdeaEntity.UpdatedDate)} desc" };
                break;

            case IdeaSearchScope.Approved:
                searchParameters.Filter  = $"{nameof(IdeaEntity.Status)} eq {(int)IdeaStatus.Approved} ";
                searchParameters.OrderBy = new[] { $"{nameof(IdeaEntity.UpdatedDate)} desc" };
                break;

            case IdeaSearchScope.FilterTeamPosts:

                if (!string.IsNullOrEmpty(sortBy))
                {
                    searchParameters.OrderBy = sortBy == SortByPopular ? new[] { $"{nameof(IdeaEntity.TotalVotes)} desc" } : new[] { $"{nameof(IdeaEntity.UpdatedDate)} desc" };
                }

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

            return(searchParameters);
        }