Skip to content

Commit 50e28e1

Browse files
committed
added support simple_query_string
1 parent cdebd99 commit 50e28e1

File tree

9 files changed

+191
-2
lines changed

9 files changed

+191
-2
lines changed

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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ internal QueryDescriptor(bool forceConditionless)
6969
[JsonProperty(PropertyName = "terms")]
7070
[JsonConverter(typeof(DictionaryKeysAreNotPropertyNamesJsonConverter))]
7171
internal IDictionary<PropertyPathMarker, object> TermsQueryDescriptor { get; set; }
72+
[JsonProperty(PropertyName = "simple_query_string")]
73+
internal SimpleQueryStringQueryDescriptor<T> SimpleQueryStringDescriptor { get; set; }
7274
[JsonProperty(PropertyName = "query_string")]
7375
internal QueryStringDescriptor<T> QueryStringDescriptor { get; set; }
7476
[JsonProperty(PropertyName = "regexp")]
@@ -160,6 +162,20 @@ public BaseQuery QueryString(Action<QueryStringDescriptor<T>> selector)
160162
selector(query);
161163
return this.New(query, q => q.QueryStringDescriptor = query);
162164
}
165+
166+
/// <summary>
167+
/// A query that uses the SimpleQueryParser to parse its context.
168+
/// Unlike the regular query_string query, the simple_query_string query will
169+
/// never throw an exception, and discards invalid parts of the query.
170+
/// </summary>
171+
public BaseQuery SimpleQueryString(Action<SimpleQueryStringQueryDescriptor<T>> selector)
172+
{
173+
var query = new SimpleQueryStringQueryDescriptor<T>();
174+
selector(query);
175+
return this.New(query, q => q.SimpleQueryStringDescriptor = query);
176+
}
177+
178+
163179
/// <summary>
164180
/// 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.
165181
/// </summary>

src/Nest/Domain/DSL/Query.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ public static BaseQuery Prefix(string field, string value, double? Boost = null)
130130
return new QueryDescriptor<T>().Prefix(field, value, Boost);
131131
}
132132

133+
public static BaseQuery SimpleQueryString(Action<SimpleQueryStringQueryDescriptor<T>> selector)
134+
{
135+
return new QueryDescriptor<T>().SimpleQueryString(selector);
136+
}
137+
133138
public static BaseQuery QueryString(Action<QueryStringDescriptor<T>> selector)
134139
{
135140
return new QueryDescriptor<T>().QueryString(selector);

src/Nest/Nest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@
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\SimpeQueryStringQueryDescriptor.cs" />
173174
<Compile Include="DSL\SourceDescriptor.cs" />
174175
<Compile Include="DSL\NodesStatsDescriptor.cs" />
175176
<Compile Include="DSL\NodesInfoDescriptor.cs" />

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@
175175
<Compile Include="Search\Filter\Singles\HasParentFilterJson.cs" />
176176
<Compile Include="Search\Query\Modes\ConditionlessQueryJson.cs" />
177177
<Compile Include="Search\Query\Singles\FunctionScoreQueryJson.cs" />
178+
<Compile Include="Search\Query\Singles\SimpleQueryString\SimpleQueryStringQueryJson.cs" />
178179
<Compile Include="Search\Query\Singles\RegexpQueryJson.cs" />
179180
<Compile Include="Search\Query\Singles\HasParentQueryJson.cs" />
180181
<Compile Include="Search\Query\Singles\MultiMatch\MultiMatchJson.cs" />
@@ -503,6 +504,9 @@
503504
<None Include="Search\Query\Modes\VerbatimDoesNotThrowOnEmptyTerm.json">
504505
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
505506
</None>
507+
<None Include="Search\Query\Singles\SimpleQueryString\SimpleQueryStringFull.json">
508+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
509+
</None>
506510
<None Include="Search\Query\Singles\Terms\TermsQueryDescriptorUsingExternalField.json">
507511
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
508512
</None>

src/Tests/Nest.Tests.Unit/Search/Query/Singles/QueryStringQueryJson.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void QueryStringQueryWithAllOptions()
4343
.QueryString(qs => qs
4444
.OnField(f=>f.Name)
4545
.Query("this that thus")
46-
.Operator(Operator.and)
46+
.DefaultOperator(Operator.and)
4747
.Analyzer("my_analyzer")
4848
.AllowLeadingWildcard(true)
4949
.LowercaseExpendedTerms(true)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"from": 0,
3+
"size": 10,
4+
"query": {
5+
"simple_query_string": {
6+
"query": "\"fried eggs\" +(eggplant | potato) -frittata",
7+
"fields": [
8+
"name^2",
9+
"country^5"
10+
],
11+
"default_operator": "and",
12+
"lowercase_expanded_terms": true,
13+
"flags": "ALL",
14+
"locale": "ROOT"
15+
}
16+
}
17+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Reflection;
2+
using Nest.Tests.MockData.Domain;
3+
using NUnit.Framework;
4+
5+
namespace Nest.Tests.Unit.Search.Query.Singles.SimpleQueryString
6+
{
7+
[TestFixture]
8+
public class QueryStringQueryJson : BaseJsonTests
9+
{
10+
[Test]
11+
public void SimpleQueryStringFull()
12+
{
13+
var s = new SearchDescriptor<ElasticsearchProject>()
14+
.From(0)
15+
.Size(10)
16+
.Query(q=>q
17+
.SimpleQueryString(qs=>qs
18+
.Query("\"fried eggs\" +(eggplant | potato) -frittata")
19+
.OnFieldsWithBoost(d=>d
20+
.Add(f=>f.Name, 2.0)
21+
.Add(f=>f.Country, 5.0)
22+
)
23+
.Flags("ALL")
24+
.DefaultOperator(Operator.and)
25+
.LowercaseExpendedTerms(true)
26+
.Locale("ROOT")
27+
)
28+
);
29+
30+
this.JsonEquals(s, MethodInfo.GetCurrentMethod());
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)