private PagedList <string> RunQuery(SightingsQueryInput sightingsQueryInput) { using (var session = _documentStore.OpenSession()) { RavenQueryStatistics stats; var result = session.Query <Observation>() .Statistics(out stats) .Where(x => x.Identifications.Any() && x.Media.Any(y => y.MediaResource.MediaResourceType == "image")) .Skip(sightingsQueryInput.GetSkipIndex()) .Take(sightingsQueryInput.GetPageSize()) .ToList() .Select(Transformer.MakeSighting) .ToPagedList( sightingsQueryInput.GetPage(), sightingsQueryInput.GetPageSize(), stats.TotalResults ); _anyMoreRecords = result.PagedListItems.Any(); return(result); } }
private object ExecuteQuery(SightingsQueryInput sightingsQueryInput, IEnumerable <string> groupIds) { RavenQueryStatistics stats; User authenticatedUser = null; if (_userContext.IsUserAuthenticated()) { authenticatedUser = _documentSession.Load <User>(_userContext.GetAuthenticatedUserId()); } var query = _documentSession .Advanced .LuceneQuery <All_Contributions.Result, All_Contributions>() .Statistics(out stats) .SelectFields <All_Contributions.Result>("GroupIds", "CreatedDateTime", "ParentContributionId", "SubContributionId", "ParentContributionType", "SubContributionType", "UserId", "Observation", "Record", "Post", "User") .WhereIn("ParentContributionType", new[] { "observation", "record" }) .AndAlso() .WhereEquals("SubContributionType", null); if (groupIds.Any()) { query = query .AndAlso() .WhereIn("GroupIds", groupIds); } if (!string.IsNullOrWhiteSpace(sightingsQueryInput.Category)) { query = query .AndAlso() .WhereEquals("SightingCategory", sightingsQueryInput.Category); } if (sightingsQueryInput.NeedsId) { query = query .AndAlso() .WhereEquals("SightingIdentificationCount", 0); } if (!string.IsNullOrWhiteSpace(sightingsQueryInput.Query)) { var field = "SightingAllFields"; if (sightingsQueryInput.Field.ToLower() == "title") { field = "SightingTitle"; } if (sightingsQueryInput.Field.ToLower() == "descriptions") { field = "SightingDescriptions"; } if (sightingsQueryInput.Field.ToLower() == "tags") { field = "SightingTags"; } query = query .AndAlso() .Search(field, sightingsQueryInput.Query); } if (!string.IsNullOrWhiteSpace(sightingsQueryInput.Taxonomy)) { var ranks = sightingsQueryInput.Taxonomy.Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries); foreach (var rank in ranks) { query = query .AndAlso() .Search("SightingTaxonomicRanks", rank); } } switch (sightingsQueryInput.Sort.ToLower()) { default: case "newest": query = query.AddOrder(x => x.CreatedDateTime, true); break; case "oldest": query = query.AddOrder(x => x.CreatedDateTime, false); break; case "a-z": query = query.AddOrder(x => x.SightingSortTitle, false); break; case "z-a": query = query.AddOrder(x => x.SightingSortTitle, true); break; case "popular": query = query.AddOrder(x => x.SightingVoteCount, true); break; //case "active": // Having most activity // break; //case "needsid": // Needs an identification // break; } return(query .Skip(sightingsQueryInput.GetSkipIndex()) .Take(sightingsQueryInput.GetPageSize()) .ToList() .Select(x => _sightingViewFactory.Make(x.Contribution as Sighting, x.User, x.Groups, authenticatedUser)) .ToPagedList( sightingsQueryInput.GetPage(), sightingsQueryInput.GetPageSize(), stats.TotalResults )); }
private object ExecuteQuery(SightingsQueryInput sightingsQueryInput, IEnumerable<string> groupIds) { RavenQueryStatistics stats; User authenticatedUser = null; if (_userContext.IsUserAuthenticated()) { authenticatedUser = _documentSession.Load<User>(_userContext.GetAuthenticatedUserId()); } var query = _documentSession .Advanced .LuceneQuery<All_Contributions.Result, All_Contributions>() .Statistics(out stats) .SelectFields<All_Contributions.Result>("GroupIds", "CreatedDateTime", "ParentContributionId", "SubContributionId", "ParentContributionType", "SubContributionType", "UserId", "Observation", "Record", "Post", "User") .WhereIn("ParentContributionType", new[] { "observation", "record" }) .AndAlso() .WhereEquals("SubContributionType", null); if (groupIds.Any()) { query = query .AndAlso() .WhereIn("GroupIds", groupIds); } if (!string.IsNullOrWhiteSpace(sightingsQueryInput.Category)) { query = query .AndAlso() .WhereEquals("SightingCategory", sightingsQueryInput.Category); } if (sightingsQueryInput.NeedsId) { query = query .AndAlso() .WhereEquals("SightingIdentificationCount", 0); } if (!string.IsNullOrWhiteSpace(sightingsQueryInput.Query)) { var field = "SightingAllFields"; if (sightingsQueryInput.Field.ToLower() == "title") { field = "SightingTitle"; } if (sightingsQueryInput.Field.ToLower() == "descriptions") { field = "SightingDescriptions"; } if (sightingsQueryInput.Field.ToLower() == "tags") { field = "SightingTags"; } query = query .AndAlso() .Search(field, sightingsQueryInput.Query); } if (!string.IsNullOrWhiteSpace(sightingsQueryInput.Taxonomy)) { var ranks = sightingsQueryInput.Taxonomy.Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries); foreach (var rank in ranks) { query = query .AndAlso() .Search("SightingTaxonomicRanks", rank); } } switch (sightingsQueryInput.Sort.ToLower()) { default: case "newest": query = query.AddOrder(x => x.CreatedDateTime, true); break; case "oldest": query = query.AddOrder(x => x.CreatedDateTime, false); break; case "a-z": query = query.AddOrder(x => x.SightingSortTitle, false); break; case "z-a": query = query.AddOrder(x => x.SightingSortTitle, true); break; case "popular": query = query.AddOrder(x => x.SightingVoteCount, true); break; //case "active": // Having most activity // break; //case "needsid": // Needs an identification // break; } return query .Skip(sightingsQueryInput.GetSkipIndex()) .Take(sightingsQueryInput.GetPageSize()) .ToList() .Select(x => _sightingViewFactory.Make(x.Contribution as Sighting, x.User, x.Groups, authenticatedUser)) .ToPagedList( sightingsQueryInput.GetPage(), sightingsQueryInput.GetPageSize(), stats.TotalResults ); }