Skip to content

Implement _source parameter on action and doc lines. #4339

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 9 commits into from
Feb 10, 2020
50 changes: 50 additions & 0 deletions examples/Docs/BulkPage/8cd00a3aba7c3c158277bc032aac2830.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
////
IMPORTANT NOTE
==============
This file is generated from method Line405 in https://github.com/elastic/elasticsearch-net/tree/master/src/Examples/Examples/Docs/BulkPage.cs#L47-L113.
If you wish to submit a PR to change this example, please change the source method above
and run dotnet run -- asciidoc in the ExamplesGenerator project directory.
////
[source, csharp]
----
var bulkResponse = client.Bulk(b => b
.Update<object>(u => u
.Index("index1")
.Id("1")
.RetriesOnConflict(3)
.Doc(new { field = "value" })
)
.Update<object>(u => u
.Index("index1")
.Id("0")
.RetriesOnConflict(3)
.Script(s => s
.Source("ctx._source.counter += params.param1")
.Lang("painless")
.Params(d => d
.Add("param1", 1)
)
)
.Upsert(new { counter = 1 })
)
.Update<object>(u => u
.Index("index1")
.Id("2")
.RetriesOnConflict(3)
.Doc(new { field = "value" })
.DocAsUpsert(true)
)
.Update<object>(u => u
.Index("index1")
.Id("3")
.Source(true)
.Doc(new { field = "value" })
)
.Update<object>(u => u
.Index("index1")
.Id("4")
.Source(true)
.Doc(new { field = "value" })
)
);
----
25 changes: 23 additions & 2 deletions src/Nest/Document/Multiple/Bulk/BulkOperation/BulkUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public interface IBulkUpdateOperation<TDocument, TPartialDocument> : IBulkOperat
long? IfSequenceNumber { get; set; }

long? IfPrimaryTerm { get; set; }

/// <summary>
/// True or false to return the _source field or not, or a list of fields to return.
/// </summary>
[DataMember(Name = "_source")]
Union<bool, ISourceFilter> Source { get; set; }
}

[DataContract]
Expand Down Expand Up @@ -120,6 +126,11 @@ public BulkUpdateOperation(TDocument idFrom, TPartialDocument update, bool useId

public long? IfPrimaryTerm { get; set; }

/// <summary>
/// True or false to return the _source field or not, or a list of fields to return.
/// </summary>
public Union<bool, ISourceFilter> Source { get; set; }

protected override Type ClrType => typeof(TDocument);

protected override string Operation => "update";
Expand Down Expand Up @@ -150,7 +161,8 @@ protected override object GetBody() =>
DocAsUpsert = DocAsUpsert,
ScriptedUpsert = ScriptedUpsert,
IfPrimaryTerm = IfPrimaryTerm,
IfSequenceNumber = IfSequenceNumber
IfSequenceNumber = IfSequenceNumber,
Source = Source
};
}

Expand All @@ -175,6 +187,8 @@ public class BulkUpdateDescriptor<TDocument, TPartialDocument>

long? IBulkUpdateOperation<TDocument, TPartialDocument>.IfPrimaryTerm { get; set; }

Union<bool, ISourceFilter> IBulkUpdateOperation<TDocument, TPartialDocument>.Source { get; set; }

protected override object GetBulkOperationBody() =>
new BulkUpdateBody<TDocument, TPartialDocument>
{
Expand All @@ -184,7 +198,8 @@ protected override object GetBulkOperationBody() =>
DocAsUpsert = Self.DocAsUpsert,
ScriptedUpsert = Self.ScriptedUpsert,
IfPrimaryTerm = Self.IfPrimaryTerm,
IfSequenceNumber = Self.IfSequenceNumber
IfSequenceNumber = Self.IfSequenceNumber,
Source = Self.Source
};

