public void TestDefaultContextParentWithACollectionOfThreeChildObjectsOfNestedType()
        {
            var testSkillParentObject = new NestedCollectionTest
            {
                CreatedSkillParent     = DateTime.UtcNow,
                UpdatedSkillParent     = DateTime.UtcNow,
                DescriptionSkillParent = "A test entity description",
                Id = 8,
                NameSkillParent = "cool",
                SkillChildren   = new Collection <SkillChild> {
                    _entitiesForSkillChild[0], _entitiesForSkillChild[1], _entitiesForSkillChild[2]
                }
            };

            using (var context = new ElasticsearchContext(ConnectionString, _elasticsearchMappingResolver))
            {
                context.TraceProvider = new ConsoleTraceProvider();
                context.IndexCreate <NestedCollectionTest>();
                Thread.Sleep(1200);
                context.AddUpdateDocument(testSkillParentObject, testSkillParentObject.Id);

                // Save to Elasticsearch
                var ret = context.SaveChanges();
                Assert.Equal(ret.Status, HttpStatusCode.OK);

                var roundTripResult = context.GetDocument <NestedCollectionTest>(testSkillParentObject.Id);
                Assert.Equal(roundTripResult.DescriptionSkillParent, testSkillParentObject.DescriptionSkillParent);
                Assert.Equal(roundTripResult.SkillChildren.First().DescriptionSkillChild, testSkillParentObject.SkillChildren.First().DescriptionSkillChild);
                Assert.Equal(roundTripResult.SkillChildren.ToList()[1].DescriptionSkillChild, testSkillParentObject.SkillChildren.ToList()[1].DescriptionSkillChild);
                Assert.Equal(roundTripResult.SkillChildren.ToList()[1].DescriptionSkillChild, testSkillParentObject.SkillChildren.ToList()[1].DescriptionSkillChild);
            }
        }
        public void SearchAggNestedBucketAggregationWithSubAggMaxMetricWithNoHits()
        {
            var testSkillParentObject = new NestedCollectionTest
            {
                CreatedSkillParent     = DateTime.UtcNow,
                UpdatedSkillParent     = DateTime.UtcNow,
                DescriptionSkillParent = "A test entity description",
                Id = 8,
                NameSkillParent = "cool",
                SkillChildren   = new Collection <SkillChild> {
                    _entitiesForSkillChild[0], _entitiesForSkillChild[1], _entitiesForSkillChild[2]
                }
            };

            using (var context = new ElasticsearchContext(ConnectionString, _elasticsearchMappingResolver))
            {
                context.IndexCreate <NestedCollectionTest>();
                Thread.Sleep(1200);
                context.AddUpdateDocument(testSkillParentObject, testSkillParentObject.Id);
                var ret = context.SaveChanges();
                Assert.Equal(ret.Status, HttpStatusCode.OK);
            }

            Thread.Sleep(1200);

            var search = new Search
            {
                Aggs = new List <IAggs>
                {
                    new NestedBucketAggregation("nestedagg", "skillchildren")
                    {
                        Aggs = new List <IAggs>
                        {
                            new MaxMetricAggregation("test", "skillchildren.updatedskillchild")
                        }
                    }
                }
            };

            using (var context = new ElasticsearchContext(ConnectionString, _elasticsearchMappingResolver))
            {
                Assert.True(context.IndexTypeExists <NestedCollectionTest>());
                var items = context.Search <NestedCollectionTest>(search, new SearchUrlParameters {
                    SeachType = SeachType.count
                });
                var aggResult = items.PayloadResult.Aggregations.GetComplexValue <NestedBucketAggregationsResult>("nestedagg");
                var max       = aggResult.GetSingleMetricSubAggregationValue <long>("test");
                Assert.Equal(3, aggResult.DocCount);
                Assert.InRange(max, 1, 1553210851080);
            }
        }
        public void SearchFilterNestedQueryInnerHits()
        {
            var testSkillParentObject = new NestedCollectionTest
            {
                CreatedSkillParent     = DateTime.UtcNow,
                UpdatedSkillParent     = DateTime.UtcNow,
                DescriptionSkillParent = "A test entity description",
                Id = 8,
                NameSkillParent = "cool",
                SkillChildren   = new Collection <SkillChild> {
                    _entitiesForSkillChild[0], _entitiesForSkillChild[1], _entitiesForSkillChild[2]
                }
            };

            using (var context = new ElasticsearchContext(ConnectionString, _elasticsearchMappingResolver))
            {
                context.IndexCreate <NestedCollectionTest>();
                Thread.Sleep(1200);
                context.AddUpdateDocument(testSkillParentObject, testSkillParentObject.Id);
                var ret = context.SaveChanges();
                Assert.Equal(ret.Status, HttpStatusCode.OK);
            }

            Thread.Sleep(1200);
            var search = new Search
            {
                Query =
                    new Query(
                        new NestedQuery(
                            new MatchAllQuery(), "skillchildren")
                {
                    InnerHits = new InnerHits()
                })
            };

            using (var context = new ElasticsearchContext(ConnectionString, _elasticsearchMappingResolver))
            {
                Assert.True(context.IndexTypeExists <NestedCollectionTest>());
                var items = context.Search <NestedCollectionTest>(search);
                Assert.Equal(8, items.PayloadResult.Hits.HitsResult[0].Source.Id);
            }
        }
        public void SearchAggNestedBucketAggregationWithNoHits()
        {
            var testSkillParentObject = new NestedCollectionTest
            {
                CreatedSkillParent = DateTime.UtcNow,
                UpdatedSkillParent = DateTime.UtcNow,
                DescriptionSkillParent = "A test entity description",
                Id = 8,
                NameSkillParent = "cool",
                SkillChildren = new Collection<SkillChild> { _entitiesForSkillChild[0], _entitiesForSkillChild[1], _entitiesForSkillChild[2] }
            };

            using (var context = new ElasticsearchContext(ConnectionString, _elasticsearchMappingResolver))
            {
                context.IndexCreate<NestedCollectionTest>();
                Thread.Sleep(1200);
                context.AddUpdateDocument(testSkillParentObject, testSkillParentObject.Id);
                var ret = context.SaveChanges();
                Assert.AreEqual(ret.Status, HttpStatusCode.OK);
            }

            Thread.Sleep(1200);

            var search = new Search
            {
                Aggs = new List<IAggs>
                {
                    new NestedBucketAggregation("nestedagg", "skillchildren")
                }
            };

            using (var context = new ElasticsearchContext(ConnectionString, _elasticsearchMappingResolver))
            {
                Assert.IsTrue(context.IndexTypeExists<NestedCollectionTest>());
                var items = context.Search<NestedCollectionTest>(search, new SearchUrlParameters { SeachType = SeachType.count });
                var aggResult = items.PayloadResult.Aggregations.GetComplexValue<NestedBucketAggregationsResult>("nestedagg");
                Assert.AreEqual(3, aggResult.DocCount);
            }
        }
        public void TestDefaultContextParentWithACollectionOfThreeChildObjectsOfNestedType()
        {
            var testSkillParentObject = new NestedCollectionTest
            {
                CreatedSkillParent = DateTime.UtcNow,
                UpdatedSkillParent = DateTime.UtcNow,
                DescriptionSkillParent = "A test entity description",
                Id = 8,
                NameSkillParent = "cool",
                SkillChildren = new Collection<SkillChild> { _entitiesForSkillChild[0], _entitiesForSkillChild[1], _entitiesForSkillChild[2] }
            };

            using (var context = new ElasticsearchContext(ConnectionString, _elasticsearchMappingResolver))
            {
                context.TraceProvider = new ConsoleTraceProvider();
                context.IndexCreate<NestedCollectionTest>();
                Thread.Sleep(1200);
                context.AddUpdateDocument(testSkillParentObject, testSkillParentObject.Id);

                // Save to Elasticsearch
                var ret = context.SaveChanges();
                Assert.AreEqual(ret.Status, HttpStatusCode.OK);

                var roundTripResult = context.GetDocument<NestedCollectionTest>(testSkillParentObject.Id);
                Assert.AreEqual(roundTripResult.DescriptionSkillParent, testSkillParentObject.DescriptionSkillParent);
                Assert.AreEqual(roundTripResult.SkillChildren.First().DescriptionSkillChild, testSkillParentObject.SkillChildren.First().DescriptionSkillChild);
                Assert.AreEqual(roundTripResult.SkillChildren.ToList()[1].DescriptionSkillChild, testSkillParentObject.SkillChildren.ToList()[1].DescriptionSkillChild);
                Assert.AreEqual(roundTripResult.SkillChildren.ToList()[1].DescriptionSkillChild, testSkillParentObject.SkillChildren.ToList()[1].DescriptionSkillChild);
            }
        }
        public void SearchFilterNestedQueryInnerHits()
        {
            var testSkillParentObject = new NestedCollectionTest
            {
                CreatedSkillParent = DateTime.UtcNow,
                UpdatedSkillParent = DateTime.UtcNow,
                DescriptionSkillParent = "A test entity description",
                Id = 8,
                NameSkillParent = "cool",
                SkillChildren = new Collection<SkillChild> { _entitiesForSkillChild[0], _entitiesForSkillChild[1], _entitiesForSkillChild[2] }
            };

            using (var context = new ElasticsearchContext(ConnectionString, _elasticsearchMappingResolver))
            {
                context.IndexCreate<NestedCollectionTest>();
                Thread.Sleep(1200);
                context.AddUpdateDocument(testSkillParentObject, testSkillParentObject.Id);
                var ret = context.SaveChanges();
                Assert.AreEqual(ret.Status, HttpStatusCode.OK);
            }

            Thread.Sleep(1200);
            var search = new Search
            {
                Query =
                    new Query(
                    new NestedQuery(
                    new MatchAllQuery(), "skillchildren")
                    {
                        InnerHits = new InnerHits()
                    })
            };

            using (var context = new ElasticsearchContext(ConnectionString, _elasticsearchMappingResolver))
            {
                Assert.IsTrue(context.IndexTypeExists<NestedCollectionTest>());
                var items = context.Search<NestedCollectionTest>(search);
                Assert.AreEqual(8, items.PayloadResult.Hits.HitsResult[0].Source.Id);
            }
        }
        public void SearchAggNestedBucketAggregationWithSubReverseNestedWithNoHits()
        {
            var testSkillParentObject = new NestedCollectionTest
            {
                CreatedSkillParent = DateTime.UtcNow,
                UpdatedSkillParent = DateTime.UtcNow,
                DescriptionSkillParent = "A test entity description",
                Id = 8,
                NameSkillParent = "cool",
                SkillChildren = new Collection<SkillChild> { _entitiesForSkillChild[0], _entitiesForSkillChild[1], _entitiesForSkillChild[2] }
            };

            using (var context = new ElasticsearchContext(ConnectionString, _elasticsearchMappingResolver))
            {
                context.IndexCreate<NestedCollectionTest>();
                Thread.Sleep(1200);
                context.AddUpdateDocument(testSkillParentObject, testSkillParentObject.Id);
                var ret = context.SaveChanges();
                Assert.AreEqual(ret.Status, HttpStatusCode.OK);
            }

            Thread.Sleep(1200);

            var search = new Search
            {
                Aggs = new List<IAggs>
                {
                    new NestedBucketAggregation("nestedagg", "skillchildren")
                    {
                        Aggs = new List<IAggs>
                        {
                            new MaxMetricAggregation("test", "skillchildren.updatedskillchild"),
                            new ReverseNestedBucketAggregation("goingUp")
                            {
                                Aggs = new List<IAggs>
                                {
                                    new TermsBucketAggregation("termParent", "descriptionskillparent")
                                }
                            }
                        }
                    }
                }
            };

            using (var context = new ElasticsearchContext(ConnectionString, _elasticsearchMappingResolver))
            {
                Assert.IsTrue(context.IndexTypeExists<NestedCollectionTest>());
                var items = context.Search<NestedCollectionTest>(search, new SearchUrlParameters { SeachType = SeachType.count });
                var aggResult = items.PayloadResult.Aggregations.GetComplexValue<NestedBucketAggregationsResult>("nestedagg");
                var max = aggResult.GetSingleMetricSubAggregationValue<long>("test");
                var nesteTestResult = aggResult.GetSubAggregationsFromJTokenName<ReverseNestedBucketAggregationsResult>("goingUp");
                var termParentAgg = nesteTestResult.GetSubAggregationsFromJTokenName<TermsBucketAggregationsResult>("termParent");

                Assert.AreEqual(3, aggResult.DocCount);
                Assert.Greater(max, 1423210851080);
                Assert.AreEqual(1, nesteTestResult.DocCount);
                Assert.AreEqual("a", termParentAgg.Buckets[0].Key);
            }
        }