Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented the completion suggester for auto-complete functionality #376

Merged
merged 1 commit into from
Nov 17, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Nest/DSL/SearchDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,19 @@ public SearchDescriptor<T> PhraseSuggest(string name, Func<PhraseSuggestDescript
return this;
}

public SearchDescriptor<T> CompletionSuggest(string name, Func<CompletionSuggestDescriptor<T>, CompletionSuggestDescriptor<T>> suggest)
{
name.ThrowIfNullOrEmpty("name");
suggest.ThrowIfNull("suggest");
if (this._Suggest == null)
this._Suggest = new Dictionary<String, SuggestDescriptorBucket<T>>();
CompletionSuggestDescriptor<T> desc = new CompletionSuggestDescriptor<T>();
CompletionSuggestDescriptor<T> item = suggest(desc);
SuggestDescriptorBucket<T> bucket = new SuggestDescriptorBucket<T> { _Text = item._Text, CompletionSuggest = item };
this._Suggest.Add(name, bucket);
return this;
}

/// <summary>
/// Describe the query to perform using a query descriptor lambda
/// </summary>
Expand Down
32 changes: 32 additions & 0 deletions src/Nest/DSL/Suggest/CompletionSuggestDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Nest.Resolvers;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Nest
{
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public class CompletionSuggestDescriptor<T> : BaseSuggestDescriptor<T> where T : class
{
public CompletionSuggestDescriptor<T> Text(string text)
{
this._Text = text;
return this;
}

public CompletionSuggestDescriptor<T> OnField(string field)
{
this._Field = field;
return this;
}

public CompletionSuggestDescriptor<T> OnField(Expression<Func<T, object>> objectPath)
{
var fieldName = new PropertyNameResolver().Resolve(objectPath);
return this.OnField(fieldName);
}
}
}
3 changes: 3 additions & 0 deletions src/Nest/DSL/Suggest/SuggestDescriptorBucket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ public class SuggestDescriptorBucket<T> where T : class

[JsonProperty(PropertyName = "phrase")]
public PhraseSuggestDescriptor<T> PhraseSuggest { get; set; }

[JsonProperty(PropertyName = "completion")]
public CompletionSuggestDescriptor<T> CompletionSuggest { get; set; }
}
}
63 changes: 63 additions & 0 deletions src/Nest/Domain/Mapping/Descriptors/CompletionMappingDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Nest.Resolvers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Nest
{
public class CompletionMappingDescriptor<T>
{
internal CompletionMapping _Mapping = new CompletionMapping();

public CompletionMappingDescriptor<T> Name(string name)
{
this._Mapping.TypeNameMarker = name;
return this;
}

public CompletionMappingDescriptor<T> Name(Expression<Func<T, object>> objectPath)
{
var name = new PropertyNameResolver().ResolveToLastToken(objectPath);
this._Mapping.TypeNameMarker = name;
return this;
}

public CompletionMappingDescriptor<T> SearchAnalyzer(string name)
{
this._Mapping.SearchAnalyzer = name;
return this;
}

public CompletionMappingDescriptor<T> IndexAnalyzer(string name)
{
this._Mapping.IndexAnalyzer = name;
return this;
}

public CompletionMappingDescriptor<T> Payloads(bool payloads)
{
this._Mapping.Payloads = payloads;
return this;
}

public CompletionMappingDescriptor<T> PreserveSeparators(bool preserveSeparators)
{
this._Mapping.PreserveSeparators = preserveSeparators;
return this;
}

public CompletionMappingDescriptor<T> PreservePositionIncrements(bool preservePositionIncrements)
{
this._Mapping.PreservePositionIncrements = preservePositionIncrements;
return this;
}

public CompletionMappingDescriptor<T> MaxInputLength(int maxInputLength)
{
this._Mapping.MaxInputLength = maxInputLength;
return this;
}
}
}
10 changes: 10 additions & 0 deletions src/Nest/Domain/Mapping/Descriptors/PropertiesDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ public PropertiesDescriptor<T> Generic(Func<GenericMappingDescriptor<T>, Generic
return this;
}

