Skip to content

Commit 4980075

Browse files
committed
Merge pull request #1369 from elastic/feature/inner-hits
Feature/inner hits
2 parents 5659302 + 5fcb9a4 commit 4980075

33 files changed

+1208
-48
lines changed

src/Nest/DSL/Filter/HasChildFilterDescriptor.cs

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ public interface IHasChildFilter : IFilter
1818

1919
[JsonProperty("filter")]
2020
IFilterContainer Filter { get; set; }
21+
22+
[JsonProperty("inner_hits")]
23+
[JsonConverter(typeof(ReadAsTypeConverter<InnerHits>))]
24+
IInnerHits InnerHits { get; set; }
25+
2126
}
2227

2328
public class HasChildFilter : PlainFilter, IHasChildFilter
@@ -30,25 +35,22 @@ protected internal override void WrapInContainer(IFilterContainer container)
3035
public TypeNameMarker Type { get; set; }
3136
public IQueryContainer Query { get; set; }
3237
public IFilterContainer Filter { get; set; }
38+
public IInnerHits InnerHits { get; set; }
3339
}
3440

3541
public class HasChildFilterDescriptor<T> : FilterBase, IHasChildFilter where T : class
3642
{
43+
private IHasChildFilter Self { get { return this; } }
44+
3745
bool IFilter.IsConditionless
3846
{
3947
get
4048
{
41-
var hf = ((IHasChildFilter)this);
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;
49+
if (Self.Type.IsNullOrEmpty()) return true;
50+
if (Self.Query == null && Self.Filter == null) return true;
51+
if (Self.Filter == null && Self.Query != null) return Self.Query.IsConditionless;
52+
if (Self.Filter != null && Self.Query == null) return Self.Filter.IsConditionless;
53+
return Self.Query.IsConditionless && Self.Filter.IsConditionless;
5254
}
5355
}
5456

@@ -58,28 +60,43 @@ bool IFilter.IsConditionless
5860

5961
IFilterContainer IHasChildFilter.Filter { get; set; }
6062

63+
IInnerHits IHasChildFilter.InnerHits { get; set; }
64+
6165
public HasChildFilterDescriptor()
6266
{
63-
((IHasChildFilter)this).Type = TypeNameMarker.Create<T>();
67+
Self.Type = TypeNameMarker.Create<T>();
6468
}
6569

6670
public HasChildFilterDescriptor<T> Query(Func<QueryDescriptor<T>, QueryContainer> querySelector)
6771
{
6872
var q = new QueryDescriptor<T>();
69-
((IHasChildFilter)this).Query = querySelector(q);
73+
Self.Query = querySelector(q);
7074
return this;
7175
}
7276

7377
public HasChildFilterDescriptor<T> Filter(Func<FilterDescriptor<T>, FilterContainer> filterSelector)
7478
{
7579
var f = new FilterDescriptor<T>();
76-
((IHasChildFilter) this).Filter = filterSelector(f);
80+
Self.Filter = filterSelector(f);
7781
return this;
7882
}
7983

8084
public HasChildFilterDescriptor<T> Type(string type)
8185
{
82-
((IHasChildFilter)this).Type = type;
86+
Self.Type = type;
87+
return this;
88+
}
89+
90+
public HasChildFilterDescriptor<T> InnerHits()
91+
{
92+
Self.InnerHits = new InnerHits();
93+
return this;
94+
}
95+
96+
public HasChildFilterDescriptor<T> InnerHits(Func<InnerHitsDescriptor<T>, IInnerHits> innerHitsSelector)
97+
{
98+
if (innerHitsSelector == null) return this;
99+
Self.InnerHits = innerHitsSelector(new InnerHitsDescriptor<T>());
83100
return this;
84101
}
85102
}

src/Nest/DSL/Filter/HasParentFilterDescriptor.cs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ public interface IHasParentFilter : IFilter
1616
[JsonProperty("query")]
1717
[JsonConverter(typeof(CompositeJsonConverter<ReadAsTypeConverter<QueryDescriptor<object>>, CustomJsonConverter>))]
1818
IQueryContainer Query { get; set; }
19+
20+
[JsonProperty("inner_hits")]
21+
[JsonConverter(typeof(ReadAsTypeConverter<InnerHits>))]
22+
IInnerHits InnerHits { get; set; }
23+
1924
}
2025

2126
public class HasParentFilter : PlainFilter, IHasParentFilter
@@ -27,40 +32,57 @@ protected internal override void WrapInContainer(IFilterContainer container)
2732

2833
public TypeNameMarker Type { get; set; }
2934
public IQueryContainer Query { get; set; }
35+
public IInnerHits InnerHits { get; set; }
3036
}
3137

3238
public class HasParentFilterDescriptor<T> : FilterBase, IHasParentFilter where T : class
3339
{
40+
private IHasParentFilter Self { get { return this; } }
41+
3442
TypeNameMarker IHasParentFilter.Type { get; set; }
3543

3644
IQueryContainer IHasParentFilter.Query { get; set; }
3745

46+
IInnerHits IHasParentFilter.InnerHits { get; set; }
47+
3848
bool IFilter.IsConditionless
3949
{
4050
get
4151
{
42-
var pf = ((IHasParentFilter)this);
43-
return pf.Query == null
44-
|| pf.Query.IsConditionless
45-
|| pf.Type.IsNullOrEmpty();
52+
return Self.Query == null
53+
|| Self.Query.IsConditionless
54+
|| Self.Type.IsNullOrEmpty();
4655
}
4756
}
4857

4958
public HasParentFilterDescriptor()
5059
{
51-
((IHasParentFilter)this).Type = TypeNameMarker.Create<T>();
60+
Self.Type = TypeNameMarker.Create<T>();
5261
}
5362

5463
public HasParentFilterDescriptor<T> Query(Func<QueryDescriptor<T>, QueryContainer> querySelector)
5564
{
5665
var q = new QueryDescriptor<T>();
57-
((IHasParentFilter)this).Query = querySelector(q);
66+
Self.Query = querySelector(q);
5867
return this;
5968
}
6069

6170
public HasParentFilterDescriptor<T> Type(string type)
6271
{
63-
((IHasParentFilter)this).Type = type;
72+
Self.Type = type;
73+
return this;
74+
}
75+
76+
public HasParentFilterDescriptor<T> InnerHits()
77+
{
78+
Self.InnerHits = new InnerHits();
79+
return this;
80+
}
81+
82+
public HasParentFilterDescriptor<T> InnerHits(Func<InnerHitsDescriptor<T>, IInnerHits> innerHitsSelector)
83+
{
84+
if (innerHitsSelector == null) return this;
85+
Self.InnerHits = innerHitsSelector(new InnerHitsDescriptor<T>());
6486
return this;
6587
}
6688
}

src/Nest/DSL/Filter/NestedFilterDescriptor.cs

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public interface INestedFilter : IFilter
2828

2929
[JsonProperty("join")]
3030
bool? Join { get; set; }
31+
32+
[JsonProperty("inner_hits")]
33+
[JsonConverter(typeof(ReadAsTypeConverter<InnerHits>))]
34+
IInnerHits InnerHits { get; set; }
35+
3136
}
3237

3338
public class NestedFilter : PlainFilter, INestedFilter
@@ -41,11 +46,14 @@ protected internal override void WrapInContainer(IFilterContainer container)
4146
public IFilterContainer Filter { get; set; }
4247
public IQueryContainer Query { get; set; }
4348
public PropertyPathMarker Path { get; set; }
44-
public bool? Join { get; set; }
49+
public bool? Join { get; set; }
50+
public IInnerHits InnerHits { get; set; }
4551
}
4652

