|
| 1 | +[[esql]] |
| 2 | +== ES|QL in the .NET client |
| 3 | +++++ |
| 4 | +<titleabbrev>Using ES|QL</titleabbrev> |
| 5 | +++++ |
| 6 | + |
| 7 | +This page helps you understand and use {ref}/esql.html[ES|QL] in the |
| 8 | +.NET client. |
| 9 | + |
| 10 | +There are two ways to use ES|QL in the .NET client: |
| 11 | + |
| 12 | +* Use the Elasticsearch {es-docs}/esql-apis.html[ES|QL API] directly: This |
| 13 | +is the most flexible approach, but it's also the most complex because you must handle |
| 14 | +results in their raw form. You can choose the precise format of results, |
| 15 | +such as JSON, CSV, or text. |
| 16 | +* Use ES|QL high-level helpers: These helpers take care of parsing the raw |
| 17 | +response into something readily usable by the application. Several helpers are |
| 18 | +available for different use cases, such as object mapping, cursor |
| 19 | +traversal of results (in development), and dataframes (in development). |
| 20 | + |
| 21 | +[discrete] |
| 22 | +[[esql-how-to]] |
| 23 | +=== How to use the ES|QL API |
| 24 | + |
| 25 | +The {es-docs}/esql-query-api.html[ES|QL query API] allows you to specify how |
| 26 | +results should be returned. You can choose a |
| 27 | +{es-docs}/esql-rest.html#esql-rest-format[response format] such as CSV, text, or |
| 28 | +JSON, then fine-tune it with parameters like column separators |
| 29 | +and locale. |
| 30 | + |
| 31 | +The following example gets ES|QL results as CSV and parses them: |
| 32 | + |
| 33 | +[source,charp] |
| 34 | +---- |
| 35 | +var response = await client.Esql.QueryAsync(r => r |
| 36 | + .Query("FROM index") |
| 37 | + .Format("csv") |
| 38 | +); |
| 39 | +var csvContents = Encoding.UTF8.GetString(response.Data); |
| 40 | +---- |
| 41 | + |
| 42 | +[discrete] |
| 43 | +[[esql-consume-results]] |
| 44 | +=== Consume ES|QL results |
| 45 | + |
| 46 | +The previous example showed that although the raw ES|QL API offers maximum |
| 47 | +flexibility, additional work is required in order to make use of the |
| 48 | +result data. |
| 49 | + |
| 50 | +To simplify things, try working with these three main representations of ES|QL |
| 51 | +results (each with its own mapping helper): |
| 52 | + |
| 53 | +* **Objects**, where each row in the results is mapped to an object from your |
| 54 | +application domain. This is similar to what ORMs (object relational mappers) |
| 55 | +commonly do. |
| 56 | +* **Cursors**, where you scan the results row by row and access the data using |
| 57 | +column names. This is similar to database access libraries. |
| 58 | +* **Dataframes**, where results are organized in a column-oriented structure that |
| 59 | +allows efficient processing of column data. |
| 60 | + |
| 61 | +[source,charp] |
| 62 | +---- |
| 63 | +// ObjectAPI example |
| 64 | +var response = await client.Esql.QueryAsObjectsAsync<Person>(x => x.Query("FROM index")); |
| 65 | +foreach (var person in response) |
| 66 | +{ |
| 67 | + // ... |
| 68 | +} |
| 69 | +---- |
0 commit comments