Skip to content

Commit

Permalink
HLRC: Adding ml get filters api (#35502)
Browse files Browse the repository at this point in the history
* HLRC: Adding ml get filters api

* refactoring setId name
  • Loading branch information
benwtrent committed Nov 13, 2018
1 parent da117fa commit 853eb9e
Show file tree
Hide file tree
Showing 12 changed files with 573 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.elasticsearch.client.ml.GetCategoriesRequest;
import org.elasticsearch.client.ml.GetDatafeedRequest;
import org.elasticsearch.client.ml.GetDatafeedStatsRequest;
import org.elasticsearch.client.ml.GetFiltersRequest;
import org.elasticsearch.client.ml.GetInfluencersRequest;
import org.elasticsearch.client.ml.GetJobRequest;
import org.elasticsearch.client.ml.GetJobStatsRequest;
Expand All @@ -55,6 +56,7 @@
import org.elasticsearch.client.ml.StopDatafeedRequest;
import org.elasticsearch.client.ml.UpdateDatafeedRequest;
import org.elasticsearch.client.ml.UpdateJobRequest;
import org.elasticsearch.client.ml.job.util.PageParams;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;

Expand Down Expand Up @@ -476,4 +478,22 @@ static Request putFilter(PutFilterRequest putFilterRequest) throws IOException {
request.setEntity(createEntity(putFilterRequest, REQUEST_BODY_CONTENT_TYPE));
return request;
}

static Request getFilter(GetFiltersRequest getFiltersRequest) {
String endpoint = new EndpointBuilder()
.addPathPartAsIs("_xpack")
.addPathPartAsIs("ml")
.addPathPartAsIs("filters")
.addPathPart(getFiltersRequest.getFilterId())
.build();
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
RequestConverters.Params params = new RequestConverters.Params(request);
if (getFiltersRequest.getSize() != null) {
params.putParam(PageParams.SIZE.getPreferredName(), getFiltersRequest.getSize().toString());
}
if (getFiltersRequest.getFrom() != null) {
params.putParam(PageParams.FROM.getPreferredName(), getFiltersRequest.getFrom().toString());
}
return request;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import org.elasticsearch.client.ml.GetDatafeedResponse;
import org.elasticsearch.client.ml.GetDatafeedStatsRequest;
import org.elasticsearch.client.ml.GetDatafeedStatsResponse;
import org.elasticsearch.client.ml.GetFiltersRequest;
import org.elasticsearch.client.ml.GetFiltersResponse;
import org.elasticsearch.client.ml.GetInfluencersRequest;
import org.elasticsearch.client.ml.GetInfluencersResponse;
import org.elasticsearch.client.ml.GetJobRequest;
Expand Down Expand Up @@ -1207,4 +1209,41 @@ public void putFilterAsync(PutFilterRequest request, RequestOptions options, Act
Collections.emptySet());
}

/**
* Gets Machine Learning Filters
* <p>
* For additional info
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-filter.html">ML GET Filter documentation</a>
*
* @param request The request
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @return GetFilterResponse with enclosed {@link org.elasticsearch.client.ml.job.config.MlFilter} objects
* @throws IOException when there is a serialization issue sending the request or receiving the response
*/
public GetFiltersResponse getFilter(GetFiltersRequest request, RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(request,
MLRequestConverters::getFilter,
options,
GetFiltersResponse::fromXContent,
Collections.emptySet());
}

/**
* Gets Machine Learning Filters asynchronously and notifies listener on completion
* <p>
* For additional info
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-filter.html">ML GET Filter documentation</a>
*
* @param request The request
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @param listener Listener to be notified upon request completion
*/
public void getFilterAsync(GetFiltersRequest request, RequestOptions options, ActionListener<GetFiltersResponse> listener) {
restHighLevelClient.performRequestAsyncAndParseEntity(request,
MLRequestConverters::getFilter,
options,
GetFiltersResponse::fromXContent,
listener,
Collections.emptySet());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.client.ml;

import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.client.ml.job.config.MlFilter;
import org.elasticsearch.client.ml.job.util.PageParams;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;

import java.io.IOException;
import java.util.Objects;

/**
* A request to retrieve {@link MlFilter}s
*/
public class GetFiltersRequest extends ActionRequest implements ToXContentObject {

public static final ObjectParser<GetFiltersRequest, Void> PARSER =
new ObjectParser<>("get_filters_request", GetFiltersRequest::new);

static {
PARSER.declareString(GetFiltersRequest::setFilterId, MlFilter.ID);
PARSER.declareInt(GetFiltersRequest::setFrom, PageParams.FROM);
PARSER.declareInt(GetFiltersRequest::setSize, PageParams.SIZE);
}

private String filterId;
private Integer from;
private Integer size;

public String getFilterId() {
return filterId;
}

public Integer getFrom() {
return from;
}

public Integer getSize() {
return size;
}

/**
* Sets the filter id
* @param filterId the filter id
*/
public void setFilterId(String filterId) {
this.filterId = filterId;
}

/**
* Sets the number of filters to skip.
* @param from set the `from` parameter
*/
public void setFrom(Integer from) {
this.from = from;
}

/**
* Sets the number of filters to return.
* @param size set the `size` parameter
*/
public void setSize(Integer size) {
this.size = size;
}

@Override
public ActionRequestValidationException validate() {
return null;
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
if (filterId != null) {
builder.field(MlFilter.ID.getPreferredName(), filterId);
}
if (from != null) {
builder.field(PageParams.FROM.getPreferredName(), from);
}
if (size != null) {
builder.field(PageParams.SIZE.getPreferredName(), size);
}
builder.endObject();
return builder;
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
GetFiltersRequest request = (GetFiltersRequest) obj;
return Objects.equals(filterId, request.filterId)
&& Objects.equals(from, request.from)
&& Objects.equals(size, request.size);
}

@Override
public int hashCode() {
return Objects.hash(filterId, from, size);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.client.ml;

import org.elasticsearch.client.ml.job.config.MlFilter;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.XContentParser;

import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;

/**
* Contains a {@link List} of the found {@link MlFilter} objects and the total count found
*/
public class GetFiltersResponse extends AbstractResultResponse<MlFilter> {

public static final ParseField RESULTS_FIELD = new ParseField("filters");

@SuppressWarnings("unchecked")
public static final ConstructingObjectParser<GetFiltersResponse, Void> PARSER =
new ConstructingObjectParser<>("get_filters_response", true,
a -> new GetFiltersResponse((List<MlFilter.Builder>) a[0], (long) a[1]));

static {
PARSER.declareObjectArray(constructorArg(), MlFilter.PARSER, RESULTS_FIELD);
PARSER.declareLong(constructorArg(), AbstractResultResponse.COUNT);
}

GetFiltersResponse(List<MlFilter.Builder> filters, long count) {
super(RESULTS_FIELD, filters.stream().map(MlFilter.Builder::build).collect(Collectors.toList()), count);
}

/**
* The collection of {@link MlFilter} objects found in the query
*/
public List<MlFilter> filters() {
return results;
}

public static GetFiltersResponse fromXContent(XContentParser parser) throws IOException {
return PARSER.parse(parser, null);
}

@Override
public int hashCode() {
return Objects.hash(results, count);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}

if (obj == null || getClass() != obj.getClass()) {
return false;
}

GetFiltersResponse other = (GetFiltersResponse) obj;
return Objects.equals(results, other.results) && count == other.count;
}

@Override
public final String toString() {
return Strings.toString(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.elasticsearch.client.ml.GetCategoriesRequest;
import org.elasticsearch.client.ml.GetDatafeedRequest;
import org.elasticsearch.client.ml.GetDatafeedStatsRequest;
import org.elasticsearch.client.ml.GetFiltersRequest;
import org.elasticsearch.client.ml.GetInfluencersRequest;
import org.elasticsearch.client.ml.GetJobRequest;
import org.elasticsearch.client.ml.GetJobStatsRequest;
Expand Down Expand Up @@ -77,6 +78,8 @@
import java.util.Map;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.core.IsNull.nullValue;

public class MLRequestConvertersTests extends ESTestCase {

Expand Down Expand Up @@ -515,7 +518,7 @@ public void testDeleteCalendar() {
}

public void testPutFilter() throws IOException {
MlFilter filter = MlFilterTests.createRandom("foo");
MlFilter filter = MlFilterTests.createRandomBuilder("foo").build();
PutFilterRequest putFilterRequest = new PutFilterRequest(filter);

Request request = MLRequestConverters.putFilter(putFilterRequest);
Expand All @@ -528,6 +531,25 @@ public void testPutFilter() throws IOException {
}
}

public void testGetFilter() throws IOException {
String id = randomAlphaOfLength(10);
GetFiltersRequest getFiltersRequest = new GetFiltersRequest();

getFiltersRequest.setFilterId(id);

Request request = MLRequestConverters.getFilter(getFiltersRequest);
assertEquals(HttpGet.METHOD_NAME, request.getMethod());
assertEquals("/_xpack/ml/filters/" + id, request.getEndpoint());
assertThat(request.getParameters().get(PageParams.FROM.getPreferredName()), is(nullValue()));
assertThat(request.getParameters().get(PageParams.SIZE.getPreferredName()), is(nullValue()));

getFiltersRequest.setFrom(1);
getFiltersRequest.setSize(10);
request = MLRequestConverters.getFilter(getFiltersRequest);
assertThat(request.getParameters().get(PageParams.FROM.getPreferredName()), equalTo("1"));
assertThat(request.getParameters().get(PageParams.SIZE.getPreferredName()), equalTo("10"));
}

private static Job createValidJob(String jobId) {
AnalysisConfig.Builder analysisConfig = AnalysisConfig.builder(Collections.singletonList(
Detector.builder().setFunction("count").build()));
Expand Down
Loading

0 comments on commit 853eb9e

Please sign in to comment.