Skip to content

Commit 1d651dd

Browse files
committed
Merge pull request #598 from andersosthus/master
Adding fuzzy on suggest
2 parents 43a1414 + 56b3441 commit 1d651dd

File tree

8 files changed

+266
-81
lines changed

8 files changed

+266
-81
lines changed

Diff for: new_docs/contents/nest/search/suggest.markdown

+33
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();

Diff for: src/Nest/DSL/Suggest/CompletionSuggestDescriptor.cs

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Nest.Resolvers;
1+
using Nest.DSL.Suggest;
2+
using Nest.Resolvers;
23
using Newtonsoft.Json;
34
using System;
45
using System.Collections.Generic;
@@ -12,13 +13,13 @@ namespace Nest
1213
public class CompletionSuggestDescriptor<T> : BaseSuggestDescriptor<T> where T : class
1314
{
1415
[JsonProperty(PropertyName = "fuzzy")]
15-
internal FuzzySuggestDescriptor<T> _Fuzzy { get; set; }
16+
internal IFuzzySuggestDescriptor<T> _Fuzzy { get; set; }
1617

1718
public CompletionSuggestDescriptor<T> Size(int size)
1819
{
1920
this._Size = size;
2021
return this;
21-
}
22+
}
2223

2324
public CompletionSuggestDescriptor<T> Text(string text)
2425
{
@@ -49,5 +50,17 @@ public CompletionSuggestDescriptor<T> Fuzzy()
4950
this._Fuzzy = new FuzzySuggestDescriptor<T>();
5051
return this;
5152
}
53+
54+
public CompletionSuggestDescriptor<T> Fuzziness(Func<FuzzinessSuggestDescriptor<T>, FuzzinessSuggestDescriptor<T>> fuzzinessDescriptor)
55+
{
56+
this._Fuzzy = fuzzinessDescriptor(new FuzzinessSuggestDescriptor<T>());
57+
return this;
58+
}
59+
60+
public CompletionSuggestDescriptor<T> Fuzziness()
61+
{
62+
this._Fuzzy = new FuzzinessSuggestDescriptor<T>();
63+
return this;
64+
}
5265
}
5366
}

Diff for: src/Nest/DSL/Suggest/FuzzinessSuggestDescriptor.cs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Nest.DSL.Suggest
4+
{
5+
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
6+
public class FuzzinessSuggestDescriptor<T> : IFuzzySuggestDescriptor<T> where T : class
7+
{
8+
[JsonProperty(PropertyName = "fuzziness")]
9+
internal object _Fuzziness { get; private set; }
10+
11+
public FuzzinessSuggestDescriptor()
12+
{
13+
this._Fuzziness = new object();
14+
}
15+
16+
public FuzzinessSuggestDescriptor<T> Fuzziness(string fuzziness)
17+
{
18+
this._Fuzziness = fuzziness;
19+
return this;
20+
}
21+
22+
public FuzzinessSuggestDescriptor<T> Fuzziness(int fuzziness)
23+
{
24+
this._Fuzziness = fuzziness;
25+
return this;
26+
}
27+
28+
public FuzzinessSuggestDescriptor<T> Fuzziness(double fuzziness)
29+
{
30+
this._Fuzziness = fuzziness;
31+
return this;
32+
}
33+
}
34+
}

