Skip to content

Commit 9c6e5c9

Browse files
Update README.md (#33)
Signed-off-by: Yury Fridlyand <[email protected]> Signed-off-by: MaxKsyunz <[email protected]> Co-authored-by: Yury Fridlyand <[email protected]>
1 parent 9f3cf81 commit 9c6e5c9

File tree

1 file changed

+111
-5
lines changed

1 file changed

+111
-5
lines changed

README.md

Lines changed: 111 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,40 +43,146 @@ Include OSC in your .csproj file.
4343
<Project>
4444
...
4545
<ItemGroup>
46-
<ProjectReference Include="..\opensearch-net\src\Osc\Osc.csproj" />
46+
<PackageReference Include="Osc" Version="1.0.0" />
4747
</ItemGroup>
4848
</Project>
4949
```
5050

51+
### Connecting
52+
53+
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+
5155
**Connecting to a single node**
5256
```csharp
5357
var node = new Uri("http://myserver:9200");
54-
var settings = new ConnectionSettings(node);
58+
var config = new ConnectionConfiguration(node);
59+
var client = new OpenSearchClient(config);
60+
```
61+
62+
**Connecting to multiple nodes using a connection pool**
63+
```csharp
64+
var nodes = new Uri[]
65+
{
66+
new Uri("http://myserver1:9200"),
67+
new Uri("http://myserver2:9200"),
68+
new Uri("http://myserver3:9200")
69+
};
70+
71+
var pool = new StaticNodePool(nodes);
72+
var settings = new ConnectionSettings(pool);
5573
var client = new OpenSearchClient(settings);
5674
```
5775

76+
### Indexing
77+
78+
Indexing a document is as simple as:
79+
80+
```csharp
81+
var tweet = new Tweet
82+
{
83+
Id = 1,
84+
User = "kimchy",
85+
PostDate = new DateTime(2009, 11, 15),
86+
Message = "Trying out OSC, so far so good?"
87+
};
88+
89+
var indexingResponse = 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+
var indexingResponseTask = client.IndexAsync(tweet, idx => idx.Index("mytweetindex")); // returns a Task<IndexResponse>
96+
97+
// Or, in an async-context
98+
var indexingResponse = await client.IndexAsync(tweet, idx => idx.Index("mytweetindex")); // awaits a Task<IndexResponse>
99+
```
100+
101+
### Getting a document
102+
103+
```csharp
104+
var getResponse = client.Get<Tweet>(indexingResponse.Id, idx => idx.Index("mytweetindex")); // returns an IGetResponse mapped 1-to-1 with the OpenSearch JSON response
105+
var tweet = 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+
var searchResponse = client.Search<Tweet>(s => s
114+
.Index("mytweetindex") //or specify index via settings.DefaultIndex("mytweetindex");
115+
.From(0)
116+
.Size(10)
117+
.Query(q => q
118+
.Term(t => t.User, "kimchy") || q
119+
.Match(mq => mq.Field(f => f.User).Query("osc"))
120+
)
121+
);
122+
```
123+
124+
As well as an object initializer syntax:
125+
126+
```csharp
127+
var request = new SearchRequest
128+
{
129+
From = 0,
130+
Size = 10,
131+
Query = new TermQuery { Field = "user", Value = "kimchy" } ||
132+
new MatchQuery { Field = "description", Query = "OSC" }
133+
};
134+
135+
var searchResponse = client.Search<Tweet>(request);
136+
```
137+
### Falling back to OpenSearch.Net
138+
139+
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:
140+
141+
```csharp
142+
IOpenSearchLowLevelClient lowLevelClient = client.LowLevel;
143+
// Generic parameter of Search<> is the type of .Body on response
144+
var response = lowLevelClient.Search<SearchResponse<Tweet>>("mytweetindex", PostData.Serializable(new
145+
{
146+
from = 0,
147+
size = 10,
148+
fields = new [] {"id", "name"},
149+
query = new {
150+
term = new {
151+
name = new {
152+
value= "OSC"
153+
}
154+
}
155+
}
156+
}));
157+
```
158+
58159
# [OpenSearch.Net](src/OpenSearch.Net)
59160

60-
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.
61162

62163
## Getting Started
63164
Include OpenSearch.Net in your .csproj file.
64165
```xml
65166
<Project>
66167
...
67168
<ItemGroup>
68-
<ProjectReference Include="..\opensearch-net\src\OpenSearch.Net\OpenSearch.Net.csproj" />
169+
<PackageReference Include="OpenSearch.Net" Version="1.0.0" />
69170
</ItemGroup>
70171
</Project>
71172
```
72173

73-
**Connecting**
174+
### Connecting
175+
176+
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+
74178
```csharp
75179
var node = new Uri("http://myserver:9200");
76180
var config = new ConnectionConfiguration(node);
77181
var client = new OpenSearchLowLevelClient(config);
78182
```
79183

184+
Note the main difference here is that we are instantiating an `OpenSearchLowLevelClient` rather than `OpenSearchClient`, and `ConnectionConfiguration` instead of `ConnectionSettings`.
185+
80186
## Code of Conduct
81187

82188
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

Comments
 (0)