Skip to content

Commit

Permalink
Add ignore_unmapped to nested, has_parent and has_child queries
Browse files Browse the repository at this point in the history
  • Loading branch information
russcam committed Jun 16, 2016
1 parent dc79003 commit 238d9f6
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
8 changes: 8 additions & 0 deletions src/Nest/QueryDsl/Joining/HasChild/HasChildQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public interface IHasChildQuery : IQuery

[JsonProperty("inner_hits")]
IInnerHits InnerHits { get; set; }

[JsonProperty("ignore_unmapped")]
bool? IgnoreUnmapped { get; set; }
}

public class HasChildQuery : QueryBase, IHasChildQuery
Expand All @@ -42,6 +45,7 @@ public class HasChildQuery : QueryBase, IHasChildQuery
public int? MaxChildren { get; set; }
public QueryContainer Query { get; set; }
public IInnerHits InnerHits { get; set; }
public bool? IgnoreUnmapped { get; set; }

internal override void InternalWrapInContainer(IQueryContainer c) => c.HasChild = this;
internal static bool IsConditionless(IHasChildQuery q) => q.Query == null || q.Query.IsConditionless || q.Type == null;
Expand All @@ -62,6 +66,7 @@ public class HasChildQueryDescriptor<T>
int? IHasChildQuery.MaxChildren { get; set; }
QueryContainer IHasChildQuery.Query { get; set; }
IInnerHits IHasChildQuery.InnerHits { get; set; }
bool? IHasChildQuery.IgnoreUnmapped { get; set; }

public HasChildQueryDescriptor()
{
Expand All @@ -84,5 +89,8 @@ public HasChildQueryDescriptor<T> Query(Func<QueryContainerDescriptor<T>, QueryC

public HasChildQueryDescriptor<T> InnerHits(Func<InnerHitsDescriptor<T>, IInnerHits> selector = null) =>
Assign(a => a.InnerHits = selector.InvokeOrDefault(new InnerHitsDescriptor<T>()));

public HasChildQueryDescriptor<T> IgnoreUnmapped(bool ignoreUnmapped = false) =>
Assign(a => a.IgnoreUnmapped = ignoreUnmapped);
}
}
9 changes: 8 additions & 1 deletion src/Nest/QueryDsl/Joining/HasParent/HasParentQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public interface IHasParentQuery : IQuery
[JsonProperty("inner_hits")]
IInnerHits InnerHits { get; set; }

[JsonProperty("ignore_unmapped")]
bool? IgnoreUnmapped { get; set; }
}

public class HasParentQuery : QueryBase, IHasParentQuery
Expand All @@ -37,12 +39,13 @@ public class HasParentQuery : QueryBase, IHasParentQuery
public bool? Score{ get; set; }
public QueryContainer Query { get; set; }
public IInnerHits InnerHits { get; set; }
public bool? IgnoreUnmapped { get; set; }

internal override void InternalWrapInContainer(IQueryContainer c) => c.HasParent = this;
internal static bool IsConditionless(IHasParentQuery q) => q.Query == null || q.Query.IsConditionless || q.Type == null;
}

public class HasParentQueryDescriptor<T>
public class HasParentQueryDescriptor<T>
: QueryDescriptorBase<HasParentQueryDescriptor<T>, IHasParentQuery>
, IHasParentQuery where T : class
{
Expand All @@ -56,6 +59,7 @@ public class HasParentQueryDescriptor<T>
bool? IHasParentQuery.Score { get; set; }
IInnerHits IHasParentQuery.InnerHits { get; set; }
QueryContainer IHasParentQuery.Query { get; set; }
bool? IHasParentQuery.IgnoreUnmapped { get; set; }

public HasParentQueryDescriptor() { Self.Type = TypeName.Create<T>(); }

Expand All @@ -72,5 +76,8 @@ public HasParentQueryDescriptor<T> Query(Func<QueryContainerDescriptor<T>, Query

public HasParentQueryDescriptor<T> InnerHits(Func<InnerHitsDescriptor<T>, IInnerHits> selector = null) =>
Assign(a => a.InnerHits = selector.InvokeOrDefault(new InnerHitsDescriptor<T>()));

public HasParentQueryDescriptor<T> IgnoreUnmapped(bool ignoreUnmapped = false) =>
Assign(a => a.IgnoreUnmapped = ignoreUnmapped);
}
}
15 changes: 11 additions & 4 deletions src/Nest/QueryDsl/Joining/Nested/NestedQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public interface INestedQuery : IQuery
[JsonProperty("inner_hits")]
IInnerHits InnerHits { get; set; }

