Skip to content

Commit bbc966d

Browse files
committed
Add Attachment type
Cherry pick commits from 2.x - 8bcfb0a - 9105620 detect language not required when explicitly sending language
1 parent 4a538a4 commit bbc966d

File tree

9 files changed

+512
-80
lines changed

9 files changed

+512
-80
lines changed
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Newtonsoft.Json;
4+
5+
namespace Nest
6+
{
7+
[JsonConverter(typeof(AttachmentConverter))]
8+
public class Attachment
9+
{
10+
/// <summary>
11+
/// The author.
12+
/// </summary>
13+
[JsonProperty("author")]
14+
public string Author { get; set; }
15+
16+
/// <summary>
17+
/// The base64 encoded content. Can be explicitly set
18+
/// </summary>
19+
[JsonProperty("content")]
20+
public string Content { get; set; }
21+
22+
/// <summary>
23+
/// The length of the content before text extraction.
24+
/// </summary>
25+
[JsonProperty("content_length")]
26+
public long? ContentLength { get; set; }
27+
28+
/// <summary>
29+
/// The content type of the attachment. Can be explicitly set
30+
/// </summary>
31+
[JsonProperty("content_type")]
32+
public string ContentType { get; set; }
33+
34+
/// <summary>
35+
/// The date of the attachment.
36+
/// </summary>
37+
[JsonProperty("date")]
38+
public DateTime? Date { get; set; }
39+
40+
/// <summary>
41+
/// The keywords in the attachment.
42+
/// </summary>
43+
[JsonProperty("keywords")]
44+
public string Keywords { get; set; }
45+
46+
/// <summary>
47+
/// The language of the attachment. Can be explicitly set.
48+
/// </summary>
49+
[JsonProperty("language")]
50+
public string Language { get; set; }
51+
52+
/// <summary>
53+
/// Detect the language of the attachment. Language detection is
54+
/// disabled by default.
55+
/// </summary>
56+
[JsonProperty("detect_language")]
57+
public bool? DetectLanguage { get; set; }
58+
59+
/// <summary>
60+
/// The name of the attachment. Can be explicitly set
61+
/// </summary>
62+
[JsonProperty("name")]
63+
public string Name { get; set; }
64+
65+
/// <summary>
66+
/// The title of the attachment.
67+
/// </summary>
68+
[JsonProperty("title")]
69+
public string Title { get; set; }
70+
71+
/// <summary>
72+
/// Determines how many characters are extracted when indexing the content.
73+
/// By default, 100000 characters are extracted when indexing the content.
74+
/// -1 can be set to extract all text, but note that all the text needs to be
75+
/// allowed to be represented in memory
76+
/// </summary>
77+
[JsonProperty("indexed_chars")]
78+
public long? IndexedCharacters { get; set; }
79+
80+
/// <summary>
81+
/// Whether the attachment contains explicit metadata in addition to the
82+
/// content. Used at indexing time to determine the serialized form of the
83+
/// attachment.
84+
/// </summary>
85+
[JsonIgnore]
86+
public bool ContainsMetadata => !Name.IsNullOrEmpty() ||
87+
!ContentType.IsNullOrEmpty() ||
88+
!Language.IsNullOrEmpty() ||
89+
DetectLanguage.HasValue ||
90+
IndexedCharacters.HasValue;
91+
}
92+
93+
internal class AttachmentConverter : JsonConverter
94+
{
95+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
96+
{
97+
var attachment = (Attachment)value;
98+
if (!attachment.ContainsMetadata)
99+
{
100+
writer.WriteValue(attachment.Content);
101+
}
102+
else
103+
{
104+
writer.WriteStartObject();
105+
writer.WritePropertyName("_content");
106+
writer.WriteValue(attachment.Content);
107+
108+
if (!string.IsNullOrEmpty(attachment.Name))
109+
{
110+
writer.WritePropertyName("_name");
111+
writer.WriteValue(attachment.Name);
112+
}
113+
114+
if (!string.IsNullOrEmpty(attachment.ContentType))
115+
{
116+
writer.WritePropertyName("_content_type");
117+
writer.WriteValue(attachment.ContentType);
118+
}
119+
120+
if (!string.IsNullOrEmpty(attachment.Language))
121+
{
122+
writer.WritePropertyName("_language");
123+
writer.WriteValue(attachment.Language);
124+
}
125+
126+
if (attachment.DetectLanguage.HasValue)
127+
{
128+
writer.WritePropertyName("_detect_language");
129+
writer.WriteValue(attachment.DetectLanguage.Value);
130+
}
131+
132+
if (attachment.IndexedCharacters.HasValue)
133+
{
134+
writer.WritePropertyName("_indexed_chars");
135+
writer.WriteValue(attachment.IndexedCharacters.Value);
136+
}
137+
138+
writer.WriteEndObject();
139+
}
140+
}
141+
142+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
143+
{
144+
if (reader.TokenType == JsonToken.String)
145+
{
146+
return new Attachment { Content = (string)reader.Value };
147+
}
148+
149+
if (reader.TokenType == JsonToken.StartObject)
150+
{
151+
var attachment = new Attachment();
152+
while (reader.Read())
153+
{
154+
if (reader.TokenType == JsonToken.PropertyName)
155+
{
156+
var propertyName = (string)reader.Value;
157+
switch (propertyName)
158+
{
159+
case "_content":
160+
attachment.Content = reader.ReadAsString();
161+
break;
162+
case "_name":
163+
attachment.Name = reader.ReadAsString();
164+
break;
165+
case "_content_type":
166+
attachment.ContentType = reader.ReadAsString();
167+
break;
168+
case "_language":
169+
attachment.Language = reader.ReadAsString();
170+
break;
171+
case "_detect_language":
172+
attachment.DetectLanguage = reader.ReadAsBoolean();
173+
break;
174+
case "_indexed_chars":
175+
reader.Read();
176+
attachment.IndexedCharacters = (long?)reader.Value;
177+
break;
178+
}
179+
}
180+
if (reader.TokenType == JsonToken.EndObject)
181+
{
182+
break;
183+
}
184+
}
185+
return attachment;
186+
}
187+
return null;
188+
}
189+
190+
public override bool CanConvert(Type objectType) => objectType == typeof(Attachment);
191+
}
192+
}