public PropertiesDescriptor<T> Completion(Func<CompletionMappingDescriptor<T>, CompletionMappingDescriptor<T>> selector)
{
selector.ThrowIfNull("selector");
var d = selector(new CompletionMappingDescriptor<T>());
if (d == null || d._Mapping.TypeNameMarker.IsNullOrEmpty())
throw new Exception("Could not get field name for completion mapping");
this.Properties.Add(d._Mapping.TypeNameMarker.Name, d._Mapping);
return this;
}

//Reminder if you are adding a new mapping type, may one appear in the future
//Add them to PropertiesDescriptor, CorePropertiesDescriptor (if its a new core type), SingleMappingDescriptor
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ public IElasticType Generic(Func<GenericMappingDescriptor<T>, GenericMappingDesc
return d._Mapping;
}

public IElasticType Completion(Func<CompletionMappingDescriptor<T>, CompletionMappingDescriptor<T>> selector)
{
selector.ThrowIfNull("selector");
var d = selector(new CompletionMappingDescriptor<T>());
if (d == null)
throw new Exception("Could not get completion mapping");
return d._Mapping;
}

//Reminder if you are adding a new mapping type, may one appear in the future
//Add them to PropertiesDescriptor, CorePropertiesDescriptor (if its a new core type), SingleMappingDescriptor
}
Expand Down
42 changes: 42 additions & 0 deletions src/Nest/Domain/Mapping/Types/CompletionMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Nest.Resolvers;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nest
{
public class CompletionMapping : IElasticType
{
[JsonProperty("name")]
public string Name { get; set; }

[JsonIgnore]
public TypeNameMarker TypeNameMarker { get; set; }

[JsonProperty("type")]
public virtual TypeNameMarker Type { get { return new TypeNameMarker { Name = "completion" }; } }

[JsonProperty("similarity")]
public string Similarity { get; set; }

[JsonProperty("search_analyzer")]
public string SearchAnalyzer { get; set; }

[JsonProperty("index_analyzer")]
public string IndexAnalyzer { get; set; }

[JsonProperty("payloads")]
public bool? Payloads { get; set; }

[JsonProperty("preserve_separators")]
public bool? PreserveSeparators { get; set; }

[JsonProperty("preserve_position_increments")]
public bool? PreservePositionIncrements { get; set; }

[JsonProperty("max_input_len")]
public int? MaxInputLength { get; set; }
}
}
3 changes: 3 additions & 0 deletions src/Nest/Domain/Suggest/SuggestOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ public class SuggestOption

[JsonProperty("text")]
public string Text { get; internal set; }

[JsonProperty("payload")]
public object Payload { get; internal set; }
}
}
4 changes: 4 additions & 0 deletions src/Nest/Enums/FieldType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public enum FieldType
/// Nested type.
/// </summary>
nested,
/// <summary>
/// Completion type.
/// </summary>
completion,
/// <summary>
/// object type, no need to set this manually if its not a value type this will be set.
/// Only set this if you need to force a value type to be mapped to an elasticsearch object type.
Expand Down
3 changes: 3 additions & 0 deletions src/Nest/Nest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Domain\Connection\AsyncRequestOperation.cs" />
<Compile Include="Domain\Mapping\Descriptors\CompletionMappingDescriptor.cs" />
<Compile Include="Domain\Mapping\Types\CompletionMapping.cs" />
<Compile Include="Domain\Responses\RootVersionInfoResponse.cs" />
<Compile Include="DSL\Suggest\CompletionSuggestDescriptor.cs" />
<Compile Include="ExposedInternals\ElasticInferrer.cs" />
<Compile Include="ExposedInternals\ElasticSerializer.cs" />
<Compile Include="Exception\ConnectionException.cs" />
Expand Down
2 changes: 2 additions & 0 deletions src/Nest/Resolvers/Writers/TypeMappingWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ private string GetElasticSearchTypeFromFieldType(FieldType? fieldType)
return "boolean";
case FieldType.nested:
return "nested";
case FieldType.completion:
return "completion";
case FieldType.@object:
return "object";
default:
Expand Down