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

Add Index API to High Level Rest Client #23040

Merged
merged 5 commits into from
Feb 15, 2017

Conversation

tlrx
Copy link
Member

@tlrx tlrx commented Feb 8, 2017

This commit adds support for the Index API in the High Level Rest Client.

It changes few things in the way request's parameters map is created in the high level client and changea bit RefreshPolicy in core.

/**
* Leave this request open until a refresh has made the contents of this request visible to search. This refresh policy is
* compatible with high indexing and search throughput but it causes the request to wait to reply until a refresh occurs.
*/
WAIT_UNTIL;
WAIT_UNTIL((byte) 2, "wait_for");
Copy link
Member

@javanna javanna Feb 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you make this change separately please? It will need to be backported , while this PR targets master only and should only change stuff under rest-high-level.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure - this part has been removed from the PR, I'll create another PR for the WriteRequest changes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR created here: #23106

@tlrx tlrx force-pushed the add-index-request-to-high-level-rest-client branch from e227619 to 3f8dc3c Compare February 10, 2017 11:13
@tlrx
Copy link
Member Author

tlrx commented Feb 10, 2017

Code rebased and updated

Copy link
Member

@cbuescher cbuescher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tlrx I left a few questions and minor remarks, some of it just ideas so from my point of view this is very close.

}

Params withRefreshPolicy(WriteRequest.RefreshPolicy refreshPolicy) {
if (refreshPolicy != WriteRequest.RefreshPolicy.NONE) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be paranoid, but in case some other previous call to the builder set this to some other value, can we remove that value from the map in case it is set to NONE later?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scratch that, I see this is private and only used internally, and the request only has one of each parameters, so duplicate calls are unlikely. Just me being paranoid.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Paranoid is good sometimes :) You're right, I'll improve that in order to be remove the value in case of NONE.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we rather have a check in putParam for the return value and assert that the value should never be there. If it is it's our bug right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

return parameters.getParams();
}

private static String getEndpoint(GetRequest getRequest) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can replace calls of this helper with the new Endpoint class? Might get a tad bit longer in the end, so I'm fine either way.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we can. I'm also waiting for Luca opinion on those Endpoint/Params classes, but if he likes it I'll move those as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feel free to unify this with what you do with index. I like these changes overall, I left one comment on the endpoint part which may help improving things a bit, that's it.

return pathJoiner.add(getRequest.index()).add(getRequest.type()).add(getRequest.id()).toString();
}

static Request index(IndexRequest indexRequest) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: maybe move this up next to the other methods (ping/exists/get) that output new Requests.

}
}

final XContentType xContentType = randomFrom(XContentType.values());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous test tries all content Types, this one random choice only. Any reason for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not, it's a leftover in the previous test, XContent type must be chosen randomly at the beginning of all tests. I changed that.


Map<String, String> expectedParams = new HashMap<>();
long version = randomFrom(Versions.MATCH_DELETED, Versions.NOT_FOUND, randomNonNegativeLong());
indexRequest.version(version);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would not setting the version at all be valid here? If so, maybe also test this (e.g. rarely?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

builder.field("field_" + i, i);
}
builder.endObject();
indexRequest.source(builder);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if it is compatible, but maybe you can use RandomObjects#randomSource(Random random) here instead?

Copy link
Member

@javanna javanna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left a few comments but no blockers, LGTM

private static class Endpoint {
private final List<String> parts = new ArrayList<>();
private final String delimiter = "/";
private final String prefix = "/";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given that we build a url, do we really need two constants for the same thing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed to

        private static final String DELIMITER = "/";
        private String prefix = DELIMITER;
        private String suffix = "";

and also added a withPrefix method that was missing. I find it useful for building endpoints for Snapshot/Restore: /_snapshot could be defined as a prefix. Just a matter of taste

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I removed all this stuff in favor of a simple concatenating method helper.

Endpoint endpoint = Endpoint.builder();
endpoint.add(indexRequest.index());
endpoint.add(indexRequest.type());
endpoint.add(indexRequest.id());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we make it accept a varargs instead and make this a single line?

private Endpoint() {
}

Endpoint withSuffix(String suffix) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given that the suffix is also known before we go and provide index, type, and id... (even in case of e.g. _search) I wonder if we can get rid of the list, and just provide parts and suffix as constructor argument, then convert it into string straightaway. Not even sure we need a builder for this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, let's do this.

}

Params withRefreshPolicy(WriteRequest.RefreshPolicy refreshPolicy) {
if (refreshPolicy != WriteRequest.RefreshPolicy.NONE) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we rather have a check in putParam for the return value and assert that the value should never be there. If it is it's our bug right?

assertEquals(RestStatus.CONFLICT, exception.status());
assertEquals("Elasticsearch exception [type=version_conflict_engine_exception, reason=[type][with_create_op_type]: " +
"version conflict, document already exists (current version [1])]", exception.getMessage());
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very nice tests, thanks!

}

static Request index(IndexRequest indexRequest) {
String method = Strings.hasLength(indexRequest.id()) ? "PUT" : "POST";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use HttpPut.METHOD_NAME maybe? or define our own constants?

return parameters.getParams();
}

private static String getEndpoint(GetRequest getRequest) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feel free to unify this with what you do with index. I like these changes overall, I left one comment on the endpoint part which may help improving things a bit, that's it.

@tlrx tlrx force-pushed the add-index-request-to-high-level-rest-client branch from 3f8dc3c to 269e377 Compare February 13, 2017 11:29
@tlrx
Copy link
Member Author

tlrx commented Feb 13, 2017

@cbuescher @javanna Thanks for the reviews. I updated the code according to your comments. I also had to rebase in order to use #23106

Copy link
Member

@cbuescher cbuescher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tlrx tlrx merged commit 86993ad into elastic:master Feb 15, 2017
@tlrx
Copy link
Member Author

tlrx commented Feb 15, 2017

Thanks @cbuescher @javanna

@tlrx tlrx deleted the add-index-request-to-high-level-rest-client branch February 15, 2017 12:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants