Beispiel #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);
        }
Beispiel #3
0
        public void Create_person_search_index()
        {
            //ARRANGE
            //var id = Guid.NewGuid();

            var dependencyResolver = new DependencyResolverMock();

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

            //create an unique index
            var indexName       = String.Format("{0}", typeof(EsPersonSearch).Name).ToLower();
            var responseHandler = new ResponseHandler();
            var indexManager    = new IndexManager(dependencyResolver, clientFactory, responseHandler);
            var indexContext    = new IndexContext <ParentTestClass>(indexName);
            var index           = new IndexName {
                Name = indexName, Type = typeof(ParentTestClass)
            };

            var indexDescriptor = new CreateIndexDescriptor(index).Mappings(map => map.Map <EsPersonSearch>(m => m.AutoMap()));

            //        .Mappings(map => map.Map<ParentTestClass>(m =>
            //        m.Properties(p => p.Keyword(d => d.Name(n => n.Email))).AutoMap()));
            //var index = indexManager.BuildIndexName(indexContext);
            //ACT

            try
            {
                client.CreateIndex(indexDescriptor);
                //indexManager.BuildIndex(index).Wait();
                //Thread.Sleep(1000);
                //var d = client.Get<EsPersonSearch>(new DocumentPath<EsPersonSearch>(new EsPersonSearch { Id = Guid.Parse("a8de6650-236e-46ff-b3b9-b0bb24f0a38b") })
                //    .Index(index));
                var documents = client.Search <EsPersonSearch>(s =>
                                                               s.Query(q => q.MatchAll())
                                                               .Index(index));

                var document = client.Search <EsPersonSearch>(s =>
                                                              s.Query(q => q.Match(m =>
                                                                                   m.Field(f => f.PersonName.LastName)
                                                                                   .Query("Doe")))
                                                              .Index(index));

                var indexState       = indexManager.GetIndexState(indexContext);
                var providedName     = indexState.Settings["index.provided_name"];
                var propertiesMapped = indexState.Mappings.SelectMany(x => x.Value.Properties);
                //ASSERT
                Assert.AreEqual(indexName, providedName);
            }
            finally
            {
                //client.DeleteIndex(index);
            }
        }
Beispiel #4
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);
            }
        }
Beispiel #5
0
        public void GetIndexState()
        {
            //ARRANGE
            //var id = Guid.NewGuid();

            var dependencyResolver = new DependencyResolverMock();

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

            //create an unique index
            var indexName       = String.Format("{0}", typeof(EsPersonSearch).Name).ToLower();
            var responseHandler = new ResponseHandler();
            var indexManager    = new IndexManager(dependencyResolver, clientFactory, responseHandler);
            var indexContext    = new IndexContext <ParentTestClass>(indexName);
            var index           = new IndexName {
                Name = indexName, Type = typeof(ParentTestClass)
            };

            var indexDescriptor = new CreateIndexDescriptor(index).Mappings(map => map.Map <EsPersonSearch>(m => m.AutoMap()));

            //ACT

            try
            {
                var template = client.GetIndexTemplate(d => d.Name("personsearch"));
                //var documents = client.Search<EsPersonSearch>(s =>
                //s.Query(q => q.MatchAll())
                //.Index(index));

                // var document = client.Search<EsPersonSearch>(s =>
                //s.Query(q => q.Match(m =>
                //                    m.Field(f => f.PersonName.LastName)
                //.Query("Doe")))
                //.Index(index));
                //var allIndices = client.GetIndex(Indices.All);
                var indexState       = indexManager.GetIndexState(indexContext);
                var providedName     = indexState.Settings["index.provided_name"];
                var propertiesMapped = indexState.Mappings.SelectMany(x => x.Value.Properties);
                //ASSERT
                Assert.AreEqual(indexName, providedName);
            }
            finally
            {
                //client.DeleteIndex(index);
            }
        }
