/// <summary>
        /// Search for packages. Includes prerelease packages.
        /// </summary>
        /// <param name="query">
        /// The search query. If <see langword="null"/>, gets default search results.
        /// </param>
        /// <param name="cancellationToken">A token to cancel the task.</param>
        /// <returns>The search results, including prerelease packages.</returns>
        public virtual async Task <IReadOnlyList <SearchResult> > SearchAsync(
            string query = null,
            CancellationToken cancellationToken = default)
        {
            var response = await _searchClient.SearchAsync(query, cancellationToken : cancellationToken);

            return(response.Data);
        }
Пример #2
0
        public async Task <List <SearchResult> > GetUrlRankingsAsync(string searchText, string domainUrl)
        {
            const int pageEnd       = 90;
            var       googleResults = await _searchClient.SearchAsync(searchText, pageEnd);

            var rankings = googleResults?.Where(a => a.Url.Contains(domainUrl.ToLower()));

            return(rankings?.ToList());
        }
Пример #3
0
        public async Task <PluginsResponse> SearchAsync(string query, int skip, int take, bool includePrerelease)
        {
            query  = query.Replace("tags:", "", StringComparison.OrdinalIgnoreCase);
            query += " Tags:\"openmod-plugin\"";

            var response = await _searchClient.SearchAsync(query, skip, take, includePrerelease);

            Filter(response, out IReadOnlyList <Plugin> plugins, out long total);
            return(new PluginsResponse(total, plugins));
        }
Пример #4
0
        public async Task <SearchResultViewModel> SearchAsync(string queryWord)
        {
            try
            {
                var response = await _searchClient.SearchAsync(queryWord);

                if (response.HasErrors)
                {
                    _logger.Error(string.Format("Error occurred: search action for query word '{0}' failed", queryWord));
                    return(errorViewModel());
                }

                return(createViewModel(response));
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                return(errorViewModel());
            }
        }
Пример #5
0
        public async Task <SearchResponse <Specimen> > SearchAsync(FindParams <Data.Shared.Models.Specimen> findParams, Data.Shared.Models.User user)
        {
            var specimenFindParams = findParams as SpecimenFindParams;
            var searchTerm         = findParams.SearchText;
            var musts   = GetFilters(findParams);
            var shoulds = new List <QueryContainer>();
            var query   = new QueryContainerDescriptor <Specimen>();

            if (!string.IsNullOrEmpty(searchTerm))
            {
                var fields = new FieldsDescriptor <Specimen>();

                fields = fields.Field(f => f.Name)
                         .Field(f => f.Lifeform.CommonName)
                         .Field(f => f.Lifeform.ScientificName);

                if (findParams.UseNGrams)
                {
                    fields = fields.Field("name.nameSearch");
                }

                shoulds.Add(query.MultiMatch(mm => mm.Fields(mmf => fields)
                                             .Query(searchTerm)
                                             .Fuzziness(Fuzziness.AutoLength(1, 5))));
            }

            musts.Add(FilterByVisibility(query, user));

            var searchDescriptor = new SearchDescriptor <Specimen>()
                                   .Query(q => q
                                          .Bool(b => b
                                                .Should(shoulds.ToArray())
                                                .Must(musts.ToArray()).MinimumShouldMatch(string.IsNullOrEmpty(searchTerm) ? 0 : 1)));

            var countDescriptor = new CountDescriptor <Specimen>()
                                  .Query(q => q
                                         .Bool(b => b
                                               .Should(shoulds.ToArray())
                                               .Must(musts.ToArray()).MinimumShouldMatch(string.IsNullOrEmpty(searchTerm) ? 0 : 1)));

            // Sort
            var specimenSorts = GetSpecimenSorts();

            if (!string.IsNullOrEmpty(findParams.SortBy))
            {
                if (findParams.SortDirection == SortDirection.Ascending)
                {
                    searchDescriptor.Sort(s => s.Field(f => f.Field(specimenSorts[findParams.SortBy]).Ascending()));
                }
                else
                {
                    searchDescriptor.Sort(s => s.Field(f => f.Field(specimenSorts[findParams.SortBy]).Descending()));
                }
            }
            else if (string.IsNullOrEmpty(findParams.SearchText))
            {
                searchDescriptor.Sort(s => s.Field(f => f.Field(specimenSorts["DateModified"]).Descending()).Field(f => f.Field(specimenSorts["DateCreated"]).Descending()));
            }

            var aggregations  = new AggregationContainerDescriptor <Specimen>();
            var searchFilters = new List <SearchFilter <Specimen> >
            {
                new SearchValueFilter <Specimen, string>("Stage", "specimenStage", specimenFindParams.Filters.StageFilter.Value)
            };

            foreach (var filter in searchFilters)
            {
                if (filter is NestedSearchValueFilter <Specimen, string> nestedFilter)
                {
                    aggregations = nestedFilter.ToAggregationContainerDescriptor(aggregations);
                }
                else if (filter is SearchRangeFilter <Specimen, double> searchRangeFilter)
                {
                    aggregations = searchRangeFilter.ToAggregationContainerDescriptor(aggregations);
                }
                else if (filter is SearchValueFilter <Specimen, string> searchValueFilter)
                {
                    aggregations = searchValueFilter.ToAggregationContainerDescriptor(aggregations);
                }
            }

            searchDescriptor.Aggregations(a => aggregations);

            var response = await _searchClient.SearchAsync(pi => searchDescriptor.Skip(findParams.Skip).Take(findParams.Take), pi => countDescriptor);

            response.AggregationResult = ProcessAggregations(response, specimenFindParams);

            return(response);
        }
