Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Nest.Tests.Unit/Nest.Tests.Unit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
<Compile Include="Internals\Inferno\MapTypeNamesTests.cs" />
<Compile Include="Internals\Serialize\OptOutTests.cs" />
<Compile Include="Search\Filter\Singles\HasParentFilterJson.cs" />
<Compile Include="Search\Query\Singles\HasParentQueryJson.cs" />
<Compile Include="Search\Query\Singles\MultiMatch\MultiMatchJson.cs" />
<Compile Include="Search\Query\Singles\Term\TermToStringJson.cs" />
<Compile Include="Search\Rescore\RescoreTests.cs" />
Expand Down
2 changes: 2 additions & 0 deletions src/Nest.Tests.Unit/Search/Query/Singles/HasChildQueryJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ public void HasChildThisQuery()
.HasChild<Person>(fz => fz
.Query(qq=>qq.Term(f=>f.FirstName, "john"))
.Scope("my_scope")
.Score(ChildScoreType.avg)
)
);
var json = TestElasticClient.Serialize(s);
var expected = @"{ from: 0, size: 10, query :
{ has_child: {
type: ""people"",
_scope: ""my_scope"",
score_type: ""avg"",
query: {
term: {
firstName: {
Expand Down
41 changes: 41 additions & 0 deletions src/Nest.Tests.Unit/Search/Query/Singles/HasParentQueryJson.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using NUnit.Framework;
using Nest.Tests.MockData.Domain;

namespace Nest.Tests.Unit.Search.Query.Singles
{
[TestFixture]
public class HasParentQueryJson
{
[Test]
public void HasParentThisQuery()
{
var s = new SearchDescriptor<ElasticSearchProject>()
.From(0)
.Size(10)
.Query(q => q
.HasParent<Person>(fz => fz
.Query(qq=>qq.Term(f=>f.FirstName, "john"))
.Scope("my_scope")
.Score()
)
);
var json = TestElasticClient.Serialize(s);
var expected = @"{ from: 0, size: 10, query :
{ has_parent: {
type: ""people"",
_scope: ""my_scope"",
score_type: ""score"",
query: {
term: {
firstName: {
value: ""john""
}
}
}

}}}";
Assert.True(json.JsonEquals(expected), json);
}

}
}
1 change: 1 addition & 0 deletions src/Nest/DSL/IQueryDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface IQueryDescriptor<T>
BaseQuery FuzzyLikeThis(Action<FuzzyLikeThisDescriptor<T>> selector);
BaseQuery FuzzyNumeric(Action<FuzzyNumericQueryDescriptor<T>> selector);
BaseQuery HasChild<K>(Action<HasChildQueryDescriptor<K>> selector) where K : class;
BaseQuery HasParent<K>(Action<HasParentQueryDescriptor<K>> selector) where K : class;
BaseQuery Ids(IEnumerable<string> types, IEnumerable<string> values);
BaseQuery Ids(IEnumerable<string> values);
BaseQuery Ids(string type, IEnumerable<string> values);
Expand Down
15 changes: 15 additions & 0 deletions src/Nest/DSL/Query/ChildScoreType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nest
{
public enum ChildScoreType
{
none,
avg,
sum,
max
}
}
13 changes: 13 additions & 0 deletions src/Nest/DSL/Query/HasChildQueryDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Newtonsoft.Json;
using System.Linq.Expressions;
using Nest.Resolvers;
using Newtonsoft.Json.Converters;

namespace Nest
{
Expand All @@ -29,6 +30,10 @@ public HasChildQueryDescriptor()
[JsonProperty("_scope")]
internal string _Scope { get; set; }

[JsonProperty("score_type")]
[JsonConverter(typeof(StringEnumConverter))]
internal ChildScoreType? _ScoreType { get; set; }

[JsonProperty("query")]
internal BaseQuery _QueryDescriptor { get; set; }

Expand All @@ -48,6 +53,14 @@ public HasChildQueryDescriptor<T> Type(string type)
this._Type = type;
return this;
}

public HasChildQueryDescriptor<T> Score(ChildScoreType? scoreType)
{
this._ScoreType = scoreType;
return this;
}


[JsonProperty(PropertyName = "_cache")]
internal bool? _Cache { get; set; }

Expand Down
66 changes: 66 additions & 0 deletions src/Nest/DSL/Query/HasParentQueryDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using System.Linq.Expressions;
using Nest.Resolvers;
using Newtonsoft.Json.Converters;

namespace Nest
{
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public class HasParentQueryDescriptor<T> : IQuery where T : class
{
internal bool IsConditionless
{
get
{
return this._QueryDescriptor == null || this._QueryDescriptor.IsConditionless;
}
}

public HasParentQueryDescriptor()
{
this._Type = new TypeNameResolver().GetTypeNameFor<T>();
}
[JsonProperty("type")]
internal TypeNameMarker _Type { get; set; }

[JsonProperty("_scope")]
internal string _Scope { get; set; }

[JsonProperty("score_type")]
[JsonConverter(typeof(StringEnumConverter))]
internal ParentScoreType? _ScoreType { get; set; }

[JsonProperty("query")]
internal BaseQuery _QueryDescriptor { get; set; }

public HasParentQueryDescriptor<T> Query(Func<QueryDescriptor<T>, BaseQuery> querySelector)
{
var q = new QueryDescriptor<T>();
this._QueryDescriptor = querySelector(q);
return this;
}
public HasParentQueryDescriptor<T> Scope(string scope)
{
this._Scope = scope;
return this;
}
public HasParentQueryDescriptor<T> Type(string type)
{
this._Type = type;
return this;
}

public HasParentQueryDescriptor<T> Score(ParentScoreType? scoreType = ParentScoreType.score)
{
_ScoreType = scoreType;
return this;
}

[JsonProperty(PropertyName = "_name")]
internal string _Name { get; set; }
}
}
13 changes: 13 additions & 0 deletions src/Nest/DSL/Query/ParentScoreType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nest
{
public enum ParentScoreType
{
none = 0,
score
}
}
18 changes: 18 additions & 0 deletions src/Nest/DSL/QueryDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public QueryDescriptor()
internal FuzzyLikeThisDescriptor<T> FuzzyLikeThisDescriptor { get; set; }
[JsonProperty(PropertyName = "has_child")]
internal object HasChildQueryDescriptor { get; set; }
[JsonProperty(PropertyName = "has_parent")]
internal object HasParentQueryDescriptor { get; set; }
[JsonProperty(PropertyName = "mlt")]
internal MoreLikeThisQueryDescriptor<T> MoreLikeThisDescriptor { get; set; }
[JsonProperty(PropertyName = "range")]
Expand Down Expand Up @@ -133,6 +135,7 @@ internal QueryDescriptor<T> Clone()

FuzzyLikeThisDescriptor = FuzzyLikeThisDescriptor,
HasChildQueryDescriptor = HasChildQueryDescriptor,
HasParentQueryDescriptor = HasParentQueryDescriptor,
MoreLikeThisDescriptor = MoreLikeThisDescriptor,
RangeQueryDescriptor = RangeQueryDescriptor,

Expand Down Expand Up @@ -528,6 +531,21 @@ public BaseQuery HasChild<K>(Action<HasChildQueryDescriptor<K>> selector) where
return new QueryDescriptor<T> { HasChildQueryDescriptor = this.HasChildQueryDescriptor };
}
/// <summary>
/// The has_child query works the same as the has_child filter, by automatically wrapping the filter with a
/// constant_score.
/// </summary>
/// <typeparam name="K">Type of the child</typeparam>
public BaseQuery HasParent<K>(Action<HasParentQueryDescriptor<K>> selector) where K : class
{
var query = new HasParentQueryDescriptor<K>();
selector(query);
if (query.IsConditionless)
return CreateConditionlessQueryDescriptor(query);

this.HasParentQueryDescriptor = query;
return new QueryDescriptor<T> { HasParentQueryDescriptor = this.HasParentQueryDescriptor };
}
/// <summary>
/// The top_children query runs the child query with an estimated hits size, and out of the hit docs, aggregates
/// it into parent docs. If there aren�t enough parent docs matching the requested from/size search request,
/// then it is run again with a wider (more hits) search.
Expand Down
3 changes: 3 additions & 0 deletions src/Nest/Nest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@
<Compile Include="Domain\PathAndData.cs" />
<Compile Include="DSL\BulkUpdateDescriptor.cs" />
<Compile Include="DSL\Filter\HasParentFilterDescriptor.cs" />
<Compile Include="DSL\Query\ChildScoreType.cs" />
<Compile Include="DSL\Query\HasParentQueryDescriptor.cs" />
<Compile Include="DSL\Query\ParentScoreType.cs" />
<Compile Include="DSL\PercolatorDescriptor.cs" />
<Compile Include="DSL\PercolateDescriptor.cs" />
<Compile Include="DSL\Query\ExternalFieldDeclarationDescriptor.cs" />
Expand Down