Skip to content

Commit a796f8e

Browse files
committed
Merge pull request #1308 from robertlyson/IncludeInParentMappingFromAttributes
IncludeInParent elastic property in NEST #1307
2 parents 59aa5d5 + 14d3f4c commit a796f8e

File tree

8 files changed

+116
-0
lines changed

8 files changed

+116
-0
lines changed

src/Nest/Domain/Mapping/Attributes/ElasticPropertyAttribute.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class ElasticPropertyAttribute : Attribute, IElasticPropertyAttribute
3030
public bool OmitNorms { get; set; }
3131
public bool OmitTermFrequencyAndPositions { get; set; }
3232
public bool IncludeInAll { get; set; }
33+
public bool IncludeInParent { get; set; }
3334
public bool Store { get; set; }
3435

3536
/// <summary>

src/Nest/Domain/Mapping/Attributes/IElasticPropertyAttribute.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public interface IElasticPropertyAttribute : IPropertyMapping
2020
bool OmitNorms { get; set; }
2121
bool OmitTermFrequencyAndPositions { get; set; }
2222
bool IncludeInAll { get; set; }
23+
bool IncludeInParent { get; set; }
2324
bool Store { get; set; }
2425

2526
bool DocValues { get; set; }

src/Nest/Resolvers/Writers/WritePropertiesFromAttributeVisitor.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ public void VisitBaseAttribute(IElasticPropertyAttribute att) {
102102
this._jsonWriter.WritePropertyName("include_in_all");
103103
this._jsonWriter.WriteValue("false");
104104
}
105+
if (att.IncludeInParent)
106+
{
107+
this._jsonWriter.WritePropertyName("include_in_parent");
108+
this._jsonWriter.WriteValue(true);
109+
}
105110
if (att.Store)
106111
{
107112
this._jsonWriter.WritePropertyName("store");
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using FluentAssertions;
7+
using Nest.Tests.MockData.Domain;
8+
using NUnit.Framework;
9+
10+
namespace Nest.Tests.Integration.Mapping
11+
{
12+
[TestFixture]
13+
public class MapFromAttributeTests : IntegrationTests
14+
{
15+
class MapFromAttributeObject
16+
{
17+
public string Name { get; set; }
18+
[ElasticProperty(Type = FieldType.Nested, IncludeInParent = true)]
19+
public List<NestedObject> NestedObjects { get; set; }
20+
[ElasticProperty(Type = FieldType.Nested)]
21+
public List<NestedObject> NestedObjectsDontIncludeInParent { get; set; }
22+
}
23+
24+
class NestedObject
25+
{
26+
public string Name { get; set; }
27+
}
28+
29+
[Test]
30+
public void InlcudeInParent()
31+
{
32+
var indicesResponse = this.Client.Map<MapFromAttributeObject>(m => m.MapFromAttributes());
33+
34+
indicesResponse.IsValid.Should().BeTrue();
35+
36+
var typeMapping = this.Client.GetMapping<MapFromAttributeObject>(i => i.Type("mapfromattributeobject"));
37+
typeMapping.Should().NotBeNull();
38+
39+
typeMapping.Mapping.Properties["nestedObjects"].Type.Name.Should().Be("nested");
40+
typeMapping.Mapping.Properties["nestedObjectsDontIncludeInParent"].Type.Name.Should().Be("nested");
41+
var nestedObject = typeMapping.Mapping.Properties["nestedObjects"] as NestedObjectMapping;
42+
var nestedObjectDontincludeInParent = typeMapping.Mapping.Properties["nestedObjectsDontIncludeInParent"] as NestedObjectMapping;
43+
nestedObject.Should().NotBeNull();
44+
nestedObjectDontincludeInParent.Should().NotBeNull();
45+
nestedObject.IncludeInParent.Should().BeTrue();
46+
nestedObjectDontincludeInParent.IncludeInParent.Should().NotHaveValue();
47+
}
48+
}
49+
}

src/Tests/Nest.Tests.Integration/Nest.Tests.Integration.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@
163163
<Compile Include="Core\Exists\IndexExistsTests.cs" />
164164
<Compile Include="Indices\StatsTests.cs" />
165165
<Compile Include="Core\AsyncTests.cs" />
166+
<Compile Include="Mapping\MapFromAttributeTests.cs" />
166167
<Compile Include="Mapping\MappingVisitorTests.cs" />
167168
<Compile Include="Mapping\GetMultipleMappingTests.cs" />
168169
<Compile Include="Reproduce\Reproduce1181Tests.cs" />
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using NUnit.Framework;
2+
3+
namespace Nest.Tests.Unit.Core.AttributeBasedMap
4+
{
5+
[TestFixture]
6+
public class IncludeInParentTests : BaseAttributeMappingTests
7+
{
8+
private class SimpleNestedMapParent
9+
{
10+
public string Name { get; set; }
11+
[ElasticProperty(Type = FieldType.Nested, IncludeInParent = true)]
12+
public SimpleNestedMapChild SimpleNestedMapChild { get; set; }
13+
[ElasticProperty(Type = FieldType.Nested)]
14+
public SimpleNestedMapChild SimpleNestedMapChildDontIncludeInParent { get; set; }
15+
}
16+
17+
private class SimpleNestedMapChild
18+
{
19+
public string Name { get; set; }
20+
}
21+
22+
[Test]
23+
public void TestIncludeInParent()
24+
{
25+
var json = this.CreateMapFor<SimpleNestedMapParent>();
26+
this.JsonEquals(json, System.Reflection.MethodInfo.GetCurrentMethod());
27+
}
28+
}
29+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"simplenestedmapparent": {
3+
"properties": {
4+
"name": {
5+
"type": "string"
6+
},
7+
"simpleNestedMapChild": {
8+
"type": "nested",
9+
"include_in_parent": true,
10+
"properties": {
11+
"name": {
12+
"type": "string"
13+
}
14+
}
15+
},
16+
"simpleNestedMapChildDontIncludeInParent": {
17+
"type": "nested",
18+
"properties": {
19+
"name": {
20+
"type": "string"
21+
}
22+
}
23+
}
24+
}
25+
}
26+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
<Compile Include="Cluster\NodeTests.cs" />
101101
<Compile Include="Cluster\Reroute\RerouteTests.cs" />
102102
<Compile Include="Cluster\State\StateTests.cs" />
103+
<Compile Include="Core\AttributeBasedMap\IncludeInParentTests.cs" />
103104
<Compile Include="Core\Bulk\BulkTests.cs" />
104105
<Compile Include="Core\Bulk\BulkUrlTests.cs" />
105106
<Compile Include="Core\Indices\Alias\AliasTests.cs" />
@@ -118,6 +119,9 @@
118119
<None Include="Cluster\Reroute\ClusterReroute.json">
119120
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
120121
</None>
122+
<None Include="Core\AttributeBasedMap\TestIncludeInParent.json">
123+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
124+
</None>
121125
<None Include="Core\Bulk\BulkIndexDetailsFixedPath.json">
122126
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
123127
</None>

0 commit comments

Comments
 (0)