-
Notifications
You must be signed in to change notification settings - Fork 25k
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
[ML] Add ML filter update API #31437
Merged
dimitris-athanasiou
merged 6 commits into
elastic:master
from
dimitris-athanasiou:add-ml-filter-update-api
Jun 22, 2018
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7d70b55
[ML] Add ML filter update API
dimitris-athanasiou 68b3ee1
Address review feedback
dimitris-athanasiou 775ea35
Replace deprecated uses of XContentHelper
dimitris-athanasiou 7eeb6c8
Use Collections.singletonList
dimitris-athanasiou 05ed4a3
Fix yml tests to specify content-type
dimitris-athanasiou 899ce46
Fix compilation problem after rebasing on master
dimitris-athanasiou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
187 changes: 187 additions & 0 deletions
187
.../plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateFilterAction.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,187 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.core.ml.action; | ||
|
||
import org.elasticsearch.action.Action; | ||
import org.elasticsearch.action.ActionRequest; | ||
import org.elasticsearch.action.ActionRequestBuilder; | ||
import org.elasticsearch.action.ActionRequestValidationException; | ||
import org.elasticsearch.client.ElasticsearchClient; | ||
import org.elasticsearch.common.Nullable; | ||
import org.elasticsearch.common.ParseField; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.xcontent.ObjectParser; | ||
import org.elasticsearch.common.xcontent.ToXContentObject; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
import org.elasticsearch.xpack.core.ml.job.config.MlFilter; | ||
import org.elasticsearch.xpack.core.ml.job.messages.Messages; | ||
import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Objects; | ||
import java.util.SortedSet; | ||
import java.util.TreeSet; | ||
|
||
|
||
public class UpdateFilterAction extends Action<PutFilterAction.Response> { | ||
|
||
public static final UpdateFilterAction INSTANCE = new UpdateFilterAction(); | ||
public static final String NAME = "cluster:admin/xpack/ml/filters/update"; | ||
|
||
private UpdateFilterAction() { | ||
super(NAME); | ||
} | ||
|
||
@Override | ||
public PutFilterAction.Response newResponse() { | ||
return new PutFilterAction.Response(); | ||
} | ||
|
||
public static class Request extends ActionRequest implements ToXContentObject { | ||
|
||
public static final ParseField ADD_ITEMS = new ParseField("add_items"); | ||
public static final ParseField REMOVE_ITEMS = new ParseField("remove_items"); | ||
|
||
private static final ObjectParser<Request, Void> PARSER = new ObjectParser<>(NAME, Request::new); | ||
|
||
static { | ||
PARSER.declareString((request, filterId) -> request.filterId = filterId, MlFilter.ID); | ||
PARSER.declareStringOrNull(Request::setDescription, MlFilter.DESCRIPTION); | ||
PARSER.declareStringArray(Request::setAddItems, ADD_ITEMS); | ||
PARSER.declareStringArray(Request::setRemoveItems, REMOVE_ITEMS); | ||
} | ||
|
||
public static Request parseRequest(String filterId, XContentParser parser) { | ||
Request request = PARSER.apply(parser, null); | ||
if (request.filterId == null) { | ||
request.filterId = filterId; | ||
} else if (!Strings.isNullOrEmpty(filterId) && !filterId.equals(request.filterId)) { | ||
// If we have both URI and body filter ID, they must be identical | ||
throw new IllegalArgumentException(Messages.getMessage(Messages.INCONSISTENT_ID, MlFilter.ID.getPreferredName(), | ||
request.filterId, filterId)); | ||
} | ||
return request; | ||
} | ||
|
||
private String filterId; | ||
@Nullable | ||
private String description; | ||
private SortedSet<String> addItems = Collections.emptySortedSet(); | ||
private SortedSet<String> removeItems = Collections.emptySortedSet(); | ||
|
||
public Request() { | ||
} | ||
|
||
public Request(String filterId) { | ||
this.filterId = ExceptionsHelper.requireNonNull(filterId, MlFilter.ID.getPreferredName()); | ||
} | ||
|
||
public String getFilterId() { | ||
return filterId; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
|
||
public SortedSet<String> getAddItems() { | ||
return addItems; | ||
} | ||
|
||
public void setAddItems(Collection<String> addItems) { | ||
this.addItems = new TreeSet<>(ExceptionsHelper.requireNonNull(addItems, ADD_ITEMS.getPreferredName())); | ||
} | ||
|
||
public SortedSet<String> getRemoveItems() { | ||
return removeItems; | ||
} | ||
|
||
public void setRemoveItems(Collection<String> removeItems) { | ||
this.removeItems = new TreeSet<>(ExceptionsHelper.requireNonNull(removeItems, REMOVE_ITEMS.getPreferredName())); | ||
} | ||
|
||
public boolean isNoop() { | ||
return description == null && addItems.isEmpty() && removeItems.isEmpty(); | ||
} | ||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public void readFrom(StreamInput in) throws IOException { | ||
super.readFrom(in); | ||
filterId = in.readString(); | ||
description = in.readOptionalString(); | ||
addItems = new TreeSet<>(Arrays.asList(in.readStringArray())); | ||
removeItems = new TreeSet<>(Arrays.asList(in.readStringArray())); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
out.writeString(filterId); | ||
out.writeOptionalString(description); | ||
out.writeStringArray(addItems.toArray(new String[addItems.size()])); | ||
out.writeStringArray(removeItems.toArray(new String[removeItems.size()])); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
builder.field(MlFilter.ID.getPreferredName(), filterId); | ||
if (description != null) { | ||
builder.field(MlFilter.DESCRIPTION.getPreferredName(), description); | ||
} | ||
if (addItems.isEmpty() == false) { | ||
builder.field(ADD_ITEMS.getPreferredName(), addItems); | ||
} | ||
if (removeItems.isEmpty() == false) { | ||
builder.field(REMOVE_ITEMS.getPreferredName(), removeItems); | ||
} | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(filterId, description, addItems, removeItems); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj == null) { | ||
return false; | ||
} | ||
if (getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
Request other = (Request) obj; | ||
return Objects.equals(filterId, other.filterId) | ||
&& Objects.equals(description, other.description) | ||
&& Objects.equals(addItems, other.addItems) | ||
&& Objects.equals(removeItems, other.removeItems); | ||
} | ||
} | ||
|
||
public static class RequestBuilder extends ActionRequestBuilder<Request, PutFilterAction.Response> { | ||
|
||
public RequestBuilder(ElasticsearchClient client) { | ||
super(client, INSTANCE, new Request()); | ||
} | ||
} | ||
} |
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
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
58 changes: 58 additions & 0 deletions
58
.../src/test/java/org/elasticsearch/xpack/core/ml/action/UpdateFilterActionRequestTests.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,58 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.core.ml.action; | ||
|
||
import org.elasticsearch.common.xcontent.XContentParser; | ||
import org.elasticsearch.test.AbstractStreamableXContentTestCase; | ||
import org.elasticsearch.xpack.core.ml.action.UpdateFilterAction.Request; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
public class UpdateFilterActionRequestTests extends AbstractStreamableXContentTestCase<Request> { | ||
|
||
private String filterId = randomAlphaOfLength(20); | ||
|
||
@Override | ||
protected Request createTestInstance() { | ||
UpdateFilterAction.Request request = new UpdateFilterAction.Request(filterId); | ||
if (randomBoolean()) { | ||
request.setDescription(randomAlphaOfLength(20)); | ||
} | ||
if (randomBoolean()) { | ||
request.setAddItems(generateRandomStrings()); | ||
} | ||
if (randomBoolean()) { | ||
request.setRemoveItems(generateRandomStrings()); | ||
} | ||
return request; | ||
} | ||
|
||
private static Collection<String> generateRandomStrings() { | ||
int size = randomIntBetween(0, 10); | ||
List<String> strings = new ArrayList<>(size); | ||
for (int i = 0; i < size; ++i) { | ||
strings.add(randomAlphaOfLength(20)); | ||
} | ||
return strings; | ||
} | ||
|
||
@Override | ||
protected boolean supportsUnknownFields() { | ||
return false; | ||
} | ||
|
||
@Override | ||
protected Request createBlankInstance() { | ||
return new Request(); | ||
} | ||
|
||
@Override | ||
protected Request doParseInstance(XContentParser parser) { | ||
return Request.parseRequest(filterId, parser); | ||
} | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This looks odd that it doesn't have a setter. Can you use the ConstructingObjectParser
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.
As discussed, we follow this pattern in a few request objects where there is a field that will also be in the URI. While it seems odd, it saves us from having to relax the null check in the constructor. At the end, it makes the java API cleaner at the cost of having that funky lambda in the parser.