-
Notifications
You must be signed in to change notification settings - Fork 24.8k
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
Add support for search templates to the high-level REST client. #30473
Conversation
Pinging @elastic/es-core-infra |
c88fb82
to
c9809d8
Compare
c9809d8
to
597a8c8
Compare
597a8c8
to
b75a875
Compare
b75a875
to
de86572
Compare
|
||
===== Additional References | ||
|
||
The https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html#search-template[Search Template HTTP documentation] |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this 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."; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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]. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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 + "]."); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can this be static?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
de86572
to
6ded113
Compare
Thank you @javanna for the review. I've tried to address your comments. |
aa83a0e
to
ac9a0e3
Compare
import org.elasticsearch.script.ScriptType; | ||
|
||
import java.io.IOException; | ||
import java.io.UnsupportedEncodingException; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrong import?
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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("'", "\"")); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this 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
ac9a0e3
to
776ed02
Compare
As v7 is planned far away, it would be great to back port this feature to 6.x branch ! |
backporting would definitely be the plan. @jtibshirani could you take care of it or was there any specific reason why this wasn't backported? |
Sure, I'll do that now. My mistake -- I wasn't up to speed on our backport policy. |
Great ! 👍 |
(cherry picked from commit 4f9dd37)
* 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)
No description provided.