Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.elasticsearch.client.ml.DeleteJobRequest;
import org.elasticsearch.client.ml.FlushJobRequest;
import org.elasticsearch.client.ml.GetBucketsRequest;
import org.elasticsearch.client.ml.GetInfluencersRequest;
import org.elasticsearch.client.ml.GetJobRequest;
import org.elasticsearch.client.ml.GetJobStatsRequest;
import org.elasticsearch.client.ml.GetOverallBucketsRequest;
Expand Down Expand Up @@ -186,4 +187,18 @@ static Request getRecords(GetRecordsRequest getRecordsRequest) throws IOExceptio
request.setEntity(createEntity(getRecordsRequest, REQUEST_BODY_CONTENT_TYPE));
return request;
}

static Request getInfluencers(GetInfluencersRequest getInfluencersRequest) throws IOException {
String endpoint = new EndpointBuilder()
.addPathPartAsIs("_xpack")
.addPathPartAsIs("ml")
.addPathPartAsIs("anomaly_detectors")
.addPathPart(getInfluencersRequest.getJobId())
.addPathPartAsIs("results")
.addPathPartAsIs("influencers")
.build();
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
request.setEntity(createEntity(getInfluencersRequest, REQUEST_BODY_CONTENT_TYPE));
return request;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,20 @@
package org.elasticsearch.client;

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.client.ml.FlushJobRequest;
import org.elasticsearch.client.ml.FlushJobResponse;
import org.elasticsearch.client.ml.GetJobStatsRequest;
import org.elasticsearch.client.ml.GetJobStatsResponse;
import org.elasticsearch.client.ml.job.stats.JobStats;
import org.elasticsearch.client.ml.CloseJobRequest;
import org.elasticsearch.client.ml.CloseJobResponse;
import org.elasticsearch.client.ml.DeleteJobRequest;
import org.elasticsearch.client.ml.DeleteJobResponse;
import org.elasticsearch.client.ml.FlushJobRequest;
import org.elasticsearch.client.ml.FlushJobResponse;
import org.elasticsearch.client.ml.GetBucketsRequest;
import org.elasticsearch.client.ml.GetBucketsResponse;
import org.elasticsearch.client.ml.GetInfluencersRequest;
import org.elasticsearch.client.ml.GetInfluencersResponse;
import org.elasticsearch.client.ml.GetJobRequest;
import org.elasticsearch.client.ml.GetJobResponse;
import org.elasticsearch.client.ml.GetJobStatsRequest;
import org.elasticsearch.client.ml.GetJobStatsResponse;
import org.elasticsearch.client.ml.GetOverallBucketsRequest;
import org.elasticsearch.client.ml.GetOverallBucketsResponse;
import org.elasticsearch.client.ml.GetRecordsRequest;
Expand All @@ -40,6 +41,7 @@
import org.elasticsearch.client.ml.OpenJobResponse;
import org.elasticsearch.client.ml.PutJobRequest;
import org.elasticsearch.client.ml.PutJobResponse;
import org.elasticsearch.client.ml.job.stats.JobStats;

import java.io.IOException;
import java.util.Collections;
Expand Down Expand Up @@ -464,4 +466,43 @@ public void getRecordsAsync(GetRecordsRequest request, RequestOptions options, A
listener,
Collections.emptySet());
}

/**
* Gets the influencers for a Machine Learning Job.
* <p>
* For additional info
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-influencer.html">
* ML GET influencers documentation</a>
*
* @param request the request
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
*/
public GetInfluencersResponse getInfluencers(GetInfluencersRequest request, RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(request,
MLRequestConverters::getInfluencers,
options,
GetInfluencersResponse::fromXContent,
Collections.emptySet());
}

/**
* Gets the influencers for a Machine Learning Job, notifies listener once the requested influencers are retrieved.
* <p>
* For additional info
* * see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-influencer.html">
* ML GET influencers 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 getInfluencersAsync(GetInfluencersRequest request, RequestOptions options,
ActionListener<GetInfluencersResponse> listener) {
restHighLevelClient.performRequestAsyncAndParseEntity(request,
MLRequestConverters::getInfluencers,
options,
GetInfluencersResponse::fromXContent,
listener,
Collections.emptySet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public class GetBucketsRequest extends ActionRequest implements ToXContentObject
public static final ParseField START = new ParseField("start");
public static final ParseField END = new ParseField("end");
public static final ParseField ANOMALY_SCORE = new ParseField("anomaly_score");
public static final ParseField TIMESTAMP = new ParseField("timestamp");
public static final ParseField SORT = new ParseField("sort");
public static final ParseField DESCENDING = new ParseField("desc");

Expand Down Expand Up @@ -106,11 +105,11 @@ public boolean isExpand() {
* When {@code true}, buckets will be expanded to include their records.
* @param expand value of "expand" to be set
*/
public void setExpand(boolean expand) {
public void setExpand(Boolean expand) {
this.expand = expand;
}

public boolean isExcludeInterim() {
public Boolean isExcludeInterim() {
return excludeInterim;
}

Expand All @@ -119,7 +118,7 @@ public boolean isExcludeInterim() {
* When {@code true}, interim buckets will be filtered out.
* @param excludeInterim value of "exclude_interim" to be set
*/
public void setExcludeInterim(boolean excludeInterim) {
public void setExcludeInterim(Boolean excludeInterim) {
this.excludeInterim = excludeInterim;
}

Expand Down Expand Up @@ -170,7 +169,7 @@ public Double getAnomalyScore() {
* Only buckets with "anomaly_score" equal or greater will be returned.
* @param anomalyScore value of "anomaly_score".
*/
public void setAnomalyScore(double anomalyScore) {
public void setAnomalyScore(Double anomalyScore) {
this.anomalyScore = anomalyScore;
}

Expand All @@ -187,7 +186,7 @@ public void setSort(String sort) {
this.sort = sort;
}

public boolean isDescending() {
public Boolean isDescending() {
return descending;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
/*
* 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.Job;
import org.elasticsearch.client.ml.job.util.PageParams;
import org.elasticsearch.common.ParseField;
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 influencers of a given job
*/
public class GetInfluencersRequest extends ActionRequest implements ToXContentObject {

public static final ParseField EXCLUDE_INTERIM = new ParseField("exclude_interim");
public static final ParseField START = new ParseField("start");
public static final ParseField END = new ParseField("end");
public static final ParseField INFLUENCER_SCORE = new ParseField("influencer_score");
public static final ParseField SORT = new ParseField("sort");
public static final ParseField DESCENDING = new ParseField("desc");

public static final ObjectParser<GetInfluencersRequest, Void> PARSER = new ObjectParser<>("get_influencers_request",
Copy link
Member

Choose a reason for hiding this comment

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

This should be a ConstructingObjectParser so that the private empty ctr can be removed.

GetInfluencersRequest::new);

static {
PARSER.declareString((request, jobId) -> request.jobId = jobId, Job.ID);
PARSER.declareBoolean(GetInfluencersRequest::setExcludeInterim, EXCLUDE_INTERIM);
PARSER.declareStringOrNull(GetInfluencersRequest::setStart, START);
PARSER.declareStringOrNull(GetInfluencersRequest::setEnd, END);
PARSER.declareObject(GetInfluencersRequest::setPageParams, PageParams.PARSER, PageParams.PAGE);
PARSER.declareDouble(GetInfluencersRequest::setInfluencerScore, INFLUENCER_SCORE);
PARSER.declareString(GetInfluencersRequest::setSort, SORT);
PARSER.declareBoolean(GetInfluencersRequest::setDescending, DESCENDING);
}

private String jobId;
Copy link
Member

Choose a reason for hiding this comment

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

should be final

private Boolean excludeInterim;
private String start;
private String end;
private Double influencerScore;
private PageParams pageParams;
private String sort;
private Boolean descending;

private GetInfluencersRequest() {}
Copy link
Member

Choose a reason for hiding this comment

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

Not necessary with ConstructingObjectParser


/**
* Constructs a request to retrieve influencers of a given job
* @param jobId id of the job to retrieve influencers of
*/
public GetInfluencersRequest(String jobId) {
this.jobId = Objects.requireNonNull(jobId);
}

public String getJobId() {
return jobId;
}

public Boolean isExcludeInterim() {
return excludeInterim;
}

/**
* Sets the value of "exclude_interim".
* When {@code true}, interim influencers will be filtered out.
* @param excludeInterim value of "exclude_interim" to be set
*/
public void setExcludeInterim(Boolean excludeInterim) {
this.excludeInterim = excludeInterim;
}

public String getStart() {
return start;
}

/**
* Sets the value of "start" which is a timestamp.
* Only influencers whose timestamp is on or after the "start" value will be returned.
* @param start value of "start" to be set
*/
public void setStart(String start) {
this.start = start;
}

public String getEnd() {
return end;
}

/**
* Sets the value of "end" which is a timestamp.
* Only influencers whose timestamp is before the "end" value will be returned.
* @param end value of "end" to be set
Copy link
Member

Choose a reason for hiding this comment

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

Knowing the supported time formats would be helpful for the user. (this goes for all the time fields in this object)

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 have also taken the opportunity to fix this in other get result request objects.

*/
public void setEnd(String end) {
this.end = end;
}

public PageParams getPageParams() {
return pageParams;
}

/**
* Sets the paging parameters
* @param pageParams The paging parameters
*/
public void setPageParams(PageParams pageParams) {
this.pageParams = pageParams;
}

public Double getInfluencerScore() {
return influencerScore;
}

/**
* Sets the value of "influencer_score".
* Only influencers with "influencer_score" equal or greater will be returned.
* @param influencerScore value of "influencer_score".
*/
public void setInfluencerScore(Double influencerScore) {
this.influencerScore = influencerScore;
}

public String getSort() {
return sort;
}

/**
* Sets the value of "sort".
* Specifies the influencer field to sort on.
* @param sort value of "sort".
*/
public void setSort(String sort) {
this.sort = sort;
}

public Boolean isDescending() {
return descending;
}

/**
* Sets the value of "desc".
* Specifies the sorting order.
* @param descending value of "desc"
*/
public void setDescending(Boolean descending) {
this.descending = descending;
}

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

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(Job.ID.getPreferredName(), jobId);
if (excludeInterim != null) {
builder.field(EXCLUDE_INTERIM.getPreferredName(), excludeInterim);
}
if (start != null) {
builder.field(START.getPreferredName(), start);
}
if (end != null) {
builder.field(END.getPreferredName(), end);
}
if (pageParams != null) {
builder.field(PageParams.PAGE.getPreferredName(), pageParams);
}
if (influencerScore != null) {
builder.field(INFLUENCER_SCORE.getPreferredName(), influencerScore);
}
if (sort != null) {
builder.field(SORT.getPreferredName(), sort);
}
if (descending != null) {
builder.field(DESCENDING.getPreferredName(), descending);
}
builder.endObject();
return builder;
}

@Override
public int hashCode() {
return Objects.hash(jobId, excludeInterim, influencerScore, pageParams, start, end, sort, descending);
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
GetInfluencersRequest other = (GetInfluencersRequest) obj;
return Objects.equals(jobId, other.jobId) &&
Objects.equals(excludeInterim, other.excludeInterim) &&
Objects.equals(influencerScore, other.influencerScore) &&
Objects.equals(pageParams, other.pageParams) &&
Objects.equals(start, other.start) &&
Objects.equals(end, other.end) &&
Objects.equals(sort, other.sort) &&
Objects.equals(descending, other.descending);
}
}
Loading