コード例 #1
0
        public void Visit_WhenHasSortClauseButZeroSize_KustoQLOrderingIgnored()
        {
            var sortClause = JsonConvert.DeserializeObject <SortClause>(@"
                                {
                                    ""timestamp"": {
                                        ""order"": ""asc"",
                                        ""unmapped_type"": ""boolean""
                                    }
                                }");

            var dsl = new ElasticSearchDSL
            {
                Query = new Query
                {
                    Bool = new BoolQuery(),
                },
                IndexName = "someindex",
                Size      = 0,
                Sort      = new List <SortClause> {
                    sortClause
                },
            };

            CreateVisitorAndVisit(dsl);

            Assert.False(dsl.KustoQL.Contains("\n(_data | order by timestamp asc", StringComparison.OrdinalIgnoreCase));
        }
コード例 #2
0
        public void Visit_WhenHasAggregations_KustoQLShouldContainAggs()
        {
            var agg = JsonConvert.DeserializeObject <Aggregation>(@"
                {
                    ""date_histogram"": {
                        ""field"": ""timestamp"",
                        ""interval"": ""3h"",
                        ""time_zone"": ""Asia/Jerusalem"",
                        ""min_doc_count"": 1
                    }
                }");

            var dsl = new ElasticSearchDSL
            {
                Query = new Query
                {
                    Bool = new BoolQuery(),
                },
                IndexName    = "someindex",
                Aggregations = new Dictionary <string, Aggregation>()
                {
                    { "2", agg }
                },
            };

            CreateVisitorAndVisit(dsl);

            Assert.True(dsl.KustoQL.Contains("_data | summarize count() by timestamp = bin(timestamp, 3h)\n", StringComparison.OrdinalIgnoreCase));
        }
コード例 #3
0
        private void CreateVisitorAndVisit(ElasticSearchDSL elasticSearchDSL, string dbName = "")
        {
            var visitor =
                new ElasticSearchDSLVisitor(
                    SchemaRetrieverMock.CreateMockSchemaRetriever(), dbName);

            visitor.Visit(elasticSearchDSL);
        }
コード例 #4
0
        /// <summary>
        /// A helpler method to add the default DSL to visitor in order to
        /// Have the ISchemaRetriever initialised by the default visitor.
        /// </summary>
        /// <param name="visitor"></param>
        internal static void VisitRootDsl(ElasticSearchDSLVisitor visitor)
        {
            var dsl = new ElasticSearchDSL
            {
                Query = new Query
                {
                    Bool = new BoolQuery(),
                },
                IndexName = "someindex",
            };

            visitor.Visit(dsl);
        }
コード例 #5
0
        public void Visit_CreatedWithDefaultDBName_UsesDefaultDBInQuery()
        {
            var dsl = new ElasticSearchDSL
            {
                Query = new Query
                {
                    Bool = new BoolQuery(),
                },
                IndexName = "someindex",
            };

            CreateVisitorAndVisit(dsl, "defaultDBName");

            Assert.True(dsl.KustoQL.Contains("\nlet _data = database(\"defaultDBName\").someindex", StringComparison.OrdinalIgnoreCase));
        }
コード例 #6
0
        public void Visit_CreatedWithEmptyQuery_ConvertsToEmptyString()
        {
            var dsl = new ElasticSearchDSL
            {
                Query = new Query
                {
                    Bool = new BoolQuery(),
                },
                IndexName = "someindex",
            };

            CreateVisitorAndVisit(dsl, "defaultDBName");

            Assert.AreEqual(dsl.KustoQL, "let fromUnixTimeMilli = (t:long) {datetime(1970 - 01 - 01) + t * 1millisec};\nlet _data = database(\"defaultDBName\").someindex " + string.Empty + ";\n(_data | limit 0 | as hits)");
        }
コード例 #7
0
        public void Visit_WhenHasNullAggregations_KustoQLContainNoAggs()
        {
            var dsl = new ElasticSearchDSL
            {
                Query = new Query
                {
                    Bool = new BoolQuery(),
                },
                IndexName = "someindex",
            };

            CreateVisitorAndVisit(dsl);

            Assert.False(dsl.KustoQL.Contains("| as aggs);", StringComparison.OrdinalIgnoreCase));
            Assert.False(dsl.KustoQL.Contains("summarize);", StringComparison.OrdinalIgnoreCase));
        }
コード例 #8
0
        public void Visit_WithRandomSizeValue_KustoQLContainsLimitAndHits()
        {
            var dsl = new ElasticSearchDSL
            {
                Query = new Query
                {
                    Bool = new BoolQuery(),
                },
                IndexName = "someindex",
                Size      = 17,
                Sort      = new List <SortClause>(),
            };

            CreateVisitorAndVisit(dsl);

            Assert.True(dsl.KustoQL.Contains("limit 17 | as hits", StringComparison.OrdinalIgnoreCase));
        }
コード例 #9
0
        public string Visit_WithStringFieldType_GeneratesQueryWithHas()
        {
            var queryClause = CreateQueryStringClause("dayOfWeek:1", false);
            var dsl         = new ElasticSearchDSL
            {
                Query = new Query
                {
                    Bool = new BoolQuery
                    {
                        Must = new List <IQuery> {
                            queryClause
                        },
                    },
                },
                IndexName = "myindex",
            };

            var visitor =
                new ElasticSearchDSLVisitor(
                    SchemaRetrieverMock.CreateMockSchemaRetriever());

            visitor.Visit(dsl);
            return(dsl.KustoQL);
        }
コード例 #10
0
        public string Visit_WithGreaterThanExpression_ExpectedResults()
        {
            var queryClause = CreateQueryStringClause("dayOfWeek:>2", false);
            var dsl         = new ElasticSearchDSL
            {
                Query = new Query
                {
                    Bool = new BoolQuery
                    {
                        Must = new List <IQuery> {
                            queryClause
                        },
                    },
                },
                IndexName = "myindex",
            };

            var visitor =
                new ElasticSearchDSLVisitor(
                    SchemaRetrieverMock.CreateMockNumericSchemaRetriever());

            visitor.Visit(dsl);
            return(dsl.KustoQL);
        }
コード例 #11
0
        /// <inheritdoc/>
        public void Visit(ElasticSearchDSL elasticSearchDSL)
        {
            Ensure.IsNotNull(elasticSearchDSL, nameof(elasticSearchDSL));

            // Preparing the schema with the index name to be used later
            schemaRetriever = schemaRetrieverFactory.Make(elasticSearchDSL.IndexName);

            // base query
            elasticSearchDSL.Query.Accept(this);

            var queryStringBuilder = new StringBuilder();

            var(databaseName, tableName) = KustoDatabaseTableNames.FromElasticIndexName(elasticSearchDSL.IndexName, defaultDatabaseName);

            // when an index-pattern doesn't have a default time filter the query element can be empty
            var translatedQueryExpression = !string.IsNullOrEmpty(elasticSearchDSL.Query.KustoQL) ? $"| {elasticSearchDSL.Query.KustoQL}" : string.Empty;

            // Aggregations
            if (elasticSearchDSL.Query.Bool != null)
            {
                queryStringBuilder.Append($"{KustoQLOperators.Let} _data = database(\"{databaseName}\").{tableName} {translatedQueryExpression};");

                // Aggregations
                if (elasticSearchDSL.Aggregations?.Count > 0)
                {
                    queryStringBuilder.Append('\n').Append($"(_data | {KustoQLOperators.Summarize} ");

                    foreach (var(_, aggregation) in elasticSearchDSL.Aggregations)
                    {
                        aggregation.Accept(this);
                        queryStringBuilder.Append($"{aggregation.KustoQL} ");
                    }

                    queryStringBuilder.Append("| as aggs);");
                }

                // hits (projections...)
                // The size is deserialized property
                // therefore we check 'Size >= 0' to protect the query.
                if (elasticSearchDSL.Size >= 0)
                {
                    queryStringBuilder.Append("\n(_data ");

                    if (elasticSearchDSL.Size > 0)
                    {
                        // we only need to sort if we're returning hits
                        var orderingList = new List <string>();

                        foreach (var sortClause in elasticSearchDSL.Sort)
                        {
                            sortClause.Accept(this);
                            if (!string.IsNullOrEmpty(sortClause.KustoQL))
                            {
                                orderingList.Add(sortClause.KustoQL);
                            }
                        }

                        if (orderingList.Count > 0)
                        {
                            queryStringBuilder.Append($"| {KustoQLOperators.OrderBy} {string.Join(", ", orderingList)} ");
                        }
                    }

                    queryStringBuilder.Append($"| {KustoQLOperators.Limit} {elasticSearchDSL.Size} | as hits)");
                }
            }
            else
            {
                // ViewSingleDocument query
                queryStringBuilder.Append($"database(\"{databaseName}\").{tableName} {translatedQueryExpression} | as hits;");
            }

            elasticSearchDSL.KustoQL = queryStringBuilder.ToString();
        }