Skip to content

Add Collate support to phrase suggester #975

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

Merged
merged 1 commit into from
Oct 8, 2014
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
80 changes: 80 additions & 0 deletions src/Nest/DSL/Suggest/PhraseSuggestCollateDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nest
{
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public interface IPhraseSuggestCollate
{
[JsonProperty(PropertyName = "query")]
IQueryContainer Query { get; set; }

[JsonProperty(PropertyName = "filter")]
IFilterContainer Filter { get; set; }

[JsonProperty(PropertyName = "params")]
IDictionary<string, object> Params { get; set; }

[JsonProperty(PropertyName = "preference")]
string Preference { get; set; }
}

public class PhraseSuggestCollate : IPhraseSuggestCollate
{
public IQueryContainer Query { get; set; }

public IFilterContainer Filter { get; set; }

public IDictionary<string, object> Params { get; set; }

public string Preference { get; set; }
}

public class PhraseSuggestCollateDescriptor<T> : IPhraseSuggestCollate
where T : class
{
internal IPhraseSuggestCollate Collate = new PhraseSuggestCollate();

IQueryContainer IPhraseSuggestCollate.Query { get; set; }

IFilterContainer IPhraseSuggestCollate.Filter { get; set; }

IDictionary<string, object> IPhraseSuggestCollate.Params { get; set; }

string IPhraseSuggestCollate.Preference { get; set; }

public PhraseSuggestCollateDescriptor<T> Query(Func<QueryDescriptor<T>, QueryContainer> query)
{
this.Collate.Query = query(new QueryDescriptor<T>());
return this;
}

public PhraseSuggestCollateDescriptor<T> Filter(Func<FilterDescriptor<T>, FilterContainer> filter)
{
this.Collate.Filter = filter(new FilterDescriptor<T>());
return this;
}

public PhraseSuggestCollateDescriptor<T> Params(IDictionary<string, object> paramsDictionary)
{
this.Collate.Params = paramsDictionary;
return this;
}

public PhraseSuggestCollateDescriptor<T> Params(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> paramsDictionary)
{
this.Collate.Params = paramsDictionary(new FluentDictionary<string, object>());
return this;
}

public PhraseSuggestCollateDescriptor<T> Preference(string preference)
{
this.Collate.Preference = preference;
return this;
}
}

}
14 changes: 14 additions & 0 deletions src/Nest/DSL/Suggest/PhraseSuggestDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public interface IPhraseSuggester : ISuggester

[JsonProperty(PropertyName = "direct_generator")]
IEnumerable<IDirectGenerator> DirectGenerator { get; set; }

[JsonProperty("collate")]
IPhraseSuggestCollate Collate { get; set; }
}

public class PhraseSuggester : Suggester, IPhraseSuggester
Expand All @@ -38,6 +41,8 @@ public class PhraseSuggester : Suggester, IPhraseSuggester
public decimal? MaxErrors { get; set; }
public char? Separator { get; set; }
public IEnumerable<IDirectGenerator> DirectGenerator { get; set; }

public IPhraseSuggestCollate Collate { get; set; }
}

public class PhraseSuggestDescriptor<T> : BaseSuggestDescriptor<T>, IPhraseSuggester where T : class
Expand All @@ -56,6 +61,8 @@ public class PhraseSuggestDescriptor<T> : BaseSuggestDescriptor<T>, IPhraseSugge

IEnumerable<IDirectGenerator> IPhraseSuggester.DirectGenerator { get; set; }

IPhraseSuggestCollate IPhraseSuggester.Collate { get; set; }

public PhraseSuggestDescriptor<T> Text(string text)
{
Self.Text = text;
Expand Down Expand Up @@ -121,5 +128,12 @@ public PhraseSuggestDescriptor<T> DirectGenerator(params Func<DirectGeneratorDes
Self.DirectGenerator = generators.Select(g => g(new DirectGeneratorDescriptor<T>())).ToList();
return this;
}

public PhraseSuggestDescriptor<T> Collate(Func<PhraseSuggestCollateDescriptor<T>, PhraseSuggestCollateDescriptor<T>> collateDescriptor)
{
var selector = collateDescriptor(new PhraseSuggestCollateDescriptor<T>());
Self.Collate = selector.Collate;
return this;
}
}
}
1 change: 1 addition & 0 deletions src/Nest/Nest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@
<Compile Include="DSL\Paths\RepositorySnapshotOptionalPathDescriptor.cs" />
<Compile Include="DSL\SnapshotStatusDescriptor.cs" />
<Compile Include="DSL\RecoveryStatusDescriptor.cs" />
<Compile Include="DSL\Suggest\PhraseSuggestCollateDescriptor.cs" />
<Compile Include="DSL\TemplateExistsDescriptor.cs" />
<Compile Include="Domain\Responses\PingResponse.cs" />
<Compile Include="Domain\Responses\NodesShutdownResponse.cs" />
Expand Down
58 changes: 58 additions & 0 deletions src/Tests/Nest.Tests.Unit/Search/suggest/PhraseSuggestTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Elasticsearch.Net;
using NUnit.Framework;
using Nest.Tests.MockData.Domain;
using System.Collections.Generic;

namespace Nest.Tests.Unit.Search.Suggest
{
Expand Down Expand Up @@ -84,5 +85,62 @@ public void PhraseSuggestOnSearchTest()
var json = search.ConnectionStatus.Request.Utf8String();
Assert.True(json.JsonEquals(expected), json);
}

[Test]
public void PhraseSuggestCollateTest()
{
var search = this._client.Search<ElasticsearchProject>(s => s
.SuggestPhrase("myphrasesuggest", ts => ts
.Text("n")
.Analyzer("body")
.OnField("bigram")
.Size(1)
.GramSize(2)
.MaxErrors(0.5m)
.Collate(c => c
.Query(q => q
.Match(m => m
.OnField("{{fieldname}}")
.Query("{{suggestion}}")
)
)
.Params(ps => ps
.Add("{{fieldname}}", "name")
)
.Preference("_primary")
)
)
);

var expected = @"{
suggest: {
myphrasesuggest: {
text: ""n"",
phrase: {
""field"": ""bigram"",
""analyzer"": ""body"",
""size"": 1,
""gram_size"": 2,
""max_errors"": 0.5,
""collate"": {
""query"": {
""match"": {
""{{fieldname}}"": {
""query"": ""{{suggestion}}""
}
}
},
""params"": {
""{{fieldname}}"": ""name""
},
""preference"": ""_primary""
}
}
}
}
}";
var json = search.ConnectionStatus.Request.Utf8String();
Assert.True(json.JsonEquals(expected), json);
}
}
}