Skip to content

Commit 8ca3ed1

Browse files
committed
Fix #971: FilteredQuery not properly handling conditionless query or filter
1 parent 29e6b08 commit 8ca3ed1

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

src/Nest/DSL/Query/QueryDescriptor.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ private QueryDescriptor<T> New(IQuery query, Action<IQueryContainer> fillPropert
5757

5858
if (fillProperty != null)
5959
fillProperty(q);
60+
6061
return q;
6162
}
6263

@@ -463,6 +464,14 @@ public QueryContainer Filtered(Action<FilteredQueryDescriptor<T>> selector)
463464
var query = new FilteredQueryDescriptor<T>();
464465
selector(query);
465466

467+
var filtered = query as IFilteredQuery;
468+
469+
if (filtered.Query.IsConditionless)
470+
filtered.Query = null;
471+
472+
if (filtered.Filter.IsConditionless)
473+
filtered.Filter = null;
474+
466475
return this.New(query, q => q.Filtered = query);
467476
}
468477

src/Tests/Nest.Tests.Integration/Nest.Tests.Integration.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@
186186
<Compile Include="Search\Filter\PrefixFilterTests.cs" />
187187
<Compile Include="Search\Filter\RangeFilterTests.cs" />
188188
<Compile Include="Search\Query\BoolQueryResults.cs" />
189+
<Compile Include="Search\Query\FilteredQueryTests.cs" />
189190
<Compile Include="Search\Query\SpanQueryTests.cs" />
190191
<Compile Include="Search\Query\TermToString.cs" />
191192
<Compile Include="Core\UpdateTests.cs" />
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using Nest.Tests.MockData.Domain;
2+
using NUnit.Framework;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using FluentAssertions;
9+
10+
namespace Nest.Tests.Integration.Search.Query
11+
{
12+
[TestFixture]
13+
public class FilteredQueryTests : IntegrationTests
14+
{
15+
[Test]
16+
public void QueryIsConditionless()
17+
{
18+
var response = this.Client.Search<ElasticsearchProject>(s => s
19+
.Query(q => q
20+
.Filtered(ff => ff
21+
.Query(qq => qq
22+
.Match(m => m
23+
.OnField(p => p.Name)
24+
.Query(null)
25+
)
26+
)
27+
.Filter(f => f
28+
.Term(p => p.Id, 1)
29+
)
30+
)
31+
)
32+
);
33+
34+
response.IsValid.Should().BeTrue();
35+
response.Total.ShouldBeEquivalentTo(1);
36+
}
37+
38+
[Test]
39+
public void FilterIsConditionless()
40+
{
41+
var response = this.Client.Search<ElasticsearchProject>(s => s
42+
.Query(q => q
43+
.Filtered(ff => ff
44+
.Query(qq => qq
45+
.Match(m => m
46+
.OnField(p => p.Name)
47+
.Query("elasticsearch")
48+
)
49+
)
50+
.Filter(f => f
51+
.Term(p => p.Id, null)
52+
)
53+
)
54+
)
55+
);
56+
57+
response.IsValid.Should().BeTrue();
58+
response.Total.Should().BeGreaterThan(1);
59+
}
60+
61+
[Test]
62+
public void QueryAndFilterAreConditionless()
63+
{
64+
var response = this.Client.Search<ElasticsearchProject>(s => s
65+
.Query(q => q
66+
.Filtered(ff => ff
67+
.Query(qq => qq
68+
.Match(m => m
69+
.OnField(p => p.Name)
70+
.Query(null)
71+
)
72+
)
73+
.Filter(f => f
74+
.Term(p => p.Id, null)
75+
)
76+
)
77+
)
78+
);
79+
80+
response.IsValid.Should().BeTrue();
81+
response.Total.Should().BeGreaterThan(10);
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)