Skip to content

Commit 236ace8

Browse files
committed
Merge pull request #566 from elasticsearch/feature/new-queries-filters
Feature/new queries filters
2 parents c5f6aea + ca58488 commit 236ace8

File tree

16 files changed

+330
-2
lines changed

16 files changed

+330
-2
lines changed

src/Nest/DSL/HighlightFieldDescriptor.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public class HighlightFieldDescriptor<T> where T : class
4848
[JsonProperty("type")]
4949
internal string _Type { get; set; }
5050

51+
[JsonProperty("force_source")]
52+
internal bool? _ForceSource { get; set; }
53+
5154
public HighlightFieldDescriptor<T> OnField(string field)
5255
{
5356
this._Field = field;
@@ -67,6 +70,11 @@ public HighlightFieldDescriptor<T> TagsSchema(string schema = "styled")
6770
this._TagsSchema = schema;
6871
return this;
6972
}
73+
public HighlightFieldDescriptor<T> ForceSource(bool force = true)
74+
{
75+
this._ForceSource = force;
76+
return this;
77+
}
7078
public HighlightFieldDescriptor<T> Type(string type)
7179
{
7280
this._Type = type;

src/Nest/DSL/IQueryDescriptor.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ interface IQueryDescriptor<T>
88
BaseQuery Bool(Action<BoolQueryDescriptor<T>> booleanQuery);
99
BaseQuery Boosting(Action<BoostingQueryDescriptor<T>> boostingQuery);
1010
BaseQuery ConstantScore(Action<ConstantScoreQueryDescriptor<T>> selector);
11+
[Obsolete("Custom boost factor has been removed in 1.1")]
1112
BaseQuery CustomBoostFactor(Action<CustomBoostFactorQueryDescriptor<T>> selector);
13+
[Obsolete("Custom score has been removed in 1.1")]
1214
BaseQuery CustomScore(Action<CustomScoreQueryDescriptor<T>> customScoreQuery);
1315
BaseQuery Dismax(Action<DismaxQueryDescriptor<T>> selector);
1416
BaseQuery Filtered(Action<FilteredQueryDescriptor<T>> selector);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Linq.Expressions;
5+
using System.Text;
6+
using Nest.Resolvers;
7+
using Newtonsoft.Json;
8+
using Elasticsearch.Net;
9+
10+
namespace Nest
11+
{
12+
public class GeoShapeQueryDescriptor<T> : IQuery
13+
where T : class
14+
{
15+
internal PropertyPathMarker _Field { get; set; }
16+
bool IQuery.IsConditionless
17+
{
18+
get
19+
{
20+
return this._Field.IsConditionless() || (this._Shape == null || !this._Shape.Coordinates.HasAny());
21+
}
22+
23+
}
24+
public GeoShapeQueryDescriptor<T> OnField(string field)
25+
{
26+
this._Field = field;
27+
return this;
28+
}
29+
public GeoShapeQueryDescriptor<T> OnField(Expression<Func<T, object>> objectPath)
30+
{
31+
this._Field = objectPath;
32+
return this;
33+
}
34+
[JsonProperty("shape")]
35+
internal GeoShapeVector _Shape { get; set; }
36+
37+
public GeoShapeQueryDescriptor<T> Type(string type)
38+
{
39+
if (this._Shape == null)
40+
this._Shape = new GeoShapeVector();
41+
this._Shape.Type = type;
42+
return this;
43+
}
44+
45+
public GeoShapeQueryDescriptor<T> Coordinates(IEnumerable<IEnumerable<double>> coordinates)
46+
{
47+
if (this._Shape == null)
48+
this._Shape = new GeoShapeVector();
49+
this._Shape.Coordinates = coordinates;
50+
return this;
51+
}
52+
53+
}
54+
55+
}

src/Nest/DSL/Query/QueryStringDescriptor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public QueryStringDescriptor<T> Query(string query)
106106
this._QueryString = query;
107107
return this;
108108
}
109-
public QueryStringDescriptor<T> Operator(Operator op)
109+
public QueryStringDescriptor<T> DefaultOperator(Operator op)
110110
{
111111
this._DefaultOperator = op;
112112
return this;
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using Newtonsoft.Json;
6+
using System.Linq.Expressions;
7+
using System.Globalization;
8+
using Newtonsoft.Json.Converters;
9+
using Elasticsearch.Net;
10+
using Nest.Resolvers;
11+
12+
namespace Nest
13+
{
14+
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
15+
public class SimpleQueryStringQueryDescriptor<T> : IQuery where T : class
16+
{
17+
[JsonProperty(PropertyName = "query")]
18+
internal string _QueryString { get; set; }
19+
[JsonProperty(PropertyName = "default_field")]
20+
internal PropertyPathMarker _Field { get; set; }
21+
[JsonProperty(PropertyName = "fields")]
22+
internal IEnumerable<PropertyPathMarker> _Fields { get; set; }
23+
[JsonProperty(PropertyName = "default_operator")]
24+
[JsonConverter(typeof(StringEnumConverter))]
25+
internal Operator? _DefaultOperator { get; set; }
26+
[JsonProperty(PropertyName = "analyzer")]
27+
internal string _Analyzer { get; set; }
28+
[JsonProperty(PropertyName = "lowercase_expanded_terms")]
29+
internal bool? _LowercaseExpendedTerms { get; set; }
30+
[JsonProperty(PropertyName = "flags")]
31+
internal string _Flags { get; set; }
32+
[JsonProperty(PropertyName = "locale")]
33+
internal string _Locale { get; set; }
34+
35+
36+
bool IQuery.IsConditionless
37+
{
38+
get
39+
{
40+
return this._QueryString.IsNullOrEmpty();
41+
}
42+
}
43+
44+
45+
public SimpleQueryStringQueryDescriptor<T> OnField(string field)
46+
{
47+
this._Field = field;
48+
return this;
49+
}
50+
public SimpleQueryStringQueryDescriptor<T> OnField(Expression<Func<T, object>> objectPath)
51+
{
52+
this._Field = objectPath;
53+
return this;
54+
}
55+
public SimpleQueryStringQueryDescriptor<T> OnFields(IEnumerable<string> fields)
56+
{
57+
this._Fields = fields.Select(f=>(PropertyPathMarker)f);
58+
return this;
59+
}
60+
public SimpleQueryStringQueryDescriptor<T> OnFields(
61+
params Expression<Func<T, object>>[] objectPaths)
62+
{
63+
this._Fields = objectPaths.Select(e=>(PropertyPathMarker)e);
64+
return this;
65+
}
66+
public SimpleQueryStringQueryDescriptor<T> OnFieldsWithBoost(Action<FluentDictionary<Expression<Func<T, object>>, double?>> boostableSelector)
67+
{
68+
var d = new FluentDictionary<Expression<Func<T, object>>, double?>();
69+
boostableSelector(d);
70+
this._Fields = d.Select(o => PropertyPathMarker.Create(o.Key, o.Value));
71+
return this;
72+
}
73+
public SimpleQueryStringQueryDescriptor<T> OnFieldsWithBoost(Action<FluentDictionary<string, double?>> boostableSelector)
74+
{
75+
var d = new FluentDictionary<string, double?>();
76+
boostableSelector(d);
77+
this._Fields = d.Select(o => PropertyPathMarker.Create(o.Key, o.Value));
78+
return this;
79+
}
80+
81+
public SimpleQueryStringQueryDescriptor<T> Query(string query)
82+
{
83+
this._QueryString = query;
84+
return this;
85+
}
86+
public SimpleQueryStringQueryDescriptor<T> DefaultOperator(Operator op)
87+
{
88+
this._DefaultOperator = op;
89+
return this;
90+
}
91+
public SimpleQueryStringQueryDescriptor<T> Analyzer(string analyzer)
92+
{
93+
this._Analyzer = analyzer;
94+
return this;
95+
}
96+
public SimpleQueryStringQueryDescriptor<T> Flags(string flags)
97+
{
98+
this._Flags = flags;
99+
return this;
100+
}
101+
public SimpleQueryStringQueryDescriptor<T> LowercaseExpendedTerms(bool lowercaseExpendedTerms)
102+
{
103+
this._LowercaseExpendedTerms = lowercaseExpendedTerms;
104+
return this;
105+
}
106+
public SimpleQueryStringQueryDescriptor<T> Locale(string locale)
107+
{
108+
this._Locale = locale;
109+
return this;
110+
}
111+
112+
}
113+
}

src/Nest/DSL/QueryDescriptor.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,14 @@ internal QueryDescriptor(bool forceConditionless)
6666
[JsonConverter(typeof(DictionaryKeysAreNotPropertyNamesJsonConverter))]
6767
[JsonProperty(PropertyName = "fuzzy")]
6868
internal IDictionary<PropertyPathMarker, object> FuzzyQueryDescriptor { get; set; }
69+
[JsonProperty(PropertyName = "geo_shape")]
70+
[JsonConverter(typeof(DictionaryKeysAreNotPropertyNamesJsonConverter))]
71+
internal IDictionary<PropertyPathMarker, object> GeoShapeQueryDescriptor { get; set; }
6972
[JsonProperty(PropertyName = "terms")]
7073
[JsonConverter(typeof(DictionaryKeysAreNotPropertyNamesJsonConverter))]
7174
internal IDictionary<PropertyPathMarker, object> TermsQueryDescriptor { get; set; }
75+
[JsonProperty(PropertyName = "simple_query_string")]
76+
internal SimpleQueryStringQueryDescriptor<T> SimpleQueryStringDescriptor { get; set; }
7277
[JsonProperty(PropertyName = "query_string")]
7378
internal QueryStringDescriptor<T> QueryStringDescriptor { get; set; }
7479
[JsonProperty(PropertyName = "regexp")]
@@ -160,6 +165,20 @@ public BaseQuery QueryString(Action<QueryStringDescriptor<T>> selector)
160165
selector(query);
161166
return this.New(query, q => q.QueryStringDescriptor = query);
162167
}
168+
169+
/// <summary>
170+
/// A query that uses the SimpleQueryParser to parse its context.
171+
/// Unlike the regular query_string query, the simple_query_string query will
172+
/// never throw an exception, and discards invalid parts of the query.
173+
/// </summary>
174+
public BaseQuery SimpleQueryString(Action<SimpleQueryStringQueryDescriptor<T>> selector)
175+
{
176+
var query = new SimpleQueryStringQueryDescriptor<T>();
177+
selector(query);
178+
return this.New(query, q => q.SimpleQueryStringDescriptor = query);
179+
}
180+
181+
163182
/// <summary>
164183
/// 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.
165184
/// </summary>
@@ -417,6 +436,23 @@ public BaseQuery MoreLikeThis(Action<MoreLikeThisQueryDescriptor<T>> selector)
417436

