/// <inheritdoc/> public virtual QueryInfo ToQueryInfo() { var request = ElasticQueryTranslator.Translate(this.provider.Mapping, Expression); var formatter = new SearchRequestFormatter(this.provider.Connection, this.provider.Mapping, request.SearchRequest); return(new QueryInfo(formatter.Body, this.provider.Connection.GetSearchUri(request.SearchRequest))); }
public static async void NonSuccessfulHttpRequestThrows() { var client = Substitute.For <IElasticsearchClient>(); client.SearchAsync <string>( "_all", "docType", @"{""timeout"":""10s""}", Arg.Any <Func <SearchRequestParameters, SearchRequestParameters> >()) .Returns(Task.FromResult(ElasticsearchResponse <string> .Create( new ConnectionConfiguration(), 404, "_search", "_all", new byte[0]))); var localConnection = new ElasticNetConnection(client); var request = new SearchRequest { DocumentType = "docType" }; var formatter = new SearchRequestFormatter(localConnection, mapping, request); var ex = await Record.ExceptionAsync(() => localConnection.SearchAsync( formatter.Body, request, CancellationToken.None, log)); Assert.IsType <HttpRequestException>(ex); Assert.Equal("Response status code does not indicate success: 404 (Not Found).", ex.Message); }
public void FormatTimeSpanWithMillisecondPrecisionIsUnquantifiedFormat() { var timespan = TimeSpan.FromMilliseconds(1500); var actual = SearchRequestFormatter.Format(timespan); Assert.Equal(timespan.TotalMilliseconds.ToString(CultureInfo.InvariantCulture), actual); }
public void FormatTimeSpanWithSecondPrecisionIsSecondFormat() { var timespan = TimeSpan.FromSeconds(3); var actual = SearchRequestFormatter.Format(timespan); Assert.Equal(timespan.TotalSeconds.ToString(CultureInfo.InvariantCulture) + "s", actual); }
/// <inheritdoc/> public QueryInfo ToQueryInfo() { var request = ElasticQueryTranslator.Translate(Context.Mapping, Expression); var formatter = new SearchRequestFormatter(Context.Connection, Context.Mapping, request.SearchRequest); return(new QueryInfo(formatter.Body, formatter.Uri)); }
public static async Task LogsDebugMessagesDuringExecution() { var responseString = BuildResponseString(2, 1, 1, 0.3141, "testIndex", "testType", "testId"); var messageHandler = new SpyMessageHandler(); var spyLog = new SpyLog(); messageHandler.Response.Content = new StringContent(responseString); var localConnection = new ElasticConnection(messageHandler, new Uri("http://localhost"), "myUser", "myPass", index: "SearchIndex"); var request = new SearchRequest { DocumentType = "abc123", Size = 2112 }; var formatter = new SearchRequestFormatter(localConnection, mapping, request); await localConnection.SearchAsync( formatter.Body, request, token, spyLog); Assert.Equal(4, spyLog.Entries.Count); Assert.Equal(@"Request: POST http://localhost/SearchIndex/abc123/_search", spyLog.Entries[0].Message); Assert.Equal(@"Body:" + '\n' + @"{""size"":2112,""timeout"":""10s""}", spyLog.Entries[1].Message); Assert.True(new Regex(@"Response: 200 OK \(in \d+ms\)").Match(spyLog.Entries[2].Message).Success); Assert.True(new Regex(@"Deserialized \d+ bytes into 1 hits in \d+ms").Match(spyLog.Entries[3].Message).Success); }
public void FormatTimeSpanWithMinutePrecisionIsMinuteFormat() { var timespan = TimeSpan.FromMinutes(4); var actual = SearchRequestFormatter.Format(timespan); Assert.Equal(timespan.TotalMinutes.ToString(CultureInfo.InvariantCulture) + "m", actual); }
public Task <ElasticResponse> SearchAsync(SearchRequest searchRequest) { var formatter = new SearchRequestFormatter(connection, mapping, searchRequest); log.Debug(null, null, "Request: POST {0}", formatter.Uri); log.Debug(null, null, "Body:\n{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; })); }
/// <inheritdoc/> public QueryInfo ToQueryInfo() { var prefix = Context.Mapping.GetDocumentMappingPrefix(typeof(T)); var request = ElasticQueryTranslator.Translate(Context.Mapping, prefix, Expression); var formatter = new SearchRequestFormatter(Context.Connection, Context.Mapping, request.SearchRequest); return(new QueryInfo(formatter.Body, formatter.Uri)); }
public void ParseThrowsInvalidOperationForUnknownCriteriaTypes() { var formatter = new SearchRequestFormatter(defaultConnection, mapping, new SearchRequest { IndexType = "type1", Query = new FakeCriteria() }); Assert.Throws <InvalidOperationException>(() => JObject.Parse(formatter.Body)); }
public void BodyIsValidJsonFormattedResponse() { var formatter = new SearchRequestFormatter(defaultConnection, mapping, new SearchRequest { DocumentType = "type1" }); JObject.Parse(formatter.Body); }
public void UriFormatting(string index, string documentType, string expectedUri) { var connection = new ElasticConnection(new Uri("http://a.b.com:9000/"), index: index); var formatter = new SearchRequestFormatter(connection, mapping, new SearchRequest { DocumentType = documentType }); Assert.Equal(expectedUri, formatter.Uri.ToString()); }
public void BodyContainsMatchAllFilter() { var matchAllCriteria = MatchAllCriteria.Instance; var formatter = new SearchRequestFormatter(defaultConnection, mapping, new SearchRequest { IndexType = "type1", Query = matchAllCriteria }); var body = JObject.Parse(formatter.Body); body.TraverseWithAssert("query", "match_all"); }
public void ParseThrowsInvalidOperationForUnknownCriteriaTypes() { var facets = new List <IFacet> { new FakeFacet() }; var formatter = new SearchRequestFormatter(defaultConnection, mapping, new SearchRequest { DocumentType = "type1", Facets = facets }); Assert.Throws <InvalidOperationException>(() => JObject.Parse(formatter.Body)); }
public string GetBodyExample(SearchRequest instance) { var settings = this.GetSettings(); var connection = new ElasticConnectionImpl(new Uri("http://localhost:9200"), index: "current,currentforfind"); var mapping = new VeryElasticMapping(settings); var formatter = new SearchRequestFormatter(connection, mapping, instance); return(formatter.Body); }
public void BodyDoesNotContainMinScoreWhenUnspecified() { var connection = new ElasticConnection(new Uri("http://localhost/"), timeout: TimeSpan.Zero); var formatter = new SearchRequestFormatter(connection, mapping, new SearchRequest()); var body = JObject.Parse(formatter.Body); var result = body["min_score"]; Assert.Null(result); }
public void PrettySetsUriQueryWhenNoOtherQueryUriParameters() { var prettyUri = new SearchRequestFormatter( new ElasticConnection(new Uri("http://coruscant.gov/some"), options: new ElasticConnectionOptions { Pretty = true }), mapping, new SearchRequest { DocumentType = "type1", Filter = criteria }).Uri; Assert.Equal("pretty=true", prettyUri.GetComponents(UriComponents.Query, UriFormat.Unescaped)); }
public void SearchTypeAppearsOnUriWhenSpecified() { var searchRequest = new SearchRequest { SearchType = "count" }; var formatter = new SearchRequestFormatter(defaultConnection, mapping, searchRequest); var parameters = formatter.Uri.GetComponents(UriComponents.Query, UriFormat.Unescaped).Split('&'); Assert.Single(parameters, p => p == "search_type=count"); }
public void SearchTypeDoesNotAppearOnUriWhenNotSpecified() { var searchRequest = new SearchRequest { SearchType = "" }; var formatter = new SearchRequestFormatter(defaultConnection, mapping, searchRequest); var parameters = formatter.Uri.GetComponents(UriComponents.Query, UriFormat.Unescaped).Split('&'); Assert.DoesNotContain(parameters, p => p.StartsWith("search_type=")); }
public void BodyContainsTimeoutWhenSpecified() { const string expectedTimeout = "15s"; var connection = new ElasticConnection(new Uri("http://localhost/"), timeout: TimeSpan.FromSeconds(15)); var formatter = new SearchRequestFormatter(connection, mapping, new SearchRequest()); var body = JObject.Parse(formatter.Body); var result = body.TraverseWithAssert("timeout"); Assert.Equal(expectedTimeout, result); }
public void BodyContainsMinScoreWhenSpecified() { var searchRequest = new SearchRequest { MinScore = 1.3 }; var formatter = new SearchRequestFormatter(defaultConnection, mapping, searchRequest); var body = JObject.Parse(formatter.Body); var result = body.TraverseWithAssert("min_score"); Assert.Equal(searchRequest.MinScore.ToString(), result); }
public void BodyContainsSizeWhenSpecified() { const int expectedSize = 4096; var formatter = new SearchRequestFormatter(defaultConnection, mapping, new SearchRequest { DocumentType = "type1", Size = expectedSize }); var body = JObject.Parse(formatter.Body); var result = body.TraverseWithAssert("size"); Assert.Equal(expectedSize, result); }
public void BodyContainsFromWhenSpecified() { const int expectedFrom = 1024; var formatter = new SearchRequestFormatter(defaultConnection, mapping, new SearchRequest { DocumentType = "type1", From = expectedFrom }); var body = JObject.Parse(formatter.Body); var result = body.TraverseWithAssert("from"); Assert.Equal(expectedFrom, result); }
public void BodyContainsStatisticalFacet() { var expectedFacet = new StatisticalFacet("TotalSales", "OrderTotal"); var searchRequest = new SearchRequest { Facets = new List <IFacet>(new[] { expectedFacet }) }; var formatter = new SearchRequestFormatter(defaultConnection, mapping, searchRequest); var body = JObject.Parse(formatter.Body); var result = body.TraverseWithAssert("facets", expectedFacet.Name, expectedFacet.Type, "field"); Assert.Equal(expectedFacet.Fields[0], result.ToString()); }
public void BodyContainsQueryString() { const string expectedQuery = "this is my query string"; var queryString = new QueryStringCriteria(expectedQuery); var formatter = new SearchRequestFormatter(defaultConnection, mapping, new SearchRequest { DocumentType = "type1", Query = queryString }); var body = JObject.Parse(formatter.Body); var result = body.TraverseWithAssert("query", "query_string", "query"); Assert.Equal(expectedQuery, result.ToString()); }
public void BodyContainsExists() { const string expectedFieldName = "fieldShouldExist"; var existsCriteria = new ExistsCriteria(expectedFieldName); var formatter = new SearchRequestFormatter(defaultConnection, mapping, new SearchRequest { IndexType = "type1", Query = existsCriteria }); var body = JObject.Parse(formatter.Body); var field = body.TraverseWithAssert("query", "exists", "field"); Assert.Equal(expectedFieldName, field); }
public void BodyContainsTermsFacetWithNoSizeWhenNotSpecified() { var expectedFacet = new TermsFacet("Totals", null, "OrderTotal"); var searchRequest = new SearchRequest { Facets = new List <IFacet>(new[] { expectedFacet }) }; var formatter = new SearchRequestFormatter(defaultConnection, mapping, searchRequest); var body = JObject.Parse(formatter.Body); var result = body.TraverseWithAssert("facets", expectedFacet.Name, expectedFacet.Type); Assert.False(result.Contains("size")); }
public void BodyContainsMissing() { const string expectedFieldName = "fieldShouldBeMissing"; var termCriteria = new MissingCriteria(expectedFieldName); var formatter = new SearchRequestFormatter(defaultConnection, mapping, new SearchRequest { DocumentType = "type1", Query = termCriteria }); var body = JObject.Parse(formatter.Body); var field = body.TraverseWithAssert("query", "missing", "field"); Assert.Equal(expectedFieldName, field); }
public void BodyContainsTerm() { var termCriteria = TermsCriteria.Build(TermsExecutionMode.@bool, "term1", memberInfo, "singlecriteria"); var formatter = new SearchRequestFormatter(defaultConnection, mapping, new SearchRequest { IndexType = "type1", Query = termCriteria }); var body = JObject.Parse(formatter.Body); var result = body.TraverseWithAssert("query", "term"); Assert.Equal(1, result.Count()); Assert.Equal("!!! singlecriteria !!!", result[termCriteria.Field].ToString()); Assert.Null(result["execution"]); // Only applicable to "terms" filters }
public void BodyContainsFilterFacet() { var expectedFilter = new ExistsCriteria("IsLocal"); var expectedFacet = new FilterFacet("LocalSales", expectedFilter); var searchRequest = new SearchRequest { Facets = new List <IFacet>(new[] { expectedFacet }) }; var formatter = new SearchRequestFormatter(defaultConnection, mapping, searchRequest); var body = JObject.Parse(formatter.Body); var result = body.TraverseWithAssert("facets", expectedFacet.Name, expectedFacet.Type, expectedFilter.Name, "field"); Assert.Equal(expectedFilter.Field, result.ToString()); }