Skip to content

Add Params to PhraseSuggestCollate #2965

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 5 commits into from
Dec 21, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
75 changes: 70 additions & 5 deletions src/Nest/Search/Suggesters/PhraseSuggester/PhraseSuggestCollate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,99 @@

namespace Nest
{
/// <summary>
/// Checks each suggestion against the specified query to prune suggestions
/// for which no matching docs exist in the index.
/// </summary>
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
[JsonConverter(typeof(ReadAsTypeJsonConverter<PhraseSuggestCollate>))]
public interface IPhraseSuggestCollate
{
[JsonProperty(PropertyName = "query")]
/// <summary>
/// The collate query to run.
/// </summary>
/// <remarks>
/// Query parameters should be specified using <see cref="Params"/>
/// </remarks>
[JsonProperty("query")]
ITemplateQuery Query { get; set; }

[JsonProperty(PropertyName = "prune")]
/// <summary>
/// Controls if all phrase suggestions will be returned. When set to <c>true</c>, the suggestions will have
/// an additional option collate_match, which will be <c>true</c> if matching documents for the phrase was found,
/// <c>false</c> otherwise. The default value for <see cref="Prune"/> is <c>false</c>.
/// </summary>
[JsonProperty("prune")]
bool? Prune { get; set; }

/// <summary>
/// The parameters for the query. the suggestion value will be added to the variables you specify.
/// </summary>
[JsonProperty("params")]
IDictionary<string, object> Params { get; set; }
}

/// <inheritdoc />
public class PhraseSuggestCollate : IPhraseSuggestCollate
{
public ITemplateQuery Query { get; set; }
private ITemplateQuery _query;

/// <inheritdoc />
public ITemplateQuery Query
{
get => _query;
set
{
_query = value;
if (_query != null) Params = _query.Params;
}
}

/// <inheritdoc />
public bool? Prune { get; set; }

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

public class PhraseSuggestCollateDescriptor<T> : DescriptorBase<PhraseSuggestCollateDescriptor<T>, IPhraseSuggestCollate>, IPhraseSuggestCollate
where T : class
{
ITemplateQuery IPhraseSuggestCollate.Query { get; set; }

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

/// <summary>
/// The collate query to run.
/// </summary>
/// <remarks>
/// Query parameters should be specified using <see cref="Params(IDictionary&lt;string, object&gt;)"/> or
/// Params(Func&lt;FluentDictionary&lt;string, object&gt;, FluentDictionary&lt;string, object&gt;&gt;)
/// </remarks>
public PhraseSuggestCollateDescriptor<T> Query(Func<TemplateQueryDescriptor<T>, ITemplateQuery> selector) =>
Assign(a => a.Query = selector?.Invoke(new TemplateQueryDescriptor<T>()));
Assign(a =>
{
var templateQuery = selector?.Invoke(new TemplateQueryDescriptor<T>());
a.Query = templateQuery;
if (templateQuery != null) Self.Params = templateQuery.Params;
});

/// <summary>
/// Controls if all phrase suggestions will be returned. When set to <c>true</c>, the suggestions will have
/// an additional option collate_match, which will be <c>true</c> if matching documents for the phrase was found,
/// <c>false</c> otherwise. The default value for <see cref="Prune"/> is <c>false</c>.
/// </summary>
public PhraseSuggestCollateDescriptor<T> Prune(bool? prune = true) => Assign(a => a.Prune = prune);

/// <summary>
/// The parameters for the query. the suggestion value will be added to the variables you specify.
/// </summary>
public PhraseSuggestCollateDescriptor<T> Params(IDictionary<string, object> paramsDictionary) => Assign(a => a.Params = paramsDictionary);

/// <summary>
/// The parameters for the query. the suggestion value will be added to the variables you specify.
/// </summary>
public PhraseSuggestCollateDescriptor<T> Params(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> paramsDictionary) =>
Assign(a => a.Params = paramsDictionary(new FluentDictionary<string, object>()));
}
}
24 changes: 12 additions & 12 deletions src/Tests/Search/Suggesters/SuggestApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ protected override LazyResponses ClientUsage() => Calls(
.Confidence(1)
.Collate(c => c
.Query(q => q
.Inline("{ \"match_all\": { }}")
.Params(p => p.Add("field_name", _phraseSuggestField))
.Inline("{ \"match\": { \"{{field_name}}\" : \"{{suggestion}}\" }}")
)
.Params(p => p.Add("field_name", _phraseSuggestField))
.Prune()
)
.DirectGenerator(d => d
Expand Down Expand Up @@ -170,11 +170,11 @@ protected override LazyResponses ClientUsage() => Calls(
{
Query = new TemplateQuery
{
Inline = "{ \"match_all\": { }}",
Params = new Dictionary<string, object>
{
{"field_name", _phraseSuggestField}
}
Inline = "{ \"match\": { \"{{field_name}}\" : \"{{suggestion}}\" }}",
},
Params = new Dictionary<string, object>
{
{"field_name", _phraseSuggestField}
},
Prune = true
},
Expand Down Expand Up @@ -322,11 +322,11 @@ private static void AssertCompletionSuggestResponse(ISuggestResponse<Project> re
{
query = new
{
inline = "{ \"match_all\": { }}",
@params = new
{
field_name = _phraseSuggestField
}
inline = "{ \"match\": { \"{{field_name}}\" : \"{{suggestion}}\" }}",
},
@params = new
{
field_name = _phraseSuggestField
},
prune = true,
},
Expand Down