Skip to content

Commit 6e1d3a9

Browse files
committed
Query now supports conditionless queries, still need to be able to toggle on off, Conditionless Filters, support for Conditionless && || overloading, and more unit tests in combination
1 parent 619191c commit 6e1d3a9

24 files changed

+950
-715
lines changed
Binary file not shown.

src/Nest.Tests.Unit/BaseJsonTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ protected void JsonEquals(object o, MethodBase method, string fileName = null)
3131
{
3232
var type = method.DeclaringType;
3333
var @namespace = method.DeclaringType.Namespace;
34-
var folder = @namespace.Replace("Nest.Tests.Unit.", "").Replace(".", "\\");
34+
var folder = @namespace.Replace("Nest.Tests.Unit.", "").Replace(".", "\\");
3535

3636
var file = Path.Combine(folder, (fileName ?? method.Name) + ".json");
37-
file = Path.Combine(Environment.CurrentDirectory.Replace("bin\\Debug", "").Replace("bin\\Release", ""), file);
37+
file = Path.Combine(Environment.CurrentDirectory.Replace("bin\\Debug", "").Replace("bin\\Release", ""), file);
3838

3939
var json = TestElasticClient.Serialize(o);
4040
var expected = File.ReadAllText(file);

src/Nest.Tests.Unit/QueryJson/ConditionLess/ConditionLessTests.cs

Lines changed: 76 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,29 @@ public void CustomScore()
188188
this.DoConditionlessQuery(q => q.CustomScore(csq=>csq.Query(qff=>qff.Terms(p=> p.Name, _c.Name1))));
189189
}
190190

191-
//[Test]
192-
//public void Bool()
193-
//{
194-
// this.DoConditionlessQuery(q => q.Bool());
195-
//}
191+
[Test]
192+
public void BoolEmpty()
193+
{
194+
this.DoConditionlessQuery(q => q.Bool(b => { }));
195+
}
196+
[Test]
197+
public void BoolEmptyClauses()
198+
{
199+
this.DoConditionlessQuery(q => q.Bool(b => b
200+
.Must()
201+
.MustNot()
202+
.Should()
203+
));
204+
}
205+
[Test]
206+
public void BoolConditionlessQueries()
207+
{
208+
this.DoConditionlessQuery(q => q.Bool(b => b
209+
.Must(mq => mq.Term(p => p.Name, _c.Name1), mq => mq.Term(p => p.Name, _c.Name2))
210+
.MustNot(mq => mq.Terms(p => p.Name, _c.Name1), mq => mq.Terms(p => p.Name, _c.Name2))
211+
.Should(mq => mq.Terms(p => p.Name, _c.Name1), mq => mq.Terms(p => p.Name, _c.Name2))
212+
));
213+
}
196214

197215
[Test]
198216
public void Boosting()
@@ -238,61 +256,58 @@ public void PrefixString()
238256
this.DoConditionlessQuery(q => q.Prefix(string.Empty, _c.Name1));
239257
}
240258

