Skip to content

Commit 3412783

Browse files
committed
Implemented the completion suggester for auto-complete functionality
1 parent d65ec32 commit 3412783

File tree

11 files changed

+184
-0
lines changed

11 files changed

+184
-0
lines changed

src/Nest/DSL/SearchDescriptor.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,19 @@ public SearchDescriptor<T> PhraseSuggest(string name, Func<PhraseSuggestDescript
873873
return this;
874874
}
875875

876+
public SearchDescriptor<T> CompletionSuggest(string name, Func<CompletionSuggestDescriptor<T>, CompletionSuggestDescriptor<T>> suggest)
877+
{
878+
name.ThrowIfNullOrEmpty("name");
879+
suggest.ThrowIfNull("suggest");
880+
if (this._Suggest == null)
881+
this._Suggest = new Dictionary<String, SuggestDescriptorBucket<T>>();
882+
CompletionSuggestDescriptor<T> desc = new CompletionSuggestDescriptor<T>();
883+
CompletionSuggestDescriptor<T> item = suggest(desc);
884+
SuggestDescriptorBucket<T> bucket = new SuggestDescriptorBucket<T> { _Text = item._Text, CompletionSuggest = item };
885+
this._Suggest.Add(name, bucket);
886+
return this;
887+
}
888+
876889
/// <summary>
877890
/// Describe the query to perform using a query descriptor lambda
878891
/// </summary>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Nest.Resolvers;
2+
using Newtonsoft.Json;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Linq.Expressions;
7+
using System.Text;
8+
9+
namespace Nest
10+
{
11+
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
12+
public class CompletionSuggestDescriptor<T> : BaseSuggestDescriptor<T> where T : class
13+
{
14+
public CompletionSuggestDescriptor<T> Text(string text)
15+
{
16+
this._Text = text;
17+
return this;
18+
}
19+
20+
public CompletionSuggestDescriptor<T> OnField(string field)
21+
{
22+
this._Field = field;
23+
return this;
24+
}
25+
26+
public CompletionSuggestDescriptor<T> OnField(Expression<Func<T, object>> objectPath)
27+
{
28+
var fieldName = new PropertyNameResolver().Resolve(objectPath);
29+
return this.OnField(fieldName);
30+
}
31+
}
32+
}

src/Nest/DSL/Suggest/SuggestDescriptorBucket.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@ public class SuggestDescriptorBucket<T> where T : class
1313

1414
[JsonProperty(PropertyName = "phrase")]
1515
public PhraseSuggestDescriptor<T> PhraseSuggest { get; set; }
16+
17+
[JsonProperty(PropertyName = "completion")]
18+
public CompletionSuggestDescriptor<T> CompletionSuggest { get; set; }
1619
}
1720
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using Nest.Resolvers;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Linq.Expressions;
6+
using System.Text;
7+
8+
namespace Nest
9+
{
10+
public class CompletionMappingDescriptor<T>
11+
{
12+
internal CompletionMapping _Mapping = new CompletionMapping();
13+
14+
public CompletionMappingDescriptor<T> Name(string name)
15+
{
16+
this._Mapping.TypeNameMarker = name;
17+
return this;
18+
}
19+
20+
public CompletionMappingDescriptor<T> Name(Expression<Func<T, object>> objectPath)
21+
{
22+
var name = new PropertyNameResolver().ResolveToLastToken(objectPath);
23+
this._Mapping.TypeNameMarker = name;
24+
return this;
25+
}
26+
27+
public CompletionMappingDescriptor<T> SearchAnalyzer(string name)
28+
{
29+
this._Mapping.SearchAnalyzer = name;
30+
return this;
31+
}
32+
33+
public CompletionMappingDescriptor<T> IndexAnalyzer(string name)
34+
{
35+
this._Mapping.IndexAnalyzer = name;
36+
return this;
37+
}
38+
39+
public CompletionMappingDescriptor<T> Payloads(bool payloads)
40+
{
41+
this._Mapping.Payloads = payloads;
42+
return this;
43+
}
44+
45+
public CompletionMappingDescriptor<T> PreserveSeparators(bool preserveSeparators)
46+
{
47+
this._Mapping.PreserveSeparators = preserveSeparators;
48+
return this;
49+
}
50+
51+
public CompletionMappingDescriptor<T> PreservePositionIncrements(bool preservePositionIncrements)
52+
{
53+
this._Mapping.PreservePositionIncrements = preservePositionIncrements;
54+
return this;
55+
}
56+
57+
public CompletionMappingDescriptor<T> MaxInputLength(int maxInputLength)
58+
{
59+
this._Mapping.MaxInputLength = maxInputLength;
60+
return this;
61+
}
62+
}
63+
}

