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 support for search templates to the high-level REST client. #30473

Merged
merged 7 commits into from
May 15, 2018

Conversation

jtibshirani
Copy link
Contributor

No description provided.

@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra

@jtibshirani jtibshirani force-pushed the feature/rest-search-template branch from c88fb82 to c9809d8 Compare May 9, 2018 18:16
@jtibshirani jtibshirani force-pushed the feature/rest-search-template branch from c9809d8 to 597a8c8 Compare May 10, 2018 01:55
@jtibshirani jtibshirani added v7.0.0 and removed WIP labels May 10, 2018
@jtibshirani jtibshirani force-pushed the feature/rest-search-template branch from 597a8c8 to b75a875 Compare May 10, 2018 02:07
@jtibshirani jtibshirani requested a review from javanna May 10, 2018 02:07
@jtibshirani jtibshirani force-pushed the feature/rest-search-template branch from b75a875 to de86572 Compare May 10, 2018 04:19

===== Additional References

The https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html#search-template[Search Template HTTP documentation]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasn't sure how to do a proper asciidoc link here, as I don't think these two sets of documentation are built together.

Copy link
Member

Choose a reason for hiding this comment

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

{ref}/search-template.html[Search Template documentation] should do it. That will also go to the proper branch. I would also remove the HTTP part from the description.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I updated this reference.

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, mainly on testing, but this looks really good already, thanks @jtibshirani !

