Skip to content

Commit 5a78a43

Browse files
committed
Reindex attachment honours ingest-attachment field names (#3600)
This commit updates the Attachment Json converter to use the ingest-geoip attachment field names when serializing an attachment. The current serialization implementation is based on mapper-attachments plugin which was deprecated in 5.x and removed in 6.x. Fixes #3502
1 parent ec32967 commit 5a78a43

File tree

2 files changed

+160
-29
lines changed

2 files changed

+160
-29
lines changed

src/Nest/Mapping/Types/Specialized/Attachment/Attachment.cs

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@ public class Attachment
1818
/// attachment.
1919
/// </summary>
2020
[JsonIgnore]
21-
public bool ContainsMetadata => !Name.IsNullOrEmpty() ||
21+
public bool ContainsMetadata =>
22+
!Author.IsNullOrEmpty() ||
23+
ContentLength.HasValue ||
2224
!ContentType.IsNullOrEmpty() ||
23-
!Language.IsNullOrEmpty() ||
25+
Date.HasValue ||
2426
DetectLanguage.HasValue ||
25-
IndexedCharacters.HasValue;
27+
IndexedCharacters.HasValue ||
28+
!Keywords.IsNullOrEmpty() ||
29+
!Language.IsNullOrEmpty() ||
30+
!Name.IsNullOrEmpty() ||
31+
!Title.IsNullOrEmpty();
2632

2733
/// <summary>
2834
/// The base64 encoded content. Can be explicitly set
@@ -94,51 +100,86 @@ internal class AttachmentConverter : JsonConverter
94100
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
95101
{
96102
var attachment = (Attachment)value;
97-
if (!attachment.ContainsMetadata)
98-
writer.WriteValue(attachment.Content);
99-
else
103+
if (attachment.ContainsMetadata)
100104
{
101105
writer.WriteStartObject();
102-
writer.WritePropertyName("_content");
103-
writer.WriteValue(attachment.Content);
104106

105-
if (!string.IsNullOrEmpty(attachment.Name))
107+
if (!string.IsNullOrEmpty(attachment.Content))
106108
{
107-
writer.WritePropertyName("_name");
108-
writer.WriteValue(attachment.Name);
109+
writer.WritePropertyName("content");
110+
writer.WriteValue(attachment.Content);
111+
}
112+
113+
if (!string.IsNullOrEmpty(attachment.Author))
114+
{
115+
writer.WritePropertyName("author");
116+
writer.WriteValue(attachment.Author);
117+
}
118+
119+
if (!string.IsNullOrEmpty(attachment.ContentType))
120+
{
121+
writer.WritePropertyName("content_length");
122+
writer.WriteValue(attachment.ContentLength);
109123
}
110124

111125
if (!string.IsNullOrEmpty(attachment.ContentType))
112126
{
113-
writer.WritePropertyName("_content_type");
127+
writer.WritePropertyName("content_type");
114128
writer.WriteValue(attachment.ContentType);
115129
}
116130

117-
if (!string.IsNullOrEmpty(attachment.Language))
131+
if (attachment.Date.HasValue)
118132
{
119-
writer.WritePropertyName("_language");
120-
writer.WriteValue(attachment.Language);
133+
writer.WritePropertyName("date");
134+
writer.WriteValue(attachment.Date.Value);
121135
}
122136

123137
if (attachment.DetectLanguage.HasValue)
124138
{
125-
writer.WritePropertyName("_detect_language");
139+
writer.WritePropertyName("detect_language");
126140
writer.WriteValue(attachment.DetectLanguage.Value);
127141
}
128142

129143
if (attachment.IndexedCharacters.HasValue)
130144
{
131-
writer.WritePropertyName("_indexed_chars");
145+
writer.WritePropertyName("indexed_chars");
132146
writer.WriteValue(attachment.IndexedCharacters.Value);
133147
}
134148

149+
if (!string.IsNullOrEmpty(attachment.Keywords))
150+
{
151+
writer.WritePropertyName("keywords");
152+
writer.WriteValue(attachment.Keywords);
153+
}
154+
155+
if (!string.IsNullOrEmpty(attachment.Language))
156+
{
157+
writer.WritePropertyName("language");
158+
writer.WriteValue(attachment.Language);
159+
}
160+
161+
if (!string.IsNullOrEmpty(attachment.Name))
162+
{
163+
writer.WritePropertyName("name");
164+
writer.WriteValue(attachment.Name);
165+
}
166+
167+
if (!string.IsNullOrEmpty(attachment.Title))
168+
{
169+
writer.WritePropertyName("title");
170+
writer.WriteValue(attachment.Title);
171+
}
172+
135173
writer.WriteEndObject();
136174
}
175+
else
176+
writer.WriteValue(attachment.Content);
137177
}
138178

139179
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
140180
{
141-
if (reader.TokenType == JsonToken.String) return new Attachment { Content = (string)reader.Value };
181+
if (reader.TokenType == JsonToken.String)
182+
return new Attachment { Content = (string)reader.Value };
142183

143184
if (reader.TokenType == JsonToken.StartObject)
144185
{
@@ -181,13 +222,11 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
181222
case "_content_type":
182223
case "content_type":
183224
case "contenttype":
184-
case "contentType":
185225
attachment.ContentType = reader.ReadAsString();
186226
break;
187227
case "_content_length":
188228
case "content_length":
189229
case "contentlength":
190-
case "contentLength":
191230
reader.Read();
192231
switch (reader.TokenType)
193232
{
@@ -207,6 +246,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
207246
attachment.Language = reader.ReadAsString();
208247
break;
209248
case "_detect_language":
249+
case "detect_language":
210250
attachment.DetectLanguage = reader.ReadAsBoolean();
211251
break;
212252
case "_indexed_chars":

src/Tests/Tests/Document/Single/Index/IndexIngestAttachmentApiTests.cs

Lines changed: 100 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using Elastic.Xunit.XunitPlumbing;
45
using Elasticsearch.Net;
56
using FluentAssertions;
67
using Nest;
@@ -29,16 +30,22 @@ static TestDocument()
2930
public static string TestPdfDocument { get; }
3031
}
3132

33+
public class IngestedAttachment
34+
{
35+
public Attachment Attachment { get; set; }
36+
public string Content { get; set; }
37+
public int Id { get; set; }
38+
}
39+
3240
public class IndexIngestAttachmentApiTests
3341
: ApiIntegrationTestBase<IntrusiveOperationCluster, IIndexResponse,
34-
IIndexRequest<IndexIngestAttachmentApiTests.IngestedAttachment>,
35-
IndexDescriptor<IndexIngestAttachmentApiTests.IngestedAttachment>,
36-
IndexRequest<IndexIngestAttachmentApiTests.IngestedAttachment>>
42+
IIndexRequest<IngestedAttachment>,
43+
IndexDescriptor<IngestedAttachment>,
44+
IndexRequest<IngestedAttachment>>
3745
{
3846
public IndexIngestAttachmentApiTests(IntrusiveOperationCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
3947

4048
protected override bool ExpectIsValid => true;
41-
4249
protected override object ExpectJson => new { id = 1, content = Content };
4350
protected override int ExpectStatusCode => 201;
4451

@@ -69,7 +76,7 @@ protected override string UrlPath
6976
Content = Content
7077
};
7178

72-
private static string PipelineId { get; } = "pipeline-" + Guid.NewGuid().ToString("N").Substring(0, 8);
79+
private static string PipelineId { get; } = "pipeline-" + RandomString();
7380

7481
protected override void IntegrationSetup(IElasticClient client, CallUniqueValues values)
7582
{
@@ -138,12 +145,96 @@ protected override void ExpectResponse(IIndexResponse response)
138145
attachment.ContentLength.Should().Be(96);
139146
attachment.Content.Should().Contain("mapper-attachment support");
140147
}
148+
}
149+
150+
public class ReindexAttachmentApiTests : IClusterFixture<IntrusiveOperationCluster>
151+
{
152+
private readonly IElasticClient _client;
153+
154+
private static string RandomString { get; } = Guid.NewGuid().ToString("N").Substring(0, 8);
155+
private static string Index { get; } = "project" + RandomString;
156+
private static string PipelineId { get; } = "pipeline-" + RandomString;
157+
private static string Content => TestDocument.TestPdfDocument;
158+
159+
private IngestedAttachment Document => new IngestedAttachment
160+
{
161+
Id = 1,
162+
Content = Content
163+
};
164+
165+
public ReindexAttachmentApiTests(IntrusiveOperationCluster cluster)
166+
{
167+
_client = cluster.Client;
168+
169+
_client.PutPipeline(new PutPipelineRequest(PipelineId)
170+
{
171+
Description = "Attachment pipeline test",
172+
Processors = new List<IProcessor>
173+
{
174+
new AttachmentProcessor
175+
{
176+
Field = "content",
177+
TargetField = "attachment"
178+
}
179+
}
180+
});
181+
182+
var createIndexResponse = _client.CreateIndex(Index, c => c
183+
.Mappings(m => m
184+
.Map<IngestedAttachment>(mm => mm
185+
.Properties(p => p
186+
.Text(s => s
187+
.Name(f => f.Content)
188+
)
189+
.Object<Attachment>(o => o
190+
.Name(f => f.Attachment)
191+
)
192+
)
193+
)
194+
)
195+
);
196+
197+
createIndexResponse.ShouldBeValid();
198+
var indexResponse = _client.Index<IngestedAttachment>(Document, i => i
199+
.Index(Index)
200+
.Refresh(Refresh.True)
201+
.Pipeline(PipelineId)
202+
);
203+
204+
indexResponse.ShouldBeValid();
205+
}
141206

142-
public class IngestedAttachment
207+
[I]
208+
public void ReindexIntoAnotherIndexCorrectlySerializesAttachment()
143209
{
144-
public Attachment Attachment { get; set; }
145-
public string Content { get; set; }
146-
public int Id { get; set; }
210+
var getResponse = _client.Get<IngestedAttachment>(1, g => g
211+
.Index(Index)
212+
);
213+
214+
getResponse.ShouldBeValid();
215+
var ingestedAttachment = getResponse.Source;
216+
ingestedAttachment.Should().NotBeNull();
217+
218+
var indexResponse = _client.Index<IngestedAttachment>(ingestedAttachment, i => i
219+
.Index(Index + "2")
220+
.Refresh(Refresh.True)
221+
);
222+
223+
indexResponse.ShouldBeValid();
224+
225+
getResponse = _client.Get<IngestedAttachment>(1, g => g
226+
.Index(Index + "2")
227+
);
228+
229+
getResponse.ShouldBeValid();
230+
ingestedAttachment.Should().NotBeNull();
231+
ingestedAttachment.Attachment.Title.Should().Be("Attachment Test Document");
232+
ingestedAttachment.Attachment.Keywords.Should().Be("nest,test,document");
233+
ingestedAttachment.Attachment.Date.Should().Be(new DateTime(2016, 12, 08, 3, 5, 13, DateTimeKind.Utc));
234+
ingestedAttachment.Attachment.ContentType.Should().Be("application/pdf");
235+
ingestedAttachment.Attachment.Author.Should().Be("Russ Cam");
236+
ingestedAttachment.Attachment.Language.Should().Be("fr");
237+
ingestedAttachment.Attachment.ContentLength.Should().Be(96);
147238
}
148239
}
149240
}

0 commit comments

Comments
 (0)