Skip to content

Implement adjust_offsets on word delimiter graph token filter #3934

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 8 commits into from
Jul 15, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -1712,13 +1712,6 @@ public bool? TotalHitsAsInteger
set => Q("rest_total_hits_as_int", value);
}

///<summary>Indicate if the number of documents that match the query should be tracked</summary>
public bool? TrackTotalHits
{
get => Q<bool? >("track_total_hits");
set => Q("track_total_hits", value);
}

///<summary>Specify whether aggregation and suggester names should be prefixed by their respective types in the response</summary>
public bool? TypedKeys
{
Expand Down
32 changes: 17 additions & 15 deletions src/Elasticsearch.Net/Connection/Content/RequestDataContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ namespace Elasticsearch.Net
internal class RequestDataContent : HttpContent
{
private readonly RequestData _requestData;
private readonly Func<PostData, CompleteTaskOnCloseStream, RequestDataContent, TransportContext, Task> _onStreamAvailable;

private readonly Func<RequestData, CompleteTaskOnCloseStream, RequestDataContent, TransportContext, Task> _onStreamAvailable;

public RequestDataContent(RequestData requestData)
{
Expand All @@ -35,12 +34,17 @@ public RequestDataContent(RequestData requestData)
if (requestData.HttpCompression)
Headers.ContentEncoding.Add("gzip");

Task OnStreamAvailable(PostData data, Stream stream, HttpContent content, TransportContext context)
Task OnStreamAvailable(RequestData data, Stream stream, HttpContent content, TransportContext context)
{
if (data.HttpCompression)
stream = new GZipStream(stream, CompressionMode.Compress, false);

using(stream)
data.Write(stream, requestData.ConnectionSettings);
data.PostData.Write(stream, data.ConnectionSettings);

return Task.CompletedTask;
}

_onStreamAvailable = OnStreamAvailable;
}
public RequestDataContent(RequestData requestData, CancellationToken token)
Expand All @@ -50,11 +54,15 @@ public RequestDataContent(RequestData requestData, CancellationToken token)
if (requestData.HttpCompression)
Headers.ContentEncoding.Add("gzip");

async Task OnStreamAvailable(PostData data, Stream stream, HttpContent content, TransportContext context)
async Task OnStreamAvailable(RequestData data, Stream stream, HttpContent content, TransportContext context)
{
if (data.HttpCompression)
stream = new GZipStream(stream, CompressionMode.Compress, false);

using (stream)
await data.WriteAsync(stream, requestData.ConnectionSettings, token).ConfigureAwait(false);
await data.PostData.WriteAsync(stream, data.ConnectionSettings, token).ConfigureAwait(false);
}

_onStreamAvailable = OnStreamAvailable;
}

Expand All @@ -69,16 +77,9 @@ async Task OnStreamAvailable(PostData data, Stream stream, HttpContent content,
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exception is passed as task result.")]
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
{

var data = _requestData.PostData;
if (data == null) return;

var serializeToStreamTask = new TaskCompletionSource<bool>();

if (_requestData.HttpCompression)
stream = new GZipStream(stream, CompressionMode.Compress, false);
var wrappedStream = new CompleteTaskOnCloseStream(stream, serializeToStreamTask);
await _onStreamAvailable(data, wrappedStream, this, context).ConfigureAwait(false);
await _onStreamAvailable(_requestData, wrappedStream, this, context).ConfigureAwait(false);
await serializeToStreamTask.Task.ConfigureAwait(false);
}

Expand Down Expand Up @@ -111,7 +112,6 @@ protected override void Dispose(bool disposing)
base.Dispose();
}


public override void Close() => _serializeToStreamTask.TrySetResult(true);
}

Expand Down Expand Up @@ -193,6 +193,8 @@ public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, As
public override void EndWrite(IAsyncResult asyncResult) => _innerStream.EndWrite(asyncResult);

public override void WriteByte(byte value) => _innerStream.WriteByte(value);

public override void Close() => _innerStream.Close();
}
}
}
Expand Down
Loading