Skip to content

Commit 3f42c40

Browse files
committed
finished specialized section under query dsl
1 parent 9643409 commit 3f42c40

File tree

8 files changed

+165
-36
lines changed

8 files changed

+165
-36
lines changed

src/Nest/CommonOptions/Scripting/Script.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public interface IScript
1717

1818
[JsonProperty("lang")]
1919
string Lang { get; set; }
20+
2021
}
2122

2223
public abstract class Script : IScript

src/Nest/Modules/Scripting/ScriptLang.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@
44

55
namespace Nest
66
{
7-
[JsonConverter(typeof (StringEnumConverter))]
8-
public enum ScriptLang
9-
{
10-
[EnumMember(Value = "groovy")]
11-
Groovy,
12-
[EnumMember(Value = "js")]
13-
JS,
14-
[EnumMember(Value = "expression")]
15-
Expression,
16-
[EnumMember(Value = "python")]
17-
Python,
18-
[EnumMember(Value = "native")]
19-
Native,
20-
}
7+
[JsonConverter(typeof(StringEnumConverter))]
8+
public enum ScriptLang
9+
{
10+
[EnumMember(Value = "groovy")]
11+
Groovy,
12+
[EnumMember(Value = "js")]
13+
JS,
14+
[EnumMember(Value = "expression")]
15+
Expression,
16+
[EnumMember(Value = "python")]
17+
Python,
18+
[EnumMember(Value = "native")]
19+
Native,
20+
}
21+
2122
}

src/Nest/QueryDsl/QueryContainerDescriptor.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,5 +591,9 @@ public QueryContainer FunctionScore(Func<FunctionScoreQueryDescriptor<T>, IFunct
591591

592592
public QueryContainer Template(Func<TemplateQueryDescriptor<T>, ITemplateQuery> selector) =>
593593
this._assignSelector(selector, (query, container) => container.Template = query);
594+
595+
public QueryContainer Script(Func<ScriptQueryDescriptor<T>, IScriptQuery> selector) =>
596+
this._assignSelector(selector, (query, container) => container.Script = query);
597+
594598
}
595599
}

src/Nest/QueryDsl/Specialized/Script/ScriptQuery.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ namespace Nest
1010
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
1111
public interface IScriptQuery : IQuery
1212
{
13-
[JsonProperty(PropertyName = "script")]
14-
string Script { get; set; }
15-
[JsonProperty(PropertyName = "script_id")]
16-
string ScriptId { get; set; }
13+
[JsonProperty(PropertyName = "inline")]
14+
string Inline { get; set; }
1715

18-
[JsonProperty("script_file")]
19-
string ScriptFile { get; set; }
16+
[JsonProperty(PropertyName = "id")]
17+
Id Id { get; set; }
18+
19+
[JsonProperty("file")]
20+
string File { get; set; }
2021

2122
[JsonProperty(PropertyName = "params")]
2223
[JsonConverter(typeof(VerbatimDictionaryKeysJsonConverter))]
@@ -29,41 +30,41 @@ public interface IScriptQuery : IQuery
2930
public class ScriptQuery : QueryBase, IScriptQuery
3031
{
3132
bool IQuery.Conditionless => IsConditionless(this);
32-
public string Script { get; set; }
33-
public string ScriptId { get; set; }
34-
public string ScriptFile { get; set; }
33+
public string Inline { get; set; }
34+
public Id Id { get; set; }
35+
public string File { get; set; }
3536
public Dictionary<string, object> Params { get; set; }
3637
public string Lang { get; set; }
3738

3839
protected override void WrapInContainer(IQueryContainer c) => c.Script = this;
39-
internal static bool IsConditionless(IScriptQuery q) => q.Script.IsNullOrEmpty();
40+
internal static bool IsConditionless(IScriptQuery q) => q.Inline.IsNullOrEmpty();
4041
}
4142

4243
public class ScriptQueryDescriptor<T>
4344
: QueryDescriptorBase<ScriptQueryDescriptor<T>, IScriptQuery>
4445
, IScriptQuery where T : class
4546
{
4647
bool IQuery.Conditionless => ScriptQuery.IsConditionless(this);
47-
string IScriptQuery.Script { get; set; }
48-
string IScriptQuery.ScriptId { get; set; }
49-
string IScriptQuery.ScriptFile { get; set; }
48+
string IScriptQuery.Inline { get; set; }
49+
Id IScriptQuery.Id { get; set; }
50+
string IScriptQuery.File { get; set; }
5051
string IScriptQuery.Lang { get; set; }
5152
Dictionary<string, object> IScriptQuery.Params { get; set; }
5253

5354
/// <summary>
5455
/// Inline script to execute
5556
/// </summary>
56-
public ScriptQueryDescriptor<T> Script(string script) => Assign(a => a.Script = script);
57+
public ScriptQueryDescriptor<T> Inline(string script) => Assign(a => a.Inline = script);
5758

5859
/// <summary>
5960
/// Id of an indexed script to execute
6061
/// </summary
61-
public ScriptQueryDescriptor<T> ScriptId(string scriptId) => Assign(a => a.ScriptId = scriptId);
62+
public ScriptQueryDescriptor<T> Id(string scriptId) => Assign(a => a.Id = scriptId);
6263

6364
/// <summary>
6465
/// File name of a script to execute
6566
/// </summary>
66-
public ScriptQueryDescriptor<T> ScriptFile(string scriptFile) => Assign(a => a.ScriptFile = scriptFile);
67+
public ScriptQueryDescriptor<T> File(string scriptFile) => Assign(a => a.File = scriptFile);
6768

6869
/// <summary>
6970
/// Scripts are compiled and cached for faster execution.

src/Nest/QueryDsl/Specialized/Template/TemplateQuery.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,17 @@
77
namespace Nest
88
{
99
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
10+
[JsonConverter(typeof(ReadAsTypeJsonConverter<TemplateQuery>))]
1011
public interface ITemplateQuery : IQuery
1112
{
12-
[JsonProperty("query")]
13-
string Query { get; set; }
13+
[JsonProperty("file")]
14+
string File { get; set; }
15+
16+
[JsonProperty("inline")]
17+
string Inline { get; set; }
18+
19+
[JsonProperty("id")]
20+
Id Id { get; set; }
1421

1522
[JsonProperty("params")]
1623
IDictionary<string, object> Params { get; set; }
@@ -19,22 +26,30 @@ public interface ITemplateQuery : IQuery
1926
public class TemplateQuery : QueryBase, ITemplateQuery
2027
{
2128
public bool Conditionless => IsConditionless(this);
22-
public string Query { get; set; }
29+
public string File { get; set; }
30+
public string Inline { get; set; }
31+
public Id Id { get; set; }
2332
public IDictionary<string, object> Params { get; set;}
2433

2534
protected override void WrapInContainer(IQueryContainer c) => c.Template = this;
26-
internal static bool IsConditionless(ITemplateQuery q) => q.Query.IsNullOrEmpty();
35+
internal static bool IsConditionless(ITemplateQuery q) => q.File.IsNullOrEmpty() && q.Id == null && q.Inline.IsNullOrEmpty();
2736
}
2837

2938
public class TemplateQueryDescriptor<T>
3039
: QueryDescriptorBase<TemplateQueryDescriptor<T>, ITemplateQuery>
3140
, ITemplateQuery where T : class
3241
{
3342
bool IQuery.Conditionless => TemplateQuery.IsConditionless(this);
34-
string ITemplateQuery.Query { get; set; }
43+
string ITemplateQuery.File { get; set; }
44+
string ITemplateQuery.Inline { get; set; }
45+
Id ITemplateQuery.Id { get; set; }
3546
IDictionary<string, object> ITemplateQuery.Params { get; set; }
3647

37-
public TemplateQueryDescriptor<T> Query(string query) => Assign(a => a.Query = query);
48+
public TemplateQueryDescriptor<T> Inline(string script) => Assign(a => a.Inline = script);
49+
50+
public TemplateQueryDescriptor<T> File(string file) => Assign(a => a.File = file);
51+
52+
public TemplateQueryDescriptor<T> Id(Id id) => Assign(a => a.Id = id);
3853

3954
public TemplateQueryDescriptor<T> Params(IDictionary<string, object> paramsDictionary) => Assign(a => a.Params = paramsDictionary);
4055

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Nest;
5+
using Tests.Framework.Integration;
6+
using Tests.Framework.MockData;
7+
using static Nest.Static;
8+
9+
namespace Tests.QueryDsl.Specialized.Script
10+
{
11+
public class ScriptUsageTests : QueryDslUsageTestsBase
12+
{
13+
public ScriptUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) { }
14+
15+
private static readonly string _templateString = "doc['num1'].value > param1";
16+
17+
protected override object QueryJson => new
18+
{
19+
script = new
20+
{
21+
_name = "named_query",
22+
boost = 1.1,
23+
inline = "doc['num1'].value > param1",
24+
@params = new
25+
{
26+
param1 = 1
27+
}
28+
}
29+
30+
31+
32+
};
33+
34+
protected override QueryContainer QueryInitializer => new ScriptQuery
35+
{
36+
Name = "named_query",
37+
Boost = 1.1,
38+
Inline = _templateString,
39+
Params = new Dictionary<string, object>
40+
{
41+
{ "param1", 1 }
42+
}
43+
};
44+
45+
protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project> q) => q
46+
.Script(sn => sn
47+
.Name("named_query")
48+
.Boost(1.1)
49+
.Inline(_templateString)
50+
.Params(p=>p.Add("param1", 1))
51+
);
52+
}
53+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Nest;
5+
using Tests.Framework.Integration;
6+
using Tests.Framework.MockData;
7+
using static Nest.Static;
8+
9+
namespace Tests.QueryDsl.Specialized.Template
10+
{
11+
public class TemplateUsageTests : QueryDslUsageTestsBase
12+
{
13+
public TemplateUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) { }
14+
15+
private static readonly string _templateString = "{ \"match\": { \"text\": \"{{query_string}}\" }}";
16+
17+
protected override object QueryJson => new
18+
{
19+
template = new
20+
{
21+
_name = "named_query",
22+
boost = 1.1,
23+
inline = _templateString,
24+
@params = new
25+
{
26+
query_string = "all about search"
27+
}
28+
}
29+
30+
31+
};
32+
33+
protected override QueryContainer QueryInitializer => new TemplateQuery
34+
{
35+
Name = "named_query",
36+
Boost = 1.1,
37+
Inline = _templateString,
38+
Params = new Dictionary<string, object>
39+
{
40+
{ "query_string", "all about search" }
41+
}
42+
};
43+
44+
protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project> q) => q
45+
.Template(sn => sn
46+
.Name("named_query")
47+
.Boost(1.1)
48+
.Inline(_templateString)
49+
.Params(p=>p.Add("query_string", "all about search"))
50+
);
51+
}
52+
}

src/Tests/Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,8 @@
379379
<Compile Include="QueryDsl\Span\Term\SpanTermQueryUsageTests.cs" />
380380
<Compile Include="QueryDsl\Span\Within\SpanWithinQueryUsageTests.cs" />
381381
<Compile Include="QueryDsl\Specialized\MoreLikeThis\MoreLikeThisQueryUsageTests.cs" />
382+
<Compile Include="QueryDsl\Specialized\Script\ScriptQueryUsageTests.cs" />
383+
<Compile Include="QueryDsl\Specialized\Template\TemplateQueryUsageTests.cs" />
382384
<Compile Include="Search\Count\CountUrlTests.cs" />
383385
<Compile Include="Search\Count\CountApiTests.cs" />
384386
<Compile Include="Search\Explain\ExplainUrlTests.cs" />

0 commit comments

Comments
 (0)