protected override Id GetIdForOperation(Inferrer inferrer) =>
Expand Down Expand Up @@ -264,5 +279,11 @@ public BulkUpdateDescriptor<TDocument, TPartialDocument> IfSequenceNumber(long?
/// </summary>
public BulkUpdateDescriptor<TDocument, TPartialDocument> IfPrimaryTerm(long? primaryTerm) =>
Assign(primaryTerm, (a, v) => a.IfPrimaryTerm = v);

/// <summary>
/// True or false to return the _source field or not, or a list of fields to return.
/// </summary>
public BulkUpdateDescriptor<TDocument, TPartialDocument> Source(Union<bool, ISourceFilter> source) =>
Assign(source, (a, v) => a.Source = v);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@ internal class BulkUpdateBody<TDocument, TPartialUpdate>

[DataMember(Name = "if_primary_term")]
internal long? IfPrimaryTerm { get; set; }

[DataMember(Name = "_source")]
internal Union<bool, ISourceFilter> Source { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ public abstract class BulkResponseItemBase
[DataMember(Name = "_primary_term")]
public long PrimaryTerm { get; internal set; }

[DataMember(Name = "get")]
internal LazyDocument Get { get; set; }

/// <summary>
/// Deserialize the <see cref="Get"/> property as a GetResponse<TDocument> type, where TDocument is the document type.
/// </summary>
public GetResponse<TDocument> GetResponse<TDocument>() where TDocument : class => Get?.AsUsingRequestResponseSerializer<GetResponse<TDocument>>();

/// <summary> The result of the bulk operation</summary>
[DataMember(Name = "result")]
public string Result { get; internal set; }
Expand Down
21 changes: 15 additions & 6 deletions tests/Examples/Docs/BulkPage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Elastic.Xunit.XunitPlumbing;
using Elasticsearch.Net;
using Nest;
using Newtonsoft.Json.Linq;

namespace Examples.Docs
{
Expand Down Expand Up @@ -42,7 +44,7 @@ public void Line11()
{ ""doc"" : {""field2"" : ""value2""} }");
}

[U(Skip = "Example not implemented")]
[U]
public void Line405()
{
// tag::8cd00a3aba7c3c158277bc032aac2830[]
Expand Down Expand Up @@ -76,15 +78,13 @@ public void Line405()
.Update<object>(u => u
.Index("index1")
.Id("3")
// TODO: missing
//.Source(true)
.Source(true)
.Doc(new { field = "value" })
)
.Update<object>(u => u
.Index("index1")
.Id("4")
// TODO: missing
//.Source(true)
.Source(true)
.Doc(new { field = "value" })
)
);
Expand All @@ -100,7 +100,16 @@ public void Line405()
{ ""update"" : {""_id"" : ""3"", ""_index"" : ""index1"", ""_source"" : true} }
{ ""doc"" : {""field"" : ""value""} }
{ ""update"" : {""_id"" : ""4"", ""_index"" : ""index1""} }
{ ""doc"" : {""field"" : ""value""}, ""_source"": true}");
{ ""doc"" : {""field"" : ""value""}, ""_source"": true}",
e =>
{
e.ApplyBulkBodyChanges(objects =>
{
objects[7].Add("_source", true);
(objects[8]["update"] as JObject).Add("_source", true);
});
return e;
});
}
}
}
11 changes: 11 additions & 0 deletions tests/Examples/Example.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Elasticsearch.Net;
using Newtonsoft.Json.Linq;
Expand All @@ -24,6 +26,15 @@ private Example(HttpMethod method, Uri uri, string body)

public UriBuilder Uri { get; set; }

public void ApplyBulkBodyChanges(Action<List<JObject>> action)
{
var body = Body == null
? new List<JObject>()
: ResponseExtensions.ParseJObjects(Body);
action(body);
Body = string.Join(Environment.NewLine, body.Select(b => b.ToString()).ToArray());
}

public void ApplyBodyChanges(Action<JObject> action)
{
var body = Body == null
Expand Down
2 changes: 1 addition & 1 deletion tests/Examples/ResponseExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static void MatchesExample(this IResponse response, string content, Func<
/// Parses a collection of JObjects from the JSON input. Provides support
/// for both regular JSON and newline delimited JSON
/// </summary>
private static List<JObject> ParseJObjects(string json)
public static List<JObject> ParseJObjects(string json)
{
var jObjects = new List<JObject>();
using (var stringReader = new StringReader(json))
Expand Down
70 changes: 70 additions & 0 deletions tests/Tests/Document/Multiple/Bulk/BulkSourceDeserialize.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Text;
using Elastic.Xunit.XunitPlumbing;
using Elasticsearch.Net;
using FluentAssertions;
using Nest;
using Tests.Core.Extensions;

namespace Tests.Document.Multiple.Bulk
{
public class BulkSourceDeserialize
{
[U]
public void CanDeserialize()
{
var json = @"{
""took"": 61,
""errors"": false,
""items"": [{
""update"": {
""_index"": ""test"",
""_type"": ""_doc"",
""_id"": ""1"",
""_version"": 2,
""result"": ""updated"",
""_shards"": {
""total"": 2,
""successful"": 1,
""failed"": 0
},
""_seq_no"": 3,
""_primary_term"": 1,
""get"": {
""_seq_no"": 3,
""_primary_term"": 1,
""found"": true,
""_source"": {
""field1"": ""value1"",
""field2"": ""value2""
}
},
""status"": 200
}
}]
}";

var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

var connection = new InMemoryConnection(Encoding.UTF8.GetBytes(json));
var settings = new ConnectionSettings(pool, connection);
var client = new ElasticClient(settings);

var bulkResponse = client.Bulk(r => r);

bulkResponse.ShouldBeValid();

var simpleObject = bulkResponse.Items[0].GetResponse<SimpleObject>();

simpleObject.Found.Should().BeTrue();
simpleObject.Source.field1.Should().Be("value1");
simpleObject.Source.field2.Should().Be("value2");
}

private class SimpleObject
{
public string field1 { get; set; }
public string field2 { get; set; }
}
}
}