|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.client.ml; |
| 20 | + |
| 21 | +import org.elasticsearch.client.Validatable; |
| 22 | +import org.elasticsearch.client.ml.job.config.Job; |
| 23 | +import org.elasticsearch.client.ml.job.util.PageParams; |
| 24 | +import org.elasticsearch.common.ParseField; |
| 25 | +import org.elasticsearch.common.xcontent.ObjectParser; |
| 26 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 27 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.Objects; |
| 31 | + |
| 32 | +/** |
| 33 | + * A request to retrieve records of a given job |
| 34 | + */ |
| 35 | +public class GetRecordsRequest implements ToXContentObject, Validatable { |
| 36 | + |
| 37 | + public static final ParseField EXCLUDE_INTERIM = new ParseField("exclude_interim"); |
| 38 | + public static final ParseField START = new ParseField("start"); |
| 39 | + public static final ParseField END = new ParseField("end"); |
| 40 | + public static final ParseField RECORD_SCORE = new ParseField("record_score"); |
| 41 | + public static final ParseField SORT = new ParseField("sort"); |
| 42 | + public static final ParseField DESCENDING = new ParseField("desc"); |
| 43 | + |
| 44 | + public static final ObjectParser<GetRecordsRequest, Void> PARSER = new ObjectParser<>("get_buckets_request", GetRecordsRequest::new); |
| 45 | + |
| 46 | + static { |
| 47 | + PARSER.declareString((request, jobId) -> request.jobId = jobId, Job.ID); |
| 48 | + PARSER.declareBoolean(GetRecordsRequest::setExcludeInterim, EXCLUDE_INTERIM); |
| 49 | + PARSER.declareStringOrNull(GetRecordsRequest::setStart, START); |
| 50 | + PARSER.declareStringOrNull(GetRecordsRequest::setEnd, END); |
| 51 | + PARSER.declareObject(GetRecordsRequest::setPageParams, PageParams.PARSER, PageParams.PAGE); |
| 52 | + PARSER.declareDouble(GetRecordsRequest::setRecordScore, RECORD_SCORE); |
| 53 | + PARSER.declareString(GetRecordsRequest::setSort, SORT); |
| 54 | + PARSER.declareBoolean(GetRecordsRequest::setDescending, DESCENDING); |
| 55 | + } |
| 56 | + |
| 57 | + private String jobId; |
| 58 | + private Boolean excludeInterim; |
| 59 | + private String start; |
| 60 | + private String end; |
| 61 | + private PageParams pageParams; |
| 62 | + private Double recordScore; |
| 63 | + private String sort; |
| 64 | + private Boolean descending; |
| 65 | + |
| 66 | + private GetRecordsRequest() {} |
| 67 | + |
| 68 | + /** |
| 69 | + * Constructs a request to retrieve records of a given job |
| 70 | + * @param jobId id of the job to retrieve records of |
| 71 | + */ |
| 72 | + public GetRecordsRequest(String jobId) { |
| 73 | + this.jobId = Objects.requireNonNull(jobId); |
| 74 | + } |
| 75 | + |
| 76 | + public String getJobId() { |
| 77 | + return jobId; |
| 78 | + } |
| 79 | + |
| 80 | + public boolean isExcludeInterim() { |
| 81 | + return excludeInterim; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Sets the value of "exclude_interim". |
| 86 | + * When {@code true}, interim records will be filtered out. |
| 87 | + * @param excludeInterim value of "exclude_interim" to be set |
| 88 | + */ |
| 89 | + public void setExcludeInterim(boolean excludeInterim) { |
| 90 | + this.excludeInterim = excludeInterim; |
| 91 | + } |
| 92 | + |
| 93 | + public String getStart() { |
| 94 | + return start; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Sets the value of "start" which is a timestamp. |
| 99 | + * Only records whose timestamp is on or after the "start" value will be returned. |
| 100 | + * @param start value of "start" to be set |
| 101 | + */ |
| 102 | + public void setStart(String start) { |
| 103 | + this.start = start; |
| 104 | + } |
| 105 | + |
| 106 | + public String getEnd() { |
| 107 | + return end; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Sets the value of "end" which is a timestamp. |
| 112 | + * Only records whose timestamp is before the "end" value will be returned. |
| 113 | + * @param end value of "end" to be set |
| 114 | + */ |
| 115 | + public void setEnd(String end) { |
| 116 | + this.end = end; |
| 117 | + } |
| 118 | + |
| 119 | + public PageParams getPageParams() { |
| 120 | + return pageParams; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Sets the paging parameters |
| 125 | + * @param pageParams The paging parameters |
| 126 | + */ |
| 127 | + public void setPageParams(PageParams pageParams) { |
| 128 | + this.pageParams = pageParams; |
| 129 | + } |
| 130 | + |
| 131 | + public Double getRecordScore() { |
| 132 | + return recordScore; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Sets the value of "record_score". |
| 137 | + * Only records with "record_score" equal or greater will be returned. |
| 138 | + * @param recordScore value of "record_score". |
| 139 | + */ |
| 140 | + public void setRecordScore(double recordScore) { |
| 141 | + this.recordScore = recordScore; |
| 142 | + } |
| 143 | + |
| 144 | + public String getSort() { |
| 145 | + return sort; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Sets the value of "sort". |
| 150 | + * Specifies the bucket field to sort on. |
| 151 | + * @param sort value of "sort". |
| 152 | + */ |
| 153 | + public void setSort(String sort) { |
| 154 | + this.sort = sort; |
| 155 | + } |
| 156 | + |
| 157 | + public boolean isDescending() { |
| 158 | + return descending; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Sets the value of "desc". |
| 163 | + * Specifies the sorting order. |
| 164 | + * @param descending value of "desc" |
| 165 | + */ |
| 166 | + public void setDescending(boolean descending) { |
| 167 | + this.descending = descending; |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 172 | + builder.startObject(); |
| 173 | + builder.field(Job.ID.getPreferredName(), jobId); |
| 174 | + if (excludeInterim != null) { |
| 175 | + builder.field(EXCLUDE_INTERIM.getPreferredName(), excludeInterim); |
| 176 | + } |
| 177 | + if (start != null) { |
| 178 | + builder.field(START.getPreferredName(), start); |
| 179 | + } |
| 180 | + if (end != null) { |
| 181 | + builder.field(END.getPreferredName(), end); |
| 182 | + } |
| 183 | + if (pageParams != null) { |
| 184 | + builder.field(PageParams.PAGE.getPreferredName(), pageParams); |
| 185 | + } |
| 186 | + if (recordScore != null) { |
| 187 | + builder.field(RECORD_SCORE.getPreferredName(), recordScore); |
| 188 | + } |
| 189 | + if (sort != null) { |
| 190 | + builder.field(SORT.getPreferredName(), sort); |
| 191 | + } |
| 192 | + if (descending != null) { |
| 193 | + builder.field(DESCENDING.getPreferredName(), descending); |
| 194 | + } |
| 195 | + builder.endObject(); |
| 196 | + return builder; |
| 197 | + } |
| 198 | + |
| 199 | + @Override |
| 200 | + public int hashCode() { |
| 201 | + return Objects.hash(jobId, excludeInterim, recordScore, pageParams, start, end, sort, descending); |
| 202 | + } |
| 203 | + |
| 204 | + @Override |
| 205 | + public boolean equals(Object obj) { |
| 206 | + if (obj == null) { |
| 207 | + return false; |
| 208 | + } |
| 209 | + if (getClass() != obj.getClass()) { |
| 210 | + return false; |
| 211 | + } |
| 212 | + GetRecordsRequest other = (GetRecordsRequest) obj; |
| 213 | + return Objects.equals(jobId, other.jobId) && |
| 214 | + Objects.equals(excludeInterim, other.excludeInterim) && |
| 215 | + Objects.equals(recordScore, other.recordScore) && |
| 216 | + Objects.equals(pageParams, other.pageParams) && |
| 217 | + Objects.equals(start, other.start) && |
| 218 | + Objects.equals(end, other.end) && |
| 219 | + Objects.equals(sort, other.sort) && |
| 220 | + Objects.equals(descending, other.descending); |
| 221 | + } |
| 222 | +} |
0 commit comments