public void UrlPathContainsIndexSpecifier()
        {
            const string expectedIndex = "myIndex";
            var indexConnection = new ElasticConnection(defaultConnection.Endpoint, index: expectedIndex);
            var formatter = new PostBodyRequestFormatter(indexConnection, mapping, new ElasticSearchRequest { Type = "type1" });

            Assert.Contains(expectedIndex, formatter.Uri.AbsolutePath);
        }
        public void BodyContainsFilterTerm()
        {
            var termCriteria = TermsCriteria.Build(TermsExecutionMode.@bool, "term1", memberInfo, "singlecriteria");

            var formatter = new PostBodyRequestFormatter(defaultConnection, mapping, new ElasticSearchRequest { Type = "type1", Filter = termCriteria });
            var body = JObject.Parse(formatter.Body);

            var result = TraverseWithAssert(body, "filter", "term");
            Assert.Equal(1, result.Count());
            Assert.Equal("!!! singlecriteria !!!", result[termCriteria.Field].ToString());
            Assert.Null(result["execution"]);  // Only applicable to "terms" filters
        }
        public void BodyContainsFilterTerms()
        {
            var termCriteria = TermsCriteria.Build("term1", memberInfo, "criteria1", "criteria2");

            var formatter = new PostBodyRequestFormatter(defaultConnection, mapping, new ElasticSearchRequest { Type = "type1", Filter = termCriteria });
            var body = JObject.Parse(formatter.Body);

            var result = TraverseWithAssert(body, "filter", "terms");
            var actualTerms = TraverseWithAssert(result, termCriteria.Field);
            foreach (var criteria in termCriteria.Values)
                Assert.Contains("!!! " + criteria + " !!!", actualTerms.Select(t => t.ToString()).ToArray());
            Assert.Null(result["execution"]);
        }
        public Task<ElasticResponse> SearchAsync(ElasticSearchRequest searchRequest)
        {
            var formatter = new PostBodyRequestFormatter(connection, mapping, searchRequest);
            log.Debug(null, null, "Request: POST {0}", formatter.Uri);
            log.Debug(null, null, "Body: {0}", formatter.Body);

            return retryPolicy.ExecuteAsync(
                async () =>
                {
                    using (var requestMessage = new HttpRequestMessage(HttpMethod.Post, formatter.Uri) { Content = new StringContent(formatter.Body) })
                    using (var response = await SendRequestAsync(connection.HttpClient, requestMessage))
                    using (var responseStream = await response.Content.ReadAsStreamAsync())
                        return ParseResponse(responseStream, log);
                },
                (response, exception) => exception is TaskCanceledException,
                (response, additionalInfo) =>
                {
                    additionalInfo["index"] = connection.Index;
                    additionalInfo["query"] = formatter.Body;
                });
        }
 public void ParseThrowsInvalidOperationForUnknownCriteriaTypes()
 {
     var formatter = new PostBodyRequestFormatter(defaultConnection, mapping, new ElasticSearchRequest { Type = "type1", Query = new FakeCriteria() });
     Assert.Throws<InvalidOperationException>(() => JObject.Parse(formatter.Body));
 }
        public void BodyIsValidJsonFormattedResponse()
        {
            var formatter = new PostBodyRequestFormatter(defaultConnection, mapping, new ElasticSearchRequest { Type = "type1" });

            Assert.DoesNotThrow(() => JObject.Parse(formatter.Body));
        }
        public void BodyDoesNotContainTimeoutWhenZero()
        {
            var connection = new ElasticConnection(new Uri("http://localhost/"), timeout: TimeSpan.Zero);

            var formatter = new PostBodyRequestFormatter(connection, mapping, new ElasticSearchRequest());
            var body = JObject.Parse(formatter.Body);

            var result = body["timeout"];
            Assert.Null(result);
        }
        public void BodyContainsFilterOr()
        {
            var minCriteria = new RangeCriteria("minField", memberInfo, RangeComparison.GreaterThanOrEqual, 100);
            var maxCriteria = new RangeCriteria("maxField", memberInfo, RangeComparison.LessThan, 32768);
            var orCriteria = OrCriteria.Combine(minCriteria, maxCriteria);

            var formatter = new PostBodyRequestFormatter(defaultConnection, mapping, new ElasticSearchRequest { Type = "type1", Filter = orCriteria });
            var body = JObject.Parse(formatter.Body);

            var result = TraverseWithAssert(body, "filter", "or");
            Assert.Equal(2, result.Children().Count());
            foreach (var child in result)
                Assert.True(((JProperty)(child.First)).Name == "range");
        }
        public void BodyContainsSizeWhenSpecified()
        {
            const int expectedSize = 4096;

            var formatter = new PostBodyRequestFormatter(defaultConnection, mapping, new ElasticSearchRequest { Type = "type1", Size = expectedSize });
            var body = JObject.Parse(formatter.Body);

            var result = TraverseWithAssert(body, "size");
            Assert.Equal(expectedSize, result);
        }
        public void BodyContainsTimeoutWhenSpecified()
        {
            const string expectedTimeout = "15s";
            var connection = new ElasticConnection(new Uri("http://localhost/"), timeout: TimeSpan.FromSeconds(15));

            var formatter = new PostBodyRequestFormatter(connection, mapping, new ElasticSearchRequest());
            var body = JObject.Parse(formatter.Body);

            var result = TraverseWithAssert(body, "timeout");
            Assert.Equal(expectedTimeout, result);
        }
        public void UrlPathContainsTypeSpecifier()
        {
            var formatter = new PostBodyRequestFormatter(defaultConnection, mapping, new ElasticSearchRequest { Type = "type1" });

            Assert.Contains("type1", formatter.Uri.AbsolutePath);
        }
        public void BodyContainsPrefixFilter()
        {
            const string expectedField = "motor";
            const string expectedPrefix = "SR20";
            var prefixCriteria = new PrefixCriteria(expectedField, expectedPrefix);

            var formatter = new PostBodyRequestFormatter(defaultConnection, mapping, new ElasticSearchRequest { Type = "type1", Filter = prefixCriteria });
            var body = JObject.Parse(formatter.Body);

            var actualRegexp = TraverseWithAssert(body, "filter", "prefix", expectedField);
            Assert.Equal(expectedPrefix, actualRegexp);
        }
        public void BodyContainsSortOptions()
        {
            var expectedSortOptions = new List<SortOption> { new SortOption("first", true), new SortOption("second", false), new SortOption("third", false, true) };

            var formatter = new PostBodyRequestFormatter(defaultConnection, mapping, new ElasticSearchRequest { Type = "type1", SortOptions = expectedSortOptions });
            var body = JObject.Parse(formatter.Body);

            var result = TraverseWithAssert(body, "sort");
            for (var i = 0; i < expectedSortOptions.Count; i++)
            {
                var actualSort = result[i];
                var desiredSort = expectedSortOptions[i];
                if (desiredSort.IgnoreUnmapped)
                {
                    var first = (JProperty)actualSort.First;
                    Assert.Equal(desiredSort.Name, first.Name);
                    var childProperties = first.First.Children().Cast<JProperty>().ToArray();
                    Assert.Single(childProperties, f => f.Name == "ignore_unmapped" && f.Value.ToObject<bool>());
                    Assert.Single(childProperties, f => f.Name == "order" && f.Value.ToObject<string>() == "desc");
                }
                else
                {
                    if (desiredSort.Ascending)
                    {
                        Assert.Equal(desiredSort.Name, actualSort);
                    }
                    else
                    {
                        var finalActualSort = actualSort[desiredSort.Name];
                        Assert.NotNull(finalActualSort);
                        Assert.Equal("desc", finalActualSort.ToString());
                    }
                }
            }
        }
        public void BodyContainsFilterMissing()
        {
            const string expectedFieldName = "fieldShouldBeMissing";
            var termCriteria = new MissingCriteria(expectedFieldName);

            var formatter = new PostBodyRequestFormatter(defaultConnection, mapping, new ElasticSearchRequest { Type = "type1", Filter = termCriteria });
            var body = JObject.Parse(formatter.Body);

            var field = TraverseWithAssert(body, "filter", "missing", "field");
            Assert.Equal(expectedFieldName, field);
        }
        public void BodyContainsQueryRange()
        {
            var rangeCriteria = new RangeCriteria("someField", memberInfo,
                new[]
                {
                    new RangeSpecificationCriteria(RangeComparison.LessThan, 100),
                    new RangeSpecificationCriteria(RangeComparison.GreaterThan, 200)
                });

            var formatter = new PostBodyRequestFormatter(defaultConnection, mapping, new ElasticSearchRequest { Type = "type1", Query = rangeCriteria });
            var body = JObject.Parse(formatter.Body);

            var result = TraverseWithAssert(body, "query", "range");
            var actualRange = TraverseWithAssert(result, rangeCriteria.Field);
            Assert.Equal("!!! 100 !!!", actualRange["lt"]);
            Assert.Equal("!!! 200 !!!", actualRange["gt"]);
        }
        public void BodyContainsQueryString()
        {
            const string expectedQuery = "this is my query string";
            var queryString = new QueryStringCriteria(expectedQuery);

            var formatter = new PostBodyRequestFormatter(defaultConnection, mapping, new ElasticSearchRequest { Type = "type1", Query = queryString });
            var body = JObject.Parse(formatter.Body);

            var result = TraverseWithAssert(body, "query", "query_string", "query");
            Assert.Equal(expectedQuery, result.ToString());
        }
        public void BodyContainsFilterSingleCollapsedOr()
        {
            const string expectedFieldName = "fieldShouldExist";
            var existsCriteria = new ExistsCriteria(expectedFieldName);
            var orCriteria = OrCriteria.Combine(existsCriteria);

            var formatter = new PostBodyRequestFormatter(defaultConnection, mapping, new ElasticSearchRequest { Type = "type1", Filter = orCriteria });
            var body = JObject.Parse(formatter.Body);

            var field = TraverseWithAssert(body, "filter", "exists", "field");
            Assert.Equal(expectedFieldName, field);
        }
        public void BodyContainsFilterNot()
        {
            var termCriteria = TermsCriteria.Build("term1", memberInfo, "alpha", "bravo", "charlie", "delta", "echo");
            var notCriteria = NotCriteria.Create(termCriteria);

            var formatter = new PostBodyRequestFormatter(defaultConnection, mapping, new ElasticSearchRequest { Type = "type1", Filter = notCriteria });
            var body = JObject.Parse(formatter.Body);

            var result = TraverseWithAssert(body, "filter", "not", "terms");
            var actualTerms = TraverseWithAssert(result, termCriteria.Field);
            foreach (var criteria in termCriteria.Values)
                Assert.Contains("!!! " + criteria + " !!!", actualTerms.Select(t => t.ToString()).ToArray());
        }
        public void BodyContainsFromWhenSpecified()
        {
            const int expectedFrom = 1024;

            var formatter = new PostBodyRequestFormatter(defaultConnection, mapping, new ElasticSearchRequest { Type = "type1", From = expectedFrom });
            var body = JObject.Parse(formatter.Body);

            var result = TraverseWithAssert(body, "from");
            Assert.Equal(expectedFrom, result);
        }
        public void BodyContainsFieldSelections()
        {
            var expectedFields = new List<string> { "first", "second", "third" };

            var formatter = new PostBodyRequestFormatter(defaultConnection, mapping, new ElasticSearchRequest { Type = "type1", Fields = expectedFields });
            var body = JObject.Parse(formatter.Body);

            var result = TraverseWithAssert(body, "fields");
            foreach (var field in expectedFields)
                Assert.Contains(field, result);
        }
        public void BodyContainsRangeFilter()
        {
            const string expectedField = "capacity";
            const decimal expectedRange = 2.0m;
            var rangeCriteria = new RangeCriteria(expectedField, memberInfo, RangeComparison.GreaterThanOrEqual, expectedRange);

            var formatter = new PostBodyRequestFormatter(defaultConnection, mapping, new ElasticSearchRequest { Type = "type1", Filter = rangeCriteria });
            var body = JObject.Parse(formatter.Body);

            var result = TraverseWithAssert(body, "filter", "range", expectedField, "gte");
            Assert.Equal("!!! 2.0 !!!", result);
        }