241-
//[Test]
242-
//public void Ids()
243-
//{
244-
// this.DoConditionlessQuery(q => q.Ids());
245-
//}
246-
247-
//[Test]
248-
//public void Ids()
249-
//{
250-
// this.DoConditionlessQuery(q => q.Ids());
251-
//}
252-
253-
//[Test]
254-
//public void Ids()
255-
//{
256-
// this.DoConditionlessQuery(q => q.Ids());
257-
//}
258-
259-
//[Test]
260-
//public void SpanTerm()
261-
//{
262-
// this.DoConditionlessQuery(q => q.SpanTerm());
263-
//}
264-
265-
//[Test]
266-
//public void SpanTerm()
267-
//{
268-
// this.DoConditionlessQuery(q => q.SpanTerm());
269-
//}
270-
271-
//[Test]
272-
//public void SpanFirst()
273-
//{
274-
// this.DoConditionlessQuery(q => q.SpanFirst());
275-
//}
276-
277-
//[Test]
278-
//public void SpanNear()
279-
//{
280-
// this.DoConditionlessQuery(q => q.SpanNear());
281-
//}
282-
283-
//[Test]
284-
//public void SpanOr()
285-
//{
286-
// this.DoConditionlessQuery(q => q.SpanOr());
287-
//}
288-
289-
//[Test]
290-
//public void SpanNot()
291-
//{
292-
// this.DoConditionlessQuery(q => q.SpanNot());
293-
//}
294-
295-
296-
259+
[Test]
260+
public void Ids()
261+
{
262+
this.DoConditionlessQuery(q => q.Ids(null));
263+
}
264+
265+
[Test]
266+
public void IdsArray()
267+
{
268+
this.DoConditionlessQuery(q => q.Ids(new string[] { string.Empty }));
269+
}
270+
271+
[Test]
272+
public void SpanTerm()
273+
{
274+
this.DoConditionlessQuery(q => q.SpanTerm(p=>p.Name, _c.Name1));
275+
}
276+
277+
[Test]
278+
public void SpanTermNoField()
279+
{
280+
this.DoConditionlessQuery(q => q.SpanTerm(string.Empty, _c.Name1));
281+
}
282+
283+
[Test]
284+
public void SpanFirst()
285+
{
286+
this.DoConditionlessQuery(q => q.SpanFirst(s=>s.Match(sq=>sq.SpanTerm(p=>p.Name, _c.Name1))));
287+
}
288+
289+
[Test]
290+
public void SpanNear()
291+
{
292+
this.DoConditionlessQuery(q => q.SpanNear(s=>s.Clauses()));
293+
this.DoConditionlessQuery(q => q.SpanNear(s => s.Clauses(sq => sq.SpanTerm(p => p.Name, _c.Name1))));
294+
}
295+
296+
[Test]
297+
public void SpanOr()
298+
{
299+
this.DoConditionlessQuery(q => q.SpanOr(s=>s.Clauses()));
300+
this.DoConditionlessQuery(q => q.SpanOr(s => s.Clauses(sq => sq.SpanTerm(p => p.Name, _c.Name1))));
301+
}
302+
303+
[Test]
304+
public void SpanNot()
305+
{
306+
this.DoConditionlessQuery(q => q.SpanNot(s=>s.Include(null)));
307+
this.DoConditionlessQuery(q => q.SpanNot(s => s.Include(sq => sq.SpanTerm(p => p.Name, _c.Name1))));
308+
this.DoConditionlessQuery(q => q.SpanNot(s => s.Exclude(null)));
309+
this.DoConditionlessQuery(q => q.SpanNot(s => s.Exclude(sq => sq.SpanTerm(p => p.Name, _c.Name1))));
310+
311+
}
297312
}
298313
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"from": 0,
33
"size": 10,
4-
"query": { }
54
}
65

src/Nest/DSL/Descriptors/BaseQuery.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class BaseQuery
1414
[JsonProperty(PropertyName = "bool")]
1515
internal BoolBaseQueryDescriptor BoolQueryDescriptor { get; set; }
1616

17-
internal bool IsConditionlessQueryDescriptor { get; set; }
17+
internal bool IsConditionlessQueryDescriptor { get; set; }
1818

1919
public static BaseQuery operator &(BaseQuery lbq, BaseQuery rbq)
2020
{
@@ -48,7 +48,7 @@ public class BaseQuery
4848
if (lbq.BoolQueryDescriptor._HasOnlyMustNot())
4949
{
5050
bq._MustNotQueries = lbq.BoolQueryDescriptor._MustNotQueries;
51-
bq._MustQueries = new [] { rbq };
51+
bq._MustQueries = new[] { rbq };
5252
return q;
5353
}
5454

@@ -77,7 +77,7 @@ public class BaseQuery
7777
if (rbq.BoolQueryDescriptor.CanJoinMustNot())
7878
bq._MustNotQueries = rbq.MergeMustNotQueries(lbq);
7979
return q;
80-
80+
8181
}
8282

