- .NET Clients: other versions:
- Introduction
- Breaking changes
- API Conventions
- Elasticsearch.Net - Low level client
- NEST - High level client
- Troubleshooting
- Search
- Query DSL
- Full text queries
- Term level queries
- Exists Query Usage
- Fuzzy Date Query Usage
- Fuzzy Numeric Query Usage
- Fuzzy Query Usage
- Ids Query Usage
- Prefix Query Usage
- Date Range Query Usage
- Numeric Range Query Usage
- Term Range Query Usage
- Regexp Query Usage
- Term Query Usage
- Terms List Query Usage
- Terms Lookup Query Usage
- Terms Query Usage
- Type Query Usage
- Wildcard Query Usage
- Compound queries
- Joining queries
- Geo queries
- Geo Bounding Box Query Usage
- Geo Distance Query Usage
- Geo Distance Range Query Usage
- Geo Hash Cell Query Usage
- Geo Polygon Query Usage
- Geo Shape Circle Query Usage
- Geo Shape Envelope Query Usage
- Geo Shape Geometry Collection Query Usage
- Geo Shape Indexed Shape Query Usage
- Geo Shape Line String Query Usage
- Geo Shape Multi Line String Query Usage
- Geo Shape Multi Point Query Usage
- Geo Shape Multi Polygon Query Usage
- Geo Shape Point Query Usage
- Geo Shape Polygon Query Usage
- Specialized queries
- Span queries
- NEST specific queries
- Aggregations
- Metric Aggregations
- Average Aggregation Usage
- Cardinality Aggregation Usage
- Extended Stats Aggregation Usage
- Geo Bounds Aggregation Usage
- Geo Centroid Aggregation Usage
- Max Aggregation Usage
- Min Aggregation Usage
- Percentile Ranks Aggregation Usage
- Percentiles Aggregation Usage
- Scripted Metric Aggregation Usage
- Stats Aggregation Usage
- Sum Aggregation Usage
- Top Hits Aggregation Usage
- Value Count Aggregation Usage
- Bucket Aggregations
- Adjacency Matrix Usage
- Children Aggregation Usage
- Date Histogram Aggregation Usage
- Date Range Aggregation Usage
- Filter Aggregation Usage
- Filters Aggregation Usage
- Geo Distance Aggregation Usage
- Geo Hash Grid Aggregation Usage
- Global Aggregation Usage
- Histogram Aggregation Usage
- Ip Range Aggregation Usage
- Missing Aggregation Usage
- Nested Aggregation Usage
- Range Aggregation Usage
- Reverse Nested Aggregation Usage
- Sampler Aggregation Usage
- Significant Terms Aggregation Usage
- Terms Aggregation Usage
- Pipeline Aggregations
- Average Bucket Aggregation Usage
- Bucket Script Aggregation Usage
- Bucket Selector Aggregation Usage
- Cumulative Sum Aggregation Usage
- Derivative Aggregation Usage
- Extended Stats Bucket Aggregation Usage
- Max Bucket Aggregation Usage
- Min Bucket Aggregation Usage
- Moving Average Ewma Aggregation Usage
- Moving Average Holt Linear Aggregation Usage
- Moving Average Holt Winters Aggregation Usage
- Moving Average Linear Aggregation Usage
- Moving Average Simple Aggregation Usage
- Percentiles Bucket Aggregation Usage
- Serial Differencing Aggregation Usage
- Stats Bucket Aggregation Usage
- Sum Bucket Aggregation Usage
- Matrix Aggregations
- Metric Aggregations
WARNING: Version 5.x has passed its EOL date.
This documentation is no longer being maintained and may be removed. If you are running this version, we strongly advise you to upgrade. For the latest information, see the current release documentation.
Post data
editPost data
editThe low level client allows you to post a string
or byte[]
array directly. On top of this,
if you pass a collection of string
or object
they will be serialized
using Elasticsearch’s special bulk/multi format.
Implicit Conversion
editEven though the argument for PostData on the low level client takes a PostData<object>
,
You can rely on implicit conversion to abstract the notion of PostData completely.
You can implicitly convert from the following types
-
A
string
-
A collection of
string
-
An
object
-
A collection of
object
-
A
byte[]
array
Let’s demonstrate each with some assertive examples
PostData<object> fromString = @string; PostData<object> fromByteArray = bytes; PostData<object> fromListOfString = collectionOfStrings; PostData<object> fromListOfObject = collectionOfObjects; PostData<object> fromObject = @object; fromByteArray.WrittenBytes.Should().BeSameAs(bytes);
The Type
property is representative of the original type from which post data is constructed
fromString.Type.Should().Be(PostType.LiteralString); fromByteArray.Type.Should().Be(PostType.ByteArray); fromListOfString.Type.Should().Be(PostType.EnumerableOfString); fromListOfObject.Type.Should().Be(PostType.EnumerableOfObject); fromObject.Type.Should().Be(PostType.Serializable);
and passing a PostData<object>
instance to a method that accepts PostData<object>
as an argument does not wrap it again
fromString = MethodThatAcceptsPostData(fromString); fromByteArray = MethodThatAcceptsPostData(fromByteArray); fromListOfString = MethodThatAcceptsPostData(fromListOfString); fromListOfObject = MethodThatAcceptsPostData(fromListOfObject); fromObject = MethodThatAcceptsPostData(fromObject); fromString.Type.Should().Be(PostType.LiteralString); fromByteArray.Type.Should().Be(PostType.ByteArray); fromListOfString.Type.Should().Be(PostType.EnumerableOfString); fromListOfObject.Type.Should().Be(PostType.EnumerableOfObject); fromObject.Type.Should().Be(PostType.Serializable);
Each of the implicitly converted types behaves slightly differently.
For string
, the UTF-8 bytes are sent in the request and the WrittenBytes
property is assigned
the bytes
await Post(() => @string, writes: Utf8Bytes(@string), writtenBytesIsSet: true, settings: settings);
Similarly, for byte[]
, the bytes are sent verbatim and the WrittenBytes
property is assigned
the bytes
await Post(() => bytes, writes: bytes, writtenBytesIsSet: true, settings: settings);
When passing a collection of string
, the client assumes that it’s a collection of valid serialized json,
so joins each with newline feeds, ensuring there is a trailing linefeed. As with string
and byte[]
,
the WrittenBytes
property is assigned the UTF-8 bytes of the collection of strings
await Post(() => collectionOfStrings, writes: utf8BytesOfListOfStrings, writtenBytesIsSet: true, settings: settings);
When passing a collection of object
, the client assumes that it’s a collection of objects
that needs to be serialized individually to json and joined with newline feeds. As with the collection of strings, the client ensures that
there is a trailing linefeed.
await Post(() => collectionOfObjects, writes: utf8BytesOfCollectionOfObjects, writtenBytesIsSet: false, settings: settings);
In all other cases, Post data is serialized as is and WrittenBytes
is not assigned
await Post(() => @object, writes: utf8ObjectBytes, writtenBytesIsSet: false, settings: settings);
Forcing WrittenBytes to be set
editIf you want to maintain a copy of the request that went out, you can set DisableDirectStreaming
on ConnectionConfiguration
.
In doing so, the serialized bytes are first written to a private MemoryStream
so that the client can get hold of the serialized bytes
settings = new ConnectionConfiguration().DisableDirectStreaming(); await Post(() => collectionOfObjects, writes: utf8BytesOfCollectionOfObjects, writtenBytesIsSet: true, settings: settings);
This behavior can also be observed when serializing a simple object using DisableDirectStreaming
enabled
await Post(() => @object, writes: utf8ObjectBytes, writtenBytesIsSet: true, settings: settings);