Skip to content

Commit 6a75540

Browse files
committed
Merge branch 'master' of github.com:gmarz/NEST
Conflicts: src/Nest/Nest.csproj
2 parents d8fc416 + 8bb0085 commit 6a75540

File tree

5 files changed

+151
-0
lines changed

5 files changed

+151
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@
219219
<Compile Include="Search\SearchType\ScriptFieldTests.cs" />
220220
<Compile Include="Internals\Serialize\SerializeTests.cs" />
221221
<Compile Include="Search\Sort\SortTests.cs" />
222+
<Compile Include="Search\Suggest\CompletionSuggestTests.cs" />
222223
<Compile Include="Search\Suggest\PhraseSuggestTests.cs" />
223224
<Compile Include="Search\Suggest\TermSuggestTests.cs" />
224225
<Compile Include="Settings\UsePrettyResponseTests.cs" />
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
8+
namespace Nest.Tests.Unit.Search.Suggest
9+
{
10+
[TestFixture]
11+
public class CompletionSuggestTests : BaseJsonTests
12+
{
13+
[Test]
14+
public void CompletionSuggestDescriptorTest()
15+
{
16+
var completionSuggestDescriptor = new CompletionSuggestDescriptor<ElasticSearchProject>()
17+
.OnField("suggest")
18+
.Text("n");
19+
20+
var json = TestElasticClient.Serialize(completionSuggestDescriptor);
21+
22+
var expected = @"{ ""field"": ""suggest"" }";
23+
24+
Assert.IsTrue(json.JsonEquals(expected), json);
25+
}
26+
27+
[Test]
28+
public void CompletionSuggestDescriptorDefaultFuzzyTest()
29+
{
30+
var completionSuggestDescriptor = new CompletionSuggestDescriptor<ElasticSearchProject>()
31+
.OnField("suggest")
32+
.Text("n")
33+
.Fuzzy();
34+
35+
var json = TestElasticClient.Serialize(completionSuggestDescriptor);
36+
37+
var expected = @"{
38+
""fuzzy"": {
39+
""edit_distance"": 1,
40+
""transpositions"": true,
41+
""min_length"": 3,
42+
""prefix_length"": 1
43+
},
44+
""field"": ""suggest""
45+
}";
46+
47+
Assert.IsTrue(json.JsonEquals(expected), json);
48+
}
49+
50+
[Test]
51+
public void CompletionSuggestDescriptorFuzzyTest()
52+
{
53+
var completionSuggestDescriptor = new CompletionSuggestDescriptor<ElasticSearchProject>()
54+
.OnField("suggest")
55+
.Text("n")
56+
.Fuzzy(f => f
57+
.EditDistance(2)
58+
.Transpositions(false)
59+
.MinLength(5)
60+
.PrefixLength(4));
61+
62+
var json = TestElasticClient.Serialize(completionSuggestDescriptor);
63+
64+
var expected = @"{
65+
""fuzzy"": {
66+
""edit_distance"": 2,
67+
""transpositions"": false,
68+
""min_length"": 5,
69+
""prefix_length"": 4
70+
},
71+
""field"": ""suggest""
72+
}";
73+
74+
Assert.IsTrue(json.JsonEquals(expected), json);
75+
}
76+
}
77+
}

src/Nest/DSL/Suggest/CompletionSuggestDescriptor.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ namespace Nest
1111
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
1212
public class CompletionSuggestDescriptor<T> : BaseSuggestDescriptor<T> where T : class
1313
{
14+
[JsonProperty(PropertyName="fuzzy")]
15+
internal FuzzySuggestDescriptor<T> _Fuzzy { get; set; }
16+
1417
public CompletionSuggestDescriptor<T> Text(string text)
1518
{
1619
this._Text = text;
@@ -28,5 +31,17 @@ public CompletionSuggestDescriptor<T> OnField(Expression<Func<T, object>> object
2831
var fieldName = new PropertyNameResolver().Resolve(objectPath);
2932
return this.OnField(fieldName);
3033
}
34+
35+
public CompletionSuggestDescriptor<T> Fuzzy(Func<FuzzySuggestDescriptor<T>, FuzzySuggestDescriptor<T>> fuzzyDescriptor)
36+
{
37+
this._Fuzzy = fuzzyDescriptor(new FuzzySuggestDescriptor<T>());
38+
return this;
39+
}
40+
41+
public CompletionSuggestDescriptor<T> Fuzzy()
42+
{
43+
this._Fuzzy = new FuzzySuggestDescriptor<T>();
44+
return this;
45+
}
3146
}
3247
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 class FuzzySuggestDescriptor<T> where T : class
11+
{
12+
[JsonProperty(PropertyName="edit_distance")]
13+
internal int _EditDistance { get; set; }
14+
15+
[JsonProperty(PropertyName = "transpositions")]
16+
internal bool _Transpositions { get; set; }
17+
18+
[JsonProperty(PropertyName = "min_length")]
19+
internal int _MinLength { get; set; }
20+
21+
[JsonProperty(PropertyName = "prefix_length")]
22+
internal int _PrefixLength { get; set; }
23+
24+
public FuzzySuggestDescriptor()
25+
{
26+
// Default values
27+
this._EditDistance = 1;
28+
this._Transpositions = true;
29+
this._MinLength = 3;
30+
this._PrefixLength = 1;
31+
}
32+
33+
public FuzzySuggestDescriptor<T> EditDistance(int distance)
34+
{
35+
this._EditDistance = distance;
36+
return this;
37+
}
38+
39+
public FuzzySuggestDescriptor<T> Transpositions(bool transpositions)
40+
{
41+
this._Transpositions = transpositions;
42+
return this;
43+
}
44+
45+
public FuzzySuggestDescriptor<T> MinLength(int length)
46+
{
47+
this._MinLength = length;
48+
return this;
49+
}
50+
51+
public FuzzySuggestDescriptor<T> PrefixLength(int length)
52+
{
53+
this._PrefixLength = length;
54+
return this;
55+
}
56+
}
57+
}

src/Nest/Nest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
<Compile Include="Domain\Mapping\Descriptors\CompletionMappingDescriptor.cs" />
7474
<Compile Include="Domain\Mapping\Types\CompletionMapping.cs" />
7575
<Compile Include="Domain\Responses\RootVersionInfoResponse.cs" />
76+
<Compile Include="DSL\Suggest\FuzzySuggestDescriptor.cs" />
7677
<Compile Include="DSL\Filter\RegexpFilterDescriptor.cs" />
7778
<Compile Include="DSL\Query\RegexpQueryDescriptor.cs" />
7879
<Compile Include="Enums\DynamicMappingOption.cs" />

0 commit comments

Comments
 (0)