Skip to content
30 changes: 22 additions & 8 deletions docs/common-options/date-math/date-math-expressions.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,28 @@ anchor will be an actual `DateTime`, even after a serialization/deserialization
[source,csharp]
----
var date = new DateTime(2015, 05, 05);
Expect("2015-05-05T00:00:00")
.WhenSerializing<Nest.DateMath>(date)
.AssertSubject(dateMath => ((IDateMath)dateMath)
.Anchor.Match(
d => d.Should().Be(date),
s => s.Should().BeNull()
)
);
----

will serialize to

[source,javascript]
----
"2015-05-05T00:00:00"
----

When the `DateTime` is local or UTC, the time zone information is included.
For example, for a UTC `DateTime`

[source,csharp]
----
var utcDate = new DateTime(2015, 05, 05, 0, 0, 0, DateTimeKind.Utc);
----

will serialize to

[source,javascript]
----
"2015-05-05T00:00:00Z"
----

==== Complex expressions
Expand Down
4 changes: 4 additions & 0 deletions docs/query-dsl.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ Specialized types of queries that do not fit into other groups

* <<script-query-usage,Script Query Usage>>

* <<script-score-query-usage,Script Score Query Usage>>

See the Elasticsearch documentation on {ref_current}/specialized-queries.html[Specialized queries] for more details.

:includes-from-dirs: query-dsl/specialized
Expand All @@ -287,6 +289,8 @@ include::query-dsl/specialized/percolate/percolate-query-usage.asciidoc[]

include::query-dsl/specialized/script/script-query-usage.asciidoc[]

include::query-dsl/specialized/script-score/script-score-query-usage.asciidoc[]

[[span-queries]]
== Span queries

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,36 @@ q
.MaxBoost(20.0)
.MinScore(1.0)
.Functions(f => f
.Exponential(b => b.Field(p => p.NumberOfCommits).Decay(0.5).Origin(1.0).Scale(0.1).Weight(2.1))
.Exponential(b => b
.Field(p => p.NumberOfCommits)
.Decay(0.5)
.Origin(1.0)
.Scale(0.1)
.Weight(2.1)
.Filter(fi => fi
.Range(r => r
.Field(p => p.NumberOfContributors)
.GreaterThan(10)
)
)
)
.GaussDate(b => b.Field(p => p.LastActivity).Origin(DateMath.Now).Decay(0.5).Scale("1d"))
.LinearGeoLocation(b =>
b.Field(p => p.LocationPoint).Origin(new GeoLocation(70, -70)).Scale(Distance.Miles(1)).MultiValueMode(MultiValueMode.Average))
.LinearGeoLocation(b => b
.Field(p => p.LocationPoint)
.Origin(new GeoLocation(70, -70))
.Scale(Distance.Miles(1))
.MultiValueMode(MultiValueMode.Average)
)
.FieldValueFactor(b => b.Field(p => p.NumberOfContributors).Factor(1.1).Missing(0.1).Modifier(FieldValueFactorModifier.Square))
.RandomScore(r => r.Seed(1337).Field("_seq_no"))
.RandomScore(r => r.Seed("randomstring").Field("_seq_no"))
.Weight(1.0)
.ScriptScore(s => s.Script(ss => ss.Source("Math.log(2 + doc['numberOfCommits'].value)")))
.ScriptScore(s => s
.Script(ss => ss
.Source("Math.log(2 + doc['numberOfCommits'].value)")
)
.Weight(2)
)
)
)
----
Expand All @@ -57,7 +78,19 @@ new FunctionScoreQuery()
MinScore = 1.0,
Functions = new List<IScoreFunction>
{
new ExponentialDecayFunction { Origin = 1.0, Decay = 0.5, Field = Field<Project>(p => p.NumberOfCommits), Scale = 0.1, Weight = 2.1 },
new ExponentialDecayFunction
{
Origin = 1.0,
Decay = 0.5,
Field = Field<Project>(p => p.NumberOfCommits),
Scale = 0.1,
Weight = 2.1,
Filter = new NumericRangeQuery
{
Field = Field<Project>(f => f.NumberOfContributors),
GreaterThan = 10
}
},
new GaussDateDecayFunction
{ Origin = DateMath.Now, Field = Field<Project>(p => p.LastActivity), Decay = 0.5, Scale = TimeSpan.FromDays(1) },
new LinearGeoDecayFunction
Expand All @@ -72,7 +105,7 @@ new FunctionScoreQuery()
new RandomScoreFunction { Seed = 1337, Field = "_seq_no" },
new RandomScoreFunction { Seed = "randomstring", Field = "_seq_no" },
new WeightFunction { Weight = 1.0 },
new ScriptScoreFunction { Script = new InlineScript("Math.log(2 + doc['numberOfCommits'].value)") }
new ScriptScoreFunction { Script = new InlineScript("Math.log(2 + doc['numberOfCommits'].value)"), Weight = 2.0 }
}
}
----
Expand All @@ -94,7 +127,14 @@ new FunctionScoreQuery()
"decay": 0.5
}
},
"weight": 2.1
"weight": 2.1,
"filter": {
"range": {
"numberOfContributors": {
"gt": 10.0
}
}
}
},
{
"gauss": {
Expand Down Expand Up @@ -145,7 +185,8 @@ new FunctionScoreQuery()
"script": {
"source": "Math.log(2 + doc['numberOfCommits'].value)"
}
}
},
"weight": 2.0
}
],
"max_boost": 20.0,
Expand Down
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
}
}
}
}
----

Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public static class CodeConfiguration
"ml.set_upgrade_mode.json",
"ml.find_file_structure.json",
"monitoring.bulk.json",
"indices.freeze.json",
"indices.unfreeze.json",

