You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can connect to your OpenSearch cluster via a single node, or by specifying multiple nodes using a node pool. Using a node pool has a few advantages over a single node, such as load balancing and cluster failover support.
54
+
51
55
**Connecting to a single node**
52
56
```csharp
53
57
varnode=newUri("http://myserver:9200");
54
-
varsettings=newConnectionSettings(node);
58
+
varconfig=newConnectionConfiguration(node);
59
+
varclient=newOpenSearchClient(config);
60
+
```
61
+
62
+
**Connecting to multiple nodes using a connection pool**
63
+
```csharp
64
+
varnodes=newUri[]
65
+
{
66
+
newUri("http://myserver1:9200"),
67
+
newUri("http://myserver2:9200"),
68
+
newUri("http://myserver3:9200")
69
+
};
70
+
71
+
varpool=newStaticNodePool(nodes);
72
+
varsettings=newConnectionSettings(pool);
55
73
varclient=newOpenSearchClient(settings);
56
74
```
57
75
76
+
### Indexing
77
+
78
+
Indexing a document is as simple as:
79
+
80
+
```csharp
81
+
vartweet=newTweet
82
+
{
83
+
Id=1,
84
+
User="kimchy",
85
+
PostDate=newDateTime(2009, 11, 15),
86
+
Message="Trying out OSC, so far so good?"
87
+
};
88
+
89
+
varindexingResponse=client.Index(tweet, idx=>idx.Index("mytweetindex")); //or specify index via settings.DefaultIndex("mytweetindex");
90
+
```
91
+
92
+
All the calls have async variants:
93
+
94
+
```csharp
95
+
varindexingResponseTask=client.IndexAsync(tweet, idx=>idx.Index("mytweetindex")); // returns a Task<IndexResponse>
96
+
97
+
// Or, in an async-context
98
+
varindexingResponse=awaitclient.IndexAsync(tweet, idx=>idx.Index("mytweetindex")); // awaits a Task<IndexResponse>
99
+
```
100
+
101
+
### Getting a document
102
+
103
+
```csharp
104
+
vargetResponse=client.Get<Tweet>(indexingResponse.Id, idx=>idx.Index("mytweetindex")); // returns an IGetResponse mapped 1-to-1 with the OpenSearch JSON response
105
+
vartweet=getResponse.Source; // the original document
106
+
```
107
+
108
+
### Searching for documents
109
+
110
+
OSC exposes a fluent interface and a [powerful query DSL](https://opensearch.org/docs/latest/opensearch/query-dsl/index/)
111
+
112
+
```csharp
113
+
varsearchResponse=client.Search<Tweet>(s=>s
114
+
.Index("mytweetindex") //or specify index via settings.DefaultIndex("mytweetindex");
OSC also includes and exposes the low-level [OpenSearch.Net](https://github.com/opensearch-project/opensearch-net/tree/main/src/OpenSearch.Net) client that you can fall back to in case anything is missing:
A low-level, dependency free client that has no opinions how you build and represent your requests and responses.
161
+
A low-level, dependency free client that is a simple .NET wrapper for the REST API. It allows you to build and represent your own requests and responses according to you needs.
Connecting using the low-level client is very similar to how you would connect using OSC. In fact, the connection constructs that OSC use are actually OpenSearch.Net constructs. Thus, single node connections and connection pooling still apply when using OpenSearch.Net.
177
+
74
178
```csharp
75
179
varnode=newUri("http://myserver:9200");
76
180
varconfig=newConnectionConfiguration(node);
77
181
varclient=newOpenSearchLowLevelClient(config);
78
182
```
79
183
184
+
Note the main difference here is that we are instantiating an `OpenSearchLowLevelClient` rather than `OpenSearchClient`, and `ConnectionConfiguration` instead of `ConnectionSettings`.
185
+
80
186
## Code of Conduct
81
187
82
188
This project has adopted the [Amazon Open Source Code of Conduct](CODE_OF_CONDUCT.md). For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq), or contact [[email protected]](mailto:[email protected]) with any additional questions or comments.
0 commit comments