4753
public class NestedFilterDescriptor<T> : FilterBase, INestedFilter where T : class
48-
{
54+
{
55+
private INestedFilter Self { get { return this; } }
56+
4957
NestedScore? INestedFilter.Score { get; set; }
5058

5159
IFilterContainer INestedFilter.Filter { get; set; }
@@ -56,54 +64,67 @@ public class NestedFilterDescriptor<T> : FilterBase, INestedFilter where T : cla
5664

5765
bool? INestedFilter.Join { get; set; }
5866

67+
IInnerHits INestedFilter.InnerHits { get; set; }
68+
5969
bool IFilter.IsConditionless
6070
{
6171
get
6272
{
63-
return (((INestedFilter)this).Query == null
64-
|| ((INestedFilter)this).Query.IsConditionless)
65-
&& (((INestedFilter)this).Filter == null
66-
|| ((INestedFilter)this).Filter.IsConditionless)
67-
;
73+
return (Self.Query == null || Self.Query.IsConditionless)
74+
&& (Self.Filter == null || Self.Filter.IsConditionless);
6875
}
6976
}
7077

7178
public NestedFilterDescriptor<T> Filter(Func<FilterDescriptor<T>, FilterContainer> filterSelector)
7279
{
7380
var q = new FilterDescriptor<T>();
74-
((INestedFilter)this).Filter = filterSelector(q);
81+
Self.Filter = filterSelector(q);
7582
return this;
7683
}
7784

7885
public NestedFilterDescriptor<T> Query(Func<QueryDescriptor<T>, QueryContainer> querySelector)
7986
{
8087
var q = new QueryDescriptor<T>();
81-
((INestedFilter)this).Query = querySelector(q);
88+
Self.Query = querySelector(q);
8289
return this;
8390
}
8491

8592
public NestedFilterDescriptor<T> Score(NestedScore score)
8693
{
87-
((INestedFilter)this).Score = score;
94+
Self.Score = score;
8895
return this;
8996
}
9097

9198
public NestedFilterDescriptor<T> Path(string path)
9299
{
93-
((INestedFilter)this).Path = path;
100+
Self.Path = path;
94101
return this;
95102
}
96103

97104
public NestedFilterDescriptor<T> Join(bool join = true)
98105
{
99-
((INestedFilter)this).Join = join;
106+
Self.Join = join;
100107
return this;
101108
}
102109

103110
public NestedFilterDescriptor<T> Path(Expression<Func<T, object>> objectPath)
104111
{
105-
((INestedFilter)this).Path = objectPath;
112+
Self.Path = objectPath;
106113
return this;
107114
}
115+
116+
public NestedFilterDescriptor<T> InnerHits()
117+
{
118+
Self.InnerHits = new InnerHits();
119+
return this;
120+
}
121+
122+
public NestedFilterDescriptor<T> InnerHits(Func<InnerHitsDescriptor<T>, IInnerHits> innerHitsSelector)
123+
{
124+
if (innerHitsSelector == null) return this;
125+
Self.InnerHits = innerHitsSelector(new InnerHitsDescriptor<T>());
126+
return this;
127+
}
128+
108129
}
109130
}

src/Nest/DSL/Query/HasChildQueryDescriptor.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public interface IHasChildQuery : IQuery
2121
[JsonProperty("query")]
2222
[JsonConverter(typeof(CompositeJsonConverter<ReadAsTypeConverter<QueryDescriptor<object>>, CustomJsonConverter>))]
2323
IQueryContainer Query { get; set; }
24+
25+
[JsonProperty("inner_hits")]
26+
[JsonConverter(typeof(ReadAsTypeConverter<InnerHits>))]
27+
IInnerHits InnerHits { get; set; }
28+
2429
}
2530

2631
public class HasChildQuery : PlainQuery, IHasChildQuery
@@ -35,6 +40,7 @@ protected override void WrapInContainer(IQueryContainer container)
3540
public TypeNameMarker Type { get; set; }
3641
public ChildScoreType? ScoreType { get; set; }
3742
public IQueryContainer Query { get; set; }
43+
public IInnerHits InnerHits { get; set; }
3844
}
3945

