Пример #1
0
        public SearchResults <TraineeshipSearchResponse, TraineeshipSearchParameters> FindVacancies(TraineeshipSearchParameters parameters)
        {
            var client           = _elasticsearchClientFactory.GetElasticClient();
            var indexName        = _elasticsearchClientFactory.GetIndexNameForType(typeof(TraineeshipSummary));
            var documentTypeName = _elasticsearchClientFactory.GetDocumentNameForType(typeof(TraineeshipSummary));

            _logger.Debug("Calling legacy vacancy search for DocumentNameForType={0} on IndexName={1}", documentTypeName, indexName);

            var search    = PerformSearch(parameters, client, indexName, documentTypeName);
            var responses = _vacancySearchMapper.Map <IEnumerable <TraineeshipSummary>, IEnumerable <TraineeshipSearchResponse> >(search.Documents).ToList();

            responses.ForEach(r =>
            {
                var hitMd = search.HitsMetaData.Hits.First(h => h.Id == r.Id.ToString(CultureInfo.InvariantCulture));

                if (parameters.Location != null)
                {
                    if (parameters.SortType == VacancySearchSortType.ClosingDate ||
                        parameters.SortType == VacancySearchSortType.Distance ||
                        parameters.SortType == VacancySearchSortType.RecentlyAdded)
                    {
                        r.Distance = double.Parse(hitMd.Sorts.Skip(hitMd.Sorts.Count() - 1).First().ToString());
                    }
                }

                r.Score = hitMd.Score;
            });

            _logger.Debug("{0} search results returned", search.Total);

            var results = new SearchResults <TraineeshipSearchResponse, TraineeshipSearchParameters>(search.Total, responses, null, parameters);

            return(results);
        }
        public SearchResults <ApprenticeshipSearchResponse, ApprenticeshipSearchParameters> FindVacancies(ApprenticeshipSearchParameters parameters, string indexName)
        {
            var client           = _elasticsearchClientFactory.GetElasticClient();
            var documentTypeName = _elasticsearchClientFactory.GetDocumentNameForType(typeof(ApprenticeshipSummary));

            _logger.Debug("Calling legacy vacancy search for DocumentNameForType={0} on IndexName={1}", documentTypeName,
                          indexName);

            ProcessSearchParameters(parameters);

            var search = PerformSearch(parameters, client, indexName, documentTypeName);

            var responses =
                _vacancySearchMapper.Map <IEnumerable <ApprenticeshipSummary>, IEnumerable <ApprenticeshipSearchResponse> >
                    (search.Documents).ToList();

            responses.ForEach(r =>
            {
                var hitMd = search.HitsMetaData.Hits.First(h => h.Id == r.Id.ToString(CultureInfo.InvariantCulture));

                if (parameters.Location != null)
                {
                    if (parameters.SortType == VacancySearchSortType.ClosingDate ||
                        parameters.SortType == VacancySearchSortType.Distance ||
                        parameters.SortType == VacancySearchSortType.RecentlyAdded)
                    {
                        r.Distance = double.Parse(hitMd.Sorts.Skip(hitMd.Sorts.Count() - 1).First().ToString());
                    }
                    else
                    {
                        //if anyone can find a better way to get this value out, feel free!
                        var array = hitMd.Fields.FieldValues <JArray>("distance");
                        var value = array[0];

                        r.Distance = double.Parse(value.ToString());
                    }
                }

                r.Score = hitMd.Score;
            });

            _logger.Debug("{0} search results returned", search.Total);

            var aggregationResults = GetAggregationResultsFrom(search.Aggs);
            var results            = new SearchResults <ApprenticeshipSearchResponse, ApprenticeshipSearchParameters>(search.Total, responses, aggregationResults, parameters);

            return(results);
        }
Пример #3
0
        public IEnumerable <Address> FindAddress(string postcode)
        {
            Condition.Requires(postcode, "postcode").IsNotNullOrWhiteSpace();

            _logger.Debug("FindAddress for postcode {0}", postcode);

            var client           = _elasticsearchClientFactory.GetElasticClient();
            var indexName        = _elasticsearchClientFactory.GetIndexNameForType(typeof(Elastic.Common.Entities.Address));
            var documentTypeName = _elasticsearchClientFactory.GetDocumentNameForType(typeof(Elastic.Common.Entities.Address));

            var postcodeSearch = postcode.Replace(" ", "");

            var search = client.Search <Elastic.Common.Entities.Address>(s =>
            {
                s.Index(indexName);
                s.Type(documentTypeName);
                s.Size(100);
                s.Query(q => q.MatchPhrase(mpqd => mpqd.OnField(a => a.PostcodeSearch).Query(postcodeSearch)));
                return(s);
            });

            //TODO: vga: refactor in a new ElasticClient class
            if (ThereWasAnErrorWhileSearching(search))
            {
                throw search.ConnectionStatus.OriginalException;
            }

            var addresses = _mapper
                            .Map <IEnumerable <Elastic.Common.Entities.Address>, IEnumerable <Address> >(search.Documents)
                            .ToList();

            SanitiseAddresses(addresses);

            return(addresses);
        }