src/Nest/Domain/Mapping/Descriptors/PropertiesDescriptor.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ public PropertiesDescriptor<T> Generic(Func<GenericMappingDescriptor<T>, Generic
155155
return this;
156156
}
157157

158+
public PropertiesDescriptor<T> Completion(Func<CompletionMappingDescriptor<T>, CompletionMappingDescriptor<T>> selector)
159+
{
160+
selector.ThrowIfNull("selector");
161+
var d = selector(new CompletionMappingDescriptor<T>());
162+
if (d == null || d._Mapping.TypeNameMarker.IsNullOrEmpty())
163+
throw new Exception("Could not get field name for completion mapping");
164+
this.Properties.Add(d._Mapping.TypeNameMarker.Name, d._Mapping);
165+
return this;
166+
}
167+
158168
//Reminder if you are adding a new mapping type, may one appear in the future
159169
//Add them to PropertiesDescriptor, CorePropertiesDescriptor (if its a new core type), SingleMappingDescriptor
160170
}

src/Nest/Domain/Mapping/Descriptors/SingleMappingDescriptor.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ public IElasticType Generic(Func<GenericMappingDescriptor<T>, GenericMappingDesc
130130
return d._Mapping;
131131
}
132132

133+
public IElasticType Completion(Func<CompletionMappingDescriptor<T>, CompletionMappingDescriptor<T>> selector)
134+
{
135+
selector.ThrowIfNull("selector");
136+
var d = selector(new CompletionMappingDescriptor<T>());
137+
if (d == null)
138+
throw new Exception("Could not get completion mapping");
139+
return d._Mapping;
140+
}
141+
133142
//Reminder if you are adding a new mapping type, may one appear in the future
134143
//Add them to PropertiesDescriptor, CorePropertiesDescriptor (if its a new core type), SingleMappingDescriptor
135144
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Nest.Resolvers;
2+
using Newtonsoft.Json;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
8+
namespace Nest
9+
{
10+
public class CompletionMapping : IElasticType
11+
{
12+
[JsonProperty("name")]
13+
public string Name { get; set; }
14+
15+
[JsonIgnore]
16+
public TypeNameMarker TypeNameMarker { get; set; }
17+
18+
[JsonProperty("type")]
19+
public virtual TypeNameMarker Type { get { return new TypeNameMarker { Name = "completion" }; } }
20+
21+
[JsonProperty("similarity")]
22+
public string Similarity { get; set; }
23+
24+
[JsonProperty("search_analyzer")]
25+
public string SearchAnalyzer { get; set; }
26+
27+
[JsonProperty("index_analyzer")]
28+
public string IndexAnalyzer { get; set; }
29+
30+
[JsonProperty("payloads")]
31+
public bool? Payloads { get; set; }
32+
33+
[JsonProperty("preserve_separators")]
34+
public bool? PreserveSeparators { get; set; }
35+
36+
[JsonProperty("preserve_position_increments")]
37+
public bool? PreservePositionIncrements { get; set; }
38+
39+
[JsonProperty("max_input_len")]
40+
public int? MaxInputLength { get; set; }
41+
}
42+
}

src/Nest/Domain/Suggest/SuggestOption.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@ public class SuggestOption
1414

1515
[JsonProperty("text")]
1616
public string Text { get; internal set; }
17+
18+
[JsonProperty("payload")]
19+
public object Payload { get; internal set; }
1720
}
1821
}

src/Nest/Enums/FieldType.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ public enum FieldType
6262
/// Nested type.
6363
/// </summary>
6464
nested,
65+
/// <summary>
66+
/// Completion type.
67+
/// </summary>
68+
completion,
6569
/// <summary>
6670
/// object type, no need to set this manually if its not a value type this will be set.
6771
/// Only set this if you need to force a value type to be mapped to an elasticsearch object type.

src/Nest/Nest.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@
6969
</ItemGroup>
7070
<ItemGroup>
7171
<Compile Include="Domain\Connection\AsyncRequestOperation.cs" />
72+
<Compile Include="Domain\Mapping\Descriptors\CompletionMappingDescriptor.cs" />
73+
<Compile Include="Domain\Mapping\Types\CompletionMapping.cs" />
7274
<Compile Include="Domain\Responses\RootVersionInfoResponse.cs" />
75+
<Compile Include="DSL\Suggest\CompletionSuggestDescriptor.cs" />
7376
<Compile Include="ExposedInternals\ElasticInferrer.cs" />
7477
<Compile Include="ExposedInternals\ElasticSerializer.cs" />
7578
<Compile Include="Exception\ConnectionException.cs" />

0 commit comments

Comments
 (0)