8383
public static BaseQuery operator |(BaseQuery lbq, BaseQuery rbq)
@@ -105,15 +105,15 @@ public class BaseQuery
105105
if (lbq.BoolQueryDescriptor.CanJoinShould() && rbq.BoolQueryDescriptor.CanJoinShould())
106106
bq._ShouldQueries = lbq.MergeShouldQueries(rbq);
107107
else
108-
bq._ShouldQueries = new [] {lbq, rbq};
108+
bq._ShouldQueries = new[] { lbq, rbq };
109109
}
110110

111-
112-
111+
112+
113113
return q;
114114
}
115-
116-
public static BaseQuery operator !(BaseQuery lbq)
115+
116+
public static BaseQuery operator !(BaseQuery lbq)
117117
{
118118
var q = new BaseQuery();
119119
var bq = new BoolBaseQueryDescriptor();
@@ -122,8 +122,8 @@ public class BaseQuery
122122
q.BoolQueryDescriptor = bq;
123123
return q;
124124
}
125-
126-
public static bool operator false(BaseQuery a)
125+
126+
public static bool operator false(BaseQuery a)
127127
{
128128
return false;
129129
}
@@ -133,9 +133,9 @@ public static bool operator true(BaseQuery a)
133133
return false;
134134
}
135135

136-
private static void JoinShouldOnSide(BaseQuery lbq, BaseQuery rbq, BoolBaseQueryDescriptor bq)
137-
{
138-
bq._ShouldQueries = lbq.MergeShouldQueries(rbq);
139-
}
136+
private static void JoinShouldOnSide(BaseQuery lbq, BaseQuery rbq, BoolBaseQueryDescriptor bq)
137+
{
138+
bq._ShouldQueries = lbq.MergeShouldQueries(rbq);
139+
}
140140
}
141141
}

src/Nest/DSL/Descriptors/Query/BoolQueryDescriptor.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,19 @@ public class BoolQueryDescriptor<T> : BoolBaseQueryDescriptor where T : class
118118
[JsonProperty("boost")]
119119
internal double? _Boost { get; set; }
120120