Beispiel #6
0
        public void GetPropertyMetaData_test()
        {
            //ARRANGE
            var id = Guid.NewGuid();

            var dependencyResolver = new DependencyResolverMock();

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

            dependencyResolver.RegisterFactory <QueryClauseBuilder <FieldContext> >(t => new TermClauseBuilder(), Lifetime.Transient);
            dependencyResolver.RegisterFactory <QueryClauseBuilder <NestedFieldContext> >(t => new NestedClauseBuilder(), Lifetime.Transient);
            //create an unique index
            var indexName       = String.Format("{0}_{1}", typeof(ParentTestClass).Name, id).ToLower();
            var responseHandler = new ResponseHandler();
            var indexManager    = new IndexManager(dependencyResolver, clientFactory, responseHandler);
            var indexContext    = new IndexContext <ParentTestClass>(indexName);
            var index           = new IndexName {
                Name = indexName, Type = typeof(ParentTestClass)
            };

            var indexDescriptor = new CreateIndexDescriptor(index)
                                  .Mappings(map => map.Map <ParentTestClass>(m =>
                                                                             m.Properties(p => p.Keyword(d => d.Name(n => n.Email))).AutoMap()));

            //ACT

            try
            {
                client.CreateIndex(indexDescriptor);

                Thread.Sleep(1000);

                //var indexState = client.GetIndex(index);
                var emailPropertyMetaData = indexManager.GetPropertyMetaData <ParentTestClass>(indexContext, x => x.Email);
                var childPropertyMetaData = indexManager.GetPropertyMetaData <ParentTestClass>(indexContext, x => x.Child);

                //ASSERT
                Assert.AreEqual("keyword", emailPropertyMetaData.Type.Name);
            }
            finally
            {
                client.DeleteIndex(index);
            }
        }
        public void GetIndexState_test()
        {
            //ARRANGE
            var id = Guid.NewGuid();

            var dependencyResolver = new DependencyResolverMock();

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

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

            var indexDescriptor = new CreateIndexDescriptor(index)
                                  .Mappings(map => map.Map <ParentTestClass>(m =>
                                                                             m.Properties(p => p.Keyword(d => d.Name(n => n.Email))).AutoMap()));

            //var index = indexManager.BuildIndexName(indexContext);
            //ACT

            try
            {
                client.CreateIndex(indexDescriptor);
                //indexManager.BuildIndex(index).Wait();
                Thread.Sleep(1000);

                var indexState       = indexManager.GetIndexState(indexContext);
                var providedName     = indexState.Settings["index.provided_name"];
                var propertiesMapped = indexState.Mappings.SelectMany(x => x.Value.Properties);
                //ASSERT
                Assert.AreEqual(indexName, providedName);
            }
            finally
            {
                client.DeleteIndex(index);
            }
        }
Beispiel #8
0
        public void GetNestedPropertyMetaData_test()
        {
            //ARRANGE
            var id = Guid.NewGuid();

            var dependencyResolver = new DependencyResolverMock();

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

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

            var indexDescriptor = new CreateIndexDescriptor(index)
                                  .Mappings(map => map.Map <ParentTestClass>(m =>
                                                                             m.Properties(p => p.Keyword(d => d.Name(n => n.Email))).AutoMap()));

            //ACT

            try
            {
                client.CreateIndex(indexDescriptor);

                Thread.Sleep(1000);

                //var childPropertyMetaData = indexManager.GetPropertyMetaData<ParentTestClass>(indexContext, x => x.Child.Name);
                var childPropertyMetaData = indexManager.GetPropertyMetaData <ParentTestClass>(indexContext, "ParentTestClass.Child.Name");
                //ASSERT
                Assert.AreEqual("text", childPropertyMetaData.Type.Name);
            }
            finally
            {
                client.DeleteIndex(index);
                //client.DeleteIndex(Indices.All);
            }
        }
