-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script_score query support (#3919)
This commit adds script_score query support to the high level client Closes #3843 (cherry picked from commit a474194)
- Loading branch information
Showing
13 changed files
with
381 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
docs/query-dsl/specialized/script-score/script-score-query-usage.asciidoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
:ref_current: https://www.elastic.co/guide/en/elasticsearch/reference/7.0 | ||
|
||
:github: https://github.com/elastic/elasticsearch-net | ||
|
||
:nuget: https://www.nuget.org/packages | ||
|
||
//// | ||
IMPORTANT NOTE | ||
============== | ||
This file has been generated from https://github.com/elastic/elasticsearch-net/tree/master/src/Tests/Tests/QueryDsl/Specialized/ScriptScore/ScriptScoreQueryUsageTests.cs. | ||
If you wish to submit a PR for any spelling mistakes, typos or grammatical errors for this file, | ||
please modify the original csharp file found at the link and submit the PR with that change. Thanks! | ||
//// | ||
|
||
[[script-score-query-usage]] | ||
=== Script Score Query Usage | ||
|
||
A query allowing you to modify the score of documents that are retrieved by a query. | ||
This can be useful if, for example, a score function is computationally expensive and | ||
it is sufficient to compute the score on a filtered set of documents. | ||
|
||
See the Elasticsearch documentation on {ref_current}/query-dsl-script-score-query.html[script_score query] for more details. | ||
|
||
==== Fluent DSL example | ||
|
||
[source,csharp] | ||
---- | ||
q | ||
.ScriptScore(sn => sn | ||
.Name("named_query") | ||
.Boost(1.1) | ||
.Query(qq => qq | ||
.Range(r => r | ||
.Field(f => f.NumberOfCommits) | ||
.GreaterThan(50) | ||
) | ||
) | ||
.Script(s => s | ||
.Source(_scriptScoreSource) | ||
.Params(p => p | ||
.Add("origin", 100) | ||
.Add("scale", 10) | ||
.Add("decay", 0.5) | ||
.Add("offset", 0) | ||
) | ||
) | ||
) | ||
---- | ||
|
||
==== Object Initializer syntax example | ||
|
||
[source,csharp] | ||
---- | ||
new ScriptScoreQuery | ||
{ | ||
Name = "named_query", | ||
Boost = 1.1, | ||
Query = new NumericRangeQuery | ||
{ | ||
Field = Infer.Field<Project>(f => f.NumberOfCommits), | ||
GreaterThan = 50 | ||
}, | ||
Script = new InlineScript(_scriptScoreSource) | ||
{ | ||
Params = new Dictionary<string, object> | ||
{ | ||
{ "origin", 100 }, | ||
{ "scale", 10 }, | ||
{ "decay", 0.5 }, | ||
{ "offset", 0 } | ||
} | ||
}, | ||
} | ||
---- | ||
|
||
[source,javascript] | ||
.Example json output | ||
---- | ||
{ | ||
"script_score": { | ||
"_name": "named_query", | ||
"boost": 1.1, | ||
"query": { | ||
"range": { | ||
"numberOfCommits": { | ||
"gt": 50.0 | ||
} | ||
} | ||
}, | ||
"script": { | ||
"source": "decayNumericLinear(params.origin, params.scale, params.offset, params.decay, doc['numberOfCommits'].value)", | ||
"params": { | ||
"origin": 100, | ||
"scale": 10, | ||
"decay": 0.5, | ||
"offset": 0 | ||
} | ||
} | ||
} | ||
} | ||
---- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/Nest/QueryDsl/Specialized/ScriptScore/ScriptScoreQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System; | ||
using System.Runtime.Serialization; | ||
using Elasticsearch.Net.Utf8Json; | ||
|
||
namespace Nest | ||
{ | ||
/// <summary> | ||
/// A query allowing you to modify the score of documents that are retrieved by a query. | ||
/// This can be useful if, for example, a score function is computationally expensive and it is sufficient to | ||
/// compute the score on a filtered set of documents. | ||
/// </summary> | ||
[ReadAs(typeof(ScriptScoreQuery))] | ||
[InterfaceDataContract] | ||
public interface IScriptScoreQuery : IQuery | ||
{ | ||
/// <summary> | ||
/// The query to execute | ||
/// </summary> | ||
[DataMember(Name = "query")] | ||
QueryContainer Query { get; set; } | ||
|
||
/// <summary> | ||
/// The script to execute | ||
/// </summary> | ||
[DataMember(Name = "script")] | ||
IScript Script { get; set; } | ||
} | ||
|
||
/// <inheritdoc cref="IScriptScoreQuery" /> | ||
public class ScriptScoreQuery : QueryBase, IScriptScoreQuery | ||
{ | ||
/// <inheritdoc /> | ||
public QueryContainer Query { get; set; } | ||
|
||
/// <inheritdoc /> | ||
public IScript Script { get; set; } | ||
|
||
protected override bool Conditionless => IsConditionless(this); | ||
|
||
internal override void InternalWrapInContainer(IQueryContainer c) => c.ScriptScore = this; | ||
|
||
internal static bool IsConditionless(IScriptScoreQuery q) | ||
{ | ||
if (q.Script == null || q.Query.IsConditionless()) | ||
return true; | ||
|
||
switch (q.Script) | ||
{ | ||
case IInlineScript inlineScript: | ||
return inlineScript.Source.IsNullOrEmpty(); | ||
case IIndexedScript indexedScript: | ||
return indexedScript.Id.IsNullOrEmpty(); | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
/// <inheritdoc cref="IScriptScoreQuery" /> | ||
public class ScriptScoreQueryDescriptor<T> | ||
: QueryDescriptorBase<ScriptScoreQueryDescriptor<T>, IScriptScoreQuery> | ||
, IScriptScoreQuery where T : class | ||
{ | ||
protected override bool Conditionless => ScriptScoreQuery.IsConditionless(this); | ||
QueryContainer IScriptScoreQuery.Query { get; set; } | ||
|
||
IScript IScriptScoreQuery.Script { get; set; } | ||
|
||
/// <inheritdoc cref="IScriptScoreQuery.Query" /> | ||
public ScriptScoreQueryDescriptor<T> Query(Func<QueryContainerDescriptor<T>, QueryContainer> selector) => | ||
Assign(selector, (a, v) => a.Query = v?.Invoke(new QueryContainerDescriptor<T>())); | ||
|
||
/// <inheritdoc cref="IScriptScoreQuery.Script" /> | ||
public ScriptScoreQueryDescriptor<T> Script(Func<ScriptDescriptor, IScript> selector) => | ||
Assign(selector, (a, v) => a.Script = v?.Invoke(new ScriptDescriptor())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.