public async Task ShouldSearch()
        {
            /// Arrange
            var channelIds = new List <string> {
                "rootChannel"
            };
            var searchFieldPath =
                nameof(ContentDetail.Audit).ToLowerCamelCase() + "." +
                nameof(UserAudit.CreationDate).ToLowerCamelCase();

            var sortInfos = new List <SortInfo>
            {
                new SortInfo {
                    Direction = SortDirection.Asc, Field = searchFieldPath
                }
            };

            var filter = new TermFilter {
                Field = "contentSchemaId", Term = "ImageMetadata"
            };
            var request = new ContentSearchRequest
            {
                ChannelIds   = channelIds,
                SearchString = "*",
                Sort         = sortInfos,
                Filter       = filter,
                Start        = 0
            };

            /// Act
            ContentSearchResult result = await _client.Contents.SearchAsync(request);

            /// Assert
            Assert.True(result.Results.Count > 0);
        }
Example #2
0
        public ContentList Get(string titleStartsWith, string buckets, int top = 50, int skip = 0)
        {
            List <string> bucketList = null;

            if (!string.IsNullOrEmpty(buckets))
            {
                bucketList = buckets.Split(',').ToList <string>();
            }

            ContentSearchRequest request = new ContentSearchRequest();

            if (!string.IsNullOrEmpty(titleStartsWith))
            {
                request.TitleStartsWith = titleStartsWith;
            }
            if (bucketList != null)
            {
                request.Buckets = bucketList;
            }
            request.Count  = top;
            request.Offset = skip;

            //Execute the query
            ContentList response = _client.Content.SearchContent(request);

            return(response);
        }
        public async Task ShouldSearchByChannel()
        {
            string channelId    = "rootChannel";
            string searchString = "*";

            var sortInfos = new List <SortInfo>
            {
                new SortInfo {
                    Direction = SortDirection.Asc, Field = "audit.creationDate"
                }
            };

            var request = new ContentSearchRequest
            {
                ChannelIds = new List <string> {
                    channelId
                },
                SearchString = searchString,
                Sort         = sortInfos,
                Start        = 0,
                Limit        = 8
            };

            ContentSearchResult result = await _client.Contents.SearchByChannelAsync(channelId, request);

            Assert.True(result.Results.Count > 0);
        }
Example #4
0
 public ContentSearchResult Search(ContentSearchRequest search)
 {
     return(new ContentSearchResult
     {
         Item = SearchItems(search),
     });
 }
Example #5
0
        private async Task <string> GetFilterResultAsync(List <FilterBase> filters)
        {
            var contentSearchRequest = new ContentSearchRequest()
            {
                Filter = new AndFilter
                {
                    Filters = filters
                }
            };

            var searchResults = await _client.Content.SearchAsync(contentSearchRequest);

            var count = searchResults.Results.Count;

            if (count == 0)
            {
                return(null);
            }

            if (count > 1)
            {
                throw new SyncTargetException($"Unexpected number of Picturepark asset search results ({searchResults.Results.Count} instead of 0 or 1)");
            }

            var searchResult = searchResults.Results.First();

            return(searchResult.Id);
        }
Example #6
0
    public async Task <SearchResponse <Content> > SearchContentAsync(
        ContentSearchRequest contentSearchRequest,
        int skip = 0,
        int take = 5,
        CancellationToken cancellationToken = default)
    {
        var searchResponse = await ContentRepository.SearchContentAsync(
            contentSearchRequest,
            skip,
            take,
            cancellationToken);

        foreach (var content in searchResponse.Items)
        {
            var cacheKey = $"{CacheKeyPrefix}{content.Path}";

            if (await DistributedCache.GetAsync <Content>(
                    cacheKey, JsonSerializerOptions, cancellationToken) == null)
            {
                await DistributedCache.SetAsync(
                    cacheKey,
                    content,
                    DistributedCacheEntryOptions,
                    JsonSerializerOptions,
                    cancellationToken);
            }
        }

        return(searchResponse);
    }
Example #7
0
 public async Task <ContentSearchResult> GetContents(ContentSearchRequest searchRequest)
 {
     using (var client = new HttpClient())
     {
         var url = GetSearchUrl(searchRequest);
         return(await client.GetJson <ContentSearchResult>(url));
     }
 }
        public static async Task <ContentSearchResult> GetRandomContentsAsync(PictureparkClient client, string searchString, int limit)
        {
            var request = new ContentSearchRequest()
            {
                SearchString = searchString, Limit = limit
            };

            return(await client.Contents.SearchAsync(request));
        }
Example #9
0
 private Uri GetSearchUrl(ContentSearchRequest searchRequest)
 {
     return(UriHelper.Build(GetContentsBaseUrl(), new Dictionary <string, object>
     {
         { "id", searchRequest.Id },
         { "depth", searchRequest.Depth },
         { "publishingTarget", searchRequest.PublishingTarget },
         { "language", searchRequest.Language },
     }));
 }
Example #10
0
        public async Task <ContentSearchResult> SearchContent(ContentSearchRequest contentsSearch)
        {
            if (contentsSearch.GetDepth() != ContentsSearchDepth.Item)
            {
                throw new UnsupportedFeatureException($"{contentsSearch.GetDepth()}  not supported by Contentful provider");
            }

            var entry = await _query.GetContentItem(new ContentItemReference
            {
                Id     = contentsSearch.Id,
                Locale = contentsSearch.Language,
            }, contentsSearch.PublishingTarget);

            var contentType = _query.GetContentType(entry);

            return(ConvertToContentSearchResult(entry, contentType, ParsePublishingTarget(contentsSearch.PublishingTarget)));
        }
Example #11
0
        public static async Task <ContentSearchResult> GetRandomContentsAsync(IPictureparkService client, string searchString, int limit, IReadOnlyList <ContentType> contentTypes = null)
        {
            var request = new ContentSearchRequest {
                SearchString = searchString, Limit = limit
            };

            if (contentTypes?.Any() == true)
            {
                request.Filter = new TermsFilter
                {
                    Field = nameof(ContentDetail.ContentType).ToLowerCamelCase(),
                    Terms = contentTypes.Select(ct => ct.ToString()).ToArray()
                };
            }

            return(await client.Content.SearchAsync(request));
        }
Example #12
0
        public static async Task <string> GetRandomContentIdAsync(IPictureparkService client, string searchString, int limit)
        {
            string contentId             = string.Empty;
            ContentSearchRequest request = new ContentSearchRequest {
                Limit = limit
            };

            if (!string.IsNullOrEmpty(searchString))
            {
                request.SearchString = searchString;
            }

            ContentSearchResult result = await client.Content.SearchAsync(request);

            if (result.Results.Count > 0)
            {
                int randomNumber = SafeRandomNext(result.Results.Count);
                contentId = result.Results.Skip(randomNumber).First().Id;
            }

            return(contentId);
        }
Example #13
0
        public ContentItemResult SearchItems(ContentSearchRequest search)
        {
            var item = _query.GetItem(search.Id, search.Language, search.PublishingTarget);

            if (item == null)
            {
                return(null);
            }

            switch (search.GetDepth())
            {
            case ContentsSearchDepth.Item:
                return(_adapter.AdaptItem(item));

            case ContentsSearchDepth.Children:
                return(_adapter.AdaptItemWithChildren(item, _query.GetItemChildren(item)));

            case ContentsSearchDepth.Descendants:
                return(_adapter.AdaptItemWithChildren(item, _query.GetItemDescendants(item)));

            default:
                throw new NotImplementedException($"{nameof(ContentsSearchDepth)} value {search.Depth} not implemented");
            }
        }
Example #14
0
        public async Task <ContentSearchResult> GetContent(ContentSearchRequest searchRequest)
        {
            var result = await _contentsService.SearchContent(searchRequest);

            return(FilterSearchResult(result));
        }
Example #15
0
 public async Task <ContentSearchResult> GetContent(ContentSearchRequest searchRequest)
 {
     return(await _contentQueries.GetContent(searchRequest));
 }
Example #16
0
 public async Task <ActionResult <ContentSearchResult> > GetContent([FromQuery] ContentSearchRequest searchRequest)
 {
     return(Ok(await _contentsService.GetContent(searchRequest)));
 }
Example #17
0
    public async Task <SearchResponse <Content> > SearchContentAsync(
        ContentSearchRequest contentSearchRequest,
        int skip = 0,
        int take = 5,
        CancellationToken cancellationToken = default)
    {
        var selectStatementBuilder = new StringBuilder(
            SelectStatement);
        var selectTotalCountStatementBuilder = new StringBuilder(
            SelectTotalCountStatement);

        if (contentSearchRequest.IsIndexed.HasValue)
        {
            selectStatementBuilder.Append(
                SelectStatementIsIndexedPredicate);
            selectTotalCountStatementBuilder.Append(
                SelectStatementIsIndexedPredicate);
        }
        if (!string.IsNullOrWhiteSpace(contentSearchRequest.Path))
        {
            selectStatementBuilder.Append(
                SelectStatementPathPredicate);
            selectTotalCountStatementBuilder.Append(
                SelectStatementPathPredicate);
        }

        selectStatementBuilder.Append(
            SelectStatementOrderByClause);

        selectStatementBuilder.Append(';');
        selectTotalCountStatementBuilder.Append(';');

        using var sqlConnection = await DbConnectionFactory
                                  .GetSqlConnectionAsync(cancellationToken);

        var contents = await sqlConnection
                       .QueryAsync <Content>(
            selectStatementBuilder.ToString(),
            new
        {
            contentSearchRequest.IsIndexed,
            contentSearchRequest.Path,
            Skip = skip,
            Take = take,
        });

        var totalCount = await sqlConnection
                         .ExecuteScalarAsync <int>(
            selectTotalCountStatementBuilder.ToString(),
            new
        {
            contentSearchRequest.IsIndexed,
            contentSearchRequest.Path,
        });

        return(new SearchResponse <Content>
        {
            Items = contents,
            Skip = skip,
            Take = take,
            TotalCount = totalCount,
        });
    }
Example #18
0
 public IHttpActionResult GetContents([FromUri] ContentSearchRequest searchRequest)
 {
     return(Ok(_searchService.Search(searchRequest)));
 }