private ElasticsearchResponse <T> DoRequest <T>(TransportRequestState <T> requestState, int retried = 0) { if (!SniffingDisabled(requestState.RequestConfiguration)) { SniffIfInformationIsTooOld(retried); } IElasticsearchResponse response = null; int initialSeed; bool shouldPingHint; var baseUri = GetNextBaseUri(requestState, out initialSeed, out shouldPingHint); requestState.Seed = initialSeed; var uri = CreateUriToPath(baseUri, requestState.Path); bool seenError = false; try { if (shouldPingHint && !this.ConfigurationValues.DisablePings && (requestState.RequestConfiguration == null || !requestState.RequestConfiguration.PingDisabled.GetValueOrDefault(false)) ) { this.Ping(baseUri); } var streamResponse = _doRequest(requestState.Method, uri, requestState.PostData, requestState.RequestConfiguration); if (streamResponse != null && streamResponse.SuccessOrKnownError) { var error = ThrowOrGetErrorFromStreamResponse(requestState, streamResponse); var typedResponse = this.StreamToTypedResponse <T>(streamResponse, requestState.DeserializationState); typedResponse.NumberOfRetries = retried; this.SetErrorDiagnosticsAndPatchSuccess(requestState, error, typedResponse, streamResponse); response = typedResponse; return(typedResponse); } } catch (Exception e) { var maxRetries = this.GetMaximumRetries(requestState.RequestConfiguration); if (maxRetries == 0 && retried == 0) { throw; } seenError = true; return(RetryRequest <T>(requestState, baseUri, retried, e)); } finally { //make sure we always call markalive on the uri if the connection was succesful if (!seenError && response != null && response.SuccessOrKnownError) { this._connectionPool.MarkAlive(baseUri); } } return(RetryRequest <T>(requestState, baseUri, retried)); }
private GetIndexResponse DeserializeGetIndexResponse(IElasticsearchResponse response, Stream stream) { return new GetIndexResponse { Indices = this.Serializer.Deserialize<Dictionary<string, IndexSettings>>(stream) }; }
public DeleteRepositoryRequestTests() { var request = new DeleteRepositoryRequest("my-repository"); var response = this._client.DeleteRepository(request); this._status = response.ConnectionStatus; }
public GetTemplateRequestTests() { var request = new GetTemplateRequest("me-templ"); var response = this._client.GetTemplate(request); this._status = response.ConnectionStatus; }
public BulkRequestTests() { var newProject = new ElasticsearchProject { Id = 4, Name = "new-project" }; var request = new BulkRequest() { Refresh = true, Consistency = Consistency.One, Operations = new List <IBulkOperation> { { new BulkIndexOperation <ElasticsearchProject>(newProject) { Id = "2" } }, { new BulkDeleteOperation <ElasticsearchProject>(6) }, { new BulkCreateOperation <ElasticsearchProject>(newProject) { Id = "6" } }, { new BulkUpdateOperation <ElasticsearchProject, object>(newProject, new { name = "new-project2" }) { Id = "3" } }, } }; var response = this._client.Bulk(request); this._status = response.ConnectionStatus; }
public IndexExistsRequestTests() { var request = new IndexExistsRequest("my-index"); var response = this._client.IndexExists(request); this._status = response.ConnectionStatus; }
public MultiGetRequestTests() { var request = new MultiGetRequest { GetOperations = new List <IMultiGetOperation> { { new MultiGetOperation <ElasticsearchProject>(12) { Index = "some-other-index" } }, { new MultiGetOperation <ElasticsearchProject>("composite-id") { Fields = new [] { Property.Path <ElasticsearchProject>(p => p.Name) }, Routing = "routing-value", } } } }; var response = this._client.MultiGet(request); this._status = response.ConnectionStatus; }
private TemplateResponse DeserializeTemplateResponse( IElasticsearchResponse response, Stream stream) { if (!response.Success) { return new TemplateResponse { IsValid = false } } ; var dict = this.Serializer.Deserialize <Dictionary <string, TemplateMapping> >(stream); if (dict.Count == 0) { throw new DslException("Could not deserialize TemplateMapping"); } return(new TemplateResponse { IsValid = true, Name = dict.First().Key, TemplateMapping = dict.First().Value }); } }
public DeleteTemplateRequestTests() { var request = new DeleteTemplateRequest("mah-template"); var response = this._client.DeleteTemplate(request); this._status = response.ConnectionStatus; }
// TODO: Handle IFailureReason/BulkIndexByScrollFailure and other bulk response types. public static string GetErrorMessage(this IElasticsearchResponse response) { if (response == null) { return(String.Empty); } var sb = new StringBuilder(); var apiCall = response.ApiCall; if (apiCall.OriginalException != null) { sb.AppendLine($"Original: ({apiCall.HttpStatusCode} - {apiCall.OriginalException.GetType().Name}) {apiCall.OriginalException.Message}"); } if (response is IResponse responseWithError && responseWithError.ServerError != null) { sb.AppendLine($"Server: ({responseWithError.ServerError.Status}) {responseWithError.ServerError.Error}"); } if (response is BulkResponse bulkResponse) { sb.AppendLine($"Bulk: {String.Join("\r\n", bulkResponse.ItemsWithErrors.Select(i => i.Error))}"); } if (sb.Length == 0) { sb.AppendLine("Unknown error."); } return(sb.ToString()); }
public MultiSearchRequestTest() { var request = new MultiSearchRequest { Operations = new Dictionary <string, ISearchRequest> { { "first-search", new SearchRequest { Indices = new IndexNameMarker[] { "my-index" }, Query = Query <ElasticsearchProject> .Term(p => p.Name, "NEST"), SearchType = SearchType.Scan, Preference = "local", Routing = new string[] { "routingvalue" } } }, { "second-search", new SearchRequest { Query = Query <ElasticsearchProject> .Term(p => p.Contributors.First().LastName, "Laarman") } }, } }; var response = this._client.MultiSearch(request); this._status = response.ConnectionStatus; }
public DeleteSnapshotRequestTests() { var request = new DeleteSnapshotRequest("my-repository", "my-snap"); var response = this._client.DeleteSnapshot(request); this._status = response.ConnectionStatus; }
public DeleteByQueryRequestTests() { QueryContainer query = new TermQuery() { Field = Property.Path <ElasticsearchProject>(p => p.Name), Value = "value" } && new PrefixQuery() { Field = "prefix_field", Value = "prefi", Rewrite = RewriteMultiTerm.ConstantScoreBoolean }; var request = new DeleteByQueryRequest() { AllIndices = true, AllowNoIndices = true, ExpandWildcards = ExpandWildcards.Closed, Query = query, DefaultOperator = DefaultOperator.And }; var response = this._client.DeleteByQuery(request); this._status = response.ConnectionStatus; }
public ScrollRequestTests() { var request = new ScrollRequest("scroll-id", "5m"); var response = this._client.Scroll <ElasticsearchProject>(request); this._status = response.ConnectionStatus; }
public GetSnapshotRequestTests() { var request = new GetSnapshotRequest("repos", "snap"); var response = this._client.GetSnapshot(request); this._status = response.ConnectionStatus; }
public ClearScrollRequestTests() { var request = new ClearScrollRequest("this-scroll-id"); var response = this._client.ClearScroll(request); this._status = response.ConnectionStatus; }
public UnregisterPercolatorRequestTests() { var request = new UnregisterPercolatorRequest("index", "percolator-name"); var response = this._client.UnregisterPercolator(request); this._status = response.ConnectionStatus; }
private Task <ElasticsearchResponse <T> > ReturnResponseAsTask <T>(IElasticsearchResponse response) { var tcs = new TaskCompletionSource <ElasticsearchResponse <T> >(); tcs.SetResult(response as ElasticsearchResponse <T>); return(tcs.Task); }
private PingResponse DeserializePingResponse(IElasticsearchResponse response, Stream stream) { return new PingResponse { IsValid = response.Success && response.HttpStatusCode == 200 }; }
public RootNodeInfoRequestTests() { var request = new InfoRequest(); var response = this._client.RootNodeInfo(request); this._status = response.ConnectionStatus; }
private PingResponse DeserializePingResponse(IElasticsearchResponse response, Stream stream) { return(new PingResponse { IsValid = response.Success && response.HttpStatusCode == 200 }); }
public DeleteMappingRequestTests() { var request = new DeleteMappingRequest("my-index", "my-type"); var response = this._client.DeleteMapping(request); this._status = response.ConnectionStatus; }
public SuggestRequestTests() { var request = new SuggestRequest() { AllIndices = true, AllowNoIndices = true, ExpandWildcards = ExpandWildcards.Closed, GlobalText = "global suggest text", Suggest = new Dictionary <string, ISuggester> { { "terms_sug", new CompletionSuggester { Analyzer = "standard", Field = Property.Path <ElasticsearchProject>(p => p.Content), Size = 4, ShardSize = 10, Fuzzy = new FuzzySuggester { Fuzziness = Fuzziness.Ratio(0.3), PrefixLength = 4 } } } } }; var response = this._client.Suggest(request); this._status = response.ConnectionStatus; }
public void FailEarlyIfTimeoutIsExhausted_Async() { using (var fake = new AutoFake()) { var dateTimeProvider = ProvideDateTimeProvider(fake); var config = ProvideConfiguration(dateTimeProvider); var connection = ProvideConnection(fake, config, dateTimeProvider); var getCall = FakeCalls.GetCall(fake); var ok = Task.FromResult(FakeResponse.Ok(config)); var bad = Task.FromResult(FakeResponse.Bad(config)); getCall.ReturnsNextFromSequence( bad, bad, ok ); var seenNodes = new List <Uri>(); getCall.Invokes((Uri u, IRequestConfiguration o) => seenNodes.Add(u)); var pingCall = FakeCalls.PingAtConnectionLevelAsync(fake); pingCall.Returns(ok); var client1 = fake.Resolve <ElasticsearchClient>(); //event though the third node should have returned ok, the first 2 calls took a minute var e = Assert.Throws <MaxRetryException>(async() => await client1.InfoAsync()); e.Message.Should() .StartWith("Retry timeout 00:01:20 was hit after retrying 1 times:"); IElasticsearchResponse response = null; Assert.DoesNotThrow(async() => response = await client1.InfoAsync()); response.Should().NotBeNull(); response.Success.Should().BeTrue(); } }
public static string GetRequest(this IElasticsearchResponse response) { if (response == null) { return(String.Empty); } string body = String.Empty; if (response.RequestUrl.EndsWith("_bulk") && response.Request != null && response.Request.Length > 0) { string[] bulkCommands = Encoding.UTF8.GetString(response.Request).Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); body = String.Join("\r\n", bulkCommands.Select(c => JObject.Parse(c).ToString(Formatting.Indented))); } else if (response.Request != null && response.Request.Length > 0) { body = Encoding.UTF8.GetString(response.Request); try { if (body.StartsWith("{") || body.StartsWith("[")) { body = JObject.Parse(body).ToString(Formatting.Indented); } } catch { } } return($"{response.RequestMethod.ToUpper()} {response.RequestUrl}\r\n{body}\r\n"); }
private UpgradeStatusResponse DeserializeUpgradeStatusResponse(IElasticsearchResponse response, Stream stream) { return(new UpgradeStatusResponse { Upgrades = this.Serializer.Deserialize <Dictionary <string, UpgradeStatus> >(stream) }); }
/// <inheritdoc /> private GetAliasesResponse DeserializeGetAliasesResponse(IElasticsearchResponse connectionStatus, Stream stream) { if (!connectionStatus.Success) return new GetAliasesResponse() {ConnectionStatus = connectionStatus, IsValid = false}; var dict = this.Serializer.DeserializeInternal<CrazyAliasesResponse>(stream); var d = new Dictionary<string, IList<string>>(); foreach (var kv in dict) { var indexDict = kv.Key; var aliases = new List<string>(); if (kv.Value != null && kv.Value.ContainsKey("aliases")) { var aliasDict = kv.Value["aliases"]; if (aliasDict != null) aliases = aliasDict.Select(kva => kva.Key).ToList(); } d.Add(indexDict, aliases); } return new GetAliasesResponse() { ConnectionStatus = connectionStatus, IsValid = true, Indices = d }; }
protected void HandleError(IElasticsearchResponse response) { if (!response.ApiCall.Success) { throw response.ApiCall.OriginalException; } }
private GetIndexResponse DeserializeGetIndexResponse(IElasticsearchResponse response, Stream stream) { return(new GetIndexResponse { Indices = this.Serializer.Deserialize <Dictionary <string, IndexSettings> >(stream) }); }
public DocumentExistsRequestTests() { var request = new DocumentExistsRequest(typeof(ElasticsearchProject), "hello-world", "3"); var response = this._client.DocumentExists(request); this._status = response.ConnectionStatus; }
internal GetMappingResponse(IElasticsearchResponse status, GetRootObjectMappingWrapping dict) { this.IsValid = status.Success && dict != null && dict.Count > 0; if (!this.IsValid) { return; } var firstNode = dict.First(); if (!firstNode.Value.HasAny()) { return; } var mappingNode = firstNode.Value["mappings"]; if (mappingNode == null) { this.IsValid = false; return; } var mapping = mappingNode.First(); if (mapping.Value == null) { this.IsValid = false; return; } mapping.Value.Name = mapping.Key; this.Mapping = mapping.Value; }
public CreateIndexRequestTests() { var request = new CreateIndexRequest("new-index-name") { IndexSettings = new IndexSettings { Settings = new Dictionary<string, object> { {"index.settings", "value"} }, Mappings = new List<RootObjectMapping> { { new RootObjectMapping { Name = "my_root_object", Properties = new Dictionary<PropertyNameMarker, IElasticType> { {"my_field", new StringMapping() { Name = "my_string_field "}} } }} } } }; var response = this._client.CreateIndex(request); this._status = response.ConnectionStatus; }
public CreateIndexRequestTests() { var request = new CreateIndexRequest("new-index-name") { IndexSettings = new IndexSettings { Settings = new Dictionary <string, object> { { "index.settings", "value" } }, Mappings = new List <RootObjectMapping> { { new RootObjectMapping { Name = "my_root_object", Properties = new Dictionary <PropertyNameMarker, IElasticType> { { "my_field", new StringMapping() { Name = "my_string_field " } } } } } } } }; var response = this._client.CreateIndex(request); this._status = response.ConnectionStatus; }
private static R CreateInvalidInstance <R>(IElasticsearchResponse response) where R : BaseResponse { var r = (R)typeof(R).CreateInstance(); ((IResponseWithRequestInformation)r).RequestInformation = response; r.IsValid = false; return(r); }
private GetMappingResponse DeserializeGetMappingResponse(IElasticsearchResponse response, GetMappingDescriptor d, Stream stream) { var dict = response.Success ? Serializer.DeserializeInternal<GetRootObjectMappingWrapping>(stream) : null; return new GetMappingResponse(response, dict); }
public SegmentsRequestTests() { var request = new SegmentsRequest() { AllowNoIndices = true }; var response = this._client.Segments(request); this._status = response.ConnectionStatus; }
public static IList<Uri> FromStream(IElasticsearchResponse response, Stream stream, IElasticsearchSerializer serializer, TransportAddressScheme? preferedTransportOrder = null) { var order = preferedTransportOrder.GetValueOrDefault(TransportAddressScheme.Http); var result = serializer.Deserialize<NodeInfoResponse>(stream); return result.nodes.Values .Select(kv => kv.GetFirstAddress(order)) .Where(url => url != null) .ToList(); }
public ClearCacheRequestTests() { var request = new ClearCacheRequest() { Indices = new IndexNameMarker[] { typeof(ElasticsearchProject), "my-other-index"}, }; var response = this._client.ClearCache(request); this._status = response.ConnectionStatus; }
public FlushRequestTests() { var request = new FlushRequest() { AllowNoIndices = false }; var response = this._client.Flush(request); this._status = response.ConnectionStatus; }
public GetAliasesRequestTests() { var request = new GetAliasesRequest() { Alias = "my-alias" }; var response = this._client.GetAliases(request); this._status = response.ConnectionStatus; }
public DeleteIndexRequestTests() { var request = new DeleteIndexRequest(new IndexNameMarker[] { "postfixed-index-*", typeof(ElasticsearchProject)}) { Timeout = "2m" }; var response = this._client.DeleteIndex(request); this._status = response.ConnectionStatus; }
public DeleteWarmerRequestTests() { var request = new DeleteWarmerRequest("my-warmer") { AllIndices = true }; var response = this._client.DeleteWarmer(request); this._status = response.ConnectionStatus; }
public ClusterStateRequestTests() { var request = new ClusterStateRequest() { Metrics = new [] {ClusterStateMetric.Blocks, ClusterStateMetric.MasterNode }, FlatSettings = true }; var response = this._client.ClusterState(request); this._status = response.ConnectionStatus; }
public RefreshRequestTests() { var request = new RefreshRequest() { Force = true, IgnoreUnavailable = true }; var response = this._client.Refresh(request); this._status = response.ConnectionStatus; }
public OpenIndexRequestTests() { var request = new OpenIndexRequest(typeof(ElasticsearchProject)) { ExpandWildcards = ExpandWildcards.Open }; //TODO Index(request) does not work as expected var response = this._client.OpenIndex(request); this._status = response.ConnectionStatus; }
public GetRequestTests() { var request = new GetRequest<ElasticsearchProject>(2) { Preference = "local", Routing = "2", }; var response = this._client.Get<ElasticsearchProject>(request); this._status = response.ConnectionStatus; }
public AnalyzeRequestTests() { var request = new AnalyzeRequest("analyze this text") { Indices = new IndexNameMarker[] { typeof(ElasticsearchProject)}, Field = Property.Path<ElasticsearchProject>(p=>p.Name) }; var response = this._client.Analyze(request); this._status = response.ConnectionStatus; }
public PercolateRequestTests() { var doc = new ElasticsearchProject() { Id = 19, Name = "Hello" }; var request = new PercolateRequest<ElasticsearchProject>(doc) { Query = Query<ElasticsearchProject>.Term("color", "blue") }; var response = this._client.Percolate<ElasticsearchProject>(request); this._status = response.ConnectionStatus; }
public ClusterHealthRequestTests() { var request = new ClusterHealthRequest() { Level = Level.Cluster, WaitForStatus = WaitForStatus.Yellow }; var response = this._client.ClusterHealth(request); this._status = response.ConnectionStatus; }
public GetWarmerRequestTests() { var request = new GetWarmerRequest { Indices = new IndexNameMarker[] { "test" }, Name = "warmer_1" }; var response = this._client.GetWarmer(request); _status = response.ConnectionStatus; }
public ClusterGetSettingsRequestTests() { var request = new ClusterGetSettingsRequest { FlatSettings = true, }; var response = this._client.ClusterGetSettings(request); this._status = response.ConnectionStatus; }
public StatusRequestTests() { var request = new IndicesStatusRequest() { Recovery = true, Human = true }; var response = this._client.Status(request); this._status = response.ConnectionStatus; }
public IndicesStatsRequestTests() { var request = new IndicesStatsRequest() { CompletionFields = new List<PropertyPathMarker> {"name"}, FielddataFields = new List<PropertyPathMarker> { Property.Path<ElasticsearchProject>(p=>p.PingIP)}, Metrics = new [] { IndicesStatsMetric.Completion, IndicesStatsMetric.Fielddata} }; var response = this._client.IndicesStats(request); this._status = response.ConnectionStatus; }
public NodesInfoRequestTests() { var request = new NodesInfoRequest { NodeId = "my-node-id", Human = true, FlatSettings = true }; var response = this._client.NodesInfo(request); this._status = response.ConnectionStatus; }
/// <summary> /// Handles <see cref="IElasticsearchResponse"/> and pushes <see cref="CustomTiming"/> to current <see cref="MiniProfiler"/> session. /// </summary> /// <param name="response"><see cref="IElasticsearchResponse"/> to be handled.</param> /// <param name="profiler">Current <see cref="MiniProfiler"/> session instance.</param> internal static void HandleResponse(IElasticsearchResponse response, MiniProfiler profiler) { if (profiler == null || profiler.Head == null || response.Metrics == null) return; profiler.Head.AddCustomTiming("elasticsearch", new CustomTiming(profiler, BuildCommandString(response)) { Id = Guid.NewGuid(), DurationMilliseconds = response.Metrics.Requests.Sum(c => c.EllapsedMilliseconds), ExecuteType = response.RequestMethod, }); }
public DeleteRequestTests() { var request = new DeleteRequest("my-index","my-type","my-doc-id") { Refresh = true, Consistency = Consistency.All, Version = 3, VersionType = VersionType.Force }; var response = this._client.Delete(request); this._status = response.ConnectionStatus; }
public TermVectorWithDocRequestTests() { var response = this._client.TermVector<ElasticsearchProject>(t=>t .Offsets() .Payloads() .Positions() .TermStatistics() .FieldStatistics() .Document(new { world = "doc doc doc"}) ); this._status = response.ConnectionStatus; }
public SnapshotRequestTests() { var request = new SnapshotRequest("repos", "snap") { IgnoreUnavailable = true, IncludeGlobalState = true, WaitForCompletion = true, Partial = true, }; var response = this._client.Snapshot(request); this._status = response.ConnectionStatus; }
public PutWarmerRequestTests() { QueryContainer query = new MatchAllQuery(); var request = new PutWarmerRequest("warmer_1") { Indices = new IndexNameMarker[] { "my-index" }, SearchDescriptor = new SearchRequest { Query = query } }; var response = this._client.PutWarmer(request); this._status = response.ConnectionStatus; }
public SuggestRequestTests() { var request = new SuggestRequest() { AllIndices = true, AllowNoIndices = true, ExpandWildcards = ExpandWildcards.Closed, GlobalText = "global suggest text", Suggest = new Dictionary<string, ISuggester> { { "terms_sug", new CompletionSuggester { Analyzer = "standard", Field = Property.Path<ElasticsearchProject>(p=>p.Content), Size = 4, ShardSize = 10, Fuzzy = new FuzzySuggester { Fuzziness = Fuzziness.Ratio(0.3), PrefixLength = 4 } } } } }; var response = this._client.Suggest(request); this._status = response.ConnectionStatus; }