Skip to content

Commit e40c144

Browse files
committed
Merge pull request elastic#1218 from BurlakovNick/feature/has-child-subfilter-support
Added filter support for HasChild filter.
2 parents fe26dc7 + 225b086 commit e40c144

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

src/Nest/DSL/Filter/HasChildFilterDescriptor.cs

+24-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public interface IHasChildFilter : IFilter
1515

1616
[JsonProperty("query")]
1717
IQueryContainer Query { get; set; }
18+
19+
[JsonProperty("filter")]
20+
IFilterContainer Filter { get; set; }
1821
}
1922

2023
public class HasChildFilter : PlainFilter, IHasChildFilter
@@ -26,6 +29,7 @@ protected internal override void WrapInContainer(IFilterContainer container)
2629

2730
public TypeNameMarker Type { get; set; }
2831
public IQueryContainer Query { get; set; }
32+
public IFilterContainer Filter { get; set; }
2933
}
3034

3135
public class HasChildFilterDescriptor<T> : FilterBase, IHasChildFilter where T : class
@@ -35,15 +39,25 @@ bool IFilter.IsConditionless
3539
get
3640
{
3741
var hf = ((IHasChildFilter)this);
38-
return hf.Query == null || hf.Query.IsConditionless || hf.Type.IsNullOrEmpty();
42+
if (hf.Type.IsNullOrEmpty())
43+
return true;
44+
45+
if (hf.Query == null && hf.Filter == null)
46+
return true;
47+
if (hf.Filter == null && hf.Query != null)
48+
return hf.Query.IsConditionless;
49+
if (hf.Filter != null && hf.Query == null)
50+
return hf.Filter.IsConditionless;
51+
return hf.Query.IsConditionless && hf.Filter.IsConditionless;
3952
}
4053
}
4154

4255
TypeNameMarker IHasChildFilter.Type { get; set; }
4356

44-
4557
IQueryContainer IHasChildFilter.Query { get; set; }
4658

59+
IFilterContainer IHasChildFilter.Filter { get; set; }
60+
4761
public HasChildFilterDescriptor()
4862
{
4963
((IHasChildFilter)this).Type = TypeNameMarker.Create<T>();
@@ -55,7 +69,14 @@ public HasChildFilterDescriptor<T> Query(Func<QueryDescriptor<T>, QueryContainer
5569
((IHasChildFilter)this).Query = querySelector(q);
5670
return this;
5771
}
58-
72+
73+
public HasChildFilterDescriptor<T> Filter(Func<FilterDescriptor<T>, FilterContainer> filterSelector)
74+
{
75+
var f = new FilterDescriptor<T>();
76+
((IHasChildFilter) this).Filter = filterSelector(f);
77+
return this;
78+
}
79+
5980
public HasChildFilterDescriptor<T> Type(string type)
6081
{
6182
((IHasChildFilter)this).Type = type;

src/Tests/Nest.Tests.Unit/QueryParsers/Filter/HasChildFilterTests.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@ public void HasChild_Deserializes(string cacheName, string cacheKey, bool cache)
1414
var hasChildFilter = this.SerializeThenDeserialize(cacheName, cacheKey, cache,
1515
f=>f.HasChild,
1616
f=>f.HasChild<Person>(d=>d
17-
.Query(q=>q.Term(p=>p.FirstName, "value"))
17+
.Query(q => q.Term(p => p.FirstName, "value"))
18+
.Filter(q => q.Term(p => p.Age, 42))
1819
)
1920
);
2021

2122
var query = hasChildFilter.Query;
2223
query.Should().NotBeNull();
2324
query.Term.Field.Should().Be("firstName");
25+
26+
var filter = hasChildFilter.Filter;
27+
filter.Should().NotBeNull();
28+
filter.Term.Field.Should().Be("age");
2429
}
2530

2631
}

src/Tests/Nest.Tests.Unit/Search/Filter/ConditionLess/ConditionLessTests.cs

+2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ public void HasChildTest()
116116
{
117117
this.DoConditionlessFilter(f => f.HasChild<Person>(null));
118118
this.DoConditionlessFilter(f => f.HasChild<Person>(q=>q.Query(qq=>qq.Term(p=>p.FirstName, string.Empty))));
119+
this.DoConditionlessFilter(f => f.HasChild<Person>(q=>q.Filter(qq=>qq.Term(p=>p.FirstName, string.Empty))));
120+
this.DoConditionlessFilter(f => f.HasChild<Person>(q => q.Query(qq => qq.Term(p => p.FirstName, string.Empty)).Filter(qq => qq.Term(p => p.FirstName, string.Empty))));
119121
}
120122

121123

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ public void HasChildFilter()
1212
var s = new SearchDescriptor<ElasticsearchProject>().From(0).Size(10)
1313
.Filter(ff=>ff
1414
.HasChild<Person>(d=>d
15-
.Query(q=>q.Term(p=>p.FirstName, "value"))
15+
.Query(q => q.Term(p => p.FirstName, "value"))
16+
.Filter(q => q.Term(p => p.Age, 42))
1617
)
1718
);
1819

@@ -27,6 +28,11 @@ public void HasChildFilter()
2728
""value"": ""value""
2829
}
2930
}
31+
},
32+
""filter"": {
33+
""term"": {
34+
""age"": 42
35+
}
3036
}
3137
}
3238
}

0 commit comments

Comments
 (0)