Skip to content

Commit eb997c8

Browse files
committed
Added has_parent query and added score_type to has_child query
1 parent 179e448 commit eb997c8

10 files changed

+173
-0
lines changed

src/Nest.Tests.Unit/Nest.Tests.Unit.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
<Compile Include="Internals\Inferno\MapTypeNamesTests.cs" />
109109
<Compile Include="Internals\Serialize\OptOutTests.cs" />
110110
<Compile Include="Search\Filter\Singles\HasParentFilterJson.cs" />
111+
<Compile Include="Search\Query\Singles\HasParentQueryJson.cs" />
111112
<Compile Include="Search\Query\Singles\MultiMatch\MultiMatchJson.cs" />
112113
<Compile Include="Search\Query\Singles\Term\TermToStringJson.cs" />
113114
<Compile Include="Search\Rescore\RescoreTests.cs" />

src/Nest.Tests.Unit/Search/Query/Singles/HasChildQueryJson.cs

+2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ public void HasChildThisQuery()
1616
.HasChild<Person>(fz => fz
1717
.Query(qq=>qq.Term(f=>f.FirstName, "john"))
1818
.Scope("my_scope")
19+
.Score(ChildScoreType.avg)
1920
)
2021
);
2122
var json = TestElasticClient.Serialize(s);
2223
var expected = @"{ from: 0, size: 10, query :
2324
{ has_child: {
2425
type: ""people"",
2526
_scope: ""my_scope"",
27+
score_type: ""avg"",
2628
query: {
2729
term: {
2830
firstName: {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using NUnit.Framework;
2+
using Nest.Tests.MockData.Domain;
3+
4+
namespace Nest.Tests.Unit.Search.Query.Singles
5+
{
6+
[TestFixture]
7+
public class HasParentQueryJson
8+
{
9+
[Test]
10+
public void HasParentThisQuery()
11+
{
12+
var s = new SearchDescriptor<ElasticSearchProject>()
13+
.From(0)
14+
.Size(10)
15+
.Query(q => q
16+
.HasParent<Person>(fz => fz
17+
.Query(qq=>qq.Term(f=>f.FirstName, "john"))
18+
.Scope("my_scope")
19+
.Score()
20+
)
21+
);
22+
var json = TestElasticClient.Serialize(s);
23+
var expected = @"{ from: 0, size: 10, query :
24+
{ has_parent: {
25+
type: ""people"",
26+
_scope: ""my_scope"",
27+
score_type: ""score"",
28+
query: {
29+
term: {
30+
firstName: {
31+
value: ""john""
32+
}
33+
}
34+
}
35+
36+
}}}";
37+
Assert.True(json.JsonEquals(expected), json);
38+
}
39+
40+
}
41+
}

src/Nest/DSL/IQueryDescriptor.cs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ interface IQueryDescriptor<T>
1717
BaseQuery FuzzyLikeThis(Action<FuzzyLikeThisDescriptor<T>> selector);
1818
BaseQuery FuzzyNumeric(Action<FuzzyNumericQueryDescriptor<T>> selector);
1919
BaseQuery HasChild<K>(Action<HasChildQueryDescriptor<K>> selector) where K : class;
20+
BaseQuery HasParent<K>(Action<HasParentQueryDescriptor<K>> selector) where K : class;
2021
BaseQuery Ids(IEnumerable<string> types, IEnumerable<string> values);
2122
BaseQuery Ids(IEnumerable<string> values);
2223
BaseQuery Ids(string type, IEnumerable<string> values);

src/Nest/DSL/Query/ChildScoreType.cs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Nest
7+
{
8+
public enum ChildScoreType
9+
{
10+
none,
11+
avg,
12+
sum,
13+
max
14+
}
15+
}

src/Nest/DSL/Query/HasChildQueryDescriptor.cs

+13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Newtonsoft.Json;
66
using System.Linq.Expressions;
77
using Nest.Resolvers;
8+
using Newtonsoft.Json.Converters;
89

910
namespace Nest
1011
{
@@ -29,6 +30,10 @@ public HasChildQueryDescriptor()
2930
[JsonProperty("_scope")]
3031
internal string _Scope { get; set; }
3132

33+
[JsonProperty("score_type")]
34+
[JsonConverter(typeof(StringEnumConverter))]
35+
internal ChildScoreType? _ScoreType { get; set; }
36+
3237
[JsonProperty("query")]
3338
internal BaseQuery _QueryDescriptor { get; set; }
3439

@@ -48,6 +53,14 @@ public HasChildQueryDescriptor<T> Type(string type)
4853
this._Type = type;
4954
return this;
5055
}
56+
57+
public HasChildQueryDescriptor<T> Score(ChildScoreType? scoreType)
58+
{
59+
this._ScoreType = scoreType;
60+
return this;
61+
}
62+
63+
5164
[JsonProperty(PropertyName = "_cache")]
5265
internal bool? _Cache { get; set; }
5366

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using Newtonsoft.Json;
6+
using System.Linq.Expressions;
7+
using Nest.Resolvers;
8+
using Newtonsoft.Json.Converters;
9+
10+
namespace Nest
11+
{
12+
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
13+
public class HasParentQueryDescriptor<T> : IQuery where T : class
14+
{
15+
internal bool IsConditionless
16+
{
17+
get
18+
{
19+
return this._QueryDescriptor == null || this._QueryDescriptor.IsConditionless;
20+
}
21+
}
22+
23+
public HasParentQueryDescriptor()
24+
{
25+
this._Type = new TypeNameResolver().GetTypeNameFor<T>();
26+
}
27+
[JsonProperty("type")]
28+
internal TypeNameMarker _Type { get; set; }
29+
30+
[JsonProperty("_scope")]
31+
internal string _Scope { get; set; }
32+
33+
[JsonProperty("score_type")]
34+
[JsonConverter(typeof(StringEnumConverter))]
35+
internal ParentScoreType? _ScoreType { get; set; }
36+
37+
[JsonProperty("query")]
38+
internal BaseQuery _QueryDescriptor { get; set; }
39+
40+
public HasParentQueryDescriptor<T> Query(Func<QueryDescriptor<T>, BaseQuery> querySelector)
41+
{
42+
var q = new QueryDescriptor<T>();
43+
this._QueryDescriptor = querySelector(q);
44+
return this;
45+
}
46+
public HasParentQueryDescriptor<T> Scope(string scope)
47+
{
48+
this._Scope = scope;
49+
return this;
50+
}
51+
public HasParentQueryDescriptor<T> Type(string type)
52+
{
53+
this._Type = type;
54+
return this;
55+
}
56+
57+
public HasParentQueryDescriptor<T> Score(ParentScoreType? scoreType = ParentScoreType.score)
58+
{
59+
_ScoreType = scoreType;
60+
return this;
61+
}
62+
63+
[JsonProperty(PropertyName = "_name")]
64+
internal string _Name { get; set; }
65+
}
66+
}

src/Nest/DSL/Query/ParentScoreType.cs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Nest
7+
{
8+
public enum ParentScoreType
9+
{
10+
none = 0,
11+
score
12+
}
13+
}

src/Nest/DSL/QueryDescriptor.cs

+18
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public QueryDescriptor()
6464
internal FuzzyLikeThisDescriptor<T> FuzzyLikeThisDescriptor { get; set; }
6565
[JsonProperty(PropertyName = "has_child")]
6666
internal object HasChildQueryDescriptor { get; set; }
67+
[JsonProperty(PropertyName = "has_parent")]
68+
internal object HasParentQueryDescriptor { get; set; }
6769
[JsonProperty(PropertyName = "mlt")]
6870
internal MoreLikeThisQueryDescriptor<T> MoreLikeThisDescriptor { get; set; }
6971
[JsonProperty(PropertyName = "range")]
@@ -133,6 +135,7 @@ internal QueryDescriptor<T> Clone()
133135

134136
FuzzyLikeThisDescriptor = FuzzyLikeThisDescriptor,
135137
HasChildQueryDescriptor = HasChildQueryDescriptor,
138+
HasParentQueryDescriptor = HasParentQueryDescriptor,
136139
MoreLikeThisDescriptor = MoreLikeThisDescriptor,
137140
RangeQueryDescriptor = RangeQueryDescriptor,
138141

@@ -528,6 +531,21 @@ public BaseQuery HasChild<K>(Action<HasChildQueryDescriptor<K>> selector) where
528531
return new QueryDescriptor<T> { HasChildQueryDescriptor = this.HasChildQueryDescriptor };
529532
}
530533
/// <summary>
534+
/// The has_child query works the same as the has_child filter, by automatically wrapping the filter with a
535+
/// constant_score.
536+
/// </summary>
537+
/// <typeparam name="K">Type of the child</typeparam>
538+
public BaseQuery HasParent<K>(Action<HasParentQueryDescriptor<K>> selector) where K : class
539+
{
540+
var query = new HasParentQueryDescriptor<K>();
541+
selector(query);
542+
if (query.IsConditionless)
543+
return CreateConditionlessQueryDescriptor(query);
544+
545+
this.HasParentQueryDescriptor = query;
546+
return new QueryDescriptor<T> { HasParentQueryDescriptor = this.HasParentQueryDescriptor };
547+
}
548+
/// <summary>
531549
/// The top_children query runs the child query with an estimated hits size, and out of the hit docs, aggregates
532550
/// it into parent docs. If there aren’t enough parent docs matching the requested from/size search request,
533551
/// then it is run again with a wider (more hits) search.

src/Nest/Nest.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@
7171
<Compile Include="Domain\PathAndData.cs" />
7272
<Compile Include="DSL\BulkUpdateDescriptor.cs" />
7373
<Compile Include="DSL\Filter\HasParentFilterDescriptor.cs" />
74+
<Compile Include="DSL\Query\ChildScoreType.cs" />
75+
<Compile Include="DSL\Query\HasParentQueryDescriptor.cs" />
76+
<Compile Include="DSL\Query\ParentScoreType.cs" />
7477
<Compile Include="DSL\PercolatorDescriptor.cs" />
7578
<Compile Include="DSL\PercolateDescriptor.cs" />
7679
<Compile Include="DSL\Query\ExternalFieldDeclarationDescriptor.cs" />

0 commit comments

Comments
 (0)