Esempio n. 1
0
        public void InstantiateUnderlyingSession_ElasticsearchConnectionString_IClient()
        {
            var factory = new ElasticsearchClientFactory();
            var client  = factory.Instantiate($@"Hostname=localhost;port=9200;Username=admin;password=p@ssw0rd;api=Elasticsearch");

            Assert.That(client.UnderlyingSessionType, Is.EqualTo(typeof(ElasticsearchClientOperation)));
        }
        public void Execute_Documents_DataSetFilled()
        {
            ElasticsearchClient client = new ElasticsearchClientFactory().Instantiate(ConnectionStringReader.GetElasticsearch()) as ElasticsearchClient;
            var statement = Mock.Of <IQuery>(
                x => x.Statement ==
                @"GET bank/_search" +
                @"{" +
                @"   ""query"": {""match_all"": { }}" +
                @"   , ""size"": 5" +
                @"   , ""_source"": [""gender"", ""age"", ""balance""]" +
                @"   , ""sort"" : [ { ""balance"" : {""order"" : ""desc""}}]" +
                @"}");
            ElasticsearchCommandOperation ElasticsearchQuery = new ElasticsearchCommandFactory().Instantiate(client, statement).Implementation as ElasticsearchCommandOperation;

            var engine = new ElasticsearchExecutionEngine((ElasticsearchClientOperation)(client.CreateNew()), ElasticsearchQuery);

            var ds = engine.Execute();

            Assert.That(ds.Tables, Has.Count.EqualTo(1));
            Assert.That(ds.Tables[0].Rows, Has.Count.EqualTo(5));
            Assert.That(ds.Tables[0].Columns, Has.Count.EqualTo(3));

            Assert.That(ds.Tables[0].Columns.Cast <DataColumn>().Select(x => x.ColumnName), Has.Member("gender"));
            Assert.That(ds.Tables[0].Columns.Cast <DataColumn>().Select(x => x.ColumnName), Has.Member("age"));
            Assert.That(ds.Tables[0].Columns.Cast <DataColumn>().Select(x => x.ColumnName), Has.Member("balance"));

            var genders = new List <object>();
            var ages    = new List <object>();

            foreach (DataRow row in ds.Tables[0].Rows)
            {
                for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
                {
                    if (row.Table.Columns[i].ColumnName == "gender")
                    {
                        genders.Add(row.ItemArray[i]);
                    }
                    else if (row.Table.Columns[i].ColumnName == "age")
                    {
                        ages.Add(row.ItemArray[i]);
                    }
                }
            }

            foreach (var expectedFirstName in new[] { "F", "M" })
            {
                Assert.That(genders, Has.Member(expectedFirstName));
            }

            foreach (var expectedAge in new object[] { 36, 25, 35, 40, 23 })
            {
                Assert.That(ages, Has.Member(expectedAge));
            }

            foreach (DataRow row in ds.Tables[0].Rows)
            {
                Assert.That(row["balance"], Is.Not.Null.Or.Empty);
            }
        }
        public void Instantiate_ElasticsearchUri_ElasticsearchClient()
        {
            var factory = new ElasticsearchClientFactory();
            var client  = factory.Instantiate($@"elasticsearch://locahost");

            Assert.That(client, Is.Not.Null);
            Assert.That(client, Is.TypeOf <ElasticsearchClient>());
        }
        public void Instantiate_ElasticsearchTokens_ElasticsearchClient()
        {
            var factory = new ElasticsearchClientFactory();
            var client  = factory.Instantiate($@"Hostname=localhost;port=9200;Username=admin;password=p@ssw0rd;api=Elasticsearch");

            Assert.That(client, Is.Not.Null);
            Assert.That(client, Is.TypeOf <ElasticsearchClient>());
        }
        public void ShouldReturnDocumentTypesFromConfigurationForMappedObjectType()
        {
            var clientFactory = new ElasticsearchClientFactory(ElasticsearchConfiguration.Instance, false);

            clientFactory.GetDocumentNameForType(typeof(ApprenticeshipSummary)).Should().Be("apprenticeship");
            clientFactory.GetDocumentNameForType(typeof(TraineeshipSummary)).Should().Be("traineeship");
            clientFactory.GetDocumentNameForType(typeof(TestMappingClass)).Should().Be("test_mapping_class");
        }
        public void ShouldReturnIndexNamesFromConfigurationForMappedObjectType()
        {
            var clientFactory = new ElasticsearchClientFactory(ElasticsearchConfiguration.Instance, false);

            clientFactory.GetIndexNameForType(typeof(ApprenticeshipSummary)).Should().Be("ApprenticeshipIndex");
            clientFactory.GetIndexNameForType(typeof(TraineeshipSummary)).Should().Be("TraineeshipIndex");
            clientFactory.GetIndexNameForType(typeof(TestMappingClass)).Should().Be("TestMappingClassIndex");
        }
