Skip to content

Commit 91418b4

Browse files
committed
wrote unit tests for _name handling of all the queries in the query dsl
1 parent 2737b05 commit 91418b4

File tree

71 files changed

+396
-88
lines changed

Some content is hidden

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

71 files changed

+396
-88
lines changed

src/Nest/DSL/Query/BoolQueryDescriptor.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ protected override void WrapInContainer(IQueryContainer container)
5252
public double? Boost { get; set; }
5353
}
5454

55-
55+
//TODO 2.0 why is this separate from the descriptor
5656
public class BoolBaseQueryDescriptor : IBoolQuery
5757
{
5858
[JsonProperty("must")]
@@ -99,6 +99,17 @@ public BoolQueryDescriptor<T> DisableCoord()
9999
return this;
100100
}
101101

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+
102113
/// <summary>
103114
/// Specifies a minimum number of the optional BooleanClauses which must be satisfied.
104115
/// </summary>
@@ -109,6 +120,7 @@ public BoolQueryDescriptor<T> MinimumShouldMatch(int minimumShouldMatches)
109120
((IBoolQuery)this).MinimumShouldMatch = minimumShouldMatches.ToString(CultureInfo.InvariantCulture);
110121
return this;
111122
}
123+
112124
/// <summary>
113125
/// Specifies a minimum number of the optional BooleanClauses which must be satisfied. String overload where you can specify percentages
114126
/// </summary>
@@ -184,6 +196,7 @@ public BoolQueryDescriptor<T> MustNot(params Func<QueryDescriptor<T>, QueryConta
184196
((IBoolQuery)this).MustNot = descriptors.HasAny() ? descriptors : null;
185197
return this;
186198
}
199+
187200
/// <summary>
188201
/// 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.
189202
/// The minimum number of should clauses to match can be set using minimum_should_match parameter.
@@ -202,6 +215,7 @@ public BoolQueryDescriptor<T> MustNot(params QueryContainer[] queries)
202215
((IBoolQuery)this).MustNot = descriptors.HasAny() ? descriptors : null;
203216
return this;
204217
}
218+
205219
/// <summary>
206220
/// 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.
207221
/// </summary>

src/Nest/DSL/Query/BoostingQueryDescriptor.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ bool IQuery.IsConditionless
5151
{
5252
if (Self.NegativeQuery == null && Self.PositiveQuery == null)
5353
return true;
54-
return (Self.PositiveQuery == null || Self.NegativeQuery.IsConditionless)
55-
|| (Self.NegativeQuery == null || Self.PositiveQuery.IsConditionless);
54+
55+
return (Self.PositiveQuery == null && Self.NegativeQuery.IsConditionless)
56+
|| (Self.NegativeQuery == null && Self.PositiveQuery.IsConditionless);
5657
}
5758
}
5859

src/Nest/DSL/Query/IQuery.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using Newtonsoft.Json;
45

56
namespace Nest
67
{
@@ -9,6 +10,7 @@ public interface IQuery
910
/// <summary>
1011
/// The _name of the query. this allows you to retrieve for each document what part of the query it matched on
1112
/// </summary>
13+
[JsonProperty(PropertyName = "_name")]
1214
string Name { get; set; }
1315
bool IsConditionless { get; }
1416
}

src/Nest/DSL/Query/IdsQueryDescriptor.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Globalization;
34
using System.Linq;
45
using Nest.Resolvers.Converters;
56
using Newtonsoft.Json;
@@ -30,6 +31,66 @@ protected override void WrapInContainer(IQueryContainer container)
3031
public IEnumerable<string> Values { get; set; }
3132
}
3233

34+
[Obsolete("Scheduled to be renamed in 2.0")]
35+
public class IdsQueryProperDescriptor : IIdsQuery
36+
{
37+
private IIdsQuery Self { get { return this; }}
38+
39+
IEnumerable<string> IIdsQuery.Values { get; set; }
40+
IEnumerable<string> IIdsQuery.Type { get; set; }
41+
string IQuery.Name { get; set; }
42+
bool IQuery.IsConditionless
43+
{
44+
get
45+
{
46+
return !Self.Values.HasAny() || Self.Values.All(s=>s.IsNullOrEmpty());
47+
}
48+
}
49+
50+
public IdsQueryProperDescriptor Name(string name)
51+
{
52+
Self.Name = name;
53+
return this;
54+
}
55+
56+
public IdsQueryProperDescriptor Type(params string[] types)
57+
{
58+
Self.Type = types;
59+
return this;
60+
}
61+
62+
public IdsQueryProperDescriptor Type(IEnumerable<string> values)
63+
{
64+
return this.Type(values.ToArray());
65+
}
66+
67+
public IdsQueryProperDescriptor Values(params long[] values)
68+
{
69+
if (values == null) return this;
70+
return this.Values(values.Select(v=>v.ToString(CultureInfo.InvariantCulture)).ToArray());
71+
}
72+
73+
public IdsQueryProperDescriptor Values(IEnumerable<long> values)
74+
{
75+
if (values == null) return this;
76+
return this.Values(values.Select(v=>v.ToString(CultureInfo.InvariantCulture)).ToArray());
77+
}
78+
79+
public IdsQueryProperDescriptor Values(params string[] values)
80+
{
81+
Self.Values = values;
82+
return this;
83+
}
84+
85+
public IdsQueryProperDescriptor Values(IEnumerable<string> values)
86+
{
87+
if (values == null) return this;
88+
return this.Values(values.ToArray());
89+
}
90+
}
91+
92+
93+
[Obsolete("Scheduled to be removed in 2.0")]
3394
public class IdsQueryDescriptor : IIdsQuery
3495
{
3596
[JsonProperty(PropertyName = "_name")]

src/Nest/DSL/Query/QueryDescriptor.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Nest
1010
public class QueryDescriptor<T> : QueryContainer where T : class
1111
{
1212

13-
13+
//TODO remove all the shortcuts into descriptors except for .Term(s)()
1414

1515
public QueryDescriptor()
1616
{
@@ -291,6 +291,7 @@ public QueryContainer Indices(Action<IndicesQueryDescriptor<T>> selector)
291291

292292
return this.New(query, q => q.Indices = query);
293293
}
294+
294295
/// <summary>
295296
/// Matches documents with fields that have terms within a certain range. The type of the Lucene query depends
296297
/// on the field type, for string fields, the TermRangeQuery, while for number/date fields, the query is
@@ -303,6 +304,7 @@ public QueryContainer Range(Action<RangeQueryDescriptor<T>> selector)
303304

304305
return this.New(query, q => q.Range = query);
305306
}
307+
306308
/// <summary>
307309
/// Fuzzy like this query find documents that are “like” provided text by running it against one or more fields.
308310
/// </summary>
@@ -444,6 +446,7 @@ public QueryContainer HasChild<K>(Action<HasChildQueryDescriptor<K>> selector) w
444446

445447
return this.New(query, q => q.HasChild = query);
446448
}
449+
447450
/// <summary>
448451
/// The has_child query works the same as the has_child filter, by automatically wrapping the filter with a
449452
/// constant_score.
@@ -456,6 +459,7 @@ public QueryContainer HasParent<K>(Action<HasParentQueryDescriptor<K>> selector)
456459

457460
return this.New(query, q => q.HasParent = query);
458461
}
462+
459463
/// <summary>
460464
/// The top_children query runs the child query with an estimated hits size, and out of the hit docs, aggregates
461465
/// it into parent docs. If there aren’t enough parent docs matching the requested from/size search request,
@@ -469,6 +473,7 @@ public QueryContainer TopChildren<K>(Action<TopChildrenQueryDescriptor<K>> selec
469473

470474
return this.New(query, q => q.TopChildren = query);
471475
}
476+
472477
/// <summary>
473478
/// A query that applies a filter to the results of another query. This query maps to Lucene FilteredQuery.
474479
/// </summary>
@@ -500,6 +505,7 @@ public QueryContainer Dismax(Action<DisMaxQueryDescriptor<T>> selector)
500505

501506
return this.New(query, q => q.DisMax = query);
502507
}
508+
503509
/// <summary>
504510
/// A query that wraps a filter or another query and simply returns a constant score equal to the query boost
505511
/// for every document in the filter. Maps to Lucene ConstantScoreQuery.
@@ -511,6 +517,7 @@ public QueryContainer ConstantScore(Action<ConstantScoreQueryDescriptor<T>> sele
511517

512518
return this.New(query, q => q.ConstantScore = query);
513519
}
520+
514521
/// <summary>
515522
/// custom_boost_factor query allows to wrap another query and multiply its score by the provided boost_factor.
516523
/// This can sometimes be desired since boost value set on specific queries gets normalized, while this
@@ -524,6 +531,7 @@ public QueryContainer CustomBoostFactor(Action<CustomBoostFactorQueryDescriptor<
524531

525532
return this.New(query, q => q.CustomBoostFactor = query);
526533
}
534+
527535
/// <summary>
528536
/// custom_score query allows to wrap another query and customize the scoring of it optionally with a
529537
/// computation derived from other field values in the doc (numeric ones) using script expression
@@ -536,6 +544,7 @@ public QueryContainer CustomScore(Action<CustomScoreQueryDescriptor<T>> customSc
536544

537545
return this.New(query, q => q.CustomScore = query);
538546
}
547+
539548
/// <summary>
540549
/// custom_score query allows to wrap another query and customize the scoring of it optionally with a
541550
/// computation derived from other field values in the doc (numeric ones) using script or boost expression
@@ -547,6 +556,7 @@ public QueryContainer CustomFiltersScore(Action<CustomFiltersScoreQueryDescripto
547556

548557
return this.New(query, q => q.CustomFiltersScore = query);
549558
}
559+
550560
/// <summary>
551561
/// A query that matches documents matching boolean combinations of other queries. The bool query maps to
552562
/// Lucene BooleanQuery.
@@ -583,11 +593,13 @@ public QueryContainer Boosting(Action<BoostingQueryDescriptor<T>> boostingQuery)
583593
/// boosting into account, the norms_field needs to be provided in order to explicitly specify which
584594
/// field the boosting will be done on (Note, this will result in slower execution time).
585595
/// </param>
586-
public QueryContainer MatchAll(double? Boost = null, string NormField = null)
596+
public QueryContainer MatchAll(double? Boost = null, string NormField = null, string Name = null)
587597
{
598+
//TODO introduce a proper optional query descriptor
588599
var query = new MatchAllQuery() { NormField = NormField };
589600
if (Boost.HasValue)
590601
query.Boost = Boost.Value;
602+
query.Name = Name;
591603

592604
return this.New(query, q => q.MatchAllQuery = query);
593605
}
@@ -605,6 +617,7 @@ public QueryContainer Term<K>(Expression<Func<T, K>> fieldDescriptor, K value, d
605617
t.Boost(Boost.Value);
606618
});
607619
}
620+
608621
/// <summary>
609622
/// Matches documents that have fields that contain a term (not analyzed).
610623
/// The term query maps to Lucene TermQuery.
@@ -618,6 +631,7 @@ public QueryContainer Term(Expression<Func<T, object>> fieldDescriptor, object v
618631
t.Boost(Boost.Value);
619632
});
620633
}
634+
621635
/// <summary>
622636
/// Matches documents that have fields that contain a term (not analyzed).
623637
/// The term query maps to Lucene TermQuery.
@@ -631,6 +645,7 @@ public QueryContainer Term(string field, object value, double? Boost = null)
631645
t.Boost(Boost.Value);
632646
});
633647
}
648+
634649
/// <summary>
635650
/// Matches documents that have fields that contain a term (not analyzed).
636651
/// The term query maps to Lucene TermQuery.
@@ -641,6 +656,7 @@ public QueryContainer Term(Action<TermQueryDescriptor<T>> termSelector)
641656
termSelector(termQuery);
642657
return this.New(termQuery, q => q.Term = termQuery);
643658
}
659+
644660
/// <summary>
645661
/// Matches documents that have fields matching a wildcard expression (not analyzed).
646662
/// Supported wildcards are *, which matches any character sequence (including the empty one), and ?,
@@ -657,6 +673,7 @@ public QueryContainer Wildcard(Expression<Func<T, object>> fieldDescriptor, stri
657673
if (Rewrite.HasValue) t.Rewrite(Rewrite.Value);
658674
});
659675
}
676+
660677
/// <summary>
661678
/// Matches documents that have fields matching a wildcard expression (not analyzed).
662679
/// Supported wildcards are *, which matches any character sequence (including the empty one), and ?,
@@ -687,6 +704,7 @@ public QueryContainer Wildcard(Action<WildcardQueryDescriptor<T>> wildcardSelect
687704
wildcardSelector(wildcardQuery);
688705
return this.New(wildcardQuery, q => q.Wildcard = wildcardQuery);
689706
}
707+
690708
/// <summary>
691709
/// Matches documents that have fields containing terms with a specified prefix (not analyzed).
692710
/// The prefix query maps to Lucene PrefixQuery.
@@ -725,6 +743,7 @@ public QueryContainer Prefix(Action<PrefixQueryDescriptor<T>> prefixSelector)
725743
prefixSelector(prefixQuery);
726744
return this.New(prefixQuery, q => q.Prefix = prefixQuery);
727745
}
746+
728747
/// <summary>
729748
/// Filters documents that only have the provided ids. Note, this filter does not require
730749
/// the _id field to be indexed since it works using the _uid field.
@@ -758,6 +777,17 @@ public QueryContainer Ids(IEnumerable<string> types, IEnumerable<string> values)
758777
return this.New(ids, q => q.Ids = ids);
759778
}
760779

780+
/// <summary>
781+
/// Filters documents that only have the provided ids.
782+
/// Note, this filter does not require the _id field to be indexed since
783+
/// it works using the _uid field.
784+
/// </summary>
785+
public QueryContainer Ids(Func<IdsQueryProperDescriptor, IdsQueryProperDescriptor> selector)
786+
{
787+
var ids = selector(new IdsQueryProperDescriptor());
788+
return this.New(ids, c => c.Ids = ids);
789+
}
790+
761791
/// <summary>
762792
/// Matches spans containing a term. The span term query maps to Lucene SpanTermQuery.
763793
/// </summary>
@@ -791,6 +821,7 @@ public QueryContainer SpanTerm(Action<SpanTermQueryDescriptor<T>> spanTermSelect
791821
spanTermSelector(spanTerm);
792822
return this.New(spanTerm, q => q.SpanTerm = spanTerm);
793823
}
824+
794825
/// <summary>
795826
/// Matches spans near the beginning of a field. The span first query maps to Lucene SpanFirstQuery.
796827
/// </summary>

src/Nest/DSL/Query/QueryStringDescriptor.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,17 @@ protected override void WrapInContainer(IQueryContainer container)
8181
}
8282

8383
bool IQuery.IsConditionless { get { return false; } }
84+
8485
public string Name { get; set; }
86+
8587
public string Query { get; set; }
88+
8689
public PropertyPathMarker DefaultField { get; set; }
90+
8791
public IEnumerable<PropertyPathMarker> Fields { get; set; }
92+
8893
public Operator? DefaultOperator { get; set; }
94+
8995
public string Analyzer { get; set; }
9096
public bool? AllowLeadingWildcard { get; set; }
9197
public bool? LowercaseExpendedTerms { get; set; }
@@ -161,11 +167,13 @@ public QueryStringQueryDescriptor<T> Name(string name)
161167
Self.Name = name;
162168
return this;
163169
}
170+
164171
public QueryStringQueryDescriptor<T> DefaultField(string field)
165172
{
166173
((IQueryStringQuery)this).DefaultField = field;
167174
return this;
168175
}
176+
169177
public QueryStringQueryDescriptor<T> DefaultField(Expression<Func<T, object>> objectPath)
170178
{
171179
((IQueryStringQuery)this).DefaultField = objectPath;

src/Nest/DSL/Query/RangeQueryDescriptor.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ public interface IRangeQuery : IFieldNameQuery
2727
double? Boost { get; set; }
2828

2929
[JsonProperty(PropertyName = "_cache")]
30+
[Obsolete("scheduled to be removed in 2.o copy paste errror from filters")]
3031
bool? Cache { get; set; }
3132

32-
[JsonProperty(PropertyName = "_name")]
33-
string Name { get; set; }
34-
3533
[JsonProperty("time_zone")]
3634
string TimeZone { get; set; }
3735

@@ -81,8 +79,6 @@ public class RangeQueryDescriptor<T> : IRangeQuery where T : class
8179
double? IRangeQuery.Boost { get; set; }
8280

8381
bool? IRangeQuery.Cache { get; set; }
84-
85-
string IRangeQuery.Name { get; set; }
8682

8783
string IRangeQuery.TimeZone { get; set; }
8884

0 commit comments

Comments
 (0)