Skip to content

Commit 4e72571

Browse files
committed
Allow Attachment type to work with both mapper-attachment and ingest attachment plugin
1 parent 321868a commit 4e72571

File tree

5 files changed

+67
-19
lines changed

5 files changed

+67
-19
lines changed

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

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,26 +154,82 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
154154
if (reader.TokenType == JsonToken.PropertyName)
155155
{
156156
var propertyName = (string)reader.Value;
157-
switch (propertyName)
157+
switch (propertyName.ToLowerInvariant())
158158
{
159159
case "_content":
160+
case "content":
160161
attachment.Content = reader.ReadAsString();
161162
break;
162163
case "_name":
164+
case "name":
163165
attachment.Name = reader.ReadAsString();
164166
break;
167+
case "author":
168+
attachment.Author = reader.ReadAsString();
169+
break;
170+
case "keywords":
171+
attachment.Keywords = reader.ReadAsString();
172+
break;
173+
case "date":
174+
reader.Read();
175+
switch (reader.TokenType)
176+
{
177+
case JsonToken.String:
178+
var value = (string)reader.Value;
179+
if (!string.IsNullOrEmpty(value))
180+
attachment.Date = Convert.ToDateTime(value);
181+
break;
182+
case JsonToken.Date:
183+
attachment.Date = (DateTime?)reader.Value;
184+
break;
185+
}
186+
break;
165187
case "_content_type":
188+
case "content_type":
189+
case "contenttype":
166190
attachment.ContentType = reader.ReadAsString();
167191
break;
192+
case "_content_length":
193+
case "content_length":
194+
case "contentlength":
195+
reader.Read();
196+
switch (reader.TokenType)
197+
{
198+
case JsonToken.String:
199+
var value = (string)reader.Value;
200+
if (!string.IsNullOrEmpty(value))
201+
attachment.ContentLength = Convert.ToInt64(value);
202+
break;
203+
case JsonToken.Integer:
204+
case JsonToken.Float:
205+
attachment.ContentLength = (long?)reader.Value;
206+
break;
207+
208+
}
209+
break;
168210
case "_language":
211+
case "language":
169212
attachment.Language = reader.ReadAsString();
170213
break;
171214
case "_detect_language":
172215
attachment.DetectLanguage = reader.ReadAsBoolean();
173216
break;
174217
case "_indexed_chars":
218+
case "indexed_chars":
175219
reader.Read();
176-
attachment.IndexedCharacters = (long?)reader.Value;
220+
switch (reader.TokenType)
221+
{
222+
case JsonToken.String:
223+
var value = (string)reader.Value;
224+
if (!string.IsNullOrEmpty(value))
225+
attachment.IndexedCharacters = Convert.ToInt64(value);
226+
break;
227+
case JsonToken.Integer:
228+
case JsonToken.Float:
229+
attachment.IndexedCharacters = (long?)reader.Value;
230+
break;
231+
232+
}
177233
break;
178234
}
179235
}

src/Tests/Document/Single/Attachment/AttachmentApiTests.cs

Lines changed: 6 additions & 5 deletions
Large diffs are not rendered by default.
Binary file not shown.

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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,7 @@ protected override void IntegrationSetup(IElasticClient client, CallUniqueValues
6363
{
6464
Field = "content",
6565
TargetField = "attachment"
66-
},
67-
// rename extracted content_type and content_length fields
68-
new RenameProcessor
69-
{
70-
Field = "attachment.content_type",
71-
TargetField = "attachment.contentType"
72-
},
73-
new RenameProcessor
74-
{
75-
Field = "attachment.content_length",
76-
TargetField = "attachment.contentLength"
77-
},
66+
}
7867
}
7968
});
8069
}
@@ -140,6 +129,7 @@ protected override void ExpectResponse(IIndexResponse response)
140129
attachment.Author.Should().Be("Russ Cam");
141130
attachment.Language.Should().Be("fr");
142131
attachment.ContentLength.Should().Be(93);
132+
attachment.Content.Should().Contain("mapper-attachment support");
143133
}
144134
}
145135
}

src/Tests/Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,7 @@
627627
<None Include="client-concepts.asciidoc" />
628628
<None Include="common-options.asciidoc" />
629629
<None Include="connection-pooling.asciidoc" />
630+
<None Include="Document\Single\Attachment\Attachment_Test_Document.pdf" />
630631
<None Include="high-level.asciidoc" />
631632
<None Include="index.asciidoc" />
632633
<None Include="intro.asciidoc" />

0 commit comments

Comments
 (0)