Skip to content

Commit cf2807b

Browse files
committed
Fix #1253: terms filter/query should also work on IEnumerable field
1 parent ba24902 commit cf2807b

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

src/Nest/DSL/Filter/FilterDescriptor.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -731,8 +731,6 @@ public FilterContainer Term(Expression<Func<T, object>> fieldDescriptor, object
731731
/// </summary>
732732
public FilterContainer Term(string field, object term)
733733
{
734-
735-
736734
ITermFilter filter = new TermFilterDescriptor();
737735
filter.Field = field;
738736
filter.Value = term;
@@ -749,8 +747,20 @@ public FilterContainer Terms<K>(Expression<Func<T, K>> fieldDescriptor, IEnumera
749747
filter.Terms = (terms != null) ? terms.Cast<object>() : null;
750748
filter.Execution = Execution;
751749
return this.New(filter, f=>f.Terms = filter);
752-
}
753-
750+
}
751+
752+
/// <summary>
753+
/// Filters documents that have fields that match any of the provided terms (not analyzed).
754+
/// </summary>
755+
public FilterContainer Terms<K>(Expression<Func<T, IEnumerable<K>>> fieldDescriptor, IEnumerable<K> terms, TermsExecution? Execution = null)
756+
{
757+
ITermsFilter filter = new TermsFilterDescriptor();
758+
filter.Field = fieldDescriptor;
759+
filter.Terms = (terms != null) ? terms.Cast<object>() : null;
760+
filter.Execution = Execution;
761+
return this.New(filter, f => f.Terms = filter);
762+
}
763+
754764
/// <summary>
755765
/// Filters documents that have fields that match any of the provided terms (not analyzed).
756766
/// </summary>

src/Nest/DSL/Query/QueryDescriptor.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,19 @@ public QueryContainer Terms<K>(Expression<Func<T, K>> objectPath, IEnumerable<K>
126126
.Terms(terms)
127127
);
128128
}
129+
130+
/// <summary>
131+
/// A query that match on any (configurable) of the provided terms. This is a simpler syntax query for using a bool query with several term queries in the should clauses.
132+
/// </summary>
133+
public QueryContainer Terms<K>(Expression<Func<T, IEnumerable<K>>> objectPath, IEnumerable<K> terms)
134+
{
135+
PropertyPathMarker field = objectPath;
136+
return this.TermsDescriptor<K>(t => t
137+
.OnField(field)
138+
.Terms(terms)
139+
);
140+
}
141+
129142
/// <summary>
130143
/// A query that match on any (configurable) of the provided terms. This is a simpler syntax query for using a bool query with several term queries in the should clauses.
131144
/// </summary>

src/Nest/DSL/Query/TermsQueryDescriptor.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ public TermsQueryDescriptor<T, K> OnField(string field)
7373
return this;
7474
}
7575

76+
public TermsQueryDescriptor<T, K> OnField(PropertyPathMarker field)
77+
{
78+
((ITermsQuery)this).Field = field;
79+
return this;
80+
}
81+
7682
public TermsQueryDescriptor<T, K> OnField(Expression<Func<T, K>> objectPath)
7783
{
7884
((ITermsQuery)this).Field = objectPath;

0 commit comments

Comments
 (0)