Skip to content

Commit 7d0fcbc

Browse files
committed
created a multisyntax converter for indices and types and added ids query
1 parent b38d3a3 commit 7d0fcbc

File tree

14 files changed

+183
-61
lines changed

14 files changed

+183
-61
lines changed

src/Nest/CommonAbstractions/Infer/Id/Id.cs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,31 +42,4 @@ internal string GetString(IConnectionSettingsValues nestSettings)
4242
return s ?? this.Value?.ToString();
4343
}
4444
}
45-
46-
//TODO do we need this? discus with @gmarz
47-
public class Ids : IUrlParameter
48-
{
49-
internal IEnumerable<Id> _ids;
50-
51-
internal Ids(Id id) { _ids = new List<Id> { id }; }
52-
internal Ids(IEnumerable<Id> ids) { _ids = ids; }
53-
54-
public static Ids Single(Id id) => new Ids(id);
55-
public static Ids Single<T>(T document) where T : class => new Ids(Id.From(document));
56-
public static Ids Many(IEnumerable<Id> ids) => new Ids(ids);
57-
public static Ids Many(params Id[] ids) => new Ids(ids);
58-
59-
public string GetString(IConnectionConfigurationValues settings)
60-
{
61-
var nestSettings = settings as IConnectionSettingsValues;
62-
if (nestSettings == null)
63-
throw new Exception("Tried to pass ids on querystring but it could not be resolved because no nest settings are available");
64-
65-
var ids = _ids
66-
.Select(i=>i.GetString(nestSettings))
67-
.ToList();
68-
if (ids.Any(id => id.IsNullOrEmpty())) throw new ArgumentException("One or more ids were null or empty", "ids");
69-
return string.Join(",", ids);
70-
}
71-
}
7245
}