Beispiel #9
0
        public void CreateDocumentTest()
        {
            //ARRANGE
            var indexId = Guid.NewGuid();

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

            var index = new IndexName {
                Name = String.Format("{0}_{1}", typeof(ParentTestClass).Name, indexId).ToLower(), Type = typeof(ParentTestClass)
            };

            var testClass = new ParentTestClass();

            testClass.Children.Add(new ChildClass());

            var documentPath = new DocumentPath <ParentTestClass>(testClass)
                               .Index(index)
                               .Type(typeof(ParentTestClass));

            var createRequest = new CreateRequest <ParentTestClass>(documentPath);

            //ACT
            try
            {
                var createResponse = client.Create(createRequest);
                Thread.Sleep(1000);

                var getResponse = client.Get <ParentTestClass>(documentPath);
                var document    = getResponse.Source;
                //ASSERT
                Assert.IsTrue(createResponse.IsValid);
                Assert.IsTrue(getResponse.IsValid);
                Assert.AreEqual(testClass.Id, document.Id);
                Assert.AreEqual(testClass.Children.Count, document.Children.Count);
                Assert.AreEqual(testClass.Children.ElementAt(0).Id, document.Children.ElementAt(0).Id);
            }
            finally
            {
                var deleteIndexResult = client.DeleteIndex(index);
            }
        }
        public void BuildIndexName_name_no_name_provided_test()
        {
            //ARRANGE
            var id = Guid.NewGuid();

            var dependencyResolver = new DependencyResolverMock();
            var clientFactory      = SearchClientFactory.GetClientFactory();

            var responseHandler = new ResponseHandler();
            var indexManager    = new IndexManager(dependencyResolver, clientFactory, responseHandler);
            var indexContext    = new IndexContext(typeof(ParentTestClass));

            //ACT

            var index = indexManager.BuildIndexName(indexContext);

            //ASSERT
            Assert.AreEqual(typeof(ParentTestClass).Name.ToLower(), index.Name);
            Assert.AreEqual(typeof(ParentTestClass), index.Type);
        }
        public void DeletIndex_test()
        {
            //ARRANGE
            var id = Guid.NewGuid();

            var dependencyResolver = new DependencyResolverMock();

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

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

            //ACT

            try
            {
                client.CreateIndex(index);
                Thread.Sleep(1000);
                var indexReqiest  = new GetIndexRequest(index);
                var indexResponse = client.GetIndex(indexReqiest);

                //delete the index
                indexManager.DeleteIndex(indexContext);

                var indexResponseAfterDelete = client.GetIndex(indexReqiest);
                //ASSERT
                Assert.IsTrue(indexResponse.IsValid);
                Assert.IsFalse(indexResponseAfterDelete.IsValid);
            }
            finally
            {
                client.DeleteIndex(index);
            }
        }
        public void CreateIndex_if_exist_test()
        {
            //ARRANGE
            var id = Guid.NewGuid();

            var dependencyResolver = new DependencyResolverMock();

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

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

            //ACT

            try
            {
                indexManager.BuildIndex(index).Wait();
                Thread.Sleep(1000);
                indexManager.BuildIndex(index).Wait();
                var indexReqiest  = new GetIndexRequest(index);
                var indexResponse = client.GetIndex(indexReqiest);
                var indices       = indexResponse.Indices.ToList();
                var first         = indices.First();
                var providedName  = first.Value.Settings["index.provided_name"];
                //ASSERT
                Assert.IsTrue(indexResponse.IsValid);
                Assert.AreEqual(1, indices.Count);
                Assert.AreEqual(indexName, providedName);
            }
            finally
            {
                client.DeleteIndex(index);
            }
        }
Beispiel #13
0
        public void UpdateDocument_load_update_commit_Test()
        {
            //ARRANGE

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

            var indexId = Guid.NewGuid();
            var index   = new IndexName {
                Name = String.Format("{0}_{1}", typeof(ParentTestClass).Name, indexId).ToLower(), Type = typeof(ParentTestClass)
            };

            var testClass = new ParentTestClass();

            testClass.Children.Add(new ChildClass());

            var documentPath = new DocumentPath <ParentTestClass>(testClass)
                               .Index(index)
                               .Type(typeof(ParentTestClass));

            //ACT
            try
            {
                //create a document
                var createRequest  = new CreateRequest <ParentTestClass>(documentPath);
                var createResponse = client.Create(createRequest);
                Thread.Sleep(1000);

                //load the created document and assign a property and commit
                var getResponse = client.Get <ParentTestClass>(documentPath);
                var loadedDoc   = getResponse.Source;
                var isChildNull = loadedDoc.Child == null;

                var childToAdd       = new ChildClass();
                var documentToUpdate = loadedDoc;
                documentToUpdate.Child = childToAdd;

                var updateDocumentPath = new DocumentPath <ParentTestClass>(documentToUpdate)
                                         .Index(index)
                                         .Type(typeof(ParentTestClass));

                var updateRequest = new UpdateRequest <ParentTestClass, ParentTestClass>(updateDocumentPath);
                updateRequest.Doc = loadedDoc;

                var updateResponse = client.Update <ParentTestClass>(updateDocumentPath, d => d.Doc(loadedDoc));

                var getResponseUpdated = client.Get <ParentTestClass>(updateDocumentPath);
                var updatedDocument    = getResponseUpdated.Source;

                //ASSERT
                Assert.IsTrue(createResponse.IsValid);
                Assert.IsTrue(getResponse.IsValid);
                Assert.AreEqual(testClass.Id, loadedDoc.Id);
                Assert.AreEqual(1, loadedDoc.Children.Count);
                Assert.IsTrue(isChildNull);
                Assert.AreEqual(testClass.Id, updatedDocument.Id);
                Assert.AreEqual(1, updatedDocument.Children.Count);
                Assert.NotNull(updatedDocument.Child);
                Assert.AreEqual(childToAdd.Id, updatedDocument.Child.Id);
            }
            finally
            {
                var deleteIndexResult = client.DeleteIndex(index);
            }
        }
Beispiel #14
0
        public void PartialUpdateDocument_by_script_remove_from_collection()
        {
            //ARRANGE

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

            //create a new index
            var indexId = Guid.NewGuid();
            var index   = new IndexName {
                Name = String.Format("{0}_{1}", typeof(ParentTestClass).Name, indexId).ToLower(), Type = typeof(ParentTestClass)
            };

            //create a parent class and add 2 children
            var testClass = new ParentTestClass();
            var child1    = new ChildClass();
            var child2    = new ChildClass();

            testClass.TextCollection.Add("T1");
            testClass.TextCollection.Add("T2");
            testClass.Children.Add(child1);
            testClass.Children.Add(child2);

            //create document path
            var documentPath = new DocumentPath <ParentTestClass>(testClass)
                               .Index(index)
                               .Type(typeof(ParentTestClass));

            //ACT
            try
            {
                //create a new parent class document
                var createRequest  = new CreateRequest <ParentTestClass>(documentPath);
                var createResponse = client.Create(createRequest);

                //wait to be indexed
                Thread.Sleep(1000);

                //load the created document
                var getResponse           = client.Get <ParentTestClass>(documentPath);
                var loadedDocAfterCreated = getResponse.Source;

                //create new new path on created parent id only
                var updateDocumentPath = new DocumentPath <ParentTestClass>(testClass.Id)
                                         .Index(index)
                                         .Type(typeof(ParentTestClass));

                var lang = "painless";

                //var scriptToUpdate = "ctx._source.children.remove(params.p)";
                var scriptToUpdate = "ctx._source.children.remove(ctx._source.children.indexOf(params.p))";
                var updateResponse = client.Update <ParentTestClass, dynamic>(updateDocumentPath,
                                                                              d => d.Script(des =>
                                                                                            des.Inline(scriptToUpdate)
                                                                                            .Lang(lang)
                                                                                            .Params(p => p.Add("p", child1))));

                //load updated document
                getResponse = client.Get <ParentTestClass>(updateDocumentPath);
                var loadedDocAfterUpdated = getResponse.Source;

                //ASSERT
                Assert.AreEqual(testClass.Id, loadedDocAfterCreated.Id);
                Assert.AreEqual(testClass.Id, loadedDocAfterUpdated.Id);
                Assert.AreEqual(2, loadedDocAfterCreated.Children.Count);
                Assert.AreEqual(1, loadedDocAfterUpdated.Children.Count);
                Assert.AreEqual(child2.Id, loadedDocAfterUpdated.Children.ElementAt(0).Id);
            }
            finally
            {
                var deleteIndexResult = client.DeleteIndex(index);
            }
        }
Beispiel #15
0
        public void PartialUpdateDocument_by_doc()
        {
            //ARRANGE

            //set up search client

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

            //create an index name
            var indexId = Guid.NewGuid();
            var index   = new IndexName {
                Name = String.Format("{0}_{1}", typeof(ParentTestClass).Name, indexId).ToLower(), Type = typeof(ParentTestClass)
            };

            //create a parent class and add 2 children
            var testClass = new ParentTestClass();
            var child1    = new ChildClass();
            var child2    = new ChildClass();

            testClass.Children.Add(child1);
            testClass.Children.Add(child2);

            //create document path
            var documentPath = new DocumentPath <ParentTestClass>(testClass)
                               .Index(index)
                               .Type(typeof(ParentTestClass));

            //ACT
            try
            {
                //create a new parent class document
                var createRequest  = new CreateRequest <ParentTestClass>(documentPath);
                var createResponse = client.Create(createRequest);
                //wait to be indexed
                Thread.Sleep(1000);

                //load the created document
                var getResponse           = client.Get <ParentTestClass>(documentPath);
                var loadedDocAfterCreated = getResponse.Source;

                //create new new path on created parent id only
                var updateDocumentPath = new DocumentPath <ParentTestClass>(testClass.Id)
                                         .Index(index)
                                         .Type(typeof(ParentTestClass));

                //create an anonymous type to update property child and update parent document
                var childToUpdate  = new ChildClass();
                var partialUpdate  = new { Child = childToUpdate };
                var updateResponse = client.Update <ParentTestClass, dynamic>(updateDocumentPath, d => d.Doc(partialUpdate));

                //load updated document
                getResponse = client.Get <ParentTestClass>(updateDocumentPath);
                var loadedDocAfterUpdated = getResponse.Source;

                //ASSERT
                Assert.AreEqual(testClass.Id, loadedDocAfterCreated.Id);
                Assert.AreEqual(testClass.Id, loadedDocAfterUpdated.Id);
                Assert.AreEqual(2, loadedDocAfterCreated.Children.Count);
                Assert.AreEqual(2, loadedDocAfterUpdated.Children.Count);
                Assert.Null(loadedDocAfterCreated.Child);
                Assert.NotNull(loadedDocAfterUpdated.Child);
                Assert.AreEqual(childToUpdate.Id, loadedDocAfterUpdated.Child.Id);
            }
            finally
            {
                var deleteIndexResult = client.DeleteIndex(index);
            }
        }
Beispiel #16
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);
            }
        }
        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);
            }
        }
        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);
            }
        }