Пример #6
0
        public async Task <SearchResponse <PlantInfo> > SearchAsync(FindParams <Data.Shared.Models.PlantInfo> findParams, Data.Shared.Models.User user)
        {
            var plantInfoFindParams = findParams as PlantInfoFindParams;
            var searchTerm          = findParams.SearchText;
            var shoulds             = new List <QueryContainer>();
            var query = new QueryContainerDescriptor <PlantInfo>();

            if (!string.IsNullOrEmpty(searchTerm))
            {
                var fields = new FieldsDescriptor <PlantInfo>();

                fields = fields.Field(m => m.CommonName)
                         .Field(m => m.ScientificName)
                         .Field(m => m.Lifeform.CommonName)
                         .Field(m => m.Lifeform.ScientificName)
                         .Field("commonName.nameSearch")
                         .Field("scientificName.nameSearch")
                         .Field("lifeform.commonName.nameSearch")
                         .Field("lifeform.scientificName.nameSearch");

                shoulds.Add(query.MultiMatch(mm => mm.Fields(mmf => fields)
                                             .Query(searchTerm)
                                             .Fuzziness(Fuzziness.AutoLength(3, 5))));
                shoulds.Add(query.Nested(n => n
                                         .Path(p => p.Synonyms)
                                         .Query(q => q
                                                .Match(sq => sq
                                                       .Field("synonyms.name")
                                                       .Query(searchTerm)
                                                       .Fuzziness(Fuzziness.AutoLength(3, 5))))));
            }

            var filters       = plantInfoFindParams.Filters;
            var searchFilters = new List <SearchFilter <PlantInfo> >
            {
                new NestedSearchValueFilter <PlantInfo, string>(filters.RegionFilter.Name, "location.region.keyword", "plantLocations", filters.RegionFilter.Value),
                new SearchValuesFilter <PlantInfo, string>(filters.WaterFilter.Name, "waterTypes", filters.WaterFilter.MinimumValue, filters.WaterFilter.MaximumValue),
                new SearchValuesFilter <PlantInfo, string>(filters.LightFilter.Name, "lightTypes", filters.LightFilter.MinimumValue, filters.LightFilter.MaximumValue),
                new SearchValuesFilter <PlantInfo, string>(filters.BloomFilter.Name, "bloomTimes", filters.BloomFilter.MinimumValue?.ToString(), filters.BloomFilter.MaximumValue?.ToString()),
                new NestedSearchValueFilter <PlantInfo, string>(filters.ZoneFilter.Name, "id", "zones", filters.ZoneFilter.Value?.ToString()),
                new SearchRangeFilter <PlantInfo, double>(filters.HeightFilter.Name, "minHeight", "maxHeight", filters.HeightFilter.Values, filters.HeightFilter.Value, filters.HeightFilter.MaximumValue),
                new SearchRangeFilter <PlantInfo, double>(filters.SpreadFilter.Name, "minSpread", "maxSpread", filters.SpreadFilter.Values, filters.SpreadFilter.Value, filters.SpreadFilter.MaximumValue),
                new NestedSearchMultiValueFilter <PlantInfo, string, LocationStatus>(filters.NativeFilter.Name, "location.stateOrProvince.keyword", "plantLocations", "status",
                                                                                     filters.NativeFilter.Value, filters.NativeFilter.Status)
            };

            var musts = GetFilters(plantInfoFindParams, searchFilters);

            musts.Add(FilterByVisibility(query, user));

            if (plantInfoFindParams.Lifeform != null)
            {
                musts.Add(query.Bool(b => b.Must(m => m.Term(t => t.Lifeform.Id, plantInfoFindParams.Lifeform.LifeformId))));
            }

            var searchDescriptor = new SearchDescriptor <PlantInfo>()
                                   .Query(q => q
                                          .Bool(b => b
                                                .Should(shoulds.ToArray())
                                                .Must(musts.ToArray()).MinimumShouldMatch(string.IsNullOrEmpty(searchTerm) ? 0 : 1)));

            var countDescriptor = new CountDescriptor <PlantInfo>()
                                  .Query(q => q
                                         .Bool(b => b
                                               .Should(shoulds.ToArray())
                                               .Must(musts.ToArray()).MinimumShouldMatch(string.IsNullOrEmpty(searchTerm) ? 0 : 1)));

            var aggregations = new AggregationContainerDescriptor <PlantInfo>();

            foreach (var filter in searchFilters)
            {
                if (filter is NestedSearchMultiValueFilter <PlantInfo, string, LocationStatus> nestedMultiFilter)
                {
                    aggregations = nestedMultiFilter.ToAggregationContainerDescriptor(aggregations);
                }
                else if (filter is NestedSearchValueFilter <PlantInfo, string> nestedFilter)
                {
                    aggregations = nestedFilter.ToAggregationContainerDescriptor(aggregations);
                }
                else if (filter is SearchRangeFilter <PlantInfo, double> searchRangeFilter)
                {
                    aggregations = searchRangeFilter.ToAggregationContainerDescriptor(aggregations);
                }
                else if (filter is SearchValuesFilter <PlantInfo, string> searchValuesFilter)
                {
                    aggregations = searchValuesFilter.ToAggregationContainerDescriptor(aggregations);
                }
                else if (filter is SearchValueFilter <PlantInfo, string> searchValueFilter)
                {
                    aggregations = searchValueFilter.ToAggregationContainerDescriptor(aggregations);
                }
            }

            searchDescriptor.Aggregations(a => aggregations);

            // Sort
            var plantInfoSorts = GetPlantInfoSorts();

            if (!string.IsNullOrEmpty(findParams.SortBy))
            {
                if (findParams.SortDirection == SortDirection.Ascending)
                {
                    searchDescriptor.Sort(s => s.Field(f => f.Field(plantInfoSorts[findParams.SortBy]).Ascending()));
                }
                else
                {
                    searchDescriptor.Sort(s => s.Field(f => f.Field(plantInfoSorts[findParams.SortBy]).Descending()));
                }
            }
            else if (string.IsNullOrEmpty(findParams.SearchText))
            {
                searchDescriptor.Sort(s => s.Field(f => f.Field(plantInfoSorts["DateModified"]).Descending()).Field(f => f.Field(plantInfoSorts["DateCreated"]).Descending()));
            }

            var response = await _searchClient.SearchAsync(pi => searchDescriptor.Skip(findParams.Skip).Take(findParams.Take), pi => countDescriptor);

            response.AggregationResult = ProcessAggregations(response, plantInfoFindParams);

            return(response);
        }