Esempio n. 7
0
        public void InstantiateCreate_ElasticsearchConnectionString_IClient()
        {
            var factory           = new ElasticsearchClientFactory();
            var client            = factory.Instantiate($@"Hostname=localhost;port=9200;Username=admin;password=p@ssw0rd;api=Elasticsearch");
            var underlyingSession = client.CreateNew();

            Assert.That(underlyingSession, Is.Not.Null);
            Assert.That(underlyingSession, Is.AssignableTo <ElasticsearchClientOperation>());
        }
Esempio n. 8
0
 public void Setup()
 {
     _settingMock = new Mock <ISearchSettings>();
     _settingMock.Setup(x => x.IgnoreSslCertificateEnabled).Returns(true);
     _settingMock.Setup(x => x.ServerUrls).Returns("https://localhost:9200/");
     _settingMock.Setup(x => x.ElasticServerUrls).Returns(new[] { new Uri("https://localhost:9200/") });
     _settingMock.Setup(x => x.Elk5Enabled).Returns(true);
     _sut = new ElasticsearchClientFactory(_settingMock.Object);
 }
Esempio n. 9
0
        static void Main(string[] args)
        {
            var scroll = args[0];
            var size   = int.Parse(args[1]);

            Console.WriteLine("Scroll: {0}, Size {1}", scroll, size);

            var clientFactory = new ElasticsearchClientFactory(ElasticsearchConfiguration.Instance, false);
            var client        = clientFactory.GetElasticClient();

            Console.WriteLine("Connected. Press any key to start");
            Console.ReadKey();

            var indexName        = clientFactory.GetIndexNameForType(typeof(Elastic.Common.Entities.Address));
            var documentTypeName = clientFactory.GetDocumentNameForType(typeof(Elastic.Common.Entities.Address));
            var scanResults      = client.Search <Elastic.Common.Entities.Address>(s => s
                                                                                   .Index(indexName)
                                                                                   .Type(documentTypeName)
                                                                                   .From(0)
                                                                                   .Size(size)
                                                                                   .Filter(fd => fd.Not(fd2 => fd2.Exists(fd3 => fd3.PostcodeSearch)))
                                                                                   .SearchType(SearchType.Scan)
                                                                                   .Scroll(scroll)
                                                                                   );

            var scrolls      = 0;
            var totalUpdates = 0;
            var results      = client.Scroll <Elastic.Common.Entities.Address>(GetScrollSelector(scanResults, scroll));

            while (results.Hits.Any())
            {
                var updates    = 0;
                var descriptor = new BulkDescriptor();
                foreach (var address in results.Hits)
                {
                    AddPostcodeSearch(address, descriptor);
                    updates++;
                    totalUpdates++;
                }
                client.Bulk(descriptor);
                Console.WriteLine("Bulk updated {0} addresses. {1} addresses updated so far", updates, totalUpdates);

                results = client.Scroll <Elastic.Common.Entities.Address>(GetScrollSelector(results, scroll));
                scrolls++;
                Console.WriteLine("Updated batch {0}", scrolls);
            }

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
        public void CanHandle_ValidParser_NextNotCalled()
        {
            var uriParser = new Mock <IConnectionStringParser>();

            uriParser.Setup(x => x.CanHandle(It.IsAny <string>())).Returns(true);

            var tokenParser = new Mock <IConnectionStringParser>();

            tokenParser.Setup(x => x.CanHandle(It.IsAny <string>())).Returns(false);

            var factory = new ElasticsearchClientFactory(new IConnectionStringParser[] { uriParser.Object, tokenParser.Object });
            var result  = factory.CanHandle("myConnectionString");

            uriParser.Verify(x => x.CanHandle("myConnectionString"), Times.Once);
            tokenParser.Verify(x => x.CanHandle(It.IsAny <string>()), Times.Never);
        }
Esempio n. 11
0
        public SearchResponse Search(SearchRequest request)
        {
            var searchFactorConfiguration = new SearchFactorConfiguration
            {
                DescriptionFactors = GetFactorsFromRequest(SearchFactorKeys.Description, request),
                EmployerFactors    = GetFactorsFromRequest(SearchFactorKeys.Employer, request),
                JobTitleFactors    = GetFactorsFromRequest(SearchFactorKeys.JobTitle, request),
                ProviderFactors    = GetFactorsFromRequest(SearchFactorKeys.Provider, request)
            };

            var configurationService = new VacancySearchConfigurationService(
                _configurationService, searchFactorConfiguration);

            var elasticsearchClientFactory = new ElasticsearchClientFactory(
                configurationService, _logService);

            var searchProvider = new ApprenticeshipsSearchProvider(
                elasticsearchClientFactory, _mapper, configurationService, _logService);

            var parameters = new ApprenticeshipSearchParameters
            {
                PageNumber  = 1,
                PageSize    = 50,
                SearchField = GetSearchFieldFromRequest(request),
                Keywords    = request.SearchTerms,
                SortType    = VacancySearchSortType.Relevancy
            };

            var indexDate  = new DateTime(2020, 01, 01, 12, 00, 00);
            var indexAlias = elasticsearchClientFactory.GetIndexNameForType(typeof(ApprenticeshipSummary));
            var indexName  = string.Format("{0}.{1}", indexAlias, indexDate.ToUniversalTime().ToString("yyyy-MM-dd-HH"));

            var vacancies = searchProvider.FindVacancies(parameters, indexName);

            return(new SearchResponse
            {
                Request = request,
                SearchResults = vacancies.Results.ToArray(),
                Total = vacancies.Total,
                SearchFactorConfiguration = configurationService.Get <SearchFactorConfiguration>(),
                Parameters = parameters
            });
        }
        public void Execute_Aggregations_DataSetFilled()
        {
            ElasticsearchClient client = new ElasticsearchClientFactory().Instantiate(ConnectionStringReader.GetElasticsearch()) as ElasticsearchClient;
            var statement = Mock.Of <IQuery>(
                x => x.Statement ==
                @"GET /bank/_search" +
                @"{" +
                @"  ""size"": 5," +
                @"  ""aggs"": {" +
                @"      ""group_by_state"": {" +
                @"          ""terms"": {" +
                @"            ""field"": ""state.keyword""," +
                @"            ""size"": 3," +
                @"            ""order"": {" +
                @"              ""average_balance"": ""desc""" +
                @"            }" +
                @"          }," +
                @"          ""aggs"": {" +
                @"              ""average_balance"": {" +
                @"                  ""avg"": {" +
                @"                      ""field"": ""balance""" +
                @"                  }" +
                @"              }" +
                @"          }" +
                @"      }" +
                @"  }" +
                @"}");
            ElasticsearchCommandOperation ElasticsearchQuery = new ElasticsearchCommandFactory().Instantiate(client, statement).Implementation as ElasticsearchCommandOperation;

            var engine = new ElasticsearchExecutionEngine((ElasticsearchClientOperation)(client.CreateNew()), ElasticsearchQuery);

            var ds = engine.Execute();

            Assert.That(ds.Tables, Has.Count.EqualTo(1));
            Assert.That(ds.Tables[0].Rows, Has.Count.EqualTo(3));

            Assert.That(ds.Tables[0].Columns, Has.Count.GreaterThanOrEqualTo(2));
            Assert.That(ds.Tables[0].Columns.Cast <DataColumn>().Select(x => x.ColumnName), Has.Member("key"));
            Assert.That(ds.Tables[0].Columns.Cast <DataColumn>().Select(x => x.ColumnName), Has.Member("average_balance"));

            var states = new List <object>();

            foreach (DataRow row in ds.Tables[0].Rows)
            {
                for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
                {
                    if (row.Table.Columns[i].ColumnName == "key")
                    {
                        states.Add(row.ItemArray[i]);
                    }
                }
            }

            foreach (var expectedState in new[] { "WA", "AL", "RI" })
            {
                Assert.That(states, Has.Member(expectedState));
            }

            foreach (DataRow row in ds.Tables[0].Rows)
            {
                Assert.That(row["average_balance"], Is.Positive);
            }
        }