Diff for: src/Nest/DSL/Suggest/FuzzySuggestDescriptor.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Newtonsoft.Json;
1+
using Nest.DSL.Suggest;
2+
using Newtonsoft.Json;
23
using System;
34
using System.Collections.Generic;
45
using System.Linq;
@@ -7,7 +8,7 @@
78
namespace Nest
89
{
910
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
10-
public class FuzzySuggestDescriptor<T> where T : class
11+
public class FuzzySuggestDescriptor<T> : IFuzzySuggestDescriptor<T> where T : class
1112
{
1213
[JsonProperty(PropertyName="edit_distance")]
1314
internal int _EditDistance { get; set; }

Diff for: src/Nest/DSL/Suggest/IFuzzySuggestDescriptor.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Nest.DSL.Suggest
7+
{
8+
internal interface IFuzzySuggestDescriptor<T>
9+
{
10+
}
11+
}

Diff for: src/Nest/IElasticClient.cs

+17-14
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ ISearchResponse<T> Scroll<T>(Func<ScrollDescriptor<T>, ScrollDescriptor<T>> scro
4141
/// indicating for how long the nodes that participate in the search will maintain relevant resources in
4242
/// order to continue and support it.</para><para>
4343
/// This is very similar in its idea to opening a cursor against a database.</para>
44+
/// <para> </para><para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-scroll.html</para>
4445
/// </summary>
4546
/// <typeparam name="T">The type that represents the result hits</typeparam>
4647
/// <param name="scrollSelector">A descriptor that describes the scroll operation</param>
@@ -248,7 +249,7 @@ Task<IValidateResponse> ValidateAsync<T>(Func<ValidateQueryDescriptor<T>, Valida
248249
/// Allows to put a warmup search request on a specific index (or indices), with the body composing of a regular
249250
/// search request. Types can be provided as part of the URI if the search request is designed to be run only
250251
/// against the specific types.
251-
/// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-warmers.html
252+
/// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-warmers.html#warmer-adding
252253
/// </summary>
253254
/// <param name="name">The name for the warmer that you want to register</param>
254255
/// <param name="selector">A descriptor that further describes what the warmer should look like</param>
@@ -258,55 +259,55 @@ Task<IValidateResponse> ValidateAsync<T>(Func<ValidateQueryDescriptor<T>, Valida
258259
/// Allows to put a warmup search request on a specific index (or indices), with the body composing of a regular
259260
/// search request. Types can be provided as part of the URI if the search request is designed to be run only
260261
/// against the specific types.
261-
/// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-warmers.html
262+
/// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-warmers.html#warmer-adding
262263
/// </summary>
263264
/// <param name="name">The name for the warmer that you want to register</param>
264265
/// <param name="selector">A descriptor that further describes what the warmer should look like</param>
265266
Task<IIndicesOperationResponse> PutWarmerAsync(string name, Func<PutWarmerDescriptor, PutWarmerDescriptor> selector);
266267

267268
/// <summary>
268269
/// Getting a warmer for specific index (or alias, or several indices) based on its name.
269-
/// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-warmers.html
270+
/// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-warmers.html#warmer-retrieving
270271
/// </summary>
271272
/// <param name="name">The name of the warmer to get</param>
272273
/// <param name="selector">An optional selector specifying additional parameters for the get warmer operation</param>
273274
IWarmerResponse GetWarmer(string name, Func<GetWarmerDescriptor, GetWarmerDescriptor> selector = null);
274275

275276
/// <summary>
276277
/// Getting a warmer for specific index (or alias, or several indices) based on its name.
277-
/// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-warmers.html
278+
/// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-warmers.html#warmer-retrieving
278279
/// </summary>
279280
/// <param name="name">The name of the warmer to get</param>
280281
/// <param name="selector">An optional selector specifying additional parameters for the get warmer operation</param>
281282
Task<IWarmerResponse> GetWarmerAsync(string name, Func<GetWarmerDescriptor, GetWarmerDescriptor> selector = null);
282283

283284
/// <summary>
284285
/// Deletes a warmer
285-
/// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-warmers.html
286+
/// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-warmers.html#removing
286287
/// </summary>
287288
/// <param name="name">The name of the warmer to delete</param>
288289
/// <param name="selector">An optional selector specifying additional parameters for the delete warmer operation</param>
289290
IIndicesOperationResponse DeleteWarmer(string name, Func<DeleteWarmerDescriptor, DeleteWarmerDescriptor> selector = null);
290291

291292
/// <summary>
292293
/// Deletes a warmer
293-
/// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-warmers.html
294+
/// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-warmers.html#removing
294295
/// </summary>
295296
/// <param name="name">The name of the warmer to delete</param>
296297
/// <param name="selector">An optional selector specifying additional parameters for the delete warmer operation</param>
297298
Task<IIndicesOperationResponse> DeleteWarmerAsync(string name, Func<DeleteWarmerDescriptor, DeleteWarmerDescriptor> selector = null);
298299

299300
/// <summary>
300301
/// Gets an index template
301-
/// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-templates.html
302+
/// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-templates.html#getting
302303
/// </summary>
303304
/// <param name="name">The name of the template to get</param>
304305
/// <param name="getTemplateSelector">An optional selector specifying additional parameters for the get template operation</param>
305306
ITemplateResponse GetTemplate(string name, Func<GetTemplateDescriptor, GetTemplateDescriptor> getTemplateSelector = null);
306307

307308
/// <summary>
308309
/// Gets an index template
309-
/// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-templates.html
310+
/// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-templates.html#getting
310311
/// </summary>
311312
/// <param name="name">The name of the template to get</param>
312313
/// <param name="getTemplateSelector">An optional selector specifying additional parameters for the get template operation</param>
@@ -334,15 +335,15 @@ Task<IValidateResponse> ValidateAsync<T>(Func<ValidateQueryDescriptor<T>, Valida
334335

335336
/// <summary>
336337
/// Deletes an index template
337-
/// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-templates.html
338+
/// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-templates.html#delete
338339
/// </summary>
339340
/// <param name="name">The name of the template to delete</param>
340341
/// <param name="deleteTemplateSelector">An optional selector specifying additional parameters for the delete template operation</param>
341342
IIndicesOperationResponse DeleteTemplate(string name, Func<DeleteTemplateDescriptor, DeleteTemplateDescriptor> deleteTemplateSelector = null);
342343

343344
/// <summary>
344345
/// Deletes an index template
345-
/// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-templates.html
346+
/// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-templates.html#delete
346347
/// </summary>
347348
/// <param name="name">The name of the template to delete</param>
348349
/// <param name="deleteTemplateSelector">An optional selector specifying additional parameters for the delete template operation</param>
@@ -471,6 +472,7 @@ IPercolateCountResponse PercolateCount<T, K>(K @object, Func<PercolateCountDescr
471472
Task<IPercolateCountResponse> PercolateCountAsync<T, K>(K @object, Func<PercolateCountDescriptor<T, K>, PercolateCountDescriptor<T, K>> percolateSelector = null)
472473
where T : class
473474
where K : class;
475+
474476
/// <summary>
475477
/// The put mapping API allows to register specific mapping definition for a specific type.
476478
/// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-put-mapping.html
@@ -1034,7 +1036,7 @@ Task<IBulkResponse> IndexManyAsync<T>(IEnumerable<T> objects, string index = nul
10341036

10351037
/// <summary>
10361038
/// The suggest feature suggests similar looking terms based on a provided text by using a suggester.
1037-
/// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-status.html
1039+
/// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters.html
10381040
/// </summary>
10391041
/// <typeparam name="T">The type used to strongly type parts of the suggest operation</typeparam>
10401042
/// <param name="selector">The suggesters to use this operation (can be multiple)</param>
@@ -1043,7 +1045,7 @@ ISuggestResponse Suggest<T>(Func<SuggestDescriptor<T>, SuggestDescriptor<T>> sel
10431045

10441046
/// <summary>
10451047
/// The suggest feature suggests similar looking terms based on a provided text by using a suggester.
1046-
/// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-status.html
1048+
/// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters.html
10471049
/// </summary>
10481050
/// <typeparam name="T">The type used to strongly type parts of the suggest operation</typeparam>
10491051
/// <param name="selector">The suggesters to use this operation (can be multiple)</param>
@@ -1115,15 +1117,16 @@ Task<IExistsResponse> DocumentExistsAsync<T>(Func<DocumentExistsDescriptor<T>, D
11151117
Task<IAcknowledgedResponse> DeleteRepositoryAsync(string repository, Func<DeleteRepositoryDescriptor, DeleteRepositoryDescriptor> selector = null);
11161118

11171119
/// <summary>
1118-
/// A repository can contain multiple snapshots of the same cluster. Snapshot are identified by unique names within the cluster.
1120+
/// A repository can contain multiple snapshots of the same cluster. Snapshot are identified by unique names within the cluster.
1121+
/// /// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-snapshots.html#_snapshot
11191122
/// </summary>
11201123
/// <param name="repository">The name of the repository we want to create a snapshot in</param>
11211124
/// <param name="snapshotName">The name of the snapshot</param>
11221125
/// <param name="selector">Optionally provide more details about the snapshot operation</param>
11231126
ISnapshotResponse Snapshot(string repository, string snapshotName, Func<SnapshotDescriptor, SnapshotDescriptor> selector = null);
11241127

11251128
/// <summary>
1126-
/// A repository can contain multiple snapshots of the same cluster. Snapshot are identified by unique names within the cluster.
1129+
/// A repository can contain multiple snapshots of the same cluster. Snapshot are identified by unique names within the cluster.
11271130
/// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-snapshots.html#_snapshot
11281131
/// </summary>
11291132
/// <param name="repository">The name of the repository we want to create a snapshot in</param>

Diff for: src/Nest/Nest.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@
227227
<Compile Include="DSL\Paths\IndexTypePathTypedDescriptor.cs" />
228228
<Compile Include="DSL\SearchDescriptorBase.cs" />
229229
<Compile Include="DSL\Search\SourceDescriptor.cs" />
230+
<Compile Include="DSL\Suggest\FuzzinessSuggestDescriptor.cs" />
231+
<Compile Include="DSL\Suggest\IFuzzySuggestDescriptor.cs" />
230232
<Compile Include="DSL\UnregisterPercolatorDescriptor.cs" />
231233
<Compile Include="DSL\DeleteTemplateDescriptor.cs" />
232234
<Compile Include="DSL\GetTemplateDescriptor.cs" />

0 commit comments

Comments
 (0)