4046
public class HasChildQueryDescriptor<T> : IHasChildQuery where T : class
@@ -47,6 +53,8 @@ public class HasChildQueryDescriptor<T> : IHasChildQuery where T : class
4753

4854
IQueryContainer IHasChildQuery.Query { get; set; }
4955

56+
IInnerHits IHasChildQuery.InnerHits { get; set; }
57+
5058
string IQuery.Name { get; set; }
5159

5260
bool IQuery.IsConditionless
@@ -74,6 +82,7 @@ public HasChildQueryDescriptor<T> Query(Func<QueryDescriptor<T>, QueryContainer>
7482
Self.Query = querySelector(q);
7583
return this;
7684
}
85+
7786
public HasChildQueryDescriptor<T> Type(string type)
7887
{
7988
Self.Type = type;
@@ -86,5 +95,17 @@ public HasChildQueryDescriptor<T> Score(ChildScoreType? scoreType)
8695
return this;
8796
}
8897

98+
public HasChildQueryDescriptor<T> InnerHits()
99+
{
100+
Self.InnerHits = new InnerHits();
101+
return this;
102+
}
103+
104+
public HasChildQueryDescriptor<T> InnerHits(Func<InnerHitsDescriptor<T>, IInnerHits> innerHitsSelector)
105+
{
106+
if (innerHitsSelector == null) return this;
107+
Self.InnerHits = innerHitsSelector(new InnerHitsDescriptor<T>());
108+
return this;
109+
}
89110
}
90111
}

src/Nest/DSL/Query/HasParentQueryDescriptor.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public interface IHasParentQuery : IQuery
2222
[JsonConverter(typeof(CompositeJsonConverter<ReadAsTypeConverter<QueryDescriptor<object>>, CustomJsonConverter>))]
2323
IQueryContainer Query { get; set; }
2424

25+
[JsonProperty("inner_hits")]
26+
[JsonConverter(typeof(ReadAsTypeConverter<InnerHits>))]
27+
IInnerHits InnerHits { get; set; }
28+
2529
}
2630

2731
public class HasParentQuery : PlainQuery, IHasParentQuery
@@ -36,6 +40,7 @@ protected override void WrapInContainer(IQueryContainer container)
3640
public TypeNameMarker Type { get; set; }
3741
public ParentScoreType? ScoreType { get; set; }
3842
public IQueryContainer Query { get; set; }
43+
public IInnerHits InnerHits { get; set; }
3944
}
4045

4146
public class HasParentQueryDescriptor<T> : IHasParentQuery where T : class
@@ -45,6 +50,8 @@ public class HasParentQueryDescriptor<T> : IHasParentQuery where T : class
4550
TypeNameMarker IHasParentQuery.Type { get; set; }
4651

4752
ParentScoreType? IHasParentQuery.ScoreType { get; set; }
53+
54+
IInnerHits IHasParentQuery.InnerHits { get; set; }
4855

4956
string IQuery.Name { get; set; }
5057

@@ -87,5 +94,18 @@ public HasParentQueryDescriptor<T> Score(ParentScoreType? scoreType = ParentScor
8794
return this;
8895
}
8996

97+
public HasParentQueryDescriptor<T> InnerHits()
98+
{
99+
Self.InnerHits = new InnerHits();
100+
return this;
101+
}
102+
103+
public HasParentQueryDescriptor<T> InnerHits(Func<InnerHitsDescriptor<T>, IInnerHits> innerHitsSelector)
104+
{
105+
if (innerHitsSelector == null) return this;
106+
Self.InnerHits = innerHitsSelector(new InnerHitsDescriptor<T>());
107+
return this;
108+
}
109+
90110
}
91111
}

0 commit comments

Comments
 (0)