Skip to content

Commit 258404c

Browse files
committed
Static Query<T> now also support .Strict()
1 parent 39bac16 commit 258404c

36 files changed

+954
-475
lines changed

src/Nest.Tests.Unit/Nest.Tests.Unit.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@
117117
<Compile Include="QueryJson\ConditionLess\ConditionLessTests.cs" />
118118
<Compile Include="QueryJson\ConditionlessBool\ConditionLessBooleanTests.cs" />
119119
<Compile Include="QueryJson\ConditionLess\SemiConditionLessTests.cs" />
120+
<Compile Include="QueryJson\ConditionLessStrict\ConditionLessStrictTests.cs" />
121+
<Compile Include="QueryJson\ConditionLessStrict\SemiStrictBoolTests.cs" />
120122
<Compile Include="QueryJson\QueriesInQueries\QueriesInQueriesTests.cs" />
121123
<Compile Include="QueryTests\BoolQueryJson.cs" />
122124
<Compile Include="QueryTests\BoostingQueryJson.cs" />
@@ -364,6 +366,7 @@
364366
<None Include="QueryJson\ConditionlessBool\MyTerm.json" />
365367
<None Include="QueryJson\ConditionlessBool\MatchAll.json" />
366368
<None Include="QueryJson\ConditionLess\MatchAll.json" />
369+
<None Include="QueryJson\ConditionLessStrict\MatchAll.json" />
367370
<None Include="QueryJson\InsideBoolCalls\InsideMust.json" />
368371
<None Include="QueryJson\InsideBoolCalls\InsideMustNot.json" />
369372
<None Include="QueryJson\InsideBoolCalls\InsideShould.json" />
Lines changed: 317 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,317 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using NUnit.Framework;
6+
using Newtonsoft.Json;
7+
using Newtonsoft.Json.Linq;
8+
9+
using Nest;
10+
using Newtonsoft.Json.Converters;
11+
using Nest.Resolvers.Converters;
12+
using Nest.Tests.MockData.Domain;
13+
14+
namespace Nest.Tests.Unit.QueryJson.ConditionLessStrict
15+
{
16+
[TestFixture]
17+
public class ConditionLessStrictTests : BaseJsonTests
18+
{
19+
public class Criteria
20+
{
21+
public string Name1 { get; set; }
22+
public string Name2 { get; set; }
23+
public int? Int1 { get; set; }
24+
public DateTime? Date { get; set; }
25+
}
26+
private readonly Criteria _c = new Criteria();
27+
28+
private void DoConditionlessQuery(Func<QueryDescriptor<ElasticSearchProject>, BaseQuery> query)
29+
{
30+
Assert.Throws<DslException>(() =>
31+
{
32+
var s = new SearchDescriptor<ElasticSearchProject>()
33+
.Strict()
34+
.From(0)
35+
.Take(10)
36+
.Query(query);
37+
38+
this.JsonEquals(s, System.Reflection.MethodInfo.GetCurrentMethod(), "MatchAll");
39+
});
40+
}
41+
42+
[Test]
43+
public void QueryString()
44+
{
45+
this.DoConditionlessQuery(q => q.QueryString(qs => qs.Query(_c.Name1)));
46+
}
47+
48+
[Test]
49+
public void Terms()
50+
{
51+
this.DoConditionlessQuery(q => q.Terms(p => p.Name, new string[] { _c.Name1 }));
52+
this.DoConditionlessQuery(q => q.Terms(p => p.Name, _c.Name1, _c.Name2));
53+
}
54+
55+
56+
[Test]
57+
public void TermsDescriptor()
58+
{
59+
this.DoConditionlessQuery(q => q.TermsDescriptor(qs => qs.OnField(p => p.Name).Terms(_c.Name1)));
60+
}
61+
62+
[Test]
63+
public void Fuzzy()
64+
{
65+
this.DoConditionlessQuery(q => q.Fuzzy(fq => fq.OnField(p => p.Name).Value(_c.Name1)));
66+
}
67+
68+
[Test]
69+
public void FuzzyNumeric()
70+
{
71+
this.DoConditionlessQuery(q => q.FuzzyNumeric(fnq => fnq.OnField(p => p.LOC).Value(_c.Int1)));
72+
}
73+
74+
[Test]
75+
public void FuzzyDate()
76+
{
77+
this.DoConditionlessQuery(q => q.FuzzyDate(fdq => fdq.OnField(p => p.StartedOn).Value(_c.Date)));
78+
}
79+
80+
[Test]
81+
public void Text()
82+
{
83+
this.DoConditionlessQuery(q => q.Text(tq => tq.OnField(p => p.Name).QueryString(_c.Name1)));
84+
}
85+
86+
[Test]
87+
public void TextPhrase()
88+
{
89+
this.DoConditionlessQuery(q => q.Text(tq => tq.OnField(p => p.Name).QueryString(_c.Name1)));
90+
}
91+
92+
[Test]
93+
public void TextPhrasePrefix()
94+
{
95+
this.DoConditionlessQuery(q => q.Text(tq => tq.OnField(p => p.Name).QueryString(_c.Name1)));
96+
}
97+
98+
[Test]
99+
public void Nested()
100+
{
101+
this.DoConditionlessQuery(q => q
102+
.Nested(qn => qn
103+
.Path(p => p.Followers)
104+
.Query(nqq => nqq
105+
.Text(tq => tq
106+
.OnField(p => p.Name)
107+
.QueryString(_c.Name1)
108+
)
109+
)
110+
)
111+
);
112+
}
113+
114+
[Test]
115+
public void Indices()
116+
{
117+
this.DoConditionlessQuery(q => q
118+
.Indices(iq => iq
119+
.Indices(new[] { "_all" })
120+
.Query(tq => tq.Terms(p => p.Name, _c.Name1))
121+
)
122+
);
123+
this.DoConditionlessQuery(q => q
124+
.Indices(iq => iq
125+
.Indices(new[] { "_all" })
126+
.Query(tq => tq.Terms(p => p.Name, _c.Name1))
127+
.NoMatchQuery(tq => tq.Terms(p => p.Name, _c.Name1))
128+
)
129+
);
130+
}
131+
132+
[Test]
133+
public void Range()
134+
{
135+
//From and to are allowed to be null only field is not not
136+
this.DoConditionlessQuery(q => q.Range(rq => rq.OnField(string.Empty).From(0).To(1)));
137+
}
138+
139+
[Test]
140+
public void FuzzyLikeThis()
141+
{
142+
this.DoConditionlessQuery(q => q.FuzzyLikeThis(fq => fq.OnFields(p => p.Name).LikeText(_c.Name1)));
143+
}
144+
145+
[Test]
146+
public void MoreLikeThis()
147+
{
148+
this.DoConditionlessQuery(q => q.MoreLikeThis(mlt => mlt.OnFields(p => p.Name).LikeText(_c.Name1)));
149+
}
150+
151+
[Test]
152+
public void HasChild()
153+
{
154+
this.DoConditionlessQuery(q => q.HasChild<Person>(hcq => hcq.Query(qq => qq.Terms(p => p.FirstName, _c.Name1))));
155+
}
156+
157+
[Test]
158+
public void TopChildren()
159+
{
160+
this.DoConditionlessQuery(q => q.TopChildren<Person>(hcq => hcq.Query(qq => qq.Terms(p => p.FirstName, _c.Name1))));
161+
}
162+
163+
[Test]
164+
public void Filtered()
165+
{
166+
//TODO test .Filter
167+
this.DoConditionlessQuery(q => q.Filtered(fq => fq.Query(qff => qff.Terms(p => p.Name, _c.Name1))));
168+
}
169+
170+
[Test]
171+
public void Dismax()
172+
{
173+
this.DoConditionlessQuery(q => q.Dismax(dq => dq.Queries(qff => qff.Terms(p => p.Name, _c.Name1))));
174+
}
175+
176+
[Test]
177+
public void ConstantScore()
178+
{
179+
this.DoConditionlessQuery(q => q.ConstantScore(cq => cq.Query(qff => qff.Terms(p => p.Name, _c.Name1))));
180+
}
181+
182+
[Test]
183+
public void CustomBoostFactor()
184+
{
185+
this.DoConditionlessQuery(q => q.CustomBoostFactor(cbfq => cbfq.Query(qff => qff.Terms(p => p.Name, _c.Name1))));
186+
}
187+
188+
[Test]
189+
public void CustomScore()
190+
{
191+
this.DoConditionlessQuery(q => q.CustomScore(csq => csq.Query(qff => qff.Terms(p => p.Name, _c.Name1))));
192+
}
193+
194+
[Test]
195+
public void BoolEmpty()
196+
{
197+
this.DoConditionlessQuery(q => q.Bool(b => { }));
198+
}
199+
[Test]
200+
public void BoolEmptyClauses()
201+
{
202+
this.DoConditionlessQuery(q => q.Bool(b => b
203+
.Must()
204+
.MustNot()
205+
.Should()
206+
));
207+
}
208+
[Test]
209+
public void BoolConditionlessQueries()
210+
{
211+
this.DoConditionlessQuery(q => q.Bool(b => b
212+
.Must(mq => mq.Term(p => p.Name, _c.Name1), mq => mq.Term(p => p.Name, _c.Name2))
213+
.MustNot(mq => mq.Terms(p => p.Name, _c.Name1), mq => mq.Terms(p => p.Name, _c.Name2))
214+
.Should(mq => mq.Terms(p => p.Name, _c.Name1), mq => mq.Terms(p => p.Name, _c.Name2))
215+
));
216+
}
217+
218+
[Test]
219+
public void Boosting()
220+
{
221+
this.DoConditionlessQuery(q => q.Boosting(bq => bq.Positive(qff => qff.Terms(p => p.Name, _c.Name1))));
222+
this.DoConditionlessQuery(q => q.Boosting(bq => bq.Negative(qff => qff.Terms(p => p.Name, _c.Name1))));
223+
}
224+
225+
226+
[Test]
227+
public void Term()
228+
{
229+
this.DoConditionlessQuery(q => q.Term(p => p.Name, _c.Name1));
230+
this.DoConditionlessQuery(q => q.Term("", "myterm"));
231+
}
232+
233+
[Test]
234+
public void TermString()
235+
{
236+
this.DoConditionlessQuery(q => q.Term(string.Empty, _c.Name1));
237+
}
238+
239+
[Test]
240+
public void Wildcard()
241+
{
242+
this.DoConditionlessQuery(q => q.Wildcard(p => p.Name, _c.Name1));
243+
}
244+
245+
[Test]
246+
public void WildcardString()
247+
{
248+
this.DoConditionlessQuery(q => q.Wildcard(string.Empty, _c.Name1));
249+
}
250+
251+
[Test]
252+
public void Prefix()
253+
{
254+
this.DoConditionlessQuery(q => q.Prefix(p => p.Name, _c.Name1));
255+
}
256+
257+
[Test]
258+
public void PrefixString()
259+
{
260+
this.DoConditionlessQuery(q => q.Prefix(string.Empty, _c.Name1));
261+
}
262+
263+
[Test]
264+
public void Ids()
265+
{
266+
this.DoConditionlessQuery(q => q.Ids(null));
267+
}
268+
269+
[Test]
270+
public void IdsArray()
271+
{
272+
this.DoConditionlessQuery(q => q.Ids(new string[] { string.Empty }));
273+
}
274+
275+
[Test]
276+
public void SpanTerm()
277+
{
278+
this.DoConditionlessQuery(q => q.SpanTerm(p => p.Name, _c.Name1));
279+
}
280+
281+
[Test]
282+
public void SpanTermNoField()
283+
{
284+
this.DoConditionlessQuery(q => q.SpanTerm(string.Empty, _c.Name1));
285+
}
286+
287+
[Test]
288+
public void SpanFirst()
289+
{
290+
this.DoConditionlessQuery(q => q.SpanFirst(s => s.Match(sq => sq.SpanTerm(p => p.Name, _c.Name1))));
291+
}
292+
293+
[Test]
294+
public void SpanNear()
295+
{
296+
this.DoConditionlessQuery(q => q.SpanNear(s => s.Clauses()));
297+
this.DoConditionlessQuery(q => q.SpanNear(s => s.Clauses(sq => sq.SpanTerm(p => p.Name, _c.Name1))));
298+
}
299+
300+
[Test]
301+
public void SpanOr()
302+
{
303+
this.DoConditionlessQuery(q => q.SpanOr(s => s.Clauses()));
304+
this.DoConditionlessQuery(q => q.SpanOr(s => s.Clauses(sq => sq.SpanTerm(p => p.Name, _c.Name1))));
305+
}
306+
307+
[Test]
308+
public void SpanNot()
309+
{
310+
this.DoConditionlessQuery(q => q.SpanNot(s => s.Include(null)));
311+
this.DoConditionlessQuery(q => q.SpanNot(s => s.Include(sq => sq.SpanTerm(p => p.Name, _c.Name1))));
312+
this.DoConditionlessQuery(q => q.SpanNot(s => s.Exclude(null)));
313+
this.DoConditionlessQuery(q => q.SpanNot(s => s.Exclude(sq => sq.SpanTerm(p => p.Name, _c.Name1))));
314+
315+
}
316+
}
317+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"from": 0,
3+
"size": 10,
4+
}
5+

0 commit comments

Comments
 (0)