request = new Request(HttpGet.METHOD_NAME, "_render/template");
} else {
SearchRequest searchRequest = searchTemplateRequest.getRequest();
assert searchRequest != null : "When not simulating a template request, a search request must be present.";
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 request goes through validate first, I think we could remove this assertion, this is already checked in as part of validate which will throw an error otherwise.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, will remove.


Search templates can be registered in advance through stored scripts API. Note that
the stored scripts API is not yet available in the high-level REST client, so in this
example we use the simple REST client.
Copy link
Member

Choose a reason for hiding this comment

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

s/simple/low-level ?


// tag::register-script
Request scriptRequest = new Request("POST", "_scripts/title_search");
scriptRequest.setEntity(new NStringEntity(
Copy link
Member

Choose a reason for hiding this comment

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

you could use scriptRequest.setJsonEntity


===== Additional References

The https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html#search-template[Search Template HTTP documentation]
Copy link
Member

Choose a reason for hiding this comment

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

{ref}/search-template.html[Search Template documentation] should do it. That will also go to the proper branch. I would also remove the HTTP part from the description.


The https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html#search-template[Search Template HTTP documentation]
contains further examples of how search requests can be templated. For more detailed information on how mustache
works and what can be done with it, please consult the http://mustache.github.io/mustache.5.html[online documentation of the mustache project].
Copy link
Member

Choose a reason for hiding this comment

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

I am a bit hesitant on the second sentence about mustache. I feel like we could do without it, here we are just documenting how to use the client.

Copy link
Contributor Author

@jtibshirani jtibshirani May 14, 2018

Choose a reason for hiding this comment

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

I'm happy to remove it, especially as the mustache reference is available in the linked Search Template documentation.

" }" +
"}", ContentType.APPLICATION_JSON));
Response scriptResponse = restClient.performRequest(scriptRequest);
assertEquals(RestStatus.OK.getStatus(), scriptResponse.getStatusLine().getStatusCode());
Copy link
Member

Choose a reason for hiding this comment

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

can we leave the assertions out of the snippets that get published in the docs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed all of the assertions. I plan to go back and fix the field capabilities documentation, since I added assertions there too!

public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();

if (scriptType == ScriptType.STORED) {
Copy link
Member

Choose a reason for hiding this comment

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

super minor nit: maybe this could be a switch ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would usually do use a switch, but here I found it harder to read because the enum values must be unqualified (case STORED instead of case ScriptType.STORED).

} else if (scriptType == ScriptType.INLINE) {
builder.field(SOURCE_FIELD.getPreferredName(), script);
} else {
throw new IllegalArgumentException("Unrecognized script type [" + scriptType + "].");
Copy link
Member

Choose a reason for hiding this comment

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

maybe this should rather be an UnsupportedOperationException ? I mean can this argument be provided with a different value than the ones supported?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed, thanks.

* - We omit the random SearchRequest, since this component only affects the request
* parameters and also isn't captured in the request's xContent.
*/
public void testFromXContent() throws IOException {
Copy link
Member

Choose a reason for hiding this comment

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

Given these changes, I wonder if we should rather extend AbstractStreamableTestCase. We've had these problems in other cases, requests tend to require different equal comparisons depending on whether we are testing transport serialization or xcontent serialization. Transport serialization includes request parameters that are not part of the xcontent serialization, which is only about the http request body after all.
Got the xcontent part, maybe we could have a separate test class that extends AbstractXContentTestCase, where you can override assertEqualInstances and tweak all you need without having to override testFromXContent (hopefully)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I ended up splitting out the xContent parts of this test into a new class, SearchTemplateRequestXContentTests. Please let me know if this is not what you had in mind.

skippedShards, tookInMillis, ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY);
}

private BytesReference createSource() {
Copy link
Member

Choose a reason for hiding this comment

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

can this be static?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

@jtibshirani jtibshirani force-pushed the feature/rest-search-template branch from de86572 to 6ded113 Compare May 14, 2018 21:24
@jtibshirani
Copy link
Contributor Author

jtibshirani commented May 14, 2018

Thank you @javanna for the review. I've tried to address your comments.

@jtibshirani jtibshirani force-pushed the feature/rest-search-template branch 2 times, most recently from aa83a0e to ac9a0e3 Compare May 15, 2018 05:56
import org.elasticsearch.script.ScriptType;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
Copy link
Member

Choose a reason for hiding this comment

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

wrong import?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, thanks.

expectedInstance.setRequest(null);
newInstance.setRequest(null);

super.assertEqualInstances(expectedInstance, newInstance);
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 instead of mutating the two instances we should have the proper comparisons? I guess it is a subset of what equals and hashcode do?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I updated this and it looks clearer... we don't need the full equals and hashCode comparison because we're not supporting testEqualsAndHashcode.

*/
private XContentParser newParser(String s) throws IOException {
assertNotNull(s);
return createParser(JsonXContent.jsonXContent, s.replace("'", "\""));
Copy link
Member

Choose a reason for hiding this comment

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

you have this for readability of the snippets above? Or some other reason too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This set-up comes from some pre-existing tests that I decided not to rewrite -- I think it's simply for readability.

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 couple of nits, LGTM otherwise, thanks a lot @jtibshirani

@jtibshirani jtibshirani force-pushed the feature/rest-search-template branch from ac9a0e3 to 776ed02 Compare May 15, 2018 16:42
@jtibshirani jtibshirani merged commit 4f9dd37 into elastic:master May 15, 2018
@jtibshirani jtibshirani deleted the feature/rest-search-template branch May 15, 2018 20:08
@fbaligand
Copy link
Contributor

As v7 is planned far away, it would be great to back port this feature to 6.x branch !

@javanna
Copy link
Member

javanna commented May 22, 2018

backporting would definitely be the plan. @jtibshirani could you take care of it or was there any specific reason why this wasn't backported?

@jtibshirani
Copy link
Contributor Author

Sure, I'll do that now. My mistake -- I wasn't up to speed on our backport policy.

@fbaligand
Copy link
Contributor

Great ! 👍

jtibshirani added a commit that referenced this pull request May 22, 2018
dnhatn added a commit that referenced this pull request May 24, 2018
* 6.x:
  [DOCS] Fixes typos in security settings
  Add support for indexed shape routing in geo_shape query (#30760)
  [DOCS] Splits auditing.asciidoc into smaller files
  Painless: Types Section Clean Up (#30283)
  [test] java tests for archive packaging (#30734)
  Deprecate http.pipelining setting (#30786)
  [DOCS] Fix more edit URLs in Stack Overview (#30704)
  Use correct cluster state version for node fault detection (#30810)
  [DOCS] Fixes broken link for native realm
  [DOCS] Clarified audit.index.client.hosts (#30797)
  Change serialization version of doc-value fields.
  Add a `format` option to `docvalue_fields`. (#29639)
  [TEST] Don't expect acks when isolating nodes
  Fixes UpdateSettingsRequestStreamableTests mutate bug
  Revert "Add more yaml tests for get alias API (#29513)"
  Revert "Mutes MachineLearningTests.testNoAttributes_givenSameAndMlEnabled"
  Only allow x-pack metadata if all nodes are ready (#30743)
  Mutes MachineLearningTests.testNoAttributes_givenSameAndMlEnabled
  Use original settings on full-cluster restart (#30780)
  Only ack cluster state updates successfully applied on all nodes (#30672)
  Replace Request#setHeaders with addHeader (#30588)
  [TEST] remove endless wait in RestClientTests (#30776)
  QA: Add xpack tests to rolling upgrade (#30795)
  Add support for search templates to the high-level REST client. (#30473)
  Reduce CLI scripts to one-liners on Windows (#30772)
  Fold RestGetAllSettingsAction in RestGetSettingsAction (#30561)
  Add more yaml tests for get alias API (#29513)
  [Docs] Fix script-fields snippet execution (#30693)
  Convert FieldCapabilitiesResponse to a ToXContentObject. (#30182)
  Remove assert statements from field caps documentation. (#30601)
  Fix a bug in FieldCapabilitiesRequest#equals and hashCode. (#30181)
  Add support for field capabilities to the high-level REST client. (#29664)
  [DOCS] Add SAML configuration information (#30548)
  [DOCS] Remove X-Pack references from SQL CLI (#30694)
  [Docs] Fix typo in circuit breaker docs (#29659)
  [Feature] Adding a char_group tokenizer (#24186)
  Increase the maximum number of filters that may be in the cache. (#30655)
  [Docs] Fix broken cross link in documentation
  Test: wait for netty threads in a JUnit ClassRule (#30763)
  [Security] Include an empty json object in an json array when FLS filters out all fields (#30709)
  [DOCS] fixed incorrect default
  [TEST] Wait for CS to be fully applied in testDeleteCreateInOneBulk
  Enable installing plugins from snapshots.elastic.co (#30765)
  Ignore empty completion input (#30713)
  Fix docs failure on language analyzers (#30722)
  [Docs] Fix inconsistencies in snapshot/restore doc (#30480)
  Add Delete Repository High Level REST API (#30666)
  Reduce CLI scripts to one-liners (#30759)
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.

5 participants