opensearch-project / opensearch-net

OpenSearch .NET Client
Apache License 2.0
105 stars 49 forks source link

[FEATURE] How can I currently change the default ApiUrls ? #637

Open ianido opened 6 months ago

ianido commented 6 months ago

I have to connect to a deployed instance of opensearch with different Urls, for example a regular {indexname}/_search is {indexname}search, similar with other features, how can I change this confguration in the Client?

dblock commented 6 months ago

There's an example of customizing an URL in https://github.com/opensearch-project/opensearch-net/blob/main/USER_GUIDE.md#connecting-1, please let us know if that's not sufficient?

For faster response, a better place to ask questions is the public slack #client channel.

ianido commented 6 months ago

I do can change with the ConnectionSettings the server base url but I couln't find a way to customize the URL path for search; or any operation in general; for example:

var config = new ConnectionSettings(new Uri("{baseaddress}"));
    config.ApiKeyAuthentication("...", "...");           
    OpenSearchClient client = new OpenSearchClient(config);
    var searchResponse = client.Search<LoanProCustomersModel>(s => s
                        .Index("Customers")
                        .Query(q => q
                            .Match(m => m
                                .Field(fld => fld.lastName)
                                .Query("Dorantes Ostria"))));

The Search url will be {baseaddress}/Customers/_search

and it needed to be

{baseaddress}/Customers/Search

I am looking a way to do an interceptor or a DelegatingHandler to change or remap the URL but I dont find a way.

The reason of this feature is because we have an OpenSearch service provider that customized their urls.

dblock commented 6 months ago

I’ll reopen the issue. Why do you need to remap the URL?

ianido commented 6 months ago

We have this service provider who recently migrated from ElasticSearch to OpenSearch and they will maintain their previous URLs to keep compatibility as much as possible:

https://developers.loanpro.io/docs/opensearch-migration

So for example when we search by Customers; we need to send the POST to:

https://loanpro.simnang.com/api/public/api/1/tmp/{resource}/search

this is different from the standard search endpoint "{index}/_search" that will result in:

https://loanpro.simnang.com/api/public/api/1/tmp/{resource}/_search

Xtansia commented 6 months ago

The only real way to achieve this with the client is to use the raw HTTP request API, e.g.:

var searchResp = await client.Http.PostAsync<SearchResponse<TheDocument>>(
            $"/{resource}/search",
            r => r.SerializableBody(new
            {
                query = new
                {
                    match_all = new { }
                }
            }));
ianido commented 6 months ago

We are currently start working in this way, but definetely having support to customize the apiUrls in the High Level version of the library would be a great value, is there any way to attach a delegating handler to the HttpClient used internally?

Xtansia commented 6 months ago

@ianido You could try doing something similar to how AWS SigV4 signing is implemented, extending from HttpConnection and then overriding CreateHttpClientHandler to return your delegating handler:

You then pass it into ConnectionSettings like so: https://github.com/opensearch-project/opensearch-net/blob/main/USER_GUIDE.md#connecting-1

ianido commented 6 months ago

Great, that is what I needed, thank you so much.

dblock commented 6 months ago

@ianido Care to paste your working code here for the next person? Or maybe even a working sample?