-
Notifications
You must be signed in to change notification settings - Fork 24.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HLRC: Adding ml get filters api (#35502)
* HLRC: Adding ml get filters api * refactoring setId name
- Loading branch information
Showing
12 changed files
with
573 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetFiltersRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetFiltersResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.