Skip to content

Commit

Permalink
Merge pull request #1419 from elastic/feature/1418-adding-filter-for-…
Browse files Browse the repository at this point in the history
…function-score

Added Filter to FunctionScoreQuery
  • Loading branch information
gmarz committed May 22, 2015
2 parents dd72333 + c8606ac commit 2fec666
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/Nest/DSL/Query/FunctionScoreQueryDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public interface IFunctionScoreQuery : IQuery
[JsonConverter(typeof(CompositeJsonConverter<ReadAsTypeConverter<QueryDescriptor<object>>, CustomJsonConverter>))]
IQueryContainer Query { get; set; }

[JsonProperty(PropertyName = "filter")]
[JsonConverter(typeof(CompositeJsonConverter<ReadAsTypeConverter<FilterContainer>, CustomJsonConverter>))]
IFilterContainer Filter { get; set; }

[JsonProperty(PropertyName = "score_mode")]
[JsonConverter(typeof (StringEnumConverter))]
FunctionScoreMode? ScoreMode { get; set; }
Expand Down Expand Up @@ -57,6 +61,7 @@ protected override void WrapInContainer(IQueryContainer container)
public string Name { get; set; }
public IEnumerable<IFunctionScoreFunction> Functions { get; set; }
public IQueryContainer Query { get; set; }
public IFilterContainer Filter { get; set; }
public FunctionScoreMode? ScoreMode { get; set; }
public FunctionBoostMode? BoostMode { get; set; }
public float? MaxBoost { get; set; }
Expand All @@ -81,6 +86,8 @@ public class FunctionScoreQueryDescriptor<T> : IFunctionScoreQuery where T : cla

IQueryContainer IFunctionScoreQuery.Query { get; set; }

IFilterContainer IFunctionScoreQuery.Filter { get; set; }

FunctionScoreMode? IFunctionScoreQuery.ScoreMode { get; set; }

FunctionBoostMode? IFunctionScoreQuery.BoostMode { get; set; }
Expand Down Expand Up @@ -109,10 +116,9 @@ bool IQuery.IsConditionless
{
get
{
return _forcedConditionless || ((Self.Query == null || Self.Query.IsConditionless)
&& Self.RandomScore == null
&& Self.ScriptScore == null
&& !Self.Functions.HasAny());
return _forcedConditionless
|| ((Self.Query == null || Self.Query.IsConditionless || Self.Filter == null || Self.Filter.IsConditionless)
&& Self.RandomScore == null && Self.ScriptScore == null && !Self.Functions.HasAny());
}
}

Expand All @@ -134,10 +140,19 @@ public FunctionScoreQueryDescriptor<T> Query(Func<QueryDescriptor<T>, QueryConta
querySelector.ThrowIfNull("querySelector");
var query = new QueryDescriptor<T>();
var q = querySelector(query);
Self.Query = q.IsConditionless ? null :q;
Self.Query = q.IsConditionless ? null : q;
return this;
}

public FunctionScoreQueryDescriptor<T> Filter(Func<FilterDescriptor<T>, FilterContainer> filterSelector)
{
filterSelector.ThrowIfNull("filterSelector");
var filter = new FilterDescriptor<T>();
var f = filterSelector(filter);
Self.Filter = f.IsConditionless ? null : f;
return this;
}

public FunctionScoreQueryDescriptor<T> Functions(params Func<FunctionScoreFunctionsDescriptor<T>, FunctionScoreFunction<T>>[] functions)
{
var descriptor = new FunctionScoreFunctionsDescriptor<T>();
Expand Down

0 comments on commit 2fec666

Please sign in to comment.