Skip to content

Commit 665be12

Browse files
committed
Fix terms filter serialization of numeric terms
Serialization was being skipped when numeric terms were passed to terms filter due to incorrect IsConditionless() logic. This commit also fixes a NRE that was being thrown when passing a null terms value. An empty filter is now created instead. Closes #843.
1 parent 308de07 commit 665be12

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

src/Nest/DSL/Filter/FilterDescriptor.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ public FilterContainer Terms<K>(Expression<Func<T, K>> fieldDescriptor, IEnumera
684684
{
685685
ITermsFilter filter = new TermsFilterDescriptor();
686686
filter.Field = fieldDescriptor;
687-
filter.Terms = terms.Cast<object>();
687+
filter.Terms = (terms != null) ? terms.Cast<object>() : null;
688688
filter.Execution = Execution;
689689
return this.New(filter, f=>f.Terms = filter);
690690
}

src/Nest/DSL/Filter/TermsFilterDescriptor.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ bool IFilter.IsConditionless
4343
{
4444
return ((ITermsBaseFilter)this).Field.IsConditionless()
4545
|| !((ITermsFilter)this).Terms.HasAny()
46-
|| ((ITermsFilter)this).Terms.OfType<string>().All(s => s.IsNullOrEmpty())
46+
|| ((ITermsFilter)this).Terms.All(t => t is string && ((string)t).IsNullOrEmpty())
4747
|| ((ITermsFilter)this).Terms.All(t => t == null);
4848
}
4949
}

src/Tests/Nest.Tests.Unit/Search/Filter/Singles/TermsFilterJson.cs

+47
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,52 @@ public void TermsFilterWithConditionlessQueryWithCache()
124124
}";
125125
Assert.True(json.JsonEquals(expected), json);
126126
}
127+
128+
[Test]
129+
public void TermsFilterWithNumericTerms()
130+
{
131+
var query = Query<ElasticsearchProject>.Filtered(filtered => filtered
132+
.Filter(f =>
133+
f.Terms(t => t.LongValue, new long[] { 1, 2, 3 })
134+
)
135+
.Query(q => q.MatchAll())
136+
);
137+
138+
var json = TestElasticClient.Serialize(query);
139+
var expected = @"{
140+
filtered: {
141+
query: {
142+
match_all: {}
143+
},
144+
filter: {
145+
terms: {
146+
longValue: [1, 2, 3 ]
147+
}
148+
}
149+
}
150+
}";
151+
Assert.True(json.JsonEquals(expected), json);
152+
}
153+
154+
[Test]
155+
public void TermsFilterWithNullTerms()
156+
{
157+
var query = Query<ElasticsearchProject>.Filtered(filtered => filtered
158+
.Filter(f =>
159+
f.Terms(t => t.Name, null)
160+
)
161+
.Query(q => q.MatchAll())
162+
);
163+
var json = TestElasticClient.Serialize(query);
164+
var expected = @"{
165+
filtered: {
166+
query: {
167+
match_all: {}
168+
},
169+
filter: {}
170+
}
171+
}";
172+
Assert.True(json.JsonEquals(expected), json);
173+
}
127174
}
128175
}

0 commit comments

Comments
 (0)