Skip to content

Commit e1e467e

Browse files
committed
added more termlevel query dsl tests
1 parent 7d0fcbc commit e1e467e

19 files changed

+468
-156
lines changed

Diff for: src/Nest/CommonAbstractions/SerializationBehavior/ElasticContractResolver.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ protected override IList<JsonProperty> CreateProperties(Type type, MemberSeriali
111111

112112
defaultProperties = PropertiesOf<IQuery>(type, memberSerialization, defaultProperties, lookup);
113113
defaultProperties = PropertiesOf<ISpecialField>(type, memberSerialization, defaultProperties, lookup);
114-
defaultProperties = PropertiesOf<IExternalFieldDeclaration>(type, memberSerialization, defaultProperties, lookup);
114+
defaultProperties = PropertiesOf<IFieldLookup>(type, memberSerialization, defaultProperties, lookup);
115115
defaultProperties = PropertiesOf<IQueryContainer>(type, memberSerialization, defaultProperties, lookup);
116116
defaultProperties = PropertiesOf<IRequest>(type, memberSerialization, defaultProperties, lookup);
117117
defaultProperties = PropertiesOf<IQueryContainer>(type, memberSerialization, defaultProperties, lookup);

Diff for: src/Nest/CommonOptions/MinimumShouldMatch/MinimumShouldMatch.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ public MinimumShouldMatch(int count) : base(count) { }
1515
public MinimumShouldMatch(string percentage) : base(percentage) { }
1616

1717
public static MinimumShouldMatch Fixed(int count) => count;
18-
public static MinimumShouldMatch Percentage(float percentage) => $"{percentage}%";
18+
public static MinimumShouldMatch Percentage(double percentage) => $"{percentage}%";
1919

2020
public static implicit operator MinimumShouldMatch(string first) => new MinimumShouldMatch(first);
2121
public static implicit operator MinimumShouldMatch(int second) => new MinimumShouldMatch(second);
22+
public static implicit operator MinimumShouldMatch(double second) => Percentage(second);
2223
}
2324

2425
internal class MinimumShouldMatchJsonConverter :JsonConverter

Diff for: src/Nest/Nest.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@
838838
<Compile Include="QueryDsl\Joining\HasParent\ParentScoreMode.cs" />
839839
<Compile Include="Search\Percolator\RegisterPercolator\RegisterPercolatorRequest.cs" />
840840
<Compile Include="Search\Percolator\Percolate\PercolateRequest.cs" />
841-
<Compile Include="QueryDsl\ExternalFieldDeclaration.cs" />
841+
<Compile Include="QueryDsl\FieldLookup.cs" />
842842
<Compile Include="QueryDsl\FullText\MultiMatch\MultiMatchQuery.cs" />
843843
<Compile Include="Cluster\ClusterState\ElasticClient-ClusterState.cs" />
844844
<Compile Include="CommonAbstractions\SerializationBehavior\StatefulDeserialization\ConcreteTypeConverter.cs" />

Diff for: src/Nest/QueryDsl/ExternalFieldDeclaration.cs

-84
This file was deleted.

Diff for: src/Nest/QueryDsl/FieldLookup.cs

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Linq.Expressions;
4+
using Newtonsoft.Json;
5+
6+
namespace Nest
7+
{
8+
[JsonConverter(typeof(ReadAsTypeJsonConverter<FieldLookup>))]
9+
public interface IFieldLookup
10+
{
11+
[JsonProperty("index")]
12+
IndexName Index { get; set; }
13+
14+
[JsonProperty("type")]
15+
TypeName Type { get; set; }
16+
17+
[JsonProperty("id")]
18+
Id Id { get; set; }
19+
20+
[JsonProperty("path")]
21+
Field Path { get; set; }
22+
}
23+
24+
public class FieldLookup : IFieldLookup
25+
{
26+
public IndexName Index { get; set; }
27+
public TypeName Type { get; set; }
28+
public Id Id { get; set; }
29+
public Field Path { get; set; }
30+
}
31+
32+
public class FieldLookupDescriptor<T> : IFieldLookup
33+
where T : class
34+
{
35+
internal Type _ClrType => typeof(T);
36+
37+
private IFieldLookup Self => this;
38+
39+
IndexName IFieldLookup.Index { get; set; }
40+
41+
TypeName IFieldLookup.Type { get; set; }
42+
43+
Id IFieldLookup.Id { get; set; }
44+
45+
Field IFieldLookup.Path { get; set; }
46+
47+
public FieldLookupDescriptor()
48+
{
49+
Self.Type = new TypeName { Type = this._ClrType };
50+
Self.Index = new IndexName { Type = this._ClrType };
51+
}
52+
53+
public FieldLookupDescriptor<T> Index(IndexName index)
54+
{
55+
Self.Index = index;
56+
return this;
57+
}
58+
public FieldLookupDescriptor<T> Id(Id id)
59+
{
60+
Self.Id = id;
61+
return this;
62+
}
63+
public FieldLookupDescriptor<T> Type(TypeName type)
64+
{
65+
Self.Type = type;
66+
return this;
67+
}
68+
public FieldLookupDescriptor<T> Path(Field path)
69+
{
70+
Self.Path = path;
71+
return this;
72+
}
73+
public FieldLookupDescriptor<T> Path(Expression<Func<T, object>> objectPath)
74+
{
75+
Self.Path = objectPath;
76+
return this;
77+
}
78+
}
79+
}

Diff for: src/Nest/QueryDsl/Query.cs

-3
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,6 @@ public static QueryContainer Term<K>(Expression<Func<T, object>> fieldDescriptor
119119
public static QueryContainer Term<K>(string field, K value, double? Boost = null) =>
120120
new QueryContainerDescriptor<T>().Term(field, value, Boost);
121121

122-
public static QueryContainer Terms(string field, params string[] terms) =>
123-
new QueryContainerDescriptor<T>().Terms(field, terms);
124-
125122
public static QueryContainer TermsDescriptor(Func<TermsQueryDescriptor<T, object>, ITermsQuery> selector) =>
126123
new QueryContainerDescriptor<T>().Terms(selector);
127124

Diff for: src/Nest/QueryDsl/QueryContainerDescriptor.cs

+11-20
Original file line numberDiff line numberDiff line change
@@ -83,30 +83,13 @@ public QueryContainer SimpleQueryString(Func<SimpleQueryStringQueryDescriptor<T>
8383
/// <summary>
8484
/// A query that match on any (configurable) of the provided terms. This is a simpler syntax query for using a bool query with several term queries in the should clauses.
8585
/// </summary>
86-
public QueryContainer Terms(string field, IEnumerable<string> terms) =>
87-
this.Terms(t => t.Field(field).Terms(terms));
88-
89-
/// <summary>
90-
/// A query that match on any (configurable) of the provided terms. This is a simpler syntax query for using a bool query with several term queries in the should clauses.
91-
/// </summary>
92-
public QueryContainer Terms<K>(Expression<Func<T, object>> objectPath, IEnumerable<K> terms) =>
93-
this.Terms<K>(t => t.Field(objectPath).Terms(terms));
94-
95-
/// <summary>
96-
/// A query that match on any (configurable) of the provided terms. This is a simpler syntax query for using a bool query with several term queries in the should clauses.
97-
/// </summary>
98-
public QueryContainer Terms(Func<TermsQueryDescriptor<T, object>, ITermsQuery> selector) =>
99-
this.Terms<object>(selector);
86+
public QueryContainer Terms<TValue>(Func<TermsQueryDescriptor<T, TValue>, ITermsQuery> selector) =>
87+
this._assignSelector(selector, (query, container) => container.Terms = query);
10088

10189
/// <summary>
10290
/// A query that match on any (configurable) of the provided terms. This is a simpler syntax query for using a bool query with several term queries in the should clauses.
10391
/// </summary>
104-
public QueryContainer Terms(Expression<Func<T, object>> objectPath, IEnumerable<string> terms) =>
105-
this.Terms(t => t.Field(objectPath).Terms(terms));
106-
/// <summary>
107-
/// A query that match on any (configurable) of the provided terms. This is a simpler syntax query for using a bool query with several term queries in the should clauses.
108-
/// </summary>
109-
public QueryContainer Terms<K>(Func<TermsQueryDescriptor<T, K>, ITermsQuery> selector) =>
92+
public QueryContainer Terms(Func<TermsQueryDescriptor<T, string>, ITermsQuery> selector) =>
11093
this._assignSelector(selector, (query, container) => container.Terms = query);
11194

11295
/// <summary>
@@ -604,5 +587,13 @@ public QueryContainer Script(Func<ScriptQueryDescriptor<T>, IScriptQuery> select
604587
public QueryContainer Exists(Func<ExistsQueryDescriptor<T>, IExistsQuery> selector) =>
605588
this._assignSelector(selector, (query, container) => container.Exists = query);
606589

590+
public QueryContainer Missing(Func<MissingQueryDescriptor<T>, IMissingQuery> selector) =>
591+
this._assignSelector(selector, (query, container) => container.Missing = query);
592+
593+
public QueryContainer Type(Func<TypeQueryDescriptor, ITypeQuery> selector) =>
594+
this._assignSelector(selector, (query, container) => container.Type = query);
595+
596+
public QueryContainer Type<TOther>() => this.Type(q => q.Value<TOther>());
597+
607598
}
608599
}

Diff for: src/Nest/QueryDsl/TermLevel/Missing/MissingQuery.cs

+13-6
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
namespace Nest
88
{
9-
[JsonConverter(typeof(ReadAsTypeJsonConverter<MissingQuery>))]
109
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
11-
public interface IMissingQuery : IFieldNameQuery
10+
[JsonConverter(typeof(ReadAsTypeJsonConverter<MissingQuery>))]
11+
public interface IMissingQuery : IQuery
1212
{
13+
[JsonProperty(PropertyName = "field")]
14+
Field Field { get; set; }
1315

1416
[JsonProperty(PropertyName = "existence")]
1517
bool? Existence { get; set; }
@@ -18,25 +20,30 @@ public interface IMissingQuery : IFieldNameQuery
1820
bool? NullValue { get; set; }
1921
}
2022

21-
public class MissingQuery : FieldNameQueryBase, IMissingQuery
23+
public class MissingQuery : QueryBase, IMissingQuery
2224
{
2325
bool IQuery.Conditionless => IsConditionless(this);
2426
public bool? Existence { get; set; }
2527
public bool? NullValue { get; set; }
28+
public Field Field { get; set; }
2629
protected override void WrapInContainer(IQueryContainer container) => container.Missing = this;
2730
internal static bool IsConditionless(IMissingQuery q) => q.Field.IsConditionless();
2831
}
2932

3033
public class MissingQueryDescriptor<T>
31-
: FieldNameQueryDescriptorBase<MissingQueryDescriptor<T>, IMissingQuery, T>
34+
: QueryDescriptorBase<MissingQueryDescriptor<T>, IMissingQuery>
3235
, IMissingQuery where T : class
3336
{
3437
bool IQuery.Conditionless => MissingQuery.IsConditionless(this);
3538
bool? IMissingQuery.Existence { get; set; }
3639
bool? IMissingQuery.NullValue { get; set; }
40+
Field IMissingQuery.Field { get; set; }
41+
42+
public MissingQueryDescriptor<T> Existence(bool? existence = true) => Assign(a => a.Existence = existence);
3743

38-
public MissingQueryDescriptor<T> Existence(bool existence = true) => Assign(a => a.Existence = existence);
44+
public MissingQueryDescriptor<T> NullValue(bool? nullValue = true) => Assign(a => a.NullValue = nullValue);
3945

40-
public MissingQueryDescriptor<T> NullValue(bool nullValue = true) => Assign(a => a.NullValue = nullValue);
46+
public MissingQueryDescriptor<T> Field(Field field) => Assign(a => a.Field = field);
47+
public MissingQueryDescriptor<T> Field(Expression<Func<T, object>> objectPath) => Assign(a => a.Field = objectPath);
4148
}
4249
}

Diff for: src/Nest/QueryDsl/TermLevel/Terms/TermsQuery.cs

+16-29
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,24 @@ namespace Nest
1111
[JsonConverter(typeof(TermsQueryJsonConverter))]
1212
public interface ITermsQuery : IFieldNameQuery
1313
{
14-
string MinimumShouldMatch { get; set; }
14+
MinimumShouldMatch MinimumShouldMatch { get; set; }
1515
bool? DisableCoord { get; set; }
1616
IEnumerable<object> Terms { get; set; }
17-
IExternalFieldDeclaration ExternalField { get; set; }
17+
IFieldLookup TermsLookup { get; set; }
1818
}
1919

2020
public class TermsQuery : FieldNameQueryBase, ITermsQuery
2121
{
2222
bool IQuery.Conditionless => IsConditionless(this);
23-
public string MinimumShouldMatch { get; set; }
23+
public MinimumShouldMatch MinimumShouldMatch { get; set; }
2424
public bool? DisableCoord { get; set; }
2525
public IEnumerable<object> Terms { get; set; }
26-
public IExternalFieldDeclaration ExternalField { get; set; }
26+
public IFieldLookup TermsLookup { get; set; }
2727

2828
protected override void WrapInContainer(IQueryContainer c) => c.Terms = this;
2929
internal static bool IsConditionless(ITermsQuery q)
3030
{
31-
return q.Field.IsConditionless() || (!q.Terms.HasAny() && q.ExternalField == null);
31+
return q.Field.IsConditionless() || (!q.Terms.HasAny() && q.TermsLookup == null);
3232
}
3333
}
3434

@@ -37,40 +37,27 @@ internal static bool IsConditionless(ITermsQuery q)
3737
/// This is a simpler syntax query for using a bool query with several term queries in the should clauses.
3838
/// </summary>
3939
/// <typeparam name="T">The type that represents the expected hit type</typeparam>
40-
/// <typeparam name="K">The type of the field that we want to specfify terms for</typeparam>
41-
public class TermsQueryDescriptor<T, K>
42-
: FieldNameQueryDescriptorBase<TermsQueryDescriptor<T, K>, ITermsQuery, T>
40+
/// <typeparam name="TValue">The type of the field that we want to specfify terms for</typeparam>
41+
public class TermsQueryDescriptor<T, TValue>
42+
: FieldNameQueryDescriptorBase<TermsQueryDescriptor<T, TValue>, ITermsQuery, T>
4343
, ITermsQuery where T : class
4444
{
4545
bool IQuery.Conditionless => TermsQuery.IsConditionless(this);
46-
string ITermsQuery.MinimumShouldMatch { get; set; }
46+
MinimumShouldMatch ITermsQuery.MinimumShouldMatch { get; set; }
4747
bool? ITermsQuery.DisableCoord { get; set; }
4848
IEnumerable<object> ITermsQuery.Terms { get; set; }
49-
IExternalFieldDeclaration ITermsQuery.ExternalField { get; set; }
49+
IFieldLookup ITermsQuery.TermsLookup { get; set; }
5050

51-
public TermsQueryDescriptor<T, K> OnExternalField<TOther>(
52-
Func<ExternalFieldDeclarationDescriptor<TOther>, ExternalFieldDeclarationDescriptor<TOther>> selector)
53-
where TOther : class => Assign(a => a.ExternalField = selector(new ExternalFieldDeclarationDescriptor<TOther>()));
51+
public TermsQueryDescriptor<T, TValue> TermsLookup<TOther>(Func<FieldLookupDescriptor<TOther>, IFieldLookup> selector)
52+
where TOther : class => Assign(a => a.TermsLookup = selector(new FieldLookupDescriptor<TOther>()));
5453

55-
public TermsQueryDescriptor<T, K> MinimumShouldMatch(string minMatch) =>
56-
Assign(a => a.MinimumShouldMatch = minMatch);
54+
public TermsQueryDescriptor<T, TValue> MinimumShouldMatch(MinimumShouldMatch minMatch) => Assign(a => a.MinimumShouldMatch = minMatch);
5755

58-
public TermsQueryDescriptor<T, K> MinimumShouldMatch(int minMatch) => Assign(a => a.MinimumShouldMatch = minMatch.ToString(CultureInfo.InvariantCulture));
56+
public TermsQueryDescriptor<T, TValue> DisableCoord(bool? disable = true) => Assign(a => a.DisableCoord = disable);
5957

60-
public TermsQueryDescriptor<T, K> DisableCoord() => Assign(a => a.DisableCoord = true);
58+
public TermsQueryDescriptor<T, TValue> Terms(IEnumerable<TValue> terms) => Assign(a => a.Terms = terms.Cast<object>());
6159

62-
public TermsQueryDescriptor<T, K> Terms(IEnumerable<string> terms) => Assign(a =>
63-
{
64-
if (terms.HasAny())
65-
terms = terms.Where(t => !t.IsNullOrEmpty());
66-
a.Terms = terms;
67-
});
60+
public TermsQueryDescriptor<T, TValue> Terms(params TValue[] terms) => Assign(a => a.Terms = terms.Cast<object>());
6861

69-
public TermsQueryDescriptor<T, K> Terms(IEnumerable<K> terms) => Assign(a =>
70-
{
71-
if (terms.HasAny())
72-
terms = terms.Where(t => t != null).ToArray();
73-
a.Terms = terms.Cast<object>();
74-
});
7562
}
7663
}

0 commit comments

Comments
 (0)