|
| 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; |
| 10 | +using Newtonsoft.Json.Converters; |
| 11 | +using Tests.Framework; |
| 12 | +using Tests.Framework.Integration; |
| 13 | +using Xunit; |
| 14 | +using A = Tests.QueryDsl.BoolDsl.BoolCluster.A; |
| 15 | +using E = Tests.QueryDsl.BoolDsl.BoolCluster.E; |
| 16 | +using static Nest.Infer; |
| 17 | + |
| 18 | + |
| 19 | +namespace Tests.QueryDsl.BoolDsl |
| 20 | +{ |
| 21 | + |
| 22 | + internal static class BoolDslTestExtensions |
| 23 | + { |
| 24 | + public static QueryContainer Id(this QueryContainerDescriptor<A> q, int id) => q.Term(p => p.Id, id); |
| 25 | + public static QueryContainer O(this QueryContainerDescriptor<A> q, E option) => q.Term(p => p.Option, option); |
| 26 | + } |
| 27 | + [CollectionDefinition(IntegrationContext.Bool)] |
| 28 | + public class BoolCluster : ClusterBase, ICollectionFixture<BoolCluster> |
| 29 | + { |
| 30 | + //TODO discuss make this default? |
| 31 | + [JsonConverter(typeof(StringEnumConverter))] |
| 32 | + public enum E { Option1, Option2 } |
| 33 | + |
| 34 | + public class A |
| 35 | + { |
| 36 | + public int Id { get; set; } |
| 37 | + public E Option { get; set; } |
| 38 | + |
| 39 | + private static E[] Options = new[] { E.Option1, E.Option2 }; |
| 40 | + public static IList<A> Documents => Enumerable.Range(0, 20).Select(i => new A { Id = i + 1, Option = Options[i % 2] }).ToList(); |
| 41 | + } |
| 42 | + |
| 43 | + public override void Boostrap() |
| 44 | + { |
| 45 | + var client = this.Node.Client(); |
| 46 | + var index = client.CreateIndex(Index<A>(), i => i |
| 47 | + .Mappings(map => map |
| 48 | + .Map<A>(m => m |
| 49 | + .AutoMap() |
| 50 | + .Properties(props => props |
| 51 | + .String(s => s.Name(p => p.Option).NotAnalyzed()) |
| 52 | + ) |
| 53 | + ) |
| 54 | + ) |
| 55 | + ); |
| 56 | + var bulkResponse = client.Bulk(b => b.IndexMany(A.Documents)); |
| 57 | + if (!bulkResponse.IsValid) throw new ApplicationException("Could not bootstrap bool cluster, bulk was invalid"); |
| 58 | + client.Refresh(Indices<A>()); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + [Collection(IntegrationContext.Bool)] |
| 63 | + public class BoolsInPractice |
| 64 | + { |
| 65 | + |
| 66 | + private readonly BoolCluster _cluster; |
| 67 | + private IConnectionSettingsValues _settings; |
| 68 | + |
| 69 | + public BoolsInPractice(BoolCluster cluster) |
| 70 | + { |
| 71 | + this._cluster = cluster; |
| 72 | + } |
| 73 | + |
| 74 | + private async Task Bool( |
| 75 | + Func<A, bool> programmatic, |
| 76 | + Func<QueryContainerDescriptor<A>, QueryContainer> fluentQuery, |
| 77 | + QueryContainer initializerQuery, |
| 78 | + int expectedCount |
| 79 | + ) |
| 80 | + { |
| 81 | + var documents = A.Documents.Where(programmatic).ToList(); |
| 82 | + documents.Count().Should().Be(expectedCount, " filtering the documents in memory did not yield the expected count"); |
| 83 | + |
| 84 | + var client = this._cluster.Node.Client(); |
| 85 | + |
| 86 | + var fluent = client.Search<A>(s => s.Query(fluentQuery)); |
| 87 | + var fluentAsync = await client.SearchAsync<A>(s => s.Query(fluentQuery)); |
| 88 | + |
| 89 | + var initializer = client.Search<A>(new SearchRequest<A> { Query = initializerQuery }); |
| 90 | + var initializerAsync = await client.SearchAsync<A>(new SearchRequest<A> { Query = initializerQuery }); |
| 91 | + |
| 92 | + var responses = new[] { fluent, fluentAsync, initializer, initializerAsync }; |
| 93 | + foreach (var response in responses) |
| 94 | + { |
| 95 | + response.IsValid.Should().BeTrue(); |
| 96 | + response.Total.Should().Be(expectedCount); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private TermQuery Id(int id) => new TermQuery { Field = "id", Value = id }; |
| 101 | + private TermQuery O(E option) => new TermQuery { Field = "option", Value = option }; |
| 102 | + |
| 103 | + [I] |
| 104 | + public async Task CompareBoolQueryTranslationsToRealBooleanLogic() |
| 105 | + { |
| 106 | + await Bool( |
| 107 | + a => a.Id == 1 && a.Option == E.Option1, |
| 108 | + a => a.Id(1) && a.O(E.Option1), |
| 109 | + Id(1) && O(E.Option1), |
| 110 | + expectedCount: 1 |
| 111 | + ); |
| 112 | + |
| 113 | + await Bool( |
| 114 | + a => a.Id == 1 || a.Id == 2 || a.Id == 3 || a.Id == 4, |
| 115 | + a => +a.Id(1) || +a.Id(2) || +a.Id(3) || +a.Id(4), |
| 116 | + +Id(1) || +Id(2) || +Id(3) || +Id(4), |
| 117 | + expectedCount: 4 |
| 118 | + ); |
| 119 | + |
| 120 | + await Bool( |
| 121 | + a => a.Id == 1 || a.Id == 2 || a.Id == 3 || a.Id == 4 && (a.Option != E.Option1 || a.Option == E.Option2), |
| 122 | + a => +a.Id(1) || +a.Id(2) || +a.Id(3) || +a.Id(4) && (!a.O(E.Option1) || a.O(E.Option2)), |
| 123 | + +Id(1) || +Id(2) || +Id(3) || +Id(4) && (!O(E.Option1) || O(E.Option2)), |
| 124 | + expectedCount: 4 |
| 125 | + ); |
| 126 | + |
| 127 | + await Bool( |
| 128 | + a => a.Id == 1 || a.Id == 2 || a.Id == 3 || a.Id == 4 && a.Option != E.Option1 || a.Option == E.Option2, |
| 129 | + a => +a.Id(1) || +a.Id(2) || +a.Id(3) || +a.Id(4) && !a.O(E.Option1) || a.O(E.Option2), |
| 130 | + +Id(1) || +Id(2) || +Id(3) || +Id(4) && !O(E.Option1) || O(E.Option2), |
| 131 | + expectedCount: 12 |
| 132 | + ); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments