Skip to content

Commit b8bd7ff

Browse files
committed
Term() is now smarter and no longer requires you to manually ToString() its arguments
1 parent 49ee42b commit b8bd7ff

File tree

13 files changed

+246
-9
lines changed

13 files changed

+246
-9
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using System;
2+
using System.Reflection;
3+
using FluentAssertions;
4+
using NUnit.Framework;
5+
using Nest.Tests.Integration;
6+
using Nest.Tests.MockData;
7+
using Nest.Tests.MockData.Domain;
8+
9+
namespace Nest.Tests.Integration.Integration.Query
10+
{
11+
[TestFixture]
12+
public class TermToStringJson : IntegrationTests
13+
{
14+
15+
/// <summary>
16+
/// Document used in test.
17+
/// </summary>
18+
private ElasticSearchProject _LookFor;
19+
20+
[TestFixtureSetUp]
21+
public void Initialize()
22+
{
23+
_client.IsValid.Should().BeTrue();
24+
25+
_LookFor = NestTestData.Session.Single<ElasticSearchProject>().Get();
26+
_LookFor.Name = "one two three four";
27+
var status = this._client.Index(_LookFor, new IndexParameters { Refresh = true }).ConnectionStatus;
28+
Assert.True(status.Success, status.Result);
29+
}
30+
31+
[Test]
32+
public void IntToStringTest()
33+
{
34+
var results = _client.Search<ElasticSearchProject>(s => s
35+
.Query(q => q
36+
.Term(p => p.LOC, _LookFor.LOC)
37+
)
38+
);
39+
this.AssertTermResults(results);
40+
}
41+
42+
[Test]
43+
public void DoubleToStringTest()
44+
{
45+
var results = _client.Search<ElasticSearchProject>(s => s
46+
.Query(q => q
47+
.Term(p => p.DoubleValue, _LookFor.DoubleValue)
48+
)
49+
);
50+
this.AssertTermResults(results);
51+
}
52+
53+
[Test]
54+
public void FloatToStringTest()
55+
{
56+
var results = _client.Search<ElasticSearchProject>(s => s
57+
.Query(q => q
58+
.Term(p => p.FloatValue, _LookFor.FloatValue)
59+
)
60+
);
61+
this.AssertTermResults(results);
62+
}
63+
64+
[Test]
65+
public void LongToStringTest()
66+
{
67+
var results = _client.Search<ElasticSearchProject>(s => s
68+
.Query(q => q
69+
.Term(p => p.LongValue, _LookFor.LongValue)
70+
)
71+
);
72+
this.AssertTermResults(results);
73+
}
74+
[Test]
75+
public void DateTimeToStringTest()
76+
{
77+
//this should serialize to ISO NOT simply datetime.tostring()!
78+
var results = _client.Search<ElasticSearchProject>(s => s
79+
.Query(q => q
80+
.Term(p => p.StartedOn, _LookFor.StartedOn)
81+
)
82+
);
83+
this.AssertTermResults(results);
84+
}
85+
86+
87+
private void AssertTermResults(IQueryResponse<ElasticSearchProject> results)
88+
{
89+
Assert.True(results.IsValid, results.ConnectionStatus.Result);
90+
Assert.True(results.ConnectionStatus.Success, results.ConnectionStatus.Result);
91+
Assert.AreEqual(1, results.Total);
92+
}
93+
}
94+
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@
108108
<Compile Include="Internals\Inferno\MapTypeNamesTests.cs" />
109109
<Compile Include="Internals\Serialize\OptOutTests.cs" />
110110
<Compile Include="Search\Filter\Singles\HasParentFilterJson.cs" />
111-
<Compile Include="Search\Query\Singles\MultiMatch\TermQueryJson.cs" />
111+
<Compile Include="Search\Query\Singles\MultiMatch\MultiMatchJson.cs" />
112+
<Compile Include="Search\Query\Singles\Term\TermToStringJson.cs" />
112113
<Compile Include="Search\Rescore\RescoreTests.cs" />
113114
<Compile Include="Search\Facets\DateHistogramFacetJson.cs" />
114115
<Compile Include="Search\Facets\FacetJson.cs" />
@@ -198,7 +199,7 @@
198199
<Compile Include="Search\Query\Singles\SpanNotQueryJson.cs" />
199200
<Compile Include="Search\Query\Singles\SpanOrQueryJson.cs" />
200201
<Compile Include="Search\Query\Singles\SpanTermQueryJson.cs" />
201-
<Compile Include="Search\Query\Singles\TermQueryJson.cs" />
202+
<Compile Include="Search\Query\Singles\Term\TermQueryJson.cs" />
202203
<Compile Include="Search\Query\Singles\TermsQueryJson.cs" />
203204
<Compile Include="Search\Query\Singles\TextPhrasePrefixQueryJson.cs" />
204205
<Compile Include="Search\Query\Singles\TextPhraseQueryJson.cs" />
@@ -354,6 +355,13 @@
354355
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
355356
</None>
356357
<None Include="packages.config" />
358+
<None Include="Search\Query\Singles\MultiMatch\TestMultiMatchJson.json" />
359+
<None Include="Search\Query\Singles\Term\DateTimeWithCustomStringValue.json" />
360+
<None Include="Search\Query\Singles\Term\DateTimeToStringTest.json" />
361+
<None Include="Search\Query\Singles\Term\LongToStringTest.json" />
362+
<None Include="Search\Query\Singles\Term\FloatToStringTest.json" />
363+
<None Include="Search\Query\Singles\Term\DoubleToStringTest.json" />
364+
<None Include="Search\Query\Singles\Term\IntToStringTest.json" />
357365
<None Include="Search\Rescore\RescoreSerializes.json">
358366
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
359367
</None>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"query": {
3+
"term": {
4+
"startedOn": {
5+
"value": "1999-02-02T00:00:00"
6+
}
7+
}
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"query": {
3+
"term": {
4+
"startedOn": {
5+
"value": "1986-03-08"
6+
}
7+
}
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"query": {
3+
"term": {
4+
"doubleValue": {
5+
"value": 20.5
6+
}
7+
}
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"query": {
3+
"term": {
4+
"floatValue": {
5+
"value": 20.9
6+
}
7+
}
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"query": {
3+
"term": {
4+
"loc": {
5+
"value": 20000
6+
}
7+
}
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"query": {
3+
"term": {
4+
"longValue": {
5+
"value": 20000
6+
}
7+
}
8+
}
9+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using System.Reflection;
3+
using NUnit.Framework;
4+
using Nest.Tests.MockData.Domain;
5+
6+
namespace Nest.Tests.Unit.Search.Query.Singles.Term
7+
{
8+
[TestFixture]
9+
public class TermToStringJson : BaseJsonTests
10+
{
11+
[Test]
12+
public void IntToStringTest()
13+
{
14+
var s = new SearchDescriptor<ElasticSearchProject>()
15+
.Query(q => q
16+
.Term(p=>p.LOC, 20000)
17+
);
18+
this.JsonEquals(s, MethodInfo.GetCurrentMethod());
19+
}
20+
21+
[Test]
22+
public void DoubleToStringTest()
23+
{
24+
var s = new SearchDescriptor<ElasticSearchProject>()
25+
.Query(q => q
26+
.Term(p => p.DoubleValue, 20.5)
27+
);
28+
this.JsonEquals(s, MethodInfo.GetCurrentMethod());
29+
}
30+
31+
[Test]
32+
public void FloatToStringTest()
33+
{
34+
var s = new SearchDescriptor<ElasticSearchProject>()
35+
.Query(q => q
36+
.Term(p => p.FloatValue, 20.9f)
37+
);
38+
this.JsonEquals(s, MethodInfo.GetCurrentMethod());
39+
}
40+
41+
[Test]
42+
public void LongToStringTest()
43+
{
44+
var s = new SearchDescriptor<ElasticSearchProject>()
45+
.Query(q => q
46+
.Term(p => p.LongValue, 20000L)
47+
);
48+
this.JsonEquals(s, MethodInfo.GetCurrentMethod());
49+
}
50+
[Test]
51+
public void DateTimeToStringTest()
52+
{
53+
//this should serialize to ISO NOT simply datetime.tostring()!
54+
var s = new SearchDescriptor<ElasticSearchProject>()
55+
.Query(q => q
56+
.Term(p => p.StartedOn, new DateTime(1999,2,2))
57+
);
58+
this.JsonEquals(s, MethodInfo.GetCurrentMethod());
59+
}
60+
[Test]
61+
public void DateTimeWithCustomStringValue()
62+
{
63+
var s = new SearchDescriptor<ElasticSearchProject>()
64+
.Query(q => q
65+
.Term(p => p.StartedOn, "1986-03-08")
66+
);
67+
this.JsonEquals(s, MethodInfo.GetCurrentMethod());
68+
}
69+
}
70+
}

src/Nest/DSL/IQueryDescriptor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ interface IQueryDescriptor<T>
3434
BaseQuery SpanOr(Action<SpanOrQueryDescriptor<T>> selector);
3535
BaseQuery SpanTerm(System.Linq.Expressions.Expression<Func<T, object>> fieldDescriptor, string value, double? Boost = null);
3636
BaseQuery SpanTerm(string field, string value, double? Boost = null);
37-
BaseQuery Term(System.Linq.Expressions.Expression<Func<T, object>> fieldDescriptor, string value, double? Boost = null);
38-
BaseQuery Term(string field, string value, double? Boost = null);
37+
BaseQuery Term<K>(System.Linq.Expressions.Expression<Func<T, K>> fieldDescriptor, K value, double? Boost = null);
38+
BaseQuery Term(string field, object value, double? Boost = null);
3939
BaseQuery Terms(System.Linq.Expressions.Expression<Func<T, object>> objectPath, params string[] terms);
4040
BaseQuery Terms(string field, params string[] terms);
4141
BaseQuery TermsDescriptor(Action<TermsQueryDescriptor<T>> selector);

src/Nest/DSL/Query/Term.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ namespace Nest
1414
public class Term : IQuery
1515
{
1616
internal string Field { get; set; }
17-
internal string Value { get; set; }
17+
internal object Value { get; set; }
1818
internal double? Boost { get; set; }
1919

20-
internal bool IsConditionless { get { return this.Value.IsNullOrEmpty() || this.Field.IsNullOrEmpty(); } }
20+
internal bool IsConditionless { get { return this.Value == null || this.Value.ToString().IsNullOrEmpty() || this.Field.IsNullOrEmpty(); } }
2121

2222
public Term()
2323
{

src/Nest/DSL/QueryDescriptor.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -667,12 +667,23 @@ public BaseQuery MatchAll(double? Boost = null, string NormField = null)
667667
return new QueryDescriptor<T> { MatchAllQuery = this.MatchAllQuery };
668668
}
669669

670+
/// <summary>
671+
/// Matches documents that have fields that contain a term (not analyzed).
672+
/// The term query maps to Lucene TermQuery.
673+
/// </summary>
674+
public BaseQuery Term<K>(Expression<Func<T, K>> fieldDescriptor
675+
, K value
676+
, double? Boost = null)
677+
{
678+
var field = new PropertyNameResolver().Resolve(fieldDescriptor);
679+
return this.Term(field, value, Boost);
680+
}
670681
/// <summary>
671682
/// Matches documents that have fields that contain a term (not analyzed).
672683
/// The term query maps to Lucene TermQuery.
673684
/// </summary>
674685
public BaseQuery Term(Expression<Func<T, object>> fieldDescriptor
675-
, string value
686+
, object value
676687
, double? Boost = null)
677688
{
678689
var field = new PropertyNameResolver().Resolve(fieldDescriptor);
@@ -682,9 +693,9 @@ public BaseQuery Term(Expression<Func<T, object>> fieldDescriptor
682693
/// Matches documents that have fields that contain a term (not analyzed).
683694
/// The term query maps to Lucene TermQuery.
684695
/// </summary>
685-
public BaseQuery Term(string field, string value, double? Boost = null)
696+
public BaseQuery Term(string field, object value, double? Boost = null)
686697
{
687-
var term = new Term() { Field = field, Value = value };
698+
var term = new Term() { Field = field, Value = (value != null) ? value : null };
688699
if (term.IsConditionless)
689700
return CreateConditionlessQueryDescriptor(term);
690701
if (Boost.HasValue)

0 commit comments

Comments
 (0)