Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Support for filter_path query parameter #616

Open
balogal opened this issue Sep 12, 2024 · 3 comments
Open

[FEATURE] Support for filter_path query parameter #616

balogal opened this issue Sep 12, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@balogal
Copy link

balogal commented Sep 12, 2024

OpenSearch supports filtering response bodies with the filter_path query parameter on all REST operations.

In the previous v1 & v2 versions of this library, that parameter could be set with WithFilterPath (e.g. here). However, it seems like the possibility to set this query param was lost with the move to the more go idiomatic API design, with the release of v3. Or at least, I'm not able to figure out how.

A possible workaround is to implement custom request types and use the Do function, but that's of course quite cumbersome.

@balogal balogal added enhancement New feature or request untriaged labels Sep 12, 2024
@Jakob3xD
Copy link
Collaborator

This parameter got removed as the response does not match any Response Struct we define. Therefore using the parameter would result in an parsing error. Even if we would find out what struct we can use to parse it, we would need to return it as type any or try to cast it somehow into the response struct.

What is your use case for this parameter?

Do you have an implementation idea?

@balogal
Copy link
Author

balogal commented Sep 13, 2024

I'm using the bulk endpoint to index millions of documents. The bulk request returns a lengthy status report about each upserted document in which I am not interested. Following this advice (second to last point), I want to use the filter_path param in order to significantly reduce the response body size and ultimately improve indexing performance.

You raise some valid concerns about the implementation. My assumption was, that filtering the response body will lead to parts of the result structs being returned with their zero-value (which I as a developer would be prepared to deal with, since I'm actively taking the decision to filter out said content). However, I didn't look into it in detail and of course if we end up with some kind of parse error, that would not work.

@balogal
Copy link
Author

balogal commented Oct 23, 2024

In case someone stumbles over this, I implemented the workaround with the custom request type as follows:

type CustomBulkReq struct {
	opensearchapi.BulkReq
}

func (r CustomBulkReq) GetRequest() (*http.Request, error) {
	req, err := r.BulkReq.GetRequest()
	if err != nil {
		return req, err
	}
	q := req.URL.Query()
	q.Add("filter_path", "took,errors,items.*.error")
	req.URL.RawQuery = q.Encode()
	return req, nil
}

func main() {
	baseReq := opensearchapi.BulkReq{/* initialize as normal */}
	req := CustomBulkReq{r}
	var data opensearchapi.BulkResp
	resp, err := c.client.Client.Do(ctx, req, &data)
	// ...
}

So far everything works as expected, obviously not every field of the response will be populated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants