Exemplo n.º 1
0
        public void SearchPerson_by_last_name_wildchar_Test()
        {
            //ARRANGE
            //set up a person
            var personId = Guid.NewGuid();
            var person   = PersonAggregateFactory.BuildPersonSearchModel(personId, "John", "Doe");

            var dependencyResolver = new DependencyResolverMock();

            var client        = SearchClientFactory.GetClient();
            var clientFactory = SearchClientFactory.GetClientFactory();

            SearchClientFactory.RegisterDependencies(dependencyResolver);

            //create an unique index
            var indexId      = Guid.NewGuid();
            var indexName    = String.Format("{0}_{1}", typeof(EsPersonSearch).Name, indexId);
            var indexContext = new IndexContext(typeof(EsPersonSearch), indexName);

            //set up document CRUD controller and create a mock document
            var responseHandler    = new ResponseHandler();
            var indexManager       = new IndexManager(dependencyResolver, clientFactory, responseHandler);
            var documentDispatcher = new DocumentDispatcher(clientFactory, indexManager, responseHandler);
            var documentclient     = new DocumentController(documentDispatcher, indexManager, responseHandler);

            try
            {
                var index   = indexManager.BuildIndexName(indexContext);
                var context = new UpsertDocumentContext <EsPersonSearch>(personId)
                {
                    Document = person, IndexContext = indexContext
                };
                //create person document
                documentclient.UpsertDocument(context);
                Thread.Sleep(1000);

                var searchResponse = client
                                     .Search <EsPersonSearch>(s => s.Query(q =>
                                                                           q.Wildcard(m => m.Field(g => g.PersonName.LastName)
                                                                                      .Value("D*e")))
                                                              .Index(index)
                                                              .Explain());


                var allRecords = client
                                 .Search <EsPersonSearch>(q => q
                                                          .Index(index));

                //ASSERT
                Assert.IsTrue(searchResponse.IsValid);
                Assert.AreEqual(1, searchResponse.Documents.Count());
                Assert.AreEqual(personId, searchResponse.Documents.First().Id);
                Assert.AreEqual("John", searchResponse.Documents.First().PersonName.FirstName);
                Assert.AreEqual("Doe", searchResponse.Documents.First().PersonName.LastName);
            }
            finally
            {
                indexManager.DeleteIndex(indexContext);
            }
        }
        public void DispatchToKafkaServerTest()
        {
            //ARRANGE
            var documentId = Guid.NewGuid();
            var document   = new ParentTestClass {
                Email = "*****@*****.**", IntField = 10, DateFiled = DateTimeOffset.Now, Child = new ChildClass {
                    Name = "Child1", ChildEmail = "*****@*****.**", ChildIntField = 100, ChildDateFiled = DateTimeOffset.Now
                }
            };

            document.Child.Parent = document;

            var dependencyResolver = new DependencyResolverMock();

            //var client = SearchClientFactory.GetClient();
            //var clientFactory = SearchClientFactory.GetClientFactory();
            //SearchClientFactory.RegisterDependencies(dependencyResolver);

            ////create an unique index
            var indexId               = Guid.NewGuid();
            var indexName             = String.Format("{0}_{1}", typeof(ParentTestClass).Name, indexId);
            var indexContext          = new IndexContext(typeof(ParentTestClass), indexName);
            var producerConfigManager = new ProducerConfigManager(dependencyResolver);

            dependencyResolver.RegisterFactory <IConfigurationProvider>((t) => new ClientIdProvider(), Lifetime.Transient);
            dependencyResolver.RegisterFactory <IConfigurationProvider>((t) => new EndPoindProvider(), Lifetime.Transient);
            dependencyResolver.RegisterFactory <IConfigurationProvider>((t) => new ProducerTopicConfigProvider(), Lifetime.Transient);
            ////set up document CRUD controller and create a mock document
            //var responseHandler = new ResponseHandler();
            //var indexManager = new IndexManager(dependencyResolver, clientFactory, responseHandler);
            //var documentDispatcher = new DocumentDispatcher(clientFactory, indexManager, responseHandler);
            //var documentclient = new DocumentController(documentDispatcher, indexManager, responseHandler);
            try
            {
                var context = new UpsertDocumentContext <ParentTestClass>(documentId)
                {
                    Document = document, IndexContext = indexContext
                };
                var dispatcher = new KafkaDispatcherConfuence(producerConfigManager);
                //ACT
                dispatcher.UpsertDocument(context);
                //.Wait();

                //ASSERT
            }
            finally
            {
            }
        }
        public void DispatchToKafkaServerTest1()
        {
            //ARRANGE
            var documentId = Guid.NewGuid();
            var document   = PersonAggregateFactory.BuildPersonSearchModel(documentId, "Daniel1", "Georgiev1");

            var dependencyResolver = new DependencyResolverMock();

            ////create an unique index
            var indexId               = Guid.NewGuid();
            var indexName             = String.Format("{0}_{1}", typeof(EsPersonSearch).Name, indexId);
            var indexContext          = new IndexContext(typeof(EsPersonSearch), indexName);
            var producerConfigManager = new ProducerConfigManager(dependencyResolver);

            dependencyResolver.RegisterFactory <IConfigurationProvider>((t) => new ClientIdProvider(), Lifetime.Transient);
            dependencyResolver.RegisterFactory <IConfigurationProvider>((t) => new EndPoindProvider(), Lifetime.Transient);
            dependencyResolver.RegisterFactory <IConfigurationProvider>((t) => new ProducerTopicConfigProvider(), Lifetime.Transient);


            try
            {
                var context = new UpsertDocumentContext <EsPersonSearch>(documentId)
                {
                    Document = document, IndexContext = indexContext
                };
                var dispatcher         = new KafkaDispatcherConfuence(producerConfigManager);
                var documentController = new DocumentControllerMock(dispatcher);
                //ACT
                documentController.UpsertDocument(context);
                //.Wait();

                //ASSERT
            }
            finally
            {
            }
        }
        public void BuildBoolQueryTest_with_nested_query()
        {
            //ARRANGE
            var personId           = Guid.NewGuid();
            var person             = PersonAggregateFactory.BuildPersonSearchModel(personId, "John", "Doe");
            var person1Id          = Guid.NewGuid();
            var person1            = PersonAggregateFactory.BuildPersonSearchModel(personId, "Jane", "Doe");
            var dependencyResolver = new DependencyResolverMock();
            var organisationSearch = new EsOrganisationSearch {
                OrganisationName = "Organisation1"
            };

            person.Organisations.Add(organisationSearch);


            dependencyResolver.RegisterFactory <IQueryClauseBuilder <FieldContext> >(t => new TermClauseBuilder(), Lifetime.Transient);
            dependencyResolver.RegisterFactory <IQueryClauseBuilder <NestedFieldContext> >(t => new NestedClauseBuilder(), Lifetime.Transient);


            var client        = SearchClientFactory.GetClient();
            var clientFactory = SearchClientFactory.GetClientFactory();

            SearchClientFactory.RegisterDependencies(dependencyResolver);

            //create an unique index
            var indexId      = Guid.NewGuid();
            var indexName    = String.Format("{0}_{1}", typeof(EsPersonSearch).Name, indexId);
            var indexContext = new IndexContext(typeof(EsPersonSearch), indexName);

            //set up document CRUD controller and create a mock document
            var responseHandler    = new ResponseHandler();
            var indexManager       = new IndexManager(dependencyResolver, clientFactory, responseHandler);
            var documentDispatcher = new DocumentDispatcher(clientFactory, indexManager, responseHandler);
            var documentclient     = new DocumentController(documentDispatcher, indexManager, responseHandler);

            try
            {
                var context = new UpsertDocumentContext <EsPersonSearch>(personId)
                {
                    Document = person, IndexContext = indexContext
                };
                documentclient.UpsertDocument(context);

                var context1 = new UpsertDocumentContext <EsPersonSearch>(person1Id)
                {
                    Document = person1, IndexContext = indexContext
                };
                documentclient.UpsertDocument(context1);

                Thread.Sleep(1000);

                var queryContext = new QueryContext
                {
                    SearchFields = new[]
                    {
                        new NestedFieldContext {
                            Path = "Organisations", Value = "Organisation1", PropertyName = "organisations.organisationName"
                        },
                    },
                };
                var queryBuilder = new BoolQueryBulder <EsPersonSearch>(dependencyResolver);
                var query        = queryBuilder.BuildQuery(queryContext);

                var index          = indexManager.BuildIndexName(indexContext);
                var searchResponse = client.Search <EsPersonSearch>(s => s.Query(query)
                                                                    .Index(index));
                var documents = searchResponse.Documents;

                //ASSERT
                Assert.IsTrue(searchResponse.IsValid);
                Assert.AreEqual(1, documents.Count);
            }
            finally
            {
                indexManager.DeleteIndex(indexContext);
            }
        }
        public void BuildBoolMustQueryTest_multi_records_found()
        {
            //ARRANGE
            var personId  = Guid.NewGuid();
            var person    = PersonAggregateFactory.BuildPersonSearchModel(personId, "John", "Doe");
            var person1Id = Guid.NewGuid();
            var person1   = PersonAggregateFactory.BuildPersonSearchModel(personId, "Jane", "Doe");

            var dependencyResolver = new DependencyResolverMock();

            var client        = SearchClientFactory.GetClient();
            var clientFactory = SearchClientFactory.GetClientFactory();

            SearchClientFactory.RegisterDependencies(dependencyResolver);

            //create an unique index
            var indexId      = Guid.NewGuid();
            var indexName    = String.Format("{0}_{1}", typeof(EsPersonSearch).Name, indexId);
            var indexContext = new IndexContext(typeof(EsPersonSearch), indexName);

            //set up document CRUD controller and create a mock document
            var responseHandler = new ResponseHandler();
            var indexManager    = new IndexManager(dependencyResolver, clientFactory, responseHandler);

            dependencyResolver.RegisterFactory <ISearchClauseBuilder <EsPersonSearch> >((t) => new SearchClauseBuilder <EsPersonSearch>(new BoolQueryBulder <EsPersonSearch>(dependencyResolver), new SortClauseBuilder <EsPersonSearch>(), indexManager), Lifetime.Transient);
            dependencyResolver.RegisterFactory <ResultProjector <EsPersonSearch, QmPersonSearchResult> >(t => new PersonResultProjector(), Lifetime.Transient);
            dependencyResolver.RegisterFactory <IQueryClauseBuilder <FieldContext> >(t => new TermClauseBuilder(), Lifetime.Transient);
            dependencyResolver.RegisterFactory <IQueryClauseBuilder <NestedFieldContext> >(t => new NestedClauseBuilder(), Lifetime.Transient);
            var documentDispatcher = new DocumentDispatcher(clientFactory, indexManager, responseHandler);
            var documentclient     = new DocumentController(documentDispatcher, indexManager, responseHandler);

            var searchEngine = new SearchEngine(clientFactory, dependencyResolver, responseHandler);

            try
            {
                var context = new UpsertDocumentContext <EsPersonSearch>(personId)
                {
                    Document = person, IndexContext = indexContext
                };
                documentclient.UpsertDocument(context);

                var context1 = new UpsertDocumentContext <EsPersonSearch>(person1Id)
                {
                    Document = person1, IndexContext = indexContext
                };
                documentclient.UpsertDocument(context1);

                Thread.Sleep(1000);

                var queryContext = new QueryContext
                {
                    SearchFields = new[]
                    {
                        new FieldContext {
                            Path = "PersonName.LastName", Value = "Doe"
                        }
                    },
                    IndexContext = new IndexContext <EsPersonSearch> {
                        IndexName = indexName
                    },
                };
                queryContext.SortContext.Fields.Add(new SortField {
                    Path = "PersonName.FirstName"
                });

                var searchResponse = searchEngine.Search <EsPersonSearch, QmPersonSearchResult>(queryContext).Result;

                var documents = searchResponse.Entities;

                //ASSERT

                Assert.AreEqual(2, documents.Count());
            }
            finally
            {
                indexManager.DeleteIndex(indexContext);
            }
        }
