Github: https://github.com/elastic/elasticsearch-net
    官方文档: https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/introduction.html

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Text;
    4. using System.Text.Json.Serialization;
    5. namespace Elastic.Transport.Products.Elasticsearch
    6. {
    7. /// <summary>
    8. /// Base response for Elasticsearch responses.
    9. /// </summary>
    10. public abstract class ElasticsearchResponseBase : IElasticsearchResponse
    11. {
    12. private IApiCallDetails? _originalApiCall;
    13. /// <summary> Returns useful information about the request(s) that were part of this API call. </summary>
    14. [JsonIgnore]
    15. public virtual IApiCallDetails? ApiCall => _originalApiCall;
    16. /// <summary>
    17. /// A collection of warnings returned from Elasticsearch.
    18. /// <para>Used to provide server warnings, for example, when the request uses an API feature that is marked as deprecated.</para>
    19. /// </summary>
    20. [JsonIgnore]
    21. public IEnumerable<string> Warnings
    22. {
    23. get
    24. {
    25. if (ApiCall.ParsedHeaders is not null && ApiCall.ParsedHeaders.TryGetValue("warning", out var warnings))
    26. {
    27. foreach (var warning in warnings)
    28. yield return warning;
    29. }
    30. }
    31. }
    32. /// <inheritdoc />
    33. [JsonIgnore]
    34. public string DebugInformation
    35. {
    36. get
    37. {
    38. var sb = new StringBuilder();
    39. sb.Append($"{(!IsValid ? "Inv" : "V")}alid Elastic.Clients.Elasticsearch response built from a ");
    40. sb.AppendLine(ApiCall?.ToString().ToCamelCase() ??
    41. "null ApiCall which is highly exceptional, please open a bug if you see this");
    42. if (!IsValid)
    43. DebugIsValid(sb);
    44. if (ApiCall.ParsedHeaders is not null && ApiCall.ParsedHeaders.TryGetValue("warning", out var warnings))
    45. {
    46. sb.AppendLine($"# Server indicated warnings:");
    47. foreach (var warning in warnings)
    48. sb.AppendLine($"- {warning}");
    49. }
    50. if (ApiCall != null)
    51. ResponseStatics.DebugInformationBuilder(ApiCall, sb);
    52. return sb.ToString();
    53. }
    54. }
    55. /// <inheritdoc />
    56. [JsonIgnore]
    57. public virtual bool IsValid
    58. {
    59. get
    60. {
    61. var statusCode = ApiCall?.HttpStatusCode;
    62. // TODO - Review this on a request by reqeust basis
    63. if (statusCode == 404)
    64. return false;
    65. return (ApiCall?.Success ?? false) && (!ServerError?.HasError() ?? true);
    66. }
    67. }
    68. /// <inheritdoc />
    69. [JsonIgnore]
    70. public Exception? OriginalException => ApiCall?.OriginalException;
    71. IApiCallDetails? ITransportResponse.ApiCall
    72. {
    73. get => _originalApiCall;
    74. set => _originalApiCall = value;
    75. }
    76. /// <summary>
    77. ///
    78. /// </summary>
    79. [JsonIgnore]
    80. public ServerError ServerError { get; internal set; }
    81. /// <summary>
    82. ///
    83. /// </summary>
    84. /// <param name="exception"></param>
    85. /// <returns></returns>
    86. // TODO: We need nullable annotations here ideally as exception is not null when the return value is true.
    87. public bool TryGetOriginalException(out Exception? exception)
    88. {
    89. if (OriginalException is not null)
    90. {
    91. exception = OriginalException;
    92. return true;
    93. }
    94. exception = null;
    95. return false;
    96. }
    97. /// <summary>Subclasses can override this to provide more information on why a call is not valid.</summary>
    98. protected virtual void DebugIsValid(StringBuilder sb) { }
    99. /// <inheritdoc />
    100. public override string ToString() =>
    101. $"{(!IsValid ? "Inv" : "V")}alid Elastic.Clients.Elasticsearch response built from a {ApiCall?.ToString().ToCamelCase()}";
    102. }
    103. }
    1. using Elastic.Transport.Products.Elasticsearch;
    2. using System.Collections.Generic;
    3. using System.Text.Json.Serialization;
    4. #nullable restore
    5. namespace Elastic.Clients.Elasticsearch
    6. {
    7. public sealed partial class IndexResponse : ElasticsearchResponseBase
    8. {
    9. [JsonInclude]
    10. [JsonPropertyName("forced_refresh")]
    11. public bool? ForcedRefresh { get; init; }
    12. [JsonInclude]
    13. [JsonPropertyName("_id")]
    14. public string Id { get; init; }
    15. [JsonInclude]
    16. [JsonPropertyName("_index")]
    17. public string Index { get; init; }
    18. [JsonInclude]
    19. [JsonPropertyName("_primary_term")]
    20. public long PrimaryTerm { get; init; }
    21. [JsonInclude]
    22. [JsonPropertyName("result")]
    23. public Elastic.Clients.Elasticsearch.Result Result { get; init; }
    24. [JsonInclude]
    25. [JsonPropertyName("_seq_no")]
    26. public long SeqNo { get; init; }
    27. [JsonInclude]
    28. [JsonPropertyName("_shards")]
    29. public Elastic.Clients.Elasticsearch.ShardStatistics Shards { get; init; }
    30. [JsonInclude]
    31. [JsonPropertyName("_version")]
    32. public long Version { get; init; }
    33. }
    34. }