Skip to content

Commit 28f8608

Browse files
committed
Fuzziness now generates empty JSON object by default
.Fuzziness() now generates: ``` "fuzzy": { "fuzziness": {} } ``` Updated tests to reflect this. Also cleaned up the expected strings in some cases (and my auto format did sneak in some spaces to tabs conversions, but it looks like that's the desired format, so I hope you don't mind). Also tried to add a page to the documentation about Suggest.
1 parent dffc720 commit 28f8608

File tree

4 files changed

+141
-68
lines changed

4 files changed

+141
-68
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<hr />
2+
3+
<p>template: layout.jade
4+
title: Suggest
5+
menusection: search</p>
6+
7+
<h2>menuitem: suggest</h2>
8+
9+
<h1>Suggest</h1>
10+
11+
<p>The suggest feature suggests similar looking terms based on a provided text by using a suggester.</p>
12+
13+
<h2>Completion Suggester</h2>
14+
15+
<pre><code>client.Suggest&lt;ElasticSearchProject&gt;(s =&gt; s
16+
.Completion("my-suggestion", c =&gt; c
17+
.Text("test")));
18+
</code></pre>
19+
20+
<p>You can also use fuzzy parameters, either by specifying edit distance and such:</p>
21+
22+
<pre><code>client.Suggest&lt;ElasticSearchProject&gt;(s =&gt; s
23+
.Completion("my-suggestion", c =&gt; c
24+
.Text("test")
25+
.Fuzzy(f =&gt; f
26+
.EditDistance(2)
27+
.Transpositions(false)
28+
.MinLength(5)
29+
.PrefixLength(4))));
30+
</code></pre>
31+
32+
<p>Or by using the Fuzziness variant:</p>
33+
34+
<pre><code>client.Suggest&lt;ElasticSearchProject&gt;(s =&gt; s
35+
.Completion("my-suggestion", c =&gt; c
36+
.Text("test")
37+
.Fuzziness();
38+
</code></pre>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
template: layout.jade
3+
title: Suggest
4+
menusection: search
5+
menuitem: suggest
6+
---
7+
8+
# Suggest
9+
The suggest feature suggests similar looking terms based on a provided text by using a suggester.
10+
11+
## Completion Suggester
12+
13+
client.Suggest<ElasticSearchProject>(s => s
14+
.Completion("my-suggestion", c => c
15+
.Text("test")));
16+
17+
You can also use fuzzy parameters, either by specifying edit distance and such:
18+
19+
client.Suggest<ElasticSearchProject>(s => s
20+
.Completion("my-suggestion", c => c
21+
.Text("test")
22+
.Fuzzy(f => f
23+
.EditDistance(2)
24+
.Transpositions(false)
25+
.MinLength(5)
26+
.PrefixLength(4))));
27+
28+
Or by using the Fuzziness variant:
29+
30+
client.Suggest<ElasticSearchProject>(s => s
31+
.Completion("my-suggestion", c => c
32+
.Text("test")
33+
.Fuzziness();

src/Nest/DSL/Suggest/FuzzinessSuggestDescriptor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ namespace Nest.DSL.Suggest
66
public class FuzzinessSuggestDescriptor<T> : IFuzzySuggestDescriptor<T> where T : class
77
{
88
[JsonProperty(PropertyName = "fuzziness")]
9-
internal object _Fuzziness { get; set; }
9+
internal object _Fuzziness { get; private set; }
1010

1111
public FuzzinessSuggestDescriptor()
1212
{
13-
this._Fuzziness = null;
13+
this._Fuzziness = new object();
1414
}
1515

1616
public FuzzinessSuggestDescriptor<T> Fuzziness(string fuzziness)

src/Tests/Nest.Tests.Unit/Search/suggest/CompletionSuggestTests.cs

Lines changed: 68 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,45 @@
88

99
namespace Nest.Tests.Unit.Search.Suggest
1010
{
11-
[TestFixture]
12-
public class CompletionSuggestTests : BaseJsonTests
13-
{
14-
[Test]
15-
public void CompletionSuggestDescriptorTest()
16-
{
17-
var completionSuggestDescriptor = new CompletionSuggestDescriptor<ElasticsearchProject>()
18-
.OnField("suggest")
19-
.Text("n");
20-
21-
var json = TestElasticClient.Serialize(completionSuggestDescriptor);
22-
23-
var expected = @"{ ""field"": ""suggest"" }";
24-
25-
Assert.IsTrue(json.JsonEquals(expected), json);
26-
}
27-
28-
[Test]
29-
public void CompletionSuggestDescriptorDefaultFuzzyTest()
30-
{
31-
var completionSuggestDescriptor = new CompletionSuggestDescriptor<ElasticsearchProject>()
32-
.OnField("suggest")
33-
.Text("n")
11+
[TestFixture]
12+
public class CompletionSuggestTests : BaseJsonTests
13+
{
14+
[Test]
15+
public void CompletionSuggestDescriptorTest()
16+
{
17+
var completionSuggestDescriptor = new CompletionSuggestDescriptor<ElasticsearchProject>()
18+
.OnField("suggest")
19+
.Text("n");
20+
21+
var json = TestElasticClient.Serialize(completionSuggestDescriptor);
22+
23+
var expected = @"{ field: ""suggest"" }";
24+
25+
Assert.IsTrue(json.JsonEquals(expected), json);
26+
}
27+
28+
[Test]
29+
public void CompletionSuggestDescriptorDefaultFuzzyTest()
30+
{
31+
var completionSuggestDescriptor = new CompletionSuggestDescriptor<ElasticsearchProject>()
32+
.OnField("suggest")
33+
.Text("n")
3434
.Fuzzy();
3535

36-
var json = TestElasticClient.Serialize(completionSuggestDescriptor);
36+
var json = TestElasticClient.Serialize(completionSuggestDescriptor);
3737

38-
var expected = @"{
39-
""fuzzy"": {
40-
""edit_distance"": 1,
41-
""transpositions"": true,
42-
""min_length"": 3,
43-
""prefix_length"": 1
38+
var expected = @"{
39+
fuzzy: {
40+
edit_distance: 1,
41+
transpositions: true,
42+
min_length: 3,
43+
prefix_length: 1
4444
},
45-
""field"": ""suggest""
45+
field: ""suggest""
4646
}";
4747

48-
Assert.IsTrue(json.JsonEquals(expected), json);
49-
}
48+
Assert.IsTrue(json.JsonEquals(expected), json);
49+
}
5050

5151
[Test]
5252
public void CompletionSuggestDescriptorDefaultFuzzinessTest()
@@ -59,40 +59,41 @@ public void CompletionSuggestDescriptorDefaultFuzzinessTest()
5959
var json = TestElasticClient.Serialize(completionSuggestDescriptor);
6060

6161
var expected = @"{
62-
""fuzzy"": {
62+
fuzzy: {
63+
fuzziness: {}
6364
},
64-
""field"": ""suggest""
65+
field: ""suggest""
6566
}";
6667

6768
Assert.IsTrue(json.JsonEquals(expected), json);
6869
}
6970

70-
[Test]
71-
public void CompletionSuggestDescriptorFuzzyTest()
72-
{
73-
var completionSuggestDescriptor = new CompletionSuggestDescriptor<ElasticsearchProject>()
74-
.OnField("suggest")
75-
.Text("n")
76-
.Fuzzy(f => f
77-
.EditDistance(2)
78-
.Transpositions(false)
79-
.MinLength(5)
80-
.PrefixLength(4));
81-
82-
var json = TestElasticClient.Serialize(completionSuggestDescriptor);
83-
84-
var expected = @"{
85-
""fuzzy"": {
86-
""edit_distance"": 2,
87-
""transpositions"": false,
88-
""min_length"": 5,
89-
""prefix_length"": 4
71+
[Test]
72+
public void CompletionSuggestDescriptorFuzzyTest()
73+
{
74+
var completionSuggestDescriptor = new CompletionSuggestDescriptor<ElasticsearchProject>()
75+
.OnField("suggest")
76+
.Text("n")
77+
.Fuzzy(f => f
78+
.EditDistance(2)
79+
.Transpositions(false)
80+
.MinLength(5)
81+
.PrefixLength(4));
82+
83+
var json = TestElasticClient.Serialize(completionSuggestDescriptor);
84+
85+
var expected = @"{
86+
fuzzy"": {
87+
edit_distance: 2,
88+
transpositions: false,
89+
min_length: 5,
90+
prefix_length: 4
9091
},
91-
""field"": ""suggest""
92+
field: ""suggest""
9293
}";
9394

94-
Assert.IsTrue(json.JsonEquals(expected), json);
95-
}
95+
Assert.IsTrue(json.JsonEquals(expected), json);
96+
}
9697

9798
[Test]
9899
public void CompletionSuggestDescriptorFuzzinessTest()
@@ -105,10 +106,10 @@ public void CompletionSuggestDescriptorFuzzinessTest()
105106
var json = TestElasticClient.Serialize(completionSuggestDescriptor);
106107

107108
var expected = @"{
108-
""fuzzy"": {
109-
""fuzziness"": 1
109+
fuzzy: {
110+
fuzziness: 1
110111
},
111-
""field"": ""suggest""
112+
field: ""suggest""
112113
}";
113114

114115
Assert.IsTrue(json.JsonEquals(expected), json);
@@ -125,10 +126,10 @@ public void CompletionSuggestDescriptorFuzzinessDoubleTest()
125126
var json = TestElasticClient.Serialize(completionSuggestDescriptor);
126127

127128
var expected = @"{
128-
""fuzzy"": {
129-
""fuzziness"": 0.4
129+
fuzzy: {
130+
fuzziness: 0.4
130131
},
131-
""field"": ""suggest""
132+
field: ""suggest""
132133
}";
133134

134135
Assert.IsTrue(json.JsonEquals(expected), json);
@@ -140,7 +141,7 @@ public void CompletionSuggestOnSearchTest()
140141
var search = this._client.Search<ElasticsearchProject>(s => s
141142
.SuggestCompletion("mycompletionsuggest", ts => ts
142143
.Text("n")
143-
.OnField(p=>p.Name)
144+
.OnField(p => p.Name)
144145
.Fuzzy()
145146
)
146147
);
@@ -182,6 +183,7 @@ public void CompletionSuggestWithFuzzinessOnSearchTest()
182183
text: ""n"",
183184
completion: {
184185
fuzzy: {
186+
fuzziness: {}
185187
},
186188
field: ""name""
187189
}
@@ -191,5 +193,5 @@ public void CompletionSuggestWithFuzzinessOnSearchTest()
191193
var json = search.ConnectionStatus.Request.Utf8String();
192194
Assert.True(json.JsonEquals(expected), json);
193195
}
194-
}
196+
}
195197
}

0 commit comments

Comments
 (0)