src/Nest/Mapping/Visitor/PropertyWalker.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ private IProperty InferProperty(Type type)
137137
if (type == typeof(CompletionField))
138138
return new CompletionProperty();
139139

140+
if (type == typeof(Attachment))
141+
return new AttachmentProperty();
142+
140143
return new ObjectProperty();
141144
}
142145

src/Nest/Nest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,7 @@
850850
<Compile Include="Mapping\Types\CorePropertyDescriptorBase.cs" />
851851
<Compile Include="Mapping\Types\PropertyDescriptorBase.cs" />
852852
<Compile Include="Mapping\Types\PropertyJsonConverter.cs" />
853+
<Compile Include="Mapping\Types\Specialized\Attachment\Attachment.cs" />
853854
<Compile Include="Mapping\Types\Specialized\Attachment\AttachmentAttribute.cs" />
854855
<Compile Include="Mapping\Types\Specialized\Attachment\AttachmentProperty.cs" />
855856
<Compile Include="Mapping\Types\Specialized\Completion\CompletionAttribute.cs" />

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

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

src/Tests/Framework/MockData/Attachment.cs

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/Tests/Mapping/Types/Specialized/Attachment/AttachmentMappingTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
namespace Tests.Mapping.Types.Specialized.Attachment
1010
{
11-
public class AttachmentMappingTests : TypeMappingTestBase<Tests.Framework.MockData.Attachment>
11+
public class AttachmentMappingTests : TypeMappingTestBase<Nest.Attachment>
1212
{
1313
protected override object ExpectJson => new
1414
{
1515
properties = new
1616
{
17-
file = new
17+
content = new
1818
{
1919
type = "attachment",
2020
fields = new
@@ -66,14 +66,14 @@ protected override void AttributeBasedSerializes()
6666
// TODO: Implement
6767
}
6868

69-
protected override Func<PropertiesDescriptor<Framework.MockData.Attachment>, IPromise<IProperties>> FluentProperties => p => p
69+
protected override Func<PropertiesDescriptor<Nest.Attachment>, IPromise<IProperties>> FluentProperties => p => p
7070
.Attachment(a => a
71-
.Name(n => n.File)
71+
.Name(n => n.Content)
7272
.AuthorField(d => d
7373
.Name(n => n.Author)
7474
)
7575
.FileField(d => d
76-
.Name(n => n.File)
76+
.Name(n => n.Content)
7777
)
7878
.ContentLengthField(d => d
7979
.Name(n => n.ContentLength)

src/Tests/Tests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@
242242
<Compile Include="Framework\EndpointTests\Clusters\TypeOfCluster.cs" />
243243
<Compile Include="Framework\ManagedElasticsearch\Process\ElasticsearchNode.cs" />
244244
<Compile Include="Framework\ManagedElasticsearch\Process\ObservableProcess.cs" />
245-
<Compile Include=".\Framework\MockData\Attachment.cs" />
246245
<Compile Include=".\Framework\MockData\CommitActivity.cs" />
247246
<Compile Include=".\Framework\MockData\Developer.cs" />
248247
<Compile Include=".\Framework\MockData\Gender.cs" />

src/Tests/tests.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ elasticsearch_version: 5.0.0-alpha2
77
force_reseed: false
88
# do not spawn nodes as part of the test setup if we find a node is already running
99
# this is opt in during development in CI we never want to see our tests running against an already running node
10-
test_against_already_running_elasticsearch: true
10+
test_against_already_running_elasticsearch: true

0 commit comments

Comments
 (0)