[JsonProperty("ignore_unmapped")]
bool? IgnoreUnmapped { get; set; }
}

public class NestedQuery : QueryBase, INestedQuery
Expand All @@ -29,13 +31,14 @@ public class NestedQuery : QueryBase, INestedQuery
public QueryContainer Query { get; set; }
public Field Path { get; set; }
public IInnerHits InnerHits { get; set; }
public bool? IgnoreUnmapped { get; set; }

internal override void InternalWrapInContainer(IQueryContainer c) => c.Nested = this;
internal static bool IsConditionless(INestedQuery q) => q.Path == null || q.Query.IsConditionless();
}

[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public class NestedQueryDescriptor<T>
public class NestedQueryDescriptor<T>
: QueryDescriptorBase<NestedQueryDescriptor<T>, INestedQuery>
, INestedQuery where T : class
{
Expand All @@ -44,8 +47,9 @@ public class NestedQueryDescriptor<T>
QueryContainer INestedQuery.Query { get; set; }
Field INestedQuery.Path { get; set; }
IInnerHits INestedQuery.InnerHits { get; set; }
bool? INestedQuery.IgnoreUnmapped { get; set; }

public NestedQueryDescriptor<T> Query(Func<QueryContainerDescriptor<T>, QueryContainer> selector) =>
public NestedQueryDescriptor<T> Query(Func<QueryContainerDescriptor<T>, QueryContainer> selector) =>
Assign(a => a.Query = selector?.Invoke(new QueryContainerDescriptor<T>()));

public NestedQueryDescriptor<T> ScoreMode(NestedScoreMode scoreMode) => Assign(a => a.ScoreMode = scoreMode);
Expand All @@ -54,7 +58,10 @@ public NestedQueryDescriptor<T> Query(Func<QueryContainerDescriptor<T>, QueryCon

public NestedQueryDescriptor<T> Path(Expression<Func<T, object>> objectPath) => Assign(a => a.Path = objectPath);

public NestedQueryDescriptor<T> InnerHits(Func<InnerHitsDescriptor<T>, IInnerHits> selector = null) =>
Assign(a => a.InnerHits = selector.InvokeOrDefault(new InnerHitsDescriptor<T>()));
public NestedQueryDescriptor<T> InnerHits(Func<InnerHitsDescriptor<T>, IInnerHits> selector = null) =>
Assign(a => a.InnerHits = selector.InvokeOrDefault(new InnerHitsDescriptor<T>()));

public NestedQueryDescriptor<T> IgnoreUnmapped(bool ignoreUnmapped = false) =>
Assign(a => a.IgnoreUnmapped = ignoreUnmapped);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public HasParentUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usa
boost = 1.1,
type = "developer",
score = true,
ignore_unmapped = true,
query = new
{
match_all = new { }
Expand All @@ -34,7 +35,8 @@ public HasParentUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usa
Type = Infer.Type<Developer>(),
InnerHits = new InnerHits { Explain = true },
Query = new MatchAllQuery(),
Score = true
Score = true,
IgnoreUnmapped = true
};

protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project> q) => q
Expand All @@ -44,7 +46,7 @@ protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project>
.InnerHits(i=>i.Explain())
.Score(true)
.Query(qq=>qq.MatchAll())

.IgnoreUnmapped(true)
);

protected override ConditionlessWhen ConditionlessWhen => new ConditionlessWhen<IHasParentQuery>(a => a.HasParent)
Expand Down
5 changes: 4 additions & 1 deletion src/Tests/QueryDsl/Joining/Nested/NestedQueryUsageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public NestedUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage)
{ "curatedTags.name", new JArray("lorem", "ipsum") }
}
},
ignore_unmapped = false,
path = "curatedTags",
inner_hits = new
{
Expand All @@ -49,7 +50,8 @@ public NestedUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage)
{
Field = Field<Project>(p => p.CuratedTags.First().Name),
Terms = new[] { "lorem", "ipsum" }
}
},
IgnoreUnmapped = false
};

protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project> q) => q
Expand All @@ -64,6 +66,7 @@ protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project>
.Terms("lorem", "ipsum")
)
)
.IgnoreUnmapped()
);

protected override ConditionlessWhen ConditionlessWhen => new ConditionlessWhen<INestedQuery>(a => a.Nested)
Expand Down

0 comments on commit 238d9f6

Please sign in to comment.