Exemplo n.º 6
0
        public void SearchPerson_by_organisation_Test()
        {
            //ARRANGE
            var personId        = Guid.NewGuid();
            var organisationId1 = Guid.NewGuid();
            var organisationId2 = Guid.NewGuid();
            var person          = PersonAggregateFactory.BuildPersonSearchModel(personId, "John", "Doe");

            PersonAggregateFactory.AddPersonOrganisation(person, organisationId1, "TestOrganisation1");
            PersonAggregateFactory.AddPersonOrganisation(person, organisationId2, "TestOrganisation2");

            var dependencyResolver = new DependencyResolverMock();

            var client        = SearchClientFactory.GetClient();
            var clientFactory = SearchClientFactory.GetClientFactory();

            SearchClientFactory.RegisterDependencies(dependencyResolver);

            //create an unique index
            var indexId      = Guid.NewGuid();
            var indexName    = String.Format("{0}_{1}", typeof(EsPersonSearch).Name, indexId);
            var indexContext = new IndexContext(typeof(EsPersonSearch), indexName);

            //set up document CRUD controller and create a mock document
            var responseHandler    = new ResponseHandler();
            var indexManager       = new IndexManager(dependencyResolver, clientFactory, responseHandler);
            var documentDispatcher = new DocumentDispatcher(clientFactory, indexManager, responseHandler);
            var documentclient     = new DocumentController(documentDispatcher, indexManager, responseHandler);

            try
            {
                var index = indexManager.BuildIndexName(indexContext);
                //create person document
                var context = new UpsertDocumentContext <EsPersonSearch>(personId)
                {
                    Document = person, IndexContext = indexContext
                };
                documentclient.UpsertDocument(context);
                Thread.Sleep(1000);

                var searchResponse = client
                                     .Search <EsPersonSearch>(s =>
                                                              s.Query(q =>
                                                                      q.Nested(nq =>
                                                                               nq.Path(p => p.Organisations)
                                                                               .Query(oq =>
                                                                                      oq.Match(m => m.Field(g => g.Organisations.First().OrganisationName)
                                                                                               .Query("TestOrganisation1")))))
                                                              .Index(index));

                var allRecords = client
                                 .Search <EsPersonSearch>(q => q
                                                          .Index(index));

                //ASSERT
                Assert.IsTrue(searchResponse.IsValid);
                Assert.AreEqual(1, searchResponse.Documents.Count());
                Assert.AreEqual(person.Id, searchResponse.Documents.First().Id);
                Assert.AreEqual(2, searchResponse.Documents.First().Organisations.Count());
                Assert.AreEqual("TestOrganisation1", searchResponse.Documents.First().Organisations.First().OrganisationName);
            }
            finally
            {
                indexManager.DeleteIndex(indexContext);
            }
        }