Пример #4
0
        public bool IsIndexCorrectlyCreated(DateTime scheduledRefreshDateTime)
        {
            _logger.Debug("Checking if the index is correctly created.");

            var indexAlias       = GetIndexAlias();
            var newIndexName     = GetIndexNameAndDateExtension(indexAlias, scheduledRefreshDateTime);
            var client           = _elasticsearchClientFactory.GetElasticClient();
            var documentTypeName = _elasticsearchClientFactory.GetDocumentNameForType(typeof(TDestinationSummary));

            var search = client.Search <TDestinationSummary>(s =>
            {
                s.Index(newIndexName);
                s.Type(documentTypeName);
                s.Take(PageSize);

                s.TrackScores();

                s.Filter(f => f
                         .GeoDistance(vs => vs
                                      .Location, descriptor => descriptor
                                      .Location(LondonLatitude, LondonLongitude)
                                      .Distance(SearchRadius, GeoUnit.Miles)));

                return(s);
            });

            var result = search.Documents.Any();

            LogResult(result, newIndexName);

            return(result);
        }
        public long GetApprenticeshipsCount()
        {
            var client           = _elasticsearchClientFactory.GetElasticClient();
            var indexName        = _elasticsearchClientFactory.GetIndexNameForType(typeof(ApprenticeshipSummary));
            var documentTypeName = _elasticsearchClientFactory.GetDocumentNameForType(typeof(ApprenticeshipSummary));

            var response = client.Count <ApprenticeshipSummary>(s =>
            {
                s.Index(indexName);
                s.Type(documentTypeName);

                return(s);
            });

            var count = response.Count;

            return(count);
        }
Пример #6
0
        public IEnumerable <int> GetAllVacancyIds(string indexName)
        {
            _logger.Debug("Getting all vacancy id from index '{0}' with ScrollSize='{1}', ScrollIndexConsistencyTime='{2}', ScrollTimeout='{3}'",
                          indexName, ScrollSize, ScrollIndexConsistencyTime, ScrollTimeout);

            var client           = _elasticsearchClientFactory.GetElasticClient();
            var documentTypeName = _elasticsearchClientFactory.GetDocumentNameForType(typeof(TVacancySummary));

            // REFERENCE: http://nest.azurewebsites.net/nest/search/scroll.html.
            var scanResults = client.Search <TVacancySummary>(search => search
                                                              .Index(indexName)
                                                              .Type(documentTypeName)
                                                              .From(0)
                                                              .Size(ScrollSize)
                                                              .MatchAll()
                                                              .SearchType(SearchType.Scan)
                                                              .Scroll(ScrollIndexConsistencyTime));

            var vacancies     = new List <int>();
            var scrollRequest = new ScrollRequest(scanResults.ScrollId, ScrollTimeout);
            var scrollCount   = 0;

            while (true)
            {
                var results = client.Scroll <TVacancySummary>(scrollRequest);

                scrollCount++;

                if (!results.Documents.Any())
                {
                    break;
                }

                vacancies.AddRange(results.Documents.Select(each => each.Id));
            }

            _logger.Debug("Got {0} vacancy ids from index '{1}' in {2} 'scrolls'", vacancies.Count, indexName, scrollCount);

            return(vacancies);
        }
Пример #7
0
 public void ShouldReturnDocumentTypesFromConfigurationForMappedObjectType()
 {
     _elasticsearchClientFactory.GetDocumentNameForType(typeof(ApprenticeshipSummary)).Should().EndWith("apprenticeship");
     _elasticsearchClientFactory.GetDocumentNameForType(typeof(TraineeshipSummary)).Should().EndWith("traineeship");
     _elasticsearchClientFactory.GetDocumentNameForType(typeof(LocationLookup)).Should().EndWith("locationdatas");
 }