From ba6b2b5956fa6dfc825ec047e07fd2fa4cf0d37d Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Fri, 10 Apr 2015 13:11:14 +0200 Subject: [PATCH] Allows you to specify .ConditionlessWhen(string.IsNullOrEmpty(query)) on a function score query to prevent functions to be applied in case of an empty query or fixed references inside the functions --- src/Nest/DSL/Query/FunctionScoreQueryDescriptor.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Nest/DSL/Query/FunctionScoreQueryDescriptor.cs b/src/Nest/DSL/Query/FunctionScoreQueryDescriptor.cs index d47bce0affd..5e2171935fb 100644 --- a/src/Nest/DSL/Query/FunctionScoreQueryDescriptor.cs +++ b/src/Nest/DSL/Query/FunctionScoreQueryDescriptor.cs @@ -98,17 +98,26 @@ public class FunctionScoreQueryDescriptor : IFunctionScoreQuery where T : cla string IQuery.Name { get; set; } + private bool _forcedConditionless = false; + bool IQuery.IsConditionless { get { - return (Self.Query == null || Self.Query.IsConditionless) + return _forcedConditionless || ((Self.Query == null || Self.Query.IsConditionless) && Self.RandomScore == null && Self.ScriptScore == null - && !Self.Functions.HasAny(); + && !Self.Functions.HasAny()); } } + public FunctionScoreQueryDescriptor ConditionlessWhen(bool isConditionless) + { + this._forcedConditionless = isConditionless; + return this; + } + + public FunctionScoreQueryDescriptor Name(string name) { Self.Name = name;