src/Nest/CommonAbstractions/Infer/Indices/Indices.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Nest
99
{
10-
[JsonConverter(typeof(IndicesJsonConverter))]
10+
[JsonConverter(typeof(IndicesMultiSyntaxJsonConverter))]
1111
public class Indices : Union<Indices.AllIndicesMarker, Indices.ManyIndices>, IUrlParameter
1212
{
1313
public class AllIndicesMarker { internal AllIndicesMarker() { } }

src/Nest/CommonAbstractions/Infer/Indices/IndicesJsonConverter.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Nest
99
{
1010
internal class IndicesJsonConverter : JsonConverter
1111
{
12-
public override bool CanConvert(Type objectType) => typeof(Indices) == objectType;
12+
public override bool CanConvert(Type objectType) => typeof(Types) == objectType;
1313

1414
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
1515
{
@@ -23,19 +23,29 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
2323
if (contract == null || contract.ConnectionSettings == null)
2424
throw new Exception("If you use a custom contract resolver be sure to subclass from ElasticResolver");
2525
marker.Match(
26-
all=> writer.WriteValue("_all"),
27-
many => writer.WriteValue(((IUrlParameter)marker).GetString(contract.ConnectionSettings))
26+
all=> writer.WriteNull(),
27+
many =>
28+
{
29+
writer.WriteStartArray();
30+
foreach(var m in many.Indices.Cast<IUrlParameter>())
31+
writer.WriteValue(m.GetString(contract.ConnectionSettings));
32+
writer.WriteEndArray();
33+
}
2834
);
2935
}
3036

3137
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
3238
{
33-
if (reader.TokenType == JsonToken.String)
39+
40+
if (reader.TokenType != JsonToken.StartArray) return null;
41+
var indices = new List<IndexName> { };
42+
while (reader.TokenType != JsonToken.EndArray)
3443
{
35-
string indices = reader.Value.ToString();
36-
return (Indices)indices;
44+
var index = reader.ReadAsString();
45+
if (reader.TokenType == JsonToken.String)
46+
indices.Add(index);
3747
}
38-
return null;
48+
return new Indices(indices);
3949
}
4050

4151
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Newtonsoft.Json;
5+
using Nest.Resolvers;
6+
using Elasticsearch.Net.Serialization;
7+
8+
namespace Nest
9+
{
10+
internal class IndicesMultiSyntaxJsonConverter : JsonConverter
11+
{
12+
public override bool CanConvert(Type objectType) => typeof(Indices) == objectType;
13+
14+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
15+
{
16+
var marker = value as Indices;
17+
if (marker == null)
18+
{
19+
writer.WriteNull();
20+
return;
21+
}
22+
var contract = serializer.ContractResolver as SettingsContractResolver;
23+
if (contract == null || contract.ConnectionSettings == null)
24+
throw new Exception("If you use a custom contract resolver be sure to subclass from ElasticResolver");
25+
marker.Match(
26+
all=> writer.WriteValue("_all"),
27+
many => writer.WriteValue(((IUrlParameter)marker).GetString(contract.ConnectionSettings))
28+
);
29+
}
30+
31+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
32+
{
33+
if (reader.TokenType == JsonToken.String)
34+
{
35+
string indices = reader.Value.ToString();
36+
return (Indices)indices;
37+
}
38+
return null;
39+
}
40+
41+
}
42+
}

src/Nest/CommonAbstractions/Infer/TypeName/TypeName.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ public bool EqualsString(string other)
8080
{
8181
return !other.IsNullOrEmpty() && other == this.Name;
8282
}
83-
public string GetString(IConnectionConfigurationValues settings) => ((IUrlParameter)(Types)(Types.Type(this))).GetString(settings);
83+
84+
string IUrlParameter.GetString(IConnectionConfigurationValues settings) => ((IUrlParameter)(Types)(Types.Type(this))).GetString(settings);
8485

8586
public static TypeName From<T>() => typeof(T);
8687

src/Nest/CommonAbstractions/Infer/Types/Types.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public static Types Parse(string typesString)
4848
public static implicit operator Types(AllTypesMarker all) => new Types(all);
4949
public static implicit operator Types(ManyTypes many) => new Types(many);
5050
public static implicit operator Types(TypeName type) => new ManyTypes(new[] { type });
51+
public static implicit operator Types(TypeName[] type) => new ManyTypes(type);
5152
public static implicit operator Types(Type type) => new ManyTypes(new TypeName[] { type });
5253

5354
string IUrlParameter.GetString(IConnectionConfigurationValues settings)

src/Nest/CommonAbstractions/Infer/Types/TypesJsonConverter.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,28 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
2424
throw new Exception("If you use a custom contract resolver be sure to subclass from ElasticResolver");
2525
marker.Match(
2626
all=> writer.WriteNull(),
27-
many => writer.WriteValue(((IUrlParameter)marker).GetString(contract.ConnectionSettings))
27+
many =>
28+
{
29+
writer.WriteStartArray();
30+
foreach(var m in many.Types.Cast<IUrlParameter>())
31+
writer.WriteValue(m.GetString(contract.ConnectionSettings));
32+
writer.WriteEndArray();
33+
}
2834
);
2935
}
3036

3137
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
3238
{
33-
if (reader.TokenType == JsonToken.String)
39+
40+
if (reader.TokenType != JsonToken.StartArray) return null;
41+
var types = new List<TypeName> { };
42+
while (reader.TokenType != JsonToken.EndArray)
3443
{
35-
string types = reader.Value.ToString();
36-
return (Types)types;
44+
var type = reader.ReadAsString();
45+
if (reader.TokenType == JsonToken.String)
46+
types.Add(type);
3747
}
38-
return null;
48+
return new Types(types);
3949
}
4050

4151
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Newtonsoft.Json;
5+
using Nest.Resolvers;
6+
using Elasticsearch.Net.Serialization;
7+
8+
namespace Nest
9+
{
10+
internal class TypesMultiSyntaxJsonConverter : JsonConverter
11+
{
12+
public override bool CanConvert(Type objectType) => typeof(Types) == objectType;
13+
14+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
15+
{
16+
var marker = value as Types;
17+
if (marker == null)
18+
{
19+
writer.WriteNull();
20+
return;
21+
}
22+
var contract = serializer.ContractResolver as SettingsContractResolver;
23+
if (contract == null || contract.ConnectionSettings == null)
24+
throw new Exception("If you use a custom contract resolver be sure to subclass from ElasticResolver");
25+
marker.Match(
26+
all=> writer.WriteNull(),
27+
many => writer.WriteValue(((IUrlParameter)marker).GetString(contract.ConnectionSettings))
28+
);
29+
}
30+
31+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
32+
{
33+
if (reader.TokenType == JsonToken.String)
34+
{
35+
string types = reader.Value.ToString();
36+
return (Types)types;
37+
}
38+
return null;
39+
}
40+
41+
}
42+
}

src/Nest/Modules/SnapshotAndRestore/Snapshot/Snapshot/SnapshotRequest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace Nest
99
public partial interface ISnapshotRequest
1010
{
1111
[JsonProperty("indices")]
12+
[JsonConverter(typeof(TypesJsonConverter))]
1213
Indices Indices { get; set; }
1314

1415
[JsonProperty("ignore_unavailable")]

src/Nest/Nest.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,10 @@
126126
<Compile Include="CommonAbstractions\Infer\Fields\FieldsDescriptor.cs" />
127127
<Compile Include="CommonAbstractions\Infer\Id\Id.cs" />
128128
<Compile Include="CommonAbstractions\Infer\Id\IdJsonConverter.cs" />
129-
<Compile Include="CommonAbstractions\Infer\Indices\IndicesJsonConverter.cs" />
129+
<Compile Include="CommonAbstractions\Infer\Indices\IndicesMultiSyntaxJsonConverter.cs" />
130130
<Compile Include="CommonAbstractions\Infer\Indices\Indices.cs" />
131131
<Compile Include="CommonAbstractions\Infer\Field\FieldExtensions.cs" />
132+
<Compile Include="CommonAbstractions\Infer\Indices\IndicesJsonConverter.cs" />
132133
<Compile Include="CommonAbstractions\Infer\Metrics\IndexMetrics.cs" />
133134
<Compile Include="CommonAbstractions\Infer\Metrics\Metrics.cs" />
134135
<Compile Include="CommonAbstractions\Infer\Name\Name.cs" />
@@ -139,6 +140,7 @@
139140
<Compile Include="CommonAbstractions\Infer\PropertyName\PropertyName.cs" />
140141
<Compile Include="CommonAbstractions\Infer\PropertyName\PropertyNameExtensions.cs" />
141142
<Compile Include="CommonAbstractions\Infer\PropertyName\PropertyNameJsonConverter.cs" />
143+
<Compile Include="CommonAbstractions\Infer\Types\TypesMultiSyntaxJsonConverter.cs" />
142144
<Compile Include="CommonAbstractions\Infer\Types\TypesJsonConverter.cs" />
143145
<Compile Include="CommonAbstractions\Infer\Types\Types.cs" />
144146
<Compile Include="CommonAbstractions\LazyDocument\LazyDocumentJsonConverter.cs" />

src/Nest/QueryDsl/TermLevel/Ids/IdsQuery.cs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,39 @@ namespace Nest
1010
[JsonConverter(typeof(ReadAsTypeJsonConverter<IdsQueryDescriptor>))]
1111
public interface IIdsQuery : IQuery
1212
{
13-
[JsonProperty(PropertyName = "type")]
14-
IEnumerable<string> Type { get; set; }
13+
[JsonProperty(PropertyName = "types")]
14+
Types Types { get; set; }
1515

1616
[JsonProperty(PropertyName = "values")]
17-
IEnumerable<string> Values { get; set; }
17+
IEnumerable<Id> Values { get; set; }
1818
}
1919

2020
public class IdsQuery : QueryBase, IIdsQuery
2121
{
2222
bool IQuery.Conditionless => IsConditionless(this);
23-
public IEnumerable<string> Type { get; set; }
24-
public IEnumerable<string> Values { get; set; }
23+
public Types Types { get; set; }
24+
public IEnumerable<Id> Values { get; set; }
2525

2626
protected override void WrapInContainer(IQueryContainer c) => c.Ids = this;
27-
internal static bool IsConditionless(IIdsQuery q) => !q.Values.HasAny() || q.Values.All(v => v.IsNullOrEmpty());
27+
internal static bool IsConditionless(IIdsQuery q) => !q.Values.HasAny();
2828
}
2929

3030
public class IdsQueryDescriptor
3131
: QueryDescriptorBase<IdsQueryDescriptor, IIdsQuery>
3232
, IIdsQuery
3333
{
3434
bool IQuery.Conditionless => IdsQuery.IsConditionless(this);
35-
IEnumerable<string> IIdsQuery.Values { get; set; }
36-
IEnumerable<string> IIdsQuery.Type { get; set; }
35+
IEnumerable<Id> IIdsQuery.Values { get; set; }
36+
Types IIdsQuery.Types { get; set; }
3737

38-
public IdsQueryDescriptor Type(params string[] types) => Assign(a => a.Type = types);
38+
public IdsQueryDescriptor Types(params TypeName[] types) => Assign(a=>a.Types = types);
3939

40-
public IdsQueryDescriptor Type(IEnumerable<string> values) => Type(values.ToArray());
40+
public IdsQueryDescriptor Types(IEnumerable<TypeName> values) => Types(values.ToArray());
41+
42+
public IdsQueryDescriptor Types(Types types) => Assign(a=>a.Types = types);
4143

42-
public IdsQueryDescriptor Values(params long[] values) =>
43-
Assign(a => a.Values = values.Select(v => v.ToString(CultureInfo.InvariantCulture)).ToArray());
44+
public IdsQueryDescriptor Values(params Id[] values) => Assign(a => a.Values = values);
4445

45-
public IdsQueryDescriptor Values(IEnumerable<long> values) =>
46-
Values(values.Select(v => v.ToString(CultureInfo.InvariantCulture)).ToArray());
47-
48-
public IdsQueryDescriptor Values(params string[] values) => Assign(a => a.Values = values);
49-
50-
public IdsQueryDescriptor Values(IEnumerable<string> values) => Values(values.ToArray());
46+
public IdsQueryDescriptor Values(IEnumerable<Id> values) => Values(values.ToArray());
5147
}
5248
}

src/Tests/Document/Multiple/DeleteByQuery/DeleteByQueryApiTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ protected override void ExpectResponse(IDeleteByQueryResponse response)
7777
IgnoreUnavailable = true,
7878
Query = new IdsQuery
7979
{
80-
Values = new [] { Project.Projects.First().Name, "x" }
80+
Values = new Id[] { Project.Projects.First().Name, "x" }
8181
}
8282
};
8383
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Nest;
5+
using Tests.Framework.Integration;
6+
using Tests.Framework.MockData;
7+
using static Nest.Static;
8+
9+
namespace Tests.QueryDsl.TermLevel.Ids
10+
{
11+
public class IdsQueryUsageTests : QueryDslUsageTestsBase
12+
{
13+
public IdsQueryUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) {}
14+
15+
protected override object QueryJson => new
16+
{
17+
ids = new
18+
{
19+
_name = "named_query",
20+
boost = 1.1,
21+
types = new[] { "project", "developer" },
22+
values = new[] { 1, 2, 3, 4 }
23+
}
24+
25+
};
26+
27+
protected override QueryContainer QueryInitializer => new IdsQuery
28+
{
29+
Name = "named_query",
30+
Boost = 1.1,
31+
Values = new List<Id> { 1, 2,3,4 },
32+
Types = Type<Project>().And<Developer>()
33+
};
34+
35+
protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project> q) => q
36+
.Ids(c => c
37+
.Name("named_query")
38+
.Boost(1.1)
39+
.Values(1, 2, 3, 4)
40+
.Types(typeof(Project), typeof(Developer))
41+
);
42+
}
43+
}

src/Tests/Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@
348348
<Compile Include="QueryDsl\TermLevel\Fuzzy\FuzzyNumericQueryUsageTests.cs" />
349349
<Compile Include="QueryDsl\TermLevel\Fuzzy\FuzzyQueryUsageTests.cs" />
350350
<Compile Include="QueryDsl\TermLevel\Exists\ExistsQueryUsageTests.cs" />
351+
<Compile Include="QueryDsl\TermLevel\Ids\IdsQueryUsageTests.cs" />
351352
<Compile Include="QueryDsl\TermLevel\Term\TermQueryUsageTests.cs" />
352353
<Compile Include="QueryDsl\FullText\CommonTerms\CommonTermsUsageTests.cs" />
353354
<Compile Include="QueryDsl\FullText\Match\MatchUsageTests.cs" />

0 commit comments

Comments
 (0)