-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
HLRC: Add rollup search #36334
HLRC: Add rollup search #36334
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,8 @@ | |
import org.elasticsearch.action.bulk.BulkRequest; | ||
import org.elasticsearch.action.bulk.BulkResponse; | ||
import org.elasticsearch.action.index.IndexRequest; | ||
import org.elasticsearch.action.search.SearchRequest; | ||
import org.elasticsearch.action.search.SearchResponse; | ||
import org.elasticsearch.action.support.WriteRequest; | ||
import org.elasticsearch.client.ESRestHighLevelClientTestCase; | ||
import org.elasticsearch.client.RequestOptions; | ||
|
@@ -60,6 +62,9 @@ | |
import org.elasticsearch.common.unit.TimeValue; | ||
import org.elasticsearch.rest.RestStatus; | ||
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; | ||
import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation; | ||
import org.elasticsearch.search.aggregations.metrics.MaxAggregationBuilder; | ||
import org.elasticsearch.search.builder.SearchSourceBuilder; | ||
import org.junit.Before; | ||
|
||
import java.io.IOException; | ||
|
@@ -72,6 +77,7 @@ | |
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; | ||
import static org.hamcrest.Matchers.closeTo; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.hasSize; | ||
import static org.hamcrest.Matchers.isOneOf; | ||
|
@@ -89,7 +95,7 @@ public void setUpDocs() throws IOException { | |
.field("timestamp", String.format(Locale.ROOT, "2018-01-01T00:%02d:00Z", i)) | ||
.field("hostname", 0) | ||
.field("datacenter", 0) | ||
.field("temperature", 0) | ||
.field("temperature", i) | ||
.field("voltage", 0) | ||
.field("load", 0) | ||
.field("net_in", 0) | ||
|
@@ -330,6 +336,56 @@ public void onFailure(Exception e) { | |
assertTrue(latch.await(30L, TimeUnit.SECONDS)); | ||
} | ||
|
||
public void testSearch() throws Exception { | ||
// Setup a rollup index to query | ||
testCreateRollupJob(); | ||
|
||
RestHighLevelClient client = highLevelClient(); | ||
|
||
// tag::search-request | ||
SearchRequest request = new SearchRequest(); | ||
request.source(new SearchSourceBuilder() | ||
.size(0) | ||
.aggregation(new MaxAggregationBuilder("max_temperature") | ||
.field("temperature"))); | ||
// end::search-request | ||
|
||
// tag::search-execute | ||
SearchResponse response = | ||
client.rollup().search(request, RequestOptions.DEFAULT); | ||
// end::search-execute | ||
|
||
// tag::search-response | ||
NumericMetricsAggregation.SingleValue maxTemperature = | ||
response.getAggregations().get("max_temperature"); | ||
assertThat(maxTemperature.value(), closeTo(49.0, .00001)); | ||
// end::search-response-response | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. huh. I wonder why that didn't make the build fail.... Let me check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah! I suspect it doesn't fail because the tag checker ignores tailing characters. It probably should only ignore trailing spaces to be honest. But that is a job for another day! |
||
|
||
ActionListener<SearchResponse> listener; | ||
// tag::search-execute-listener | ||
listener = new ActionListener<SearchResponse>() { | ||
@Override | ||
public void onResponse(SearchResponse response) { | ||
// <1> | ||
} | ||
|
||
@Override | ||
public void onFailure(Exception e) { | ||
// <2> | ||
} | ||
}; | ||
// end::search-execute-listener | ||
|
||
final CountDownLatch latch = new CountDownLatch(1); | ||
listener = new LatchedActionListener<>(listener, latch); | ||
|
||
// tag::search-execute-async | ||
client.rollup().searchAsync(request, RequestOptions.DEFAULT, listener); // <1> | ||
// end::search-execute-async | ||
|
||
assertTrue(latch.await(30L, TimeUnit.SECONDS)); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public void testGetRollupCaps() throws Exception { | ||
RestHighLevelClient client = highLevelClient(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
-- | ||
:api: search | ||
:request: SearchRequest | ||
:response: SearchResponse | ||
-- | ||
|
||
[id="{upid}-{api}"] | ||
=== Rollup Search API | ||
|
||
The Rollup Search endpoint allows searching rolled-up data using the standard | ||
query DSL. The Rollup Search endpoint is needed because, internally, | ||
rolled-up documents utilize a different document structure than the original | ||
data. The Rollup Search endpoint rewrites standard query DSL into a format that | ||
matches the rollup documents, then takes the response and rewrites it back to | ||
what a client would expect given the original query. | ||
|
||
[id="{upid}-{api}-request"] | ||
==== Request | ||
|
||
Rollup Search uses the same +{request}+ that is used by the <<{mainid}-search>> | ||
but it is mostly for aggregations you should set the `size` to 0 and add | ||
aggregations like this: | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests-file}[{api}-request] | ||
-------------------------------------------------- | ||
|
||
NOTE:: Rollup Search is limited in many ways because only some query elements | ||
can be translated into queries against the rollup indices. See the main | ||
{ref}/rollup-search.html[Rollup Search] documentation for more. | ||
|
||
include::../execution.asciidoc[] | ||
|
||
[id="{upid}-{api}-response"] | ||
==== Response | ||
|
||
Rollup Search returns the same +{response}+ that is used by the | ||
<<{mainid}-search>> and everything can be accessed in exactly the same way. | ||
This will access the aggregation built by the example request above: | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests-file}[{api}-response] | ||
-------------------------------------------------- |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ and rewrites it back to what a client would expect given the original query. | |
indices. | ||
|
||
Rules for the `index` parameter: | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This page was rendering badly without this. |
||
- At least one index/index-pattern must be specified. This can be either a rollup or non-rollup index. Omitting the index parameter, | ||
or using `_all`, is not permitted | ||
- Multiple non-rollup indices may be specified | ||
|
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.
The more I look at this the less I like it. I think the other option is to build a HLRC side request object that uses the server-side agg builders. I'm not sure if that is worth it, but it would be a cleaner API. It wouldn't be exactly the API we want in the end, but it'd be better than this.
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.
@hub-cap pointed me to HLRC's
CountRequest
which is pretty similar to what I'm describing. Different because instead of rendering aSearchSourceBuilder
it we'd just do aggregations and queries and stuff like that.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 understand the concern but I find it easier if the request object is the same since you can directly use it for rollup or regular search seamlessly.
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 see that argument too!
I think in an ideal world the request for search and the request for rollup search would implement the same interface or have a common superclass....
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.
@hub-cap, what do you think about merging this like it stands now?
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 just placeholder the client side object and test the validation there instead? just extend the existing request? if not then im ok merging as is.
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 can't extend
SearchRequest
because it isfinal
. I'll merge as is then. I think it'd be nice to make the client side search request objects for rollup search at the same time as we make them for search. One day, one day....