"ccr.follow_info.json",
"ccr.forget_follower.json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public class GlobalOverrides : EndpointOverridesBase
"copy_settings", //this still needs a PR?
"source", // allows the body to be specified as a request param, we do not want to advertise this with a strongly typed method
"timestamp",
"_source_include", "_source_exclude" // can be removed once https://github.com/elastic/elasticsearch/pull/41439 is in
"_source_include", "_source_exclude", // can be removed once https://github.com/elastic/elasticsearch/pull/41439 is in
"track_total_hits"
};
}
}
5 changes: 3 additions & 2 deletions src/CodeGeneration/DocGenerator/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ public static class StringExtensions
new []{ new [] {8.2, 18.2}, new [] {8.2, -18.8}, new [] {-8.8, -10.8}, new [] {8.8, 18.2}}
}"
},
{ "ProjectFilterExpectedJson", "new {term = new {type = new {value = \"project\"}}}" }
{ "ProjectFilterExpectedJson", "new {term = new {type = new {value = \"project\"}}}" },
{ "_scriptScoreSource", "\"decayNumericLinear(params.origin, params.scale, params.offset, params.decay, doc['numberOfCommits'].value)\""}
};

private static readonly Regex LeadingSpacesAndAsterisk = new Regex(@"^(?<value>[ \t]*\*\s?).*", RegexOptions.Compiled);
Expand Down Expand Up @@ -213,7 +214,7 @@ public static string RemoveNumberOfLeadingTabsOrSpacesAfterNewline(this string i

public static string[] SplitOnNewLines(this string input, StringSplitOptions options) => input.Split(new[] { "\r\n", "\n" }, options);

public static bool TryGetJsonForAnonymousType(this string anonymousTypeString, out string json)
public static bool TryGetJsonForExpressionSyntax(this string anonymousTypeString, out string json)
{
json = null;

Expand Down
7 changes: 3 additions & 4 deletions src/CodeGeneration/DocGenerator/SyntaxNodeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,10 @@ public static bool TryGetJsonForSyntaxNode(this SyntaxNode node, out string json
json = null;

// find the first anonymous object or new object expression
var creationExpressionSyntax = node.DescendantNodes()
.FirstOrDefault(n => n is AnonymousObjectCreationExpressionSyntax || n is ObjectCreationExpressionSyntax);
var syntax = node.DescendantNodes()
.FirstOrDefault(n => n is AnonymousObjectCreationExpressionSyntax || n is ObjectCreationExpressionSyntax || n is LiteralExpressionSyntax);

return creationExpressionSyntax != null &&
creationExpressionSyntax.ToFullString().TryGetJsonForAnonymousType(out json);
return syntax != null && syntax.ToFullString().TryGetJsonForExpressionSyntax(out json);
}

/// <summary>
Expand Down
Loading