Releases: elastic/elasticsearch-net
9.0.0-preview.1
What's Changed
1.1 Request Method/API Changes
1.1.1 Synchronous Request APIs
Synchronous request APIs are no longer marked as obsolete
. We received some feedback about this deprecation and decided to revert it.
1.1.2 Separate Type Arguments for Request/Response
It is now possible to specify separate type arguments for requests/responses when executing request methods:
var response = await client.SearchAsync<Person, JsonObject>(x => x
.Query(x => x.Term(x => x.Field(x => x.FirstName).Value("Florian")))
);
var documents = response.Documents; // IReadOnlyCollection<JsonObject>
The regular APIs with merged type arguments are still available.
1.2 Improved Fluent API
The enhanced fluent API generation is likely the most notable change in the 9.0 client.
This section describes the main syntax constructs generated based on the type of the property in the corresponding object.
1.2.1 ICollection<E>
Note: This syntax already existed in 8.x.
new SearchRequestDescriptor<Person>()
.Query(q => q
.Bool(b => b
.Must(new Query()) // Scalar: Single element.
.Must(new Query(), new Query()) // Scalar: Multiple elements (params).
.Must(m => m.MatchAll()) // Fluent: Single element.
.Must(m => m.MatchAll(), m => m.MatchNone()) // Fluent: Multiple elements (params).
)
);
1.2.2 IDictionary<K, V>
The 9.0 client introduces full fluent API support for dictionary types.
new SearchRequestDescriptor<Person>()
.Aggregations(new Dictionary<string, Aggregation>()) // Scalar.
.Aggregations(aggs => aggs // Fluent: Nested.
.Add("key", new MaxAggregation()) // Scalar: Key + Value.
.Add("key", x => x.Max()) // Fluent: Key + Value.
)
.AddAggregation("key", new MaxAggregation()) // Scalar.
.AddAggregation("key", x => x.Max()); // Fluent.
Warning
The Add{Element}
methods have different semantics compared to the standard setter methods.
Standard fluent setters set or replace a value.
In contrast, the new additive methods append new elements to the dictionary.
For dictionaries where the value type does not contain required properties that must be initialized, another syntax is generated that allows easy addition of new entries by just specifying the key:
// Dictionary<Name, Alias>()
new CreateIndexRequestDescriptor("index")
// ... all previous overloads ...
.Aliases(aliases => aliases // Fluent: Nested.
.Add("key") // Key only.
)
.Aliases("key") // Key only: Single element.
.Aliases("first", "second") // Key only: Multiple elements (params).
If the value type in the dictionary is a collection, additional params
overloads are generated:
// Dictionary<Field, ICollection<CompletionContext>>
new CompletionSuggesterDescriptor<Person>()
// ... all previous overloads ...
.AddContext("key",
new CompletionContext{ Context = new Context("first") },
new CompletionContext{ Context = new Context("second") }
)
.AddContext("key",
x => x.Context(x => x.Category("first")),
x => x.Context(x => x.Category("second"))
);
1.2.3 ICollection<KeyValuePair<K, V>>
Elasticsearch often uses ICollection<KeyValuePair<K, V>>
types for ordered dictionaries.
The 9.0 client abstracts this implementation detail by providing a fluent API that can be used exactly like the one for IDictionary<K, V>
types:
new PutMappingRequestDescriptor<Person>("index")
.DynamicTemplates(new List<KeyValuePair<string, DynamicTemplate>>()) // Scalar.
.DynamicTemplates(x => x // Fluent: Nested.
.Add("key", new DynamicTemplate()) // Scalar: Key + Value.
.Add("key", x => x.Mapping(new TextProperty())) // Fluent: Key + Value.
)
.AddDynamicTemplate("key", new DynamicTemplate()) // Scalar: Key + Value.
.AddDynamicTemplate("key", x => x.Runtime(x => x.Format("123"))); // Fluent: Key + Value.
1.2.4 Union Types
Fluent syntax is now as well available for all auto-generated union- and variant-types.
// TermsQueryField : Union<ICollection<FieldValue>, TermsLookup>
new TermsQueryDescriptor()
.Terms(x => x.Value("a", "b", "c")) // ICollection<FieldValue>
.Terms(x => x.Lookup(x => x.Index("index").Id("id"))); // TermsLookup
1.3 Improved Descriptor Design
The 9.0 release features a completely overhauled descriptor design.
Descriptors now wrap the object representation. This brings several internal quality-of-life improvements as well as noticeable benefits to end-users.
1.3.1 Wrap
Use the wrap constructor to create a new descriptor for an existing object:
var request = new SearchRequest();
// Wrap.
var descriptor = new SearchRequestDescriptor(request);
All fluent methods of the descriptor will mutate the existing request
passed to the wrap constructor.
Note
Descriptors are now implemented as struct
instead of class
, reducing allocation overhead as much as possible.
1.3.2 Unwrap / Inspect
Descriptor values can now be inspected by unwrapping the object using an implicit conversion operator:
var descriptor = new SearchRequestDescriptor();
// Unwrap.
SearchRequest request = descriptor;
Unwrapping does not allocate or copy.
1.3.3 Removal of Side Effects
In 8.x, execution of (most but not all) lambda actions passed to descriptors was deferred until the actual request was made. It was never clear to the user when, and how often an action would be executed.
In 9.0, descriptor actions are always executed immediately. This ensures no unforeseen side effects occur if the user-provided lambda action mutates external state (it is still recommended to exclusively use pure/invariant actions). Consequently, the effects of all changes performed by a descriptor method are immediately applied to the wrapped object.
1.4 Request Path Parameter Properties
In 8.x, request path parameters like Index
, Id
, etc. could only be set by calling the corresponding constructor of the request. Afterwards, there was no way to read or change the current value.
In the 9.0 client, all request path parameters are exposed as get/set
properties, allowing for easy access:
// 8.x and 9.0
var request = new SearchRequest(Indices.All);
// 9.0
var request = new SearchRequest { Indices = Indices.All };
var indices = request.Indices;
request.Indices = "my_index";
1.5 Field Name Inference
The Field
type and especially its implicit conversion operations allowed for null
return values. This led to a poor developer experience, as the null-forgiveness operator (!
) had to be used frequently without good reason.
This is no longer required in 9.0:
// 8.x
Field field = "field"!;
// 9.0
Field field = "field";
1.6 Uniform Date/Time/Duration Types
The encoding of date, time and duration values in Elasticsearch often varies depending on the context. In addition to string representations in ISO 8601 and RFC 3339 format (always UTC), also Unix timestamps (in seconds, milliseconds, nanoseconds) or simply seconds, milliseconds, nanoseconds are frequently used.
In 8.x, some date/time values are already mapped as DateTimeOffset
, but the various non-ISO/RFC representations were not.
9.0 now represents all date/time values uniformly as DateTimeOffset
and also uses the native TimeSpan
type for all durations.
Note
There are some places where the Elasticsearch custom date/time/duration types are continued to be used. This is always the case when the type has special semantics and/or offers functionality that goes beyond that of the native date/time/duration types (e.g. Duration
, DateMath
).
1.7 Improved Container Design
In 8.x, container types like Query
or Aggregation
had to be initialized using static factory methods.
// 8.x
var agg = Aggregation.Max(new MaxAggregation { Field = "my_field" });
This made it mandatory to assign the created container to a temporary variable if additional properties of the container (not the contained variant) needed to be set:
// 8.x
var agg = Aggregation.Max(new MaxAggregation { Field = "my_field" });
agg.Aggregations ??= new Dictionary<string, Aggregation>();
agg.Aggregations.Add("my_sub_agg", Aggregation.Terms(new TermsAggregation()));
Additionally, it was not possible to inspect the contained variant.
In 9.0, each possible container variant is represented as a regular property of the container. This allows for determining and inspecting the contained variant and initializing container properties in one go when using an object initializer:
// 9.0
var agg = new Aggregation
{
Max = new MaxAggregation { Field = "my_field" },
Aggregations = new Dictionary<string, Aggregation>
{
{ "my_sub_agg", new Aggregation{ Terms = new TermsAggregation() } }
}
};
Warning
A container can still only contain a single variant. Setting multiple variants at once is invalid.
Consecutive assignments of variant properties (e.g., first setting Max
, then Min
) will cause the previous variant to be replaced.
1.8 Sorting
Applying a sort order to a search request using the fluent API is now more convenient:
var search = new SearchRequestDes...
8.17.4
8.17.3
8.17.2
What's Changed
- Regenerate client using the latest specification by @flobernd in #8472
- Includes latest 8.17 specification bug-fixes, enhancements and features
- Fixes an issue that caused
EnableThreadPoolStats
andEnableTcpStats
to be enabled by default
New Contributors
- @colleenmcginnis made their first contribution in #8459
Full Changelog: 8.17.1...8.17.2
8.17.1
8.17.0
Serverless 1.1.19
What's Changed
- Update
Elastic.Transport
to0.5.7
by @flobernd in #8433- Fixes
PostData.MultiJson
not working correctly whenDisableDirectStreaming
is used.
- Fixes
- Regenerate client using the latest specification by @flobernd in #8440
Full Changelog: serverless-1.1.18...serverless-1.1.19
8.16.3
Serverless 1.1.18
What's Changed
Full Changelog: serverless-1.1.17...serverless-1.1.18