예제 #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 Test()
        {
            //ARRANGE

            //set up search client
            var dependencyResolver = new DependencyResolverMock();

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

            SearchClientFactory.RegisterDependencies(dependencyResolver);

            //delete existing index
            var index = new IndexName {
                Name = typeof(ParentTestClass).Name.ToLower(), Type = typeof(ParentTestClass)
            };
            var deleteIndexResult = client.DeleteIndex(Indices.Index(index));

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

            //dependencyResolver.RegisterFactory<IndexMapper<ParentTestClass>>(t => new TestIndexMapper(), Lifetime.Transient);

            var testClass = new ParentTestClass();

            testClass.Children.Add(new ChildClass());
            //documentclient.CreateDocument(testClass);
            this.CreateDocument(testClass, index, client);

            var indexMeta = client.GetIndex(Indices.Index(index));

            Thread.Sleep(1000);
            //ACT
            //Search
            var searchResponse = client
                                 .Search <ParentTestClass>(s =>
                                                           s.Query(q =>
                                                                   q.Nested(nq =>
                                                                            nq.Path(p => p.Children)
                                                                            .Query(oq =>
                                                                                   oq.Match(m => m.Field(g => g.Children.First().Name)
                                                                                            .Query(testClass.Children.First().Name.ToString()))).IgnoreUnmapped()))
                                                           .Index(Indices.Index(index)));

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

            //ASSERT
            Assert.IsTrue(searchResponse.IsValid);
            Assert.AreEqual(1, searchResponse.Documents.Count());
            Assert.AreEqual(testClass.Id, searchResponse.Documents.First().Id);
            Assert.AreEqual(1, searchResponse.Documents.First().Children.Count());
            Assert.AreEqual(testClass.Children.First().Id, searchResponse.Documents.First().Children.First().Id);
        }
예제 #3
0
        public void MapPersonNamePropertyTests()
        {
            //ARRANGE
            var id = Guid.NewGuid();

            var dependencyResolver = new DependencyResolverMock();

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

            SearchClientFactory.RegisterDependencies(dependencyResolver);

            //create an unique index
            var indexName       = String.Format("{0}_{1}", typeof(EsPersonSearch).Name, id).ToLower();
            var responseHandler = new ResponseHandler();
            var indexManager    = new IndexManager(dependencyResolver, clientFactory, responseHandler);
            var indexContext    = new IndexContext(typeof(EsPersonSearch), indexName);
            var index           = indexManager.GetIndex(indexContext).Result;

            //ACT

            try
            {
                Thread.Sleep(1000);
                var indexReqiest  = new GetIndexRequest(index);
                var indexResponse = client.GetIndex(indexReqiest);
                var indices       = indexResponse.Indices.ToList();
                var first         = indices.First();

                var mappings = first.Value.Mappings.Select(x => x.Value)
                               .SelectMany(x => x.Properties, (x, y) =>
                {
                    return(new { y.Key, y.Value.Name, y.Value.Type, Properties = ((ObjectProperty)y.Value).Properties });
                });
                var nameProperty      = mappings.First(x => x.Name.Name.Equals("personName", StringComparison.OrdinalIgnoreCase));
                var firstNameProperty = nameProperty.Properties.First(x => x.Key.Name.Equals("firstName", StringComparison.OrdinalIgnoreCase));
                var lastNameProperty  = nameProperty.Properties.First(x => x.Key.Name.Equals("lastName", StringComparison.OrdinalIgnoreCase));

                //ASSERT person name
                Assert.IsTrue(indexResponse.IsValid);
                Assert.AreEqual(1, indices.Count);
                Assert.AreEqual("object", nameProperty.Type.Name);

                //ASSERT person first name
                Assert.AreEqual("keyword", firstNameProperty.Value.Type.Name);

                //ASSERT person last name
                Assert.AreEqual("keyword", lastNameProperty.Value.Type.Name);
            }
            finally
            {
                client.DeleteIndex(index);
            }
        }
        public void CreatePersonSearchDocumentTest()
        {
            //ARRANGE
            //set up a person

            var dependencyResolver = new DependencyResolverMock();
            var id            = Guid.NewGuid();
            var client        = SearchClientFactory.GetClient();
            var clientFactory = SearchClientFactory.GetClientFactory();

            SearchClientFactory.RegisterDependencies(dependencyResolver);

            //delete person index
            var index = new IndexName {
                Name = String.Format("{0}_{1}", typeof(EsPersonSearch).Name, id).ToLower(), Type = typeof(EsPersonSearch)
            };
            var deleteIndexResult = client.DeleteIndex(index);

            //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);

            var builder  = new AddPersonContextBuilder();
            var personId = Guid.NewGuid();
            var ev       = new NewPersonAdded(
                new AggregateId(personId),
                new CQRS.CorrelationId(personId),
                null,
                1,
                1,
                1,
                new PersonName
            {
                FirstName = "John",
                LastName  = "Doe"
            },
                null);

            var context = builder.BuildContext(ev);

            try
            {
                //create person document
                documentclient.UpsertDocument(context);
                Thread.Sleep(1000);

                var document = client.Get <EsPersonSearch>(personId, d => d.Index(index));

                var personName = new PersonName
                {
                    FirstName = "John1",
                    LastName  = "Doe1"
                };

                var builder1 = new AddPossitionContexttBuilder();
                var ev1      = new NewPositionAdded(
                    new AggregateId(personId),
                    null,
                    new CQRS.CorrelationId(personId),
                    personId,
                    Guid.NewGuid(),
                    null,
                    1,
                    1,
                    1,
                    personName,
                    "TestOrganisation");

                var newPossitionAdded = builder1.BuildContext(ev1);
                context = builder1.BuildContext(ev1);

                documentclient.UpsertDocument(context);
                Thread.Sleep(1000);

                document = client.Get <EsPersonSearch>(personId, d => d.Index(index));

                var builder2 = new PersonNameEditedContextBuilder();
                var ev2      = new NameEdited(
                    new AggregateId(personId),
                    1,
                    new CQRS.CorrelationId(personId),
                    null,
                    personName,
                    1);


                context = builder2.BuildContext(ev2);


                documentclient.UpsertDocument(context);
                Thread.Sleep(1000);

                document = client.Get <EsPersonSearch>(personId, d => d.Index(index));
            }
            finally
            {
                client.DeleteIndex(index);
            }
        }
        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);
            }
        }
예제 #7
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);
            }
        }