Skip to content

Commit 79efa95

Browse files
committed
Added test to illustrate #579 how to combine queries.
1 parent 2675d79 commit 79efa95

File tree

4 files changed

+153
-10
lines changed

4 files changed

+153
-10
lines changed

src/Tests/Nest.Tests.Integration/IntegrationTests.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,43 @@ namespace Nest.Tests.Integration
1313
{
1414
public class IntegrationTests
1515
{
16+
1617
protected readonly IElasticClient _client = ElasticsearchConfiguration.Client;
1718
protected readonly IElasticClient _clientNoRawResponse = ElasticsearchConfiguration.ClientNoRawResponse;
1819
protected readonly ElasticClient _thriftClient = ElasticsearchConfiguration.ThriftClient;
1920
protected readonly IConnectionSettingsValues _settings = ElasticsearchConfiguration.Settings();
20-
21+
2122
protected virtual void ResetIndexes()
2223
{
23-
24+
2425
}
2526

2627
protected IQueryResponse<T> SearchRaw<T>(string query) where T : class
2728
{
2829
var index = this._client.Infer.IndexName<T>();
2930
var typeName = this._client.Infer.TypeName<T>();
3031
var connectionStatus = this._client.Raw.Search<QueryResponse<T>>(index, typeName, query);
31-
var serializer = connectionStatus.Serializer as INestSerializer;
32+
var serializer = connectionStatus.Serializer as INestSerializer;
3233
return connectionStatus.Response;
33-
}
34+
}
3435

3536
public void DoFilterTest(Func<FilterDescriptor<ElasticsearchProject>, Nest.BaseFilter> filter, ElasticsearchProject project, bool queryMustHaveResults)
3637
{
3738
var filterId = Filter<ElasticsearchProject>.Term(e => e.Id, project.Id);
3839

3940
var results = this._client.Search<ElasticsearchProject>(
40-
s => s.Filter(ff => ff.And(
41-
f => f.Term(e => e.Id, project.Id),
42-
filter
43-
))
44-
);
41+
s => s.Filter(ff => ff.And(
42+
f => f.Term(e => e.Id, project.Id),
43+
filter
44+
))
45+
);
4546

4647
var rawResponse = results.ConnectionStatus.ResponseRaw.Utf8String();
4748

4849
Assert.True(results.IsValid, rawResponse);
4950
Assert.True(results.ConnectionStatus.Success, rawResponse);
5051
Assert.AreEqual(queryMustHaveResults ? 1 : 0, results.Total);
5152
}
52-
53+
5354
}
5455
}

src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@
162162
<Compile Include="Internals\Inferno\HostNameWithPathTests.cs" />
163163
<Compile Include="Internals\Inferno\MapTypeNamesTests.cs" />
164164
<Compile Include="Internals\Serialize\OptOutTests.cs" />
165+
<Compile Include="Reproduce\Reproduce579Tests.cs" />
165166
<Compile Include="Search\Fields\FieldsTests.cs" />
166167
<Compile Include="Search\Filter\Modes\ConditionlessFilterJson.cs" />
167168
<Compile Include="Search\Filter\Modes\FilterModesTests.cs" />
@@ -427,6 +428,9 @@
427428
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
428429
</None>
429430
<None Include="packages.config" />
431+
<None Include="Reproduce\Issue579.json">
432+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
433+
</None>
430434
<None Include="Search\Fields\FixedCovariantSearchResult.json">
431435
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
432436
</None>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"query": {
3+
"bool": {
4+
"must": [
5+
{
6+
"bool": {
7+
"must": [
8+
{
9+
"bool": {
10+
"should": [
11+
{
12+
"terms": {
13+
"assignedUsers": [
14+
[
15+
"6625c0bf-5414-4985-880c-3b34152e9453",
16+
"7ff7bc06-c75e-4ed1-9732-f3c6c9743670",
17+
"e4c48ebf-7602-43a3-b82f-1a83e2ae1c0b"
18+
]
19+
]
20+
}
21+
},
22+
{
23+
"term": {
24+
"overrideUsers": {
25+
"value": "8f836113-3392-4dad-9971-02ac915fd64a"
26+
}
27+
}
28+
},
29+
{
30+
"term": {
31+
"unassigned": {
32+
"value": "true"
33+
}
34+
}
35+
},
36+
{
37+
"term": {
38+
"global": {
39+
"value": "true"
40+
}
41+
}
42+
}
43+
],
44+
"minimum_number_should_match": 1
45+
}
46+
},
47+
{
48+
"term": {
49+
"locationId": {
50+
"value": "my-location"
51+
}
52+
}
53+
}
54+
]
55+
}
56+
}
57+
]
58+
}
59+
}
60+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
using NUnit.Framework;
6+
7+
namespace Nest.Tests.Unit.Reproduce
8+
{
9+
/// <summary>
10+
/// tests to reproduce reported errors
11+
/// </summary>
12+
[TestFixture]
13+
public class Reproduce579Tests : BaseJsonTests
14+
{
15+
public class ParentRecord
16+
{
17+
public int Id { get; set; }
18+
public string Name { get; set; }
19+
public string LocationId { get; set; }
20+
21+
public List<Guid> AssignedUsers { get; set; }
22+
public List<Guid> OverrideUsers { get; set; }
23+
public bool Unassigned { get; set; }
24+
public bool Global { get; set; }
25+
}
26+
27+
public class ChildRecord
28+
{
29+
30+
}
31+
32+
private static BaseQuery CreatePermissionsFilter(
33+
Guid CurrentUserId,
34+
List<Guid> usersToSearch,
35+
bool searchUnassigned,
36+
QueryDescriptor<ParentRecord> filter
37+
)
38+
{
39+
return filter.Bool(c => c
40+
.Should(
41+
s => s.Terms(p => p.AssignedUsers, usersToSearch),
42+
s => s.Term(p => p.OverrideUsers, CurrentUserId),
43+
s => s.Term(p => p.Unassigned, "true"),
44+
s => s.Term(p => p.Global, "true")
45+
)
46+
.MinimumNumberShouldMatch(1)
47+
);
48+
}
49+
50+
/// <summary>
51+
/// https://github.com/Mpdreamz/NEST/issues/579
52+
/// </summary>
53+
[Test]
54+
public void Issue579()
55+
{
56+
var currentUserId = Guid.Parse("8f836113-3392-4dad-9971-02ac915fd64a");
57+
var usersToSearch = new[]
58+
{
59+
Guid.Parse("6625c0bf-5414-4985-880c-3b34152e9453"),
60+
Guid.Parse("7ff7bc06-c75e-4ed1-9732-f3c6c9743670"),
61+
Guid.Parse("e4c48ebf-7602-43a3-b82f-1a83e2ae1c0b")
62+
}.ToList();
63+
64+
var searchUnassigned = false;
65+
var search = new SearchDescriptor<ParentRecord>()
66+
.Query(qd => qd
67+
.Bool(b => b
68+
.Must(
69+
q => q.Term(p => p.LocationId, "my-location")
70+
&& CreatePermissionsFilter(currentUserId, usersToSearch, searchUnassigned, q)
71+
)
72+
)
73+
);
74+
this.JsonEquals(search, MethodInfo.GetCurrentMethod());
75+
}
76+
77+
}
78+
}

0 commit comments

Comments
 (0)