121+
internal bool IsConditionless
122+
{
123+
get
124+
{
125+
if (!this._MustQueries.HasAny() && !this._ShouldQueries.HasAny() && !this._MustNotQueries.HasAny())
126+
return true;
127+
return (this._MustNotQueries.HasAny() && this._MustNotQueries.All(q => q.IsConditionlessQueryDescriptor))
128+
|| (this._ShouldQueries.HasAny() && this._ShouldQueries.All(q => q.IsConditionlessQueryDescriptor))
129+
|| (this._MustQueries.HasAny() && this._MustQueries.All(q => q.IsConditionlessQueryDescriptor));
130+
}
131+
132+
}
133+
121134
/// <summary>
122135
/// Specifies a minimum number of the optional BooleanClauses which must be satisfied.
123136
/// </summary>
@@ -144,6 +157,8 @@ public BoolQueryDescriptor<T> Must(params Func<QueryDescriptor<T>, BaseQuery>[]
144157
{
145158
var filter = new QueryDescriptor<T>();
146159
var q = selector(filter);
160+
if (q.IsConditionlessQueryDescriptor)
161+
continue;
147162
descriptors.Add(q);
148163
}
149164
this._MustQueries = descriptors;
@@ -161,6 +176,8 @@ public BoolQueryDescriptor<T> MustNot(params Func<QueryDescriptor<T>, BaseQuery>
161176
{
162177
var filter = new QueryDescriptor<T>();
163178
var q = selector(filter);
179+
if (q.IsConditionlessQueryDescriptor)
180+
continue;
164181
descriptors.Add(q);
165182
}
166183
this._MustNotQueries = descriptors;
@@ -178,6 +195,8 @@ public BoolQueryDescriptor<T> Should(params Func<QueryDescriptor<T>, BaseQuery>[
178195
{
179196
var filter = new QueryDescriptor<T>();
180197
var q = selector(filter);
198+
if (q.IsConditionlessQueryDescriptor)
199+
continue;
181200
descriptors.Add(q);
182201
}
183202
this._ShouldQueries = descriptors;

src/Nest/DSL/Descriptors/Query/BoostingQueryDescriptor.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,47 @@
77

88
namespace Nest
99
{
10-
[JsonObject(MemberSerialization=MemberSerialization.OptIn)]
11-
public class BoostingQueryDescriptor<T> where T : class
10+
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
11+
public class BoostingQueryDescriptor<T> where T : class
1212
{
1313
[JsonProperty("positive")]
1414
internal BaseQuery _PositiveQuery { get; set; }
1515

1616
[JsonProperty("negative")]
17-
internal BaseQuery _NegativeQuery { get; set; }
17+
internal BaseQuery _NegativeQuery { get; set; }
1818

1919
[JsonProperty("negative_boost")]
2020
internal double? _NegativeBoost { get; set; }
2121

22+
23+
internal bool IsConditionless
24+
{
25+
get
26+
{
27+
if (this._NegativeQuery == null && this._PositiveQuery == null)
28+
return true;
29+
return this._PositiveQuery == null && this._NegativeQuery.IsConditionlessQueryDescriptor
30+
|| this._NegativeQuery == null && this._PositiveQuery.IsConditionlessQueryDescriptor;
31+
}
32+
}
33+
2234
public BoostingQueryDescriptor<T> NegativeBoost(double boost)
2335
{
2436
this._NegativeBoost = boost;
2537
return this;
2638
}
2739

28-
public BoostingQueryDescriptor<T> Positive(Func<QueryDescriptor<T>, BaseQuery> selector)
40+
public BoostingQueryDescriptor<T> Positive(Func<QueryDescriptor<T>, BaseQuery> selector)
2941
{
3042
var query = new QueryDescriptor<T>();
3143
var q = selector(query);
3244
this._PositiveQuery = q;
3345
return this;
3446
}
35-
public BoostingQueryDescriptor<T> Negative(Func<QueryDescriptor<T>, BaseQuery> selector)
47+
public BoostingQueryDescriptor<T> Negative(Func<QueryDescriptor<T>, BaseQuery> selector)
3648
{
3749
var query = new QueryDescriptor<T>();
38-
var q = selector(query);
50+
var q = selector(query);
3951
this._NegativeQuery = q;
4052
return this;
4153
}

src/Nest/DSL/Descriptors/Query/ConstantScoreQueryDescriptor.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Nest
88
{
9-
public class ConstantScoreQueryDescriptor<T> where T : class
9+
public class ConstantScoreQueryDescriptor<T> where T : class
1010
{
1111
[JsonProperty(PropertyName = "query")]
1212
internal BaseQuery _Query { get; set; }
@@ -17,7 +17,20 @@ public class ConstantScoreQueryDescriptor<T> where T : class
1717
[JsonProperty(PropertyName = "boost")]
1818
internal double? _Boost { get; set; }
1919

20-
public ConstantScoreQueryDescriptor<T> Query(Func<QueryDescriptor<T>, BaseQuery> querySelector)
20+
internal bool IsConditionless
21+
{
22+
get
23+
{
24+
if (this._Query == null && this._Filter == null)
25+
return true;
26+
else if (this._Filter == null && this._Query != null)
27+
return this._Query.IsConditionlessQueryDescriptor;
28+
//TODO FILTER
29+
return false;
30+
}
31+
}
32+
33+
public ConstantScoreQueryDescriptor<T> Query(Func<QueryDescriptor<T>, BaseQuery> querySelector)
2134
{
2235
querySelector.ThrowIfNull("querySelector");
2336
this._Filter = null;
@@ -28,7 +41,7 @@ public ConstantScoreQueryDescriptor<T> Query(Func<QueryDescriptor<T>, BaseQuery>
2841
return this;
2942
}
3043

31-
public ConstantScoreQueryDescriptor<T> Filter(Func<FilterDescriptor<T>, BaseFilter> filterSelector)
44+
public ConstantScoreQueryDescriptor<T> Filter(Func<FilterDescriptor<T>, BaseFilter> filterSelector)
3245
{
3346
filterSelector.ThrowIfNull("filterSelector");
3447
this._Query = null;

0 commit comments

Comments
 (0)