Skip to content

Commit 86d9f5e

Browse files
committed
Add support for template query
1 parent 2aa172e commit 86d9f5e

File tree

9 files changed

+190
-0
lines changed

9 files changed

+190
-0
lines changed

src/Nest/DSL/Query/IQueryContainer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ public interface IQueryContainer : ICustomJson
140140
[JsonProperty(PropertyName = "function_score")]
141141
IFunctionScoreQuery FunctionScore { get; set; }
142142

143+
[JsonProperty(PropertyName = "template")]
144+
ITemplateQuery Template { get; set; }
145+
143146
void Accept(IQueryVisitor visitor);
144147
}
145148
}

src/Nest/DSL/Query/QueryContainer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ public class QueryContainer : IQueryContainer
8686
IIndicesQuery IQueryContainer.Indices { get; set; }
8787

8888
IFunctionScoreQuery IQueryContainer.FunctionScore { get; set; }
89+
90+
ITemplateQuery IQueryContainer.Template { get; set; }
8991

9092
public QueryContainer() {}
9193

src/Nest/DSL/Query/QueryDescriptor.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,5 +854,12 @@ public QueryContainer FunctionScore(Action<FunctionScoreQueryDescriptor<T>> func
854854
return this.New(query, q => q.FunctionScore = query);
855855

856856
}
857+
858+
public QueryContainer Template(Action<TemplateQueryDescriptor> selector)
859+
{
860+
var query = new TemplateQueryDescriptor();
861+
selector(query);
862+
return this.New(query, q => q.Template = query);
863+
}
857864
}
858865
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
7+
namespace Nest
8+
{
9+
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
10+
public interface ITemplateQuery : IQuery
11+
{
12+
[JsonProperty("query")]
13+
string Query { get; set; }
14+
15+
[JsonProperty("params")]
16+
IDictionary<string, object> Params { get; set; }
17+
}
18+
19+
public class TemplateQuery : PlainQuery, ITemplateQuery
20+
{
21+
protected override void WrapInContainer(IQueryContainer container)
22+
{
23+
container.Template = this;
24+
}
25+
26+
public string Query { get; set; }
27+
28+
public IDictionary<string, object> Params { get; set;}
29+
30+
public bool IsConditionless
31+
{
32+
get { return this.Query.IsNullOrEmpty(); }
33+
}
34+
}
35+
36+
public class TemplateQueryDescriptor : ITemplateQuery
37+
{
38+
ITemplateQuery Self { get { return this; } }
39+
40+
string ITemplateQuery.Query { get; set; }
41+
42+
IDictionary<string, object> ITemplateQuery.Params { get; set; }
43+
44+
bool IQuery.IsConditionless { get { return this.Self.Query.IsNullOrEmpty(); } }
45+
46+
public TemplateQueryDescriptor Query(string query)
47+
{
48+
this.Self.Query = query;
49+
return this;
50+
}
51+
52+
public TemplateQueryDescriptor Params(IDictionary<string, object> paramsDictionary)
53+
{
54+
paramsDictionary.ThrowIfNull("paramsDictionary");
55+
this.Self.Params = paramsDictionary;
56+
return this;
57+
}
58+
59+
public TemplateQueryDescriptor Params(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> paramsDictionary)
60+
{
61+
paramsDictionary.ThrowIfNull("paramsDictionary");
62+
this.Self.Params = paramsDictionary(new FluentDictionary<string,object>());
63+
return this;
64+
}
65+
}
66+
}

src/Nest/Nest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@
202202
<Compile Include="DSL\PutSearchTemplateDescriptor.cs" />
203203
<Compile Include="DSL\DeleteSearchTemplateDescriptor.cs" />
204204
<Compile Include="DSL\GetSearchTemplateDescriptor.cs" />
205+
<Compile Include="DSL\Query\TemplateQueryDescriptor.cs" />
205206
<Compile Include="DSL\Template\TemplateSectionQueryContainer.cs" />
206207
<Compile Include="DSL\Template\TemplateSectionSortFieldDescriptor.cs" />
207208
<Compile Include="DSL\CatIndicesDescriptor.cs" />

src/Tests/Nest.Tests.Integration/Nest.Tests.Integration.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@
179179
<Compile Include="Search\Filter\RangeFilterTests.cs" />
180180
<Compile Include="Search\Query\BoolQueryResults.cs" />
181181
<Compile Include="Search\Query\SpanQueryTests.cs" />
182+
<Compile Include="Search\Query\TemplateQueryTests.cs" />
182183
<Compile Include="Search\Query\TermToString.cs" />
183184
<Compile Include="Core\UpdateTests.cs" />
184185
<Compile Include="Search\Filter\BoolFilterTests.cs" />
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+

2+
using FluentAssertions;
3+
using Nest.Tests.MockData.Domain;
4+
using NUnit.Framework;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
11+
namespace Nest.Tests.Integration.Search.Query
12+
{
13+
[TestFixture]
14+
public class TemplateQueryTests : IntegrationTests
15+
{
16+
[Test]
17+
public void TemplateQuery()
18+
{
19+
var r = this.Client.Search<ElasticsearchProject>(s => s
20+
.Query(q => q
21+
.Template(t => t
22+
.Query("{ \"match\": { \"{{my_field}}\": { \"query\": \"{{my_value}}\" } } }")
23+
.Params(p => p
24+
.Add("my_field", "name")
25+
.Add("my_value", "em-elasticsearch")
26+
)
27+
)
28+
)
29+
);
30+
31+
r.IsValid.Should().BeTrue();
32+
r.Hits.Count().Should().BeGreaterThan(0);
33+
}
34+
}
35+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@
405405
<Compile Include="Search\Query\Singles\MultiMatch\MultiMatchJson.cs" />
406406
<Compile Include="Search\Query\Modes\QueryModesTests.cs" />
407407
<Compile Include="Search\Query\Singles\SpanMultiTermQueryJson.cs" />
408+
<Compile Include="Search\Query\Singles\TemplateQueryJson.cs" />
408409
<Compile Include="Search\Query\Singles\Term\TermToStringJson.cs" />
409410
<Compile Include="Search\Rescoring\RescoreTests.cs" />
410411
<Compile Include="Search\Facets\DateHistogramFacetJson.cs" />
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using Nest.Tests.MockData.Domain;
2+
using NUnit.Framework;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Nest.Tests.Unit.Search.Query.Singles
10+
{
11+
[TestFixture]
12+
public class TemplateQueryJson : BaseJsonTests
13+
{
14+
[Test]
15+
public void TemplateQuery()
16+
{
17+
var s = new SearchDescriptor<ElasticsearchProject>()
18+
.From(0)
19+
.Size(10)
20+
.Query(q => q
21+
.Template(t => t
22+
.Query("match_{{template}}")
23+
.Params(p => p
24+
.Add("template", "all")
25+
)
26+
)
27+
);
28+
29+
var json = TestElasticClient.Serialize(s);
30+
var expected = @"{ from: 0, size: 10,
31+
query: {
32+
template: {
33+
query: ""match_{{template}}"",
34+
""params"": {
35+
template: ""all""
36+
}
37+
}
38+
}
39+
}";
40+
41+
Assert.True(json.JsonEquals(expected), json);
42+
}
43+
44+
[Test]
45+
public void TemplateQueryWithEscapeString()
46+
{
47+
var s = new SearchDescriptor<ElasticsearchProject>()
48+
.From(0)
49+
.Size(10)
50+
.Query(q => q
51+
.Template(t => t
52+
.Query("\"match_{{template}}\": {}}\"")
53+
.Params(p => p
54+
.Add("template", "all")
55+
)
56+
)
57+
);
58+
59+
var json = TestElasticClient.Serialize(s);
60+
var expected = @"{ from: 0, size: 10,
61+
query: {
62+
template: {
63+
query: ""\""match_{{template}}\"": {}}\"""",
64+
""params"": {
65+
template: ""all""
66+
}
67+
}
68+
}
69+
}";
70+
71+
Assert.True(json.JsonEquals(expected), json);
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)