418437
return this.New(query, q => q.MoreLikeThisDescriptor = query);
419438
}
439+
440+
/// <summary>
441+
/// The geo_shape Filter uses the same grid square representation as the geo_shape mapping to find documents
442+
/// that have a shape that intersects with the query shape.
443+
/// It will also use the same PrefixTree configuration as defined for the field mapping.
444+
/// </summary>
445+
public BaseQuery GeoShape(Action<GeoShapeQueryDescriptor<T>> selector)
446+
{
447+
var query = new GeoShapeQueryDescriptor<T>();
448+
selector(query);
449+
var shape = new Dictionary<PropertyPathMarker, object>
450+
{
451+
{ query._Field, query }
452+
};
453+
return this.New(query, q => q.GeoShapeQueryDescriptor = shape);
454+
}
455+
420456
/// <summary>
421457
/// The has_child query works the same as the has_child filter, by automatically wrapping the filter with a
422458
/// constant_score.
@@ -493,6 +529,7 @@ public BaseQuery ConstantScore(Action<ConstantScoreQueryDescriptor<T>> selector)
493529
/// This can sometimes be desired since boost value set on specific queries gets normalized, while this
494530
/// query boost factor does not.
495531
/// </summary>
532+
[Obsolete("Custom boost factor has been removed in 1.1")]
496533
public BaseQuery CustomBoostFactor(Action<CustomBoostFactorQueryDescriptor<T>> selector)
497534
{
498535
var query = new CustomBoostFactorQueryDescriptor<T>();
@@ -504,6 +541,7 @@ public BaseQuery CustomBoostFactor(Action<CustomBoostFactorQueryDescriptor<T>> s
504541
/// custom_score query allows to wrap another query and customize the scoring of it optionally with a
505542
/// computation derived from other field values in the doc (numeric ones) using script expression
506543
/// </summary>
544+
[Obsolete("Custom score has been removed in 1.1")]
507545
public BaseQuery CustomScore(Action<CustomScoreQueryDescriptor<T>> customScoreQuery)
508546
{
509547
var query = new CustomScoreQueryDescriptor<T>();

src/Nest/Domain/DSL/Query.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ public static BaseQuery ConstantScore(Action<ConstantScoreQueryDescriptor<T>> se
3333
return new QueryDescriptor<T>().ConstantScore(selector);
3434
}
3535

36+
[Obsolete("Custom boost factor has been removed in 1.1")]
3637
public static BaseQuery CustomBoostFactor(Action<CustomBoostFactorQueryDescriptor<T>> selector)
3738
{
3839
return new QueryDescriptor<T>().CustomBoostFactor(selector);
3940
}
4041

42+
[Obsolete("Custom score has been removed in 1.1")]
4143
public static BaseQuery CustomScore(Action<CustomScoreQueryDescriptor<T>> customScoreQuery)
4244
{
4345
return new QueryDescriptor<T>().CustomScore(customScoreQuery);
@@ -128,6 +130,15 @@ public static BaseQuery Prefix(string field, string value, double? Boost = null)
128130
return new QueryDescriptor<T>().Prefix(field, value, Boost);
129131
}
130132

133+
public static BaseQuery SimpleQueryString(Action<SimpleQueryStringQueryDescriptor<T>> selector)
134+
{
135+
return new QueryDescriptor<T>().SimpleQueryString(selector);
136+
}
137+
138+
public static BaseQuery GeoShape(Action<GeoShapeQueryDescriptor<T>> selector)
139+
{
140+
return new QueryDescriptor<T>().GeoShape(selector);
141+
}
131142
public static BaseQuery QueryString(Action<QueryStringDescriptor<T>> selector)
132143
{
133144
return new QueryDescriptor<T>().QueryString(selector);

src/Nest/Nest.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@
170170
<Compile Include="DSL\Facets\Ip4Range.cs" />
171171
<Compile Include="DSL\Facets\DateExpressionRange.cs" />
172172
<Compile Include="DSL\Paths\BasePathDescriptor.cs" />
173+
<Compile Include="DSL\Query\GeoShapeQueryDescriptor.cs" />
174+
<Compile Include="DSL\Query\SimpeQueryStringQueryDescriptor.cs" />
173175
<Compile Include="DSL\SourceDescriptor.cs" />
174176
<Compile Include="DSL\NodesStatsDescriptor.cs" />
175177
<Compile Include="DSL\NodesInfoDescriptor.cs" />

src/Tests/Nest.Tests.Integration/Integration/HighlightTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public void TestHighlight()
2828
.OnField(e => e.Content)
2929
.PreTags("<em>")
3030
.PostTags("</em>")
31+
3132
)
3233
)
3334
);

src/Tests/Nest.Tests.Unit/Core/Map/FluentMappingFullExampleTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ public void MapFluentFull()
125125
)
126126
.String(s => s
127127
.Name(p => p.Name)
128+
128129
.IndexName("my_crazy_name_i_want_in_lucene")
129130
.IncludeInAll()
130131
.Index(FieldIndexOption.analyzed)

0 commit comments

Comments
 (0)