Skip to content

Commit cf1e21e

Browse files
committed
Merge branch 'develop' into fix/aggs1.5
2 parents 59b9f8a + 50c60bf commit cf1e21e

16 files changed

+272
-17
lines changed

docs/build/nest/core/multi-search.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
<p>The multi search API allows to execute several search requests within the same API.</p>
44
<h3 id="fluent-syntax">Fluent Syntax</h3>
55
<pre><code>var result = client.MultiSearch(ms =&gt; ms
6-
.Search&lt;ElasticsearchProject&gt;(&quot;esproj&quot;, s =&gt; s.MatchAll())
6+
.Search&lt;ElasticsearchProject&gt;(&quot;projects&quot;, s =&gt; s.MatchAll())
77
.Search&lt;Person&gt;(&quot;people&quot;, s =&gt; s.MatchAll())
88
);
99
</code></pre><h3 id="object-initializer-syntax">Object Initializer Syntax</h3>
1010
<pre><code>var request = new MultiSearchRequest
1111
{
1212
Operations = new Dictionary&lt;string, ISearchRequest&gt;
1313
{
14-
{ &quot;esproj&quot;, new SearchRequest
14+
{ &quot;projects&quot;, new SearchRequest&lt;ElasticsearchProject&gt;
1515
{
1616
Query = new QueryContainer(new MatchAllQuery())
1717
}
1818
},
19-
{ &quot;people&quot;, new SearchRequest
19+
{ &quot;people&quot;, new SearchRequest&lt;Person&gt;
2020
{
2121
Query = new QueryContainer(new MatchAllQuery())
2222
}
@@ -28,7 +28,7 @@ <h3 id="fluent-syntax">Fluent Syntax</h3>
2828
</code></pre><h2 id="handling-the-multi-search-response">Handling the Multi Search Response</h2>
2929
<p><code>MultiSearch</code> returns an <code>IMultiSearchResponse</code> object. Each <code>SearchResponse&lt;T&gt;</code> can be retrieved using the corresponding name that was specified in the request.</p>
3030
<pre><code>// returns a SearchResponse&lt;ElasticsearchProject&gt;&gt;
31-
var projects = result.GetResponse&lt;ElasticsearchProject&gt;(&quot;esproj&quot;);
31+
var projects = result.GetResponse&lt;ElasticsearchProject&gt;(&quot;projects&quot;);
3232

3333
// returns a SearchResponse&lt;Person&gt;&gt;
3434
var people = result.GetResponse&lt;Person&gt;(&quot;people&quot;);

docs/contents/nest/core/multi-search.markdown

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ The multi search API allows to execute several search requests within the same A
1313
### Fluent Syntax
1414

1515
var result = client.MultiSearch(ms => ms
16-
.Search<ElasticsearchProject>("esproj", s => s.MatchAll())
16+
.Search<ElasticsearchProject>("projects", s => s.MatchAll())
1717
.Search<Person>("people", s => s.MatchAll())
1818
);
1919

@@ -24,12 +24,12 @@ The multi search API allows to execute several search requests within the same A
2424
{
2525
Operations = new Dictionary<string, ISearchRequest>
2626
{
27-
{ "esproj", new SearchRequest
27+
{ "projects", new SearchRequest<ElasticsearchProject>
2828
{
2929
Query = new QueryContainer(new MatchAllQuery())
3030
}
3131
},
32-
{ "people", new SearchRequest
32+
{ "people", new SearchRequest<Person>
3333
{
3434
Query = new QueryContainer(new MatchAllQuery())
3535
}
@@ -44,7 +44,7 @@ The multi search API allows to execute several search requests within the same A
4444
`MultiSearch` returns an `IMultiSearchResponse` object. Each `SearchResponse<T>` can be retrieved using the corresponding name that was specified in the request.
4545

4646
// returns a SearchResponse<ElasticsearchProject>>
47-
var projects = result.GetResponse<ElasticsearchProject>("esproj");
47+
var projects = result.GetResponse<ElasticsearchProject>("projects");
4848

4949
// returns a SearchResponse<Person>>
5050
var people = result.GetResponse<Person>("people");

src/Nest/DSL/PutMappingDescriptor.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ public PutMappingDescriptor<T> InitializeUsing(RootObjectMapping rootObjectMappi
9999
}
100100

101101
/// <summary>
102-
/// Convenience method to map from most of the object from the attributes/properties.
103-
/// Later calls can override whatever is set is by this call.
104-
/// This helps mapping all the ints as ints, floats as floats etcetera withouth having to be overly verbose in your fluent mapping
102+
/// Convenience method to map as much as it can based on ElasticType attributes set on the type.
103+
/// <pre>This method also automatically sets up mappings for known values types (int, long, double, datetime, etcetera)</pre>
104+
/// <pre>Class types default to object and Enums to int</pre>
105+
/// <pre>Later calls can override whatever is set is by this call.</pre>
105106
/// </summary>
106-
/// <returns></returns>
107107
public PutMappingDescriptor<T> MapFromAttributes(int maxRecursion = 0)
108108
{
109109
//TODO no longer needed when we have an IPutMappingRequest

src/Nest/Resolvers/Converters/MultiSearchConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
9696
{
9797
var descriptor = m.Descriptor.Value;
9898
var concreteTypeSelector = descriptor.TypeSelector;
99-
var baseType = m.Descriptor.Value.ClrType;
99+
var baseType = m.Descriptor.Value.ClrType ?? typeof(object);
100100

101101
var generic = MakeDelegateMethodInfo.MakeGenericMethod(baseType);
102102

src/Nest/Resolvers/Writers/TypeMappingWriter.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ public class TypeMappingWriter
1515
private readonly Type _type;
1616
private readonly IConnectionSettingsValues _connectionSettings;
1717
private readonly NestSerializer _elasticSerializer;
18+
19+
private readonly static string _noFieldTypeMessage =
20+
"Property {0} on type {1} has an ElasticProperty attribute but its FieldType (Type = ) can not be inferred and is not set explicitly while calling MapFromAttributes";
21+
1822
private ElasticInferrer Infer { get; set; }
1923

2024
private int MaxRecursion { get; set; }
@@ -136,7 +140,7 @@ internal void WriteProperties(JsonWriter jsonWriter)
136140

137141
var propertyName = this.Infer.PropertyName(p);
138142
var type = GetElasticSearchType(att, p);
139-
143+
140144
if (type == null) //could not get type from attribute or infer from CLR type.
141145
continue;
142146

@@ -194,6 +198,11 @@ private string GetElasticSearchType(IElasticPropertyAttribute att, PropertyInfo
194198
if (fieldType == null || fieldType == FieldType.None)
195199
{
196200
fieldType = this.GetFieldTypeFromType(p.PropertyType);
201+
if (fieldType == null && att != null)
202+
{
203+
var message = _noFieldTypeMessage.F(p.Name, this._type.Name);
204+
throw new DslException(message);
205+
}
197206
}
198207

199208
return this.GetElasticSearchTypeFromFieldType(fieldType);
@@ -234,8 +243,8 @@ private string GetElasticSearchTypeFromFieldType(FieldType? fieldType)
234243
return "boolean";
235244
case FieldType.Completion:
236245
return "completion";
237-
case FieldType.Nested:
238-
return "nested";
246+
case FieldType.Nested:
247+
return "nested";
239248
case FieldType.Object:
240249
return "object";
241250
default:
@@ -255,6 +264,9 @@ private string GetElasticSearchTypeFromFieldType(FieldType? fieldType)
255264
if (propertyType == typeof(string))
256265
return FieldType.String;
257266

267+
if (propertyType.IsEnum)
268+
return FieldType.Integer;
269+
258270
if (propertyType.IsValueType)
259271
{
260272
switch (propertyType.Name)
@@ -284,7 +296,7 @@ private static Type GetUnderlyingType(Type type)
284296
if (type.IsArray)
285297
return type.GetElementType();
286298

287-
if (type.IsGenericType && type.GetGenericArguments().Length == 1 && (type.GetInterface("IEnumerable") != null || Nullable.GetUnderlyingType(type) != null))
299+
if (type.IsGenericType && type.GetGenericArguments().Length == 1 && (type.GetInterface("IEnumerable") != null || Nullable.GetUnderlyingType(type) != null))
288300
return type.GetGenericArguments()[0];
289301

290302
return type;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@
179179
<Compile Include="Reproduce\Reproduce994Tests.cs" />
180180
<Compile Include="Reproduce\Reproduce986Tests.cs" />
181181
<Compile Include="Reproduce\Reproduce960Tests.cs" />
182+
<Compile Include="Reproduce\Reproduce1279Tests.cs" />
182183
<Compile Include="Search\Filter\AndOrNotFilterTests.cs" />
183184
<Compile Include="Search\Filter\ScriptFilterTests.cs" />
184185
<Compile Include="Search\Filter\MissingExistsFilterTests.cs" />
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using NUnit.Framework;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using FluentAssertions;
8+
using Nest.Tests.MockData.Domain;
9+
using Newtonsoft.Json.Linq;
10+
11+
namespace Nest.Tests.Integration.Reproduce
12+
{
13+
[TestFixture]
14+
public class Reproduce1279Tests : IntegrationTests
15+
{
16+
[Test]
17+
public void MultiSearchNullArgumentException()
18+
{
19+
var request = new MultiSearchRequest
20+
{
21+
Operations = new Dictionary<string, ISearchRequest>
22+
{
23+
{ "test", new SearchRequest
24+
{
25+
Query = new QueryContainer(new MatchAllQuery()),
26+
Types = new TypeNameMarker[] { typeof(Product), typeof(ElasticsearchProject) },
27+
TypeSelector = (o, h) => typeof(ElasticsearchProject)
28+
}
29+
}
30+
}
31+
};
32+
33+
var result = Client.MultiSearch(request);
34+
result.IsValid.Should().BeTrue();
35+
var response = result.GetResponse<object>("test");
36+
var projects = response.Documents.OfType<ElasticsearchProject>().ToList();
37+
projects.Count.Should().BeGreaterThan(0);
38+
}
39+
}
40+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"myclass": {
3+
"properties": {
4+
"myEnum": {
5+
"index": "not_analyzed",
6+
"type": "string"
7+
}
8+
}
9+
}
10+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using NUnit.Framework;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Reflection;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Nest.Tests.Unit.Core.Map.Enums
10+
{
11+
[TestFixture]
12+
public class EnumMappingTests : BaseJsonTests
13+
{
14+
private class MyClass
15+
{
16+
public MyEnum MyEnum { get; set; }
17+
}
18+
19+
private enum MyEnum
20+
{
21+
Value1,
22+
Value2
23+
}
24+
25+
[Test]
26+
public void EnumShouldMapToIntByDefault()
27+
{
28+
var result = this._client.Map<MyClass>(m => m.MapFromAttributes());
29+
this.JsonEquals(result.ConnectionStatus.Request, MethodBase.GetCurrentMethod());
30+
}
31+
32+
[Test]
33+
public void EnumCanBeOverriddenAfterMapFromAttributes()
34+
{
35+
var result = this._client.Map<MyClass>(m => m
36+
.MapFromAttributes()
37+
.Properties(props=>props
38+
.String(s=>s
39+
.Name(p=>p.MyEnum)
40+
.Index(FieldIndexOption.NotAnalyzed)
41+
)
42+
)
43+
);
44+
this.JsonEquals(result.ConnectionStatus.Request, MethodBase.GetCurrentMethod());
45+
}
46+
47+
}
48+
49+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"myclass": {
3+
"properties": {
4+
"myEnum": {
5+
"type": "integer"
6+
}
7+
}
8+
}
9+
}

0 commit comments

Comments
 (0)