Skip to content

Commit c1fefa1

Browse files
committed
Merge pull request #1274 from elastic/feature/named-queries
Feature/named queries
2 parents 5e5ae39 + 91418b4 commit c1fefa1

File tree

117 files changed

+1151
-386
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+1151
-386
lines changed

src/Nest/DSL/Filter/RawFilter.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ namespace Nest
88
{
99
[JsonConverter(typeof(CustomJsonConverter))]
1010
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
11+
[Obsolete("Never used and scheduled to be removed in 2.0")]
1112
public class RawFilter : IQuery, ICustomJson
1213
{
1314
internal object Json { get; set; }
1415
bool IQuery.IsConditionless { get { return this.Json == null || this.Json.ToString().IsNullOrEmpty(); } }
16+
17+
string IQuery.Name { get; set; }
1518

1619
object ICustomJson.GetCustomJson()
1720
{

src/Nest/DSL/Query/BoolQueryDescriptor.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ protected override void WrapInContainer(IQueryContainer container)
4040
container.Bool = this;
4141
}
4242

43+
public string Name { get; set; }
44+
4345
bool IQuery.IsConditionless { get { return false; } }
4446

4547
public IEnumerable<IQueryContainer> Must { get; set; }
@@ -50,7 +52,7 @@ protected override void WrapInContainer(IQueryContainer container)
5052
public double? Boost { get; set; }
5153
}
5254

53-
55+
//TODO 2.0 why is this separate from the descriptor
5456
public class BoolBaseQueryDescriptor : IBoolQuery
5557
{
5658
[JsonProperty("must")]
@@ -70,7 +72,10 @@ public class BoolBaseQueryDescriptor : IBoolQuery
7072

7173
[JsonProperty("boost")]
7274
double? IBoolQuery.Boost { get; set; }
73-
75+
76+
[JsonProperty("_name")]
77+
string IQuery.Name { get; set; }
78+
7479
bool IQuery.IsConditionless
7580
{
7681
get
@@ -94,6 +99,17 @@ public BoolQueryDescriptor<T> DisableCoord()
9499
return this;
95100
}
96101

102+
/// <summary>
103+
///
104+
/// </summary>
105+
/// <param name="minimumShouldMatches"></param>
106+
/// <returns></returns>
107+
public BoolQueryDescriptor<T> Name(string name)
108+
{
109+
((IBoolQuery) this).Name = name;
110+
return this;
111+
}
112+
97113
/// <summary>
98114
/// Specifies a minimum number of the optional BooleanClauses which must be satisfied.
99115
/// </summary>
@@ -104,6 +120,7 @@ public BoolQueryDescriptor<T> MinimumShouldMatch(int minimumShouldMatches)
104120
((IBoolQuery)this).MinimumShouldMatch = minimumShouldMatches.ToString(CultureInfo.InvariantCulture);
105121
return this;
106122
}
123+
107124
/// <summary>
108125
/// Specifies a minimum number of the optional BooleanClauses which must be satisfied. String overload where you can specify percentages
109126
/// </summary>
@@ -179,6 +196,7 @@ public BoolQueryDescriptor<T> MustNot(params Func<QueryDescriptor<T>, QueryConta
179196
((IBoolQuery)this).MustNot = descriptors.HasAny() ? descriptors : null;
180197
return this;
181198
}
199+
182200
/// <summary>
183201
/// The clause (query) should appear in the matching document. A boolean query with no must clauses, one or more should clauses must match a document.
184202
/// The minimum number of should clauses to match can be set using minimum_should_match parameter.
@@ -197,6 +215,7 @@ public BoolQueryDescriptor<T> MustNot(params QueryContainer[] queries)
197215
((IBoolQuery)this).MustNot = descriptors.HasAny() ? descriptors : null;
198216
return this;
199217
}
218+
200219
/// <summary>
201220
/// The clause (query) must not appear in the matching documents. Note that it is not possible to search on documents that only consists of a must_not clauses.
202221
/// </summary>

src/Nest/DSL/Query/BoostingQueryDescriptor.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Configuration;
34
using System.Linq;
45
using Nest.Resolvers.Converters;
56
using Newtonsoft.Json;
@@ -27,6 +28,7 @@ protected override void WrapInContainer(IQueryContainer container)
2728
container.Boosting = this;
2829
}
2930

31+
public string Name { get; set; }
3032
bool IQuery.IsConditionless { get { return false; } }
3133
public QueryContainer PositiveQuery { get; set; }
3234
public QueryContainer NegativeQuery { get; set; }
@@ -35,6 +37,8 @@ protected override void WrapInContainer(IQueryContainer container)
3537

3638
public class BoostingQueryDescriptor<T> : IBoostingQuery where T : class
3739
{
40+
private IBoostingQuery Self { get { return this; } }
41+
3842
QueryContainer IBoostingQuery.PositiveQuery { get; set; }
3943

4044
QueryContainer IBoostingQuery.NegativeQuery { get; set; }
@@ -45,31 +49,40 @@ bool IQuery.IsConditionless
4549
{
4650
get
4751
{
48-
if (((IBoostingQuery)this).NegativeQuery == null && ((IBoostingQuery)this).PositiveQuery == null)
52+
if (Self.NegativeQuery == null && Self.PositiveQuery == null)
4953
return true;
50-
return ((IBoostingQuery)this).PositiveQuery == null && ((IBoostingQuery)this).NegativeQuery.IsConditionless
51-
|| ((IBoostingQuery)this).NegativeQuery == null && ((IBoostingQuery)this).PositiveQuery.IsConditionless;
54+
55+
return (Self.PositiveQuery == null && Self.NegativeQuery.IsConditionless)
56+
|| (Self.NegativeQuery == null && Self.PositiveQuery.IsConditionless);
5257
}
5358
}
5459

60+
string IQuery.Name { get; set; }
61+
62+
public BoostingQueryDescriptor<T> Name(string name)
63+
{
64+
Self.Name = name;
65+
return this;
66+
}
67+
5568
public BoostingQueryDescriptor<T> NegativeBoost(double boost)
5669
{
57-
((IBoostingQuery)this).NegativeBoost = boost;
70+
Self.NegativeBoost = boost;
5871
return this;
5972
}
6073

6174
public BoostingQueryDescriptor<T> Positive(Func<QueryDescriptor<T>, QueryContainer> selector)
6275
{
6376
var query = new QueryDescriptor<T>();
6477
var q = selector(query);
65-
((IBoostingQuery)this).PositiveQuery = q;
78+
Self.PositiveQuery = q;
6679
return this;
6780
}
6881
public BoostingQueryDescriptor<T> Negative(Func<QueryDescriptor<T>, QueryContainer> selector)
6982
{
7083
var query = new QueryDescriptor<T>();
7184
var q = selector(query);
72-
((IBoostingQuery)this).NegativeQuery = q;
85+
Self.NegativeQuery = q;
7386
return this;
7487
}
7588
}

src/Nest/DSL/Query/CommonTermsQueryDescriptor.cs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ protected override void WrapInContainer(IQueryContainer container)
4747
{
4848
container.CommonTerms = this;
4949
}
50-
5150
bool IQuery.IsConditionless { get { return false; } }
51+
5252
PropertyPathMarker IFieldNameQuery.GetFieldName()
5353
{
5454
return this.Field;
@@ -59,6 +59,7 @@ void IFieldNameQuery.SetFieldName(string fieldName)
5959
this.Field = fieldName;
6060
}
6161

62+
public string Name { get; set; }
6263
public string Query { get; set; }
6364
public PropertyPathMarker Field { get; set; }
6465
public double? CutoffFrequency { get; set; }
@@ -72,6 +73,8 @@ void IFieldNameQuery.SetFieldName(string fieldName)
7273

7374
public class CommonTermsQueryDescriptor<T> : ICommonTermsQuery where T : class
7475
{
76+
private ICommonTermsQuery Self { get { return this; }}
77+
7578
string ICommonTermsQuery.Query { get; set; }
7679

7780
PropertyPathMarker ICommonTermsQuery.Field { get; set; }
@@ -90,77 +93,85 @@ public class CommonTermsQueryDescriptor<T> : ICommonTermsQuery where T : class
9093

9194
bool? ICommonTermsQuery.DisableCoord { get; set; }
9295

96+
string IQuery.Name { get; set; }
97+
9398
bool IQuery.IsConditionless
9499
{
95100
get
96101
{
97-
return ((ICommonTermsQuery)this).Field.IsConditionless() || ((ICommonTermsQuery)this).Query.IsNullOrEmpty();
102+
return Self.Field.IsConditionless() || Self.Query.IsNullOrEmpty();
98103
}
99104
}
100105
void IFieldNameQuery.SetFieldName(string fieldName)
101106
{
102-
((ICommonTermsQuery)this).Field = fieldName;
107+
Self.Field = fieldName;
103108
}
104109
PropertyPathMarker IFieldNameQuery.GetFieldName()
105110
{
106-
return ((ICommonTermsQuery)this).Field;
111+
return Self.Field;
112+
}
113+
114+
public CommonTermsQueryDescriptor<T> Name(string name)
115+
{
116+
Self.Name = name;
117+
return this;
107118
}
108119

109120
public CommonTermsQueryDescriptor<T> OnField(string field)
110121
{
111-
((ICommonTermsQuery)this).Field = field;
122+
Self.Field = field;
112123
return this;
113124
}
114125
public CommonTermsQueryDescriptor<T> OnField(Expression<Func<T, object>> objectPath)
115126
{
116-
((ICommonTermsQuery)this).Field = objectPath;
127+
Self.Field = objectPath;
117128
return this;
118129
}
119130

120131
public CommonTermsQueryDescriptor<T> Query(string query)
121132
{
122-
((ICommonTermsQuery)this).Query = query;
133+
Self.Query = query;
123134
return this;
124135
}
125136
public CommonTermsQueryDescriptor<T> LowFrequencyOperator(Operator op)
126137
{
127-
((ICommonTermsQuery)this).LowFrequencyOperator = op;
138+
Self.LowFrequencyOperator = op;
128139
return this;
129140
}
130141
public CommonTermsQueryDescriptor<T> HighFrequencyOperator(Operator op)
131142
{
132-
((ICommonTermsQuery)this).HighFrequencyOperator = op;
143+
Self.HighFrequencyOperator = op;
133144
return this;
134145
}
135146
public CommonTermsQueryDescriptor<T> Analyzer(string analyzer)
136147
{
137-
((ICommonTermsQuery)this).Analyzer = analyzer;
148+
Self.Analyzer = analyzer;
138149
return this;
139150
}
140151

141152
public CommonTermsQueryDescriptor<T> CutoffFrequency(double cutOffFrequency)
142153
{
143-
((ICommonTermsQuery)this).CutoffFrequency = cutOffFrequency;
154+
Self.CutoffFrequency = cutOffFrequency;
144155
return this;
145156
}
146157
public CommonTermsQueryDescriptor<T> MinimumShouldMatch(string minimumShouldMatch)
147158
{
148-
((ICommonTermsQuery)this).MinimumShouldMatch = minimumShouldMatch;
159+
Self.MinimumShouldMatch = minimumShouldMatch;
149160
return this;
150161
}
151162
public CommonTermsQueryDescriptor<T> MinimumShouldMatch(int minimumShouldMatch)
152163
{
153-
((ICommonTermsQuery)this).MinimumShouldMatch = minimumShouldMatch.ToString(CultureInfo.InvariantCulture);
164+
Self.MinimumShouldMatch = minimumShouldMatch.ToString(CultureInfo.InvariantCulture);
154165
return this;
155166
}
156167
public CommonTermsQueryDescriptor<T> Boost(double boost)
157168
{
158-
((ICommonTermsQuery)this).Boost = boost;
169+
Self.Boost = boost;
159170
return this;
160171
}
161172
public CommonTermsQueryDescriptor<T> DisableCoord(bool disable = true)
162173
{
163-
((ICommonTermsQuery)this).DisableCoord = disable;
174+
Self.DisableCoord = disable;
164175
return this;
165176
}
166177

src/Nest/DSL/Query/ConditionlessQueryDescriptor.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ bool IQuery.IsConditionless
2121
}
2222
}
2323

24+
string IQuery.Name { get; set; }
25+
2426
public ConditionlessQueryDescriptor<T> Query(Func<QueryDescriptor<T>, QueryContainer> querySelector)
2527
{
2628
querySelector.ThrowIfNull("querySelector");

src/Nest/DSL/Query/ConstantScoreQueryDescriptor.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ protected override void WrapInContainer(IQueryContainer container)
2929
container.ConstantScore = this;
3030
}
3131

32+
public string Name { get; set; }
33+
//TODO 2.0 change to explicit IQuery implementation
3234
public bool IsConditionless { get { return false; } }
3335
public string Lang { get; set; }
3436
public string Script { get; set; }
@@ -40,51 +42,61 @@ protected override void WrapInContainer(IQueryContainer container)
4042

4143
public class ConstantScoreQueryDescriptor<T> : IConstantScoreQuery where T : class
4244
{
45+
private IConstantScoreQuery Self { get { return this; }}
46+
4347
IQueryContainer IConstantScoreQuery.Query { get; set; }
4448

4549
IFilterContainer IConstantScoreQuery.Filter { get; set; }
4650

4751
double? IConstantScoreQuery.Boost { get; set; }
52+
53+
string IQuery.Name { get; set; }
4854

4955
bool IQuery.IsConditionless
5056
{
5157
get
5258
{
53-
if (((IConstantScoreQuery)this).Query == null && ((IConstantScoreQuery)this).Filter == null)
59+
if (Self.Query == null && Self.Filter == null)
5460
return true;
55-
if (((IConstantScoreQuery)this).Filter == null && ((IConstantScoreQuery)this).Query != null)
56-
return ((IConstantScoreQuery)this).Query.IsConditionless;
57-
if (((IConstantScoreQuery)this).Filter != null && ((IConstantScoreQuery)this).Query == null)
58-
return ((IConstantScoreQuery)this).Filter.IsConditionless;
61+
if (Self.Filter == null && Self.Query != null)
62+
return Self.Query.IsConditionless;
63+
if (Self.Filter != null && Self.Query == null)
64+
return Self.Filter.IsConditionless;
5965
return false;
6066
}
6167
}
6268

69+
public ConstantScoreQueryDescriptor<T> Name(string name)
70+
{
71+
Self.Name = name;
72+
return this;
73+
}
74+
6375
public ConstantScoreQueryDescriptor<T> Query(Func<QueryDescriptor<T>, QueryContainer> querySelector)
6476
{
6577
querySelector.ThrowIfNull("querySelector");
66-
((IConstantScoreQuery)this).Filter = null;
78+
Self.Filter = null;
6779
var query = new QueryDescriptor<T>();
6880
var q = querySelector(query);
6981

70-
((IConstantScoreQuery)this).Query = q;
82+
Self.Query = q;
7183
return this;
7284
}
7385

7486
public ConstantScoreQueryDescriptor<T> Filter(Func<FilterDescriptor<T>, FilterContainer> filterSelector)
7587
{
7688
filterSelector.ThrowIfNull("filterSelector");
77-
((IConstantScoreQuery)this).Query = null;
89+
Self.Query = null;
7890
var filter = new FilterDescriptor<T>();
7991
var f = filterSelector(filter);
8092

81-
((IConstantScoreQuery)this).Filter = f;
93+
Self.Filter = f;
8294
return this;
8395
}
8496

8597
public ConstantScoreQueryDescriptor<T> Boost(double boost)
8698
{
87-
((IConstantScoreQuery)this).Boost = boost;
99+
Self.Boost = boost;
88100
return this;
89101
}
90102
}

0 commit comments

Comments
 (0)