Exemplo n.º 1
0
        public AllEntriesSearchResult Find(string query, int maxResults, bool getTotalCount)
        {
            if (string.IsNullOrWhiteSpace(query))
            {
                return(new AllEntriesSearchResult());
            }

            query = query.Trim();

            var canonized = ArtistHelper.GetCanonizedName(query);
            var matchMode = FindHelpers.GetMatchMode(query, NameMatchMode.Auto, NameMatchMode.StartsWith);
            var words     = (matchMode == NameMatchMode.Words ? FindHelpers.GetQueryWords(query) : null);

            return(HandleQuery(session => {
                var artists =
                    session.Query <ArtistName>()
                    .FilterByArtistName(query, canonized, matchMode, null)                     // Can't use the existing words collection here as they are noncanonized
                    .Where(a => !a.Artist.Deleted)
                    .Select(n => n.Artist)
                    .AddNameOrder(LanguagePreference)
                    .Distinct()
                    .Take(maxResults)
                    .ToArray();

                var artistCount = (getTotalCount ?
                                   session.Query <ArtistName>()
                                   .FilterByArtistName(query, canonized, matchMode, null)
                                   .Where(a => !a.Artist.Deleted)
                                   .Select(n => n.Artist)
                                   .Distinct()
                                   .Count()
                                        : 0);

                var albums =
                    session.Query <AlbumName>()
                    .AddEntryNameFilter(query, matchMode, words)
                    .Where(a => !a.Album.Deleted)
                    .Select(n => n.Album)
                    .AddNameOrder(LanguagePreference)
                    .Distinct()
                    .Take(maxResults)
                    .ToArray();

                var albumCount = (getTotalCount ?
                                  session.Query <AlbumName>()
                                  .AddEntryNameFilter(query, matchMode, words)
                                  .Where(a => !a.Album.Deleted)
                                  .Select(n => n.Album)
                                  .Distinct()
                                  .Count()
                                        : 0);

                var songs =
                    session.Query <SongName>()
                    .AddEntryNameFilter(query, matchMode, words)
                    .Where(a => !a.Song.Deleted)
                    .Select(n => n.Song)
                    .AddNameOrder(LanguagePreference)
                    .Distinct()
                    .Take(maxResults)
                    .ToArray();

                var songCount = (getTotalCount ?
                                 session.Query <SongName>()
                                 .AddEntryNameFilter(query, matchMode, words)
                                 .Where(a => !a.Song.Deleted)
                                 .Select(n => n.Song)
                                 .Distinct()
                                 .Count()
                                        : 0);

                var tags =
                    session.Query <Tag>()
                    .Where(t => t.Name.Contains(query))
                    .OrderBy(t => t.Name)
                    .Take(maxResults)
                    .ToArray();

                var tagCount = (getTotalCount ?
                                session.Query <Tag>()
                                .Where(t => t.Name.Contains(query))
                                .Distinct()
                                .Count()
                                        : 0);

                var artistResult = new PartialFindResult <ArtistWithAdditionalNamesContract>(
                    artists.Select(a => new ArtistWithAdditionalNamesContract(a, PermissionContext.LanguagePreference)).ToArray(), artistCount);

                var albumResult = new PartialFindResult <AlbumWithAdditionalNamesContract>(
                    albums.Select(a => new AlbumWithAdditionalNamesContract(a, PermissionContext.LanguagePreference)).ToArray(), albumCount);

                var songResult = new PartialFindResult <SongWithAlbumContract>(
                    songs.Select(a => new SongWithAlbumContract(a, PermissionContext.LanguagePreference)).ToArray(), songCount);

                var tagResult = new PartialFindResult <TagContract>(
                    tags.Select(a => new TagContract(a)).ToArray(), tagCount);

                return new AllEntriesSearchResult(query, albumResult, artistResult, songResult, tagResult);
            }));
        }
Exemplo n.º 2
0
        public AllEntriesSearchResult Find(string query, int maxResults, bool getTotalCount)
        {
            if (string.IsNullOrWhiteSpace(query))
            {
                return(new AllEntriesSearchResult());
            }

            var textQuery       = SearchTextQuery.Create(query);
            var artistTextQuery = ArtistSearchTextQuery.Create(query, textQuery.MatchMode);             // Can't use the existing words collection here as they are noncanonized
            var tagTextQuery    = TagSearchTextQuery.Create(query, textQuery.MatchMode);

            return(HandleQuery(session => {
                var artists =
                    session.Query <ArtistName>()
                    .FilterByArtistName(artistTextQuery)
                    .Where(a => !a.Artist.Deleted)
                    .Select(n => n.Artist)
                    .AddNameOrder(LanguagePreference)
                    .Distinct()
                    .Take(maxResults)
                    .ToArray();

                var artistCount = (getTotalCount ?
                                   session.Query <ArtistName>()
                                   .FilterByArtistName(artistTextQuery)
                                   .Where(a => !a.Artist.Deleted)
                                   .Select(n => n.Artist)
                                   .Distinct()
                                   .Count()
                                        : 0);

                var albums =
                    session.Query <AlbumName>()
                    .AddEntryNameFilter(textQuery)
                    .Where(a => !a.Album.Deleted)
                    .Select(n => n.Album)
                    .AddNameOrder(LanguagePreference)
                    .Distinct()
                    .Take(maxResults)
                    .ToArray();

                var albumCount = (getTotalCount ?
                                  session.Query <AlbumName>()
                                  .AddEntryNameFilter(textQuery)
                                  .Where(a => !a.Album.Deleted)
                                  .Select(n => n.Album)
                                  .Distinct()
                                  .Count()
                                        : 0);

                var songs =
                    session.Query <SongName>()
                    .AddEntryNameFilter(textQuery)
                    .Where(a => !a.Song.Deleted)
                    .Select(n => n.Song)
                    .AddNameOrder(LanguagePreference)
                    .Distinct()
                    .Take(maxResults)
                    .ToArray();

                var songCount = (getTotalCount ?
                                 session.Query <SongName>()
                                 .AddEntryNameFilter(textQuery)
                                 .Where(a => !a.Song.Deleted)
                                 .Select(n => n.Song)
                                 .Distinct()
                                 .Count()
                                        : 0);

                var tags = session.Query <Tag>()
                           .AddTagNameFilter(tagTextQuery)
                           .OrderBy(t => t.Name)
                           .Take(maxResults)
                           .ToArray();

                var tagCount = (getTotalCount ? session.Query <Tag>()
                                .AddTagNameFilter(tagTextQuery)
                                .Distinct()
                                .Count()
                                        : 0);

                var artistResult = new PartialFindResult <ArtistContract>(
                    artists.Select(a => new ArtistContract(a, PermissionContext.LanguagePreference)).ToArray(), artistCount);

                var albumResult = new PartialFindResult <AlbumContract>(
                    albums.Select(a => new AlbumContract(a, PermissionContext.LanguagePreference)).ToArray(), albumCount);

                var songResult = new PartialFindResult <SongWithAlbumContract>(
                    songs.Select(a => new SongWithAlbumContract(a, PermissionContext.LanguagePreference)).ToArray(), songCount);

                var tagResult = new PartialFindResult <TagContract>(
                    tags.Select(a => new TagContract(a)).ToArray(), tagCount);

                return new AllEntriesSearchResult(query, albumResult, artistResult, songResult, tagResult);
            }));
        }