Esempio n. 1
0
        public virtual int GetNumberOfDocumentsStored(string indexName)
        {
            var response = Client.Count <DynamicResponse>(indexName, null);

            if (!response.Success)
            {
                return(0);
            }

            return(response.Body.count);
        }
        /// <summary>
        /// Get number of items that matches filter.
        /// </summary>
        /// <param name="type">Type name.</param>
        /// <param name="filter">Filter for object of specified type.</param>
        /// <returns>Number of items that matches filter.</returns>
        protected DocumentCountResponse GetCount(string type,
                                                 string filter)
        {
            var documentCountResponse = new DocumentCountResponse();

            var response = _client.Count <ElasticsearchCountResponse>(IndexName, type, filter);

            CheckResponse(response);

            if (response.Body.IsNotNull())
            {
                documentCountResponse.ShardTotalCount      = response.Body._shards.total;
                documentCountResponse.ShardSuccessfulCount = response.Body._shards.successful;
                documentCountResponse.ShardFailedCount     = response.Body._shards.failed;
                documentCountResponse.DocumentCount        = response.Body.count;
            }

            return(documentCountResponse);
        }
Esempio n. 3
0
        public void Integration()
        {
            var builder = new ContainerBuilder();

            builder.RegisterModule(new RootModule(@"Files\Shorthand.xml"));
            var container = builder.Build();

            var pool     = new SingleNodeConnectionPool(new Uri(ElasticConnection.Url));
            var settings = new ConnectionConfiguration(pool);
            var client   = new ElasticLowLevelClient(settings);

            // CORRECT DATA AND INITIAL LOAD
            using (var cn = new SqlServerConnectionFactory(InputConnection).GetConnection()) {
                cn.Open();
                Assert.AreEqual(3, cn.Execute(@"
                    UPDATE [Order Details] SET UnitPrice = 14.40, Quantity = 42 WHERE OrderId = 10253 AND ProductId = 39;
                    UPDATE Orders SET CustomerID = 'CHOPS', Freight = 22.98 WHERE OrderId = 10254;
                    UPDATE Customers SET ContactName = 'Palle Ibsen' WHERE CustomerID = 'VAFFE';
                "));
            }

            var root        = ResolveRoot(container, SqlTestFile, true);
            var responseSql = new PipelineAction(root).Execute();

            Assert.AreEqual(200, responseSql.Code);

            root = ResolveRoot(container, ElasticTestFile, true);
            var responseElastic = new PipelineAction(root).Execute();

            Assert.AreEqual(200, responseElastic.Code);

            Assert.AreEqual(2155, client.Count <DynamicResponse>("northwind", "star", "{\"query\" : { \"match_all\" : { }}}").Body["count"].Value);

            // FIRST DELTA, NO CHANGES
            root            = ResolveRoot(container, ElasticTestFile, false);
            responseElastic = new PipelineAction(root).Execute();
            Assert.AreEqual(200, responseElastic.Code);
            Assert.AreEqual(string.Empty, responseElastic.Content);

            Assert.AreEqual(2155, client.Count <DynamicResponse>("northwind", "star", "{\"query\" : { \"match_all\" : { }}}").Body["count"].Value);

            // CHANGE 2 FIELDS IN 1 RECORD IN MASTER TABLE THAT WILL CAUSE CALCULATED FIELD TO BE UPDATED TOO
            using (var cn = new SqlServerConnectionFactory(InputConnection).GetConnection()) {
                cn.Open();
                const string sql = @"UPDATE [Order Details] SET UnitPrice = 15, Quantity = 40 WHERE OrderId = 10253 AND ProductId = 39;";
                Assert.AreEqual(1, cn.Execute(sql));
            }

            // RUN AND CHECK SQL
            root        = ResolveRoot(container, SqlTestFile, false);
            responseSql = new PipelineAction(root).Execute();
            Assert.AreEqual(200, responseSql.Code);
            Assert.AreEqual(string.Empty, responseSql.Content);

            using (var cn = new SqlServerConnectionFactory(OutputConnection).GetConnection()) {
                cn.Open();
                Assert.AreEqual(1, cn.ExecuteScalar <int>("SELECT TOP 1 Updates FROM NorthWindControl WHERE Entity = 'Order Details' AND BatchId = 9;"));
                Assert.AreEqual(15.0, cn.ExecuteScalar <decimal>("SELECT OrderDetailsUnitPrice FROM NorthWindStar WHERE OrderDetailsOrderId= 10253 AND OrderDetailsProductId = 39;"));
                Assert.AreEqual(40, cn.ExecuteScalar <int>("SELECT OrderDetailsQuantity FROM NorthWindStar WHERE OrderDetailsOrderId= 10253 AND OrderDetailsProductId = 39;"));
                Assert.AreEqual(15.0 * 40, cn.ExecuteScalar <int>("SELECT OrderDetailsExtendedPrice FROM NorthWindStar WHERE OrderDetailsOrderId= 10253 AND OrderDetailsProductId = 39;"));
            }

            // RUN AND CHECK ELASTIC
            root            = ResolveRoot(container, ElasticTestFile, false);
            responseElastic = new PipelineAction(root).Execute();
            Assert.AreEqual(200, responseElastic.Code);
            Assert.AreEqual(string.Empty, responseElastic.Content);

            var response = client.Search <DynamicResponse>(
                "northwind",
                "star", @"{
   ""query"" : {
      ""constant_score"" : { 
         ""filter"" : {
            ""bool"" : {
              ""must"" : [
                 { ""term"" : {""orderdetailsorderid"" : 10253}}, 
                 { ""term"" : {""orderdetailsproductid"" : 39}} 
              ]
           }
         }
      }
   }
}");

            var hits   = (response.Body["hits"]["hits"] as ElasticsearchDynamicValue).Value as IList <object>;
            var hit    = hits[0] as IDictionary <string, object>;
            var source = hit["_source"] as IDictionary <string, object>;

            Assert.AreEqual(source["orderdetailsunitprice"], 15.0);
            Assert.AreEqual(source["orderdetailsquantity"], 40);
            Assert.AreEqual(source["orderdetailsextendedprice"], 40 * 15.0);

            // CHANGE 1 RECORD'S CUSTOMERID AND FREIGHT ON ORDERS TABLE
            using (var cn = new SqlServerConnectionFactory(InputConnection).GetConnection()) {
                cn.Open();
                Assert.AreEqual(1, cn.Execute("UPDATE Orders SET CustomerID = 'VICTE', Freight = 20.11 WHERE OrderId = 10254;"));
            }

            // RUN AND CHECK SQL
            root        = ResolveRoot(container, SqlTestFile, false);
            responseSql = new PipelineAction(root).Execute();

            Assert.AreEqual(200, responseSql.Code);
            Assert.AreEqual(string.Empty, responseSql.Content);

            using (var cn = new SqlServerConnectionFactory(OutputConnection).GetConnection()) {
                cn.Open();
                Assert.AreEqual(1, cn.ExecuteScalar <int>("SELECT Updates FROM NorthWindControl WHERE Entity = 'Orders' AND BatchId = 18;"));
                Assert.AreEqual("VICTE", cn.ExecuteScalar <string>("SELECT OrdersCustomerId FROM NorthWindStar WHERE OrderDetailsOrderId= 10254;"));
                Assert.AreEqual(20.11, cn.ExecuteScalar <decimal>("SELECT OrdersFreight FROM NorthWindStar WHERE OrderDetailsOrderId= 10254;"));
            }

            // RUN AND CHECK ELASTIC
            root            = ResolveRoot(container, ElasticTestFile, false);
            responseElastic = new PipelineAction(root).Execute();
            Assert.AreEqual(200, responseElastic.Code);
            Assert.AreEqual(string.Empty, responseElastic.Content);

            response = client.Search <DynamicResponse>(
                "northwind",
                "star",
                @"{
   ""query"" : {
      ""constant_score"" : { 
         ""filter"" : {
            ""bool"" : {
              ""must"" : [
                 { ""term"" : {""orderdetailsorderid"" : 10254}}
              ]
           }
         }
      }
   }
}");

            hits   = (response.Body["hits"]["hits"] as ElasticsearchDynamicValue).Value as IList <object>;
            hit    = hits[0] as IDictionary <string, object>;
            source = hit["_source"] as IDictionary <string, object>;

            Assert.AreEqual(source["orderscustomerid"], "VICTE");
            Assert.AreEqual(source["ordersfreight"], 20.11);
        }