|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Elasticsearch.Net; |
| 7 | +using FluentAssertions; |
| 8 | +using Nest; |
| 9 | +using Newtonsoft.Json.Linq; |
| 10 | +using Tests.Framework; |
| 11 | +using Tests.Framework.Integration; |
| 12 | +using Tests.Framework.MockData; |
| 13 | +using Xunit; |
| 14 | + |
| 15 | +namespace Tests.Search.MultiSearch |
| 16 | +{ |
| 17 | + [Collection(IntegrationContext.ReadOnly)] |
| 18 | + public class MultiSearchLowLevelPostDataTests |
| 19 | + { |
| 20 | + private IElasticClient _client; |
| 21 | + |
| 22 | + public MultiSearchLowLevelPostDataTests(ReadOnlyCluster cluster, EndpointUsage usage) |
| 23 | + { |
| 24 | + _client = cluster.Client(); |
| 25 | + } |
| 26 | + |
| 27 | + protected List<object> Search => new object[] |
| 28 | + { |
| 29 | + new {}, |
| 30 | + new { from = 0, size = 10, query = new { match_all = new {} } }, |
| 31 | + new { search_type = "count" }, |
| 32 | + new {}, |
| 33 | + new { index = "devs", type = "developer" }, |
| 34 | + new { from = 0, size = 5, query = new { match_all = new {} } }, |
| 35 | + new { index = "devs", type = "developer" }, |
| 36 | + new { from = 0, size = 5, query = new { match_all = new {} } } |
| 37 | + }.ToList(); |
| 38 | + |
| 39 | + |
| 40 | + [I] public void PostEnumerableOfObjects() |
| 41 | + { |
| 42 | + var response = this._client.Raw.Msearch<dynamic>("project", "project", this.Search); |
| 43 | + AssertResponse(response); |
| 44 | + response = this._client.Raw.Msearch<dynamic>("project", "project", (object)this.Search); |
| 45 | + AssertResponse(response); |
| 46 | + } |
| 47 | + |
| 48 | + [I] public void PostEnumerableOfStrings() |
| 49 | + { |
| 50 | + var listOfStrings = Search |
| 51 | + .Select(s => this._client.Serializer.SerializeToString(s, SerializationFormatting.None)) |
| 52 | + .ToList(); |
| 53 | + |
| 54 | + var response = this._client.Raw.Msearch<dynamic>("project", "project", listOfStrings); |
| 55 | + AssertResponse(response); |
| 56 | + response = this._client.Raw.Msearch<dynamic>("project", "project", (object)listOfStrings); |
| 57 | + AssertResponse(response); |
| 58 | + } |
| 59 | + |
| 60 | + [I] public void PostString() |
| 61 | + { |
| 62 | + var str = Search |
| 63 | + .Select(s => this._client.Serializer.SerializeToString(s, SerializationFormatting.None)) |
| 64 | + .ToList() |
| 65 | + .Aggregate(new StringBuilder(), (sb, s) => sb.Append(s + "\n"), sb => sb.ToString()); |
| 66 | + |
| 67 | + var response = this._client.Raw.Msearch<dynamic>("project", "project", str); |
| 68 | + AssertResponse(response); |
| 69 | + response = this._client.Raw.Msearch<dynamic>("project", "project", (object)str); |
| 70 | + AssertResponse(response); |
| 71 | + } |
| 72 | + |
| 73 | + [I] public void PostByteArray() |
| 74 | + { |
| 75 | + var str = Search |
| 76 | + .Select(s => this._client.Serializer.SerializeToString(s, SerializationFormatting.None)) |
| 77 | + .ToList() |
| 78 | + .Aggregate(new StringBuilder(), (sb, s) => sb.Append(s + "\n"), sb => sb.ToString()); |
| 79 | + |
| 80 | + var bytes = Encoding.UTF8.GetBytes(str); |
| 81 | + |
| 82 | + var response = this._client.Raw.Msearch<dynamic>("project", "project", bytes); |
| 83 | + AssertResponse(response); |
| 84 | + response = this._client.Raw.Msearch<dynamic>("project", "project", (object)bytes); |
| 85 | + AssertResponse(response); |
| 86 | + } |
| 87 | + |
| 88 | + public void AssertResponse(ElasticsearchResponse<dynamic> response) |
| 89 | + { |
| 90 | + response.Success.Should().BeTrue(); |
| 91 | + |
| 92 | + var r = response.Body; |
| 93 | + |
| 94 | + JArray responses = r.responses; |
| 95 | + |
| 96 | + responses.Count().Should().Be(4); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments