Skip to content
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

IntervalsSourceProvider and IntervalFilter Plugins #49519

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
@@ -0,0 +1,55 @@
/*
* 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.index.query;

import org.apache.lucene.queries.intervals.IntervalsSource;
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.io.stream.NamedWriteable;
import org.elasticsearch.common.xcontent.NamedObjectNotFoundException;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentLocation;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.MappedFieldType;

import java.io.IOException;

public abstract class IntervalFilter implements NamedWriteable, ToXContentObject {
@Override
public abstract int hashCode();

@Override
public abstract boolean equals(Object other);

public abstract IntervalsSource filter(IntervalsSource input, QueryShardContext context, MappedFieldType fieldType)
throws IOException;

public static IntervalFilter fromXContent(XContentParser parser) throws IOException {
if (parser.nextToken() != XContentParser.Token.FIELD_NAME) {
throw new ParsingException(parser.getTokenLocation(), "Expected [FIELD_NAME] but got [" + parser.currentToken() + "]");
}
String type = parser.currentName();
try {
return parser.namedObject(IntervalFilter.class, type, null);
} catch (NamedObjectNotFoundException e) {
throw new ParsingException(
new XContentLocation(e.getLineNumber(), e.getColumnNumber()), "Unknown interval filter [" + type + "]", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.elasticsearch.script.ScriptContext;

/**
* Base class for scripts used as interval filters, see {@link IntervalsSourceProvider.IntervalFilter}
* Base class for scripts used as interval filters, see {@link IntervalFilter}
*/
public abstract class IntervalFilterScript {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,18 @@
import org.elasticsearch.common.io.stream.NamedWriteable;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.NamedObjectNotFoundException;
import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentLocation;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.analysis.NamedAnalyzer;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.script.Script;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Set;

Expand Down Expand Up @@ -74,23 +72,16 @@ public abstract class IntervalsSourceProvider implements NamedWriteable, ToXCont

public static IntervalsSourceProvider fromXContent(XContentParser parser) throws IOException {
assert parser.currentToken() == XContentParser.Token.FIELD_NAME;
switch (parser.currentName()) {
case "match":
return Match.fromXContent(parser);
case "any_of":
return Disjunction.fromXContent(parser);
case "all_of":
return Combine.fromXContent(parser);
case "prefix":
return Prefix.fromXContent(parser);
case "wildcard":
return Wildcard.fromXContent(parser);
}
throw new ParsingException(parser.getTokenLocation(),
"Unknown interval type [" + parser.currentName() + "], expecting one of [match, any_of, all_of, prefix]");
String name = parser.currentName();
try {
return parser.namedObject(IntervalsSourceProvider.class, name, null);
} catch (NamedObjectNotFoundException e) {
throw new ParsingException(
new XContentLocation(e.getLineNumber(), e.getColumnNumber()), "Unknown interval type [" + name + "]", e);
}
}

private static IntervalsSourceProvider parseInnerIntervals(XContentParser parser) throws IOException {
public static IntervalsSourceProvider parseInnerIntervals(XContentParser parser) throws IOException {
if (parser.nextToken() != XContentParser.Token.FIELD_NAME) {
throw new ParsingException(parser.getTokenLocation(), "Expected [FIELD_NAME] but got [" + parser.currentToken() + "]");
}
Expand Down Expand Up @@ -126,7 +117,7 @@ public Match(StreamInput in) throws IOException {
this.maxGaps = in.readVInt();
this.ordered = in.readBoolean();
this.analyzer = in.readOptionalString();
this.filter = in.readOptionalWriteable(IntervalFilter::new);
this.filter = in.readOptionalNamedWriteable(IntervalFilter.class);
if (in.getVersion().onOrAfter(Version.V_7_2_0)) {
this.useField = in.readOptionalString();
}
Expand Down Expand Up @@ -192,7 +183,7 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(maxGaps);
out.writeBoolean(ordered);
out.writeOptionalString(analyzer);
out.writeOptionalWriteable(filter);
out.writeOptionalNamedWriteable(filter);
if (out.getVersion().onOrAfter(Version.V_7_2_0)) {
out.writeOptionalString(useField);
}
Expand Down Expand Up @@ -255,7 +246,7 @@ public Disjunction(List<IntervalsSourceProvider> subSources, IntervalFilter filt

public Disjunction(StreamInput in) throws IOException {
this.subSources = in.readNamedWriteableList(IntervalsSourceProvider.class);
this.filter = in.readOptionalWriteable(IntervalFilter::new);
this.filter = in.readOptionalNamedWriteable(IntervalFilter.class);
}

@Override
Expand Down Expand Up @@ -299,7 +290,7 @@ public String getWriteableName() {
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeNamedWriteableList(subSources);
out.writeOptionalWriteable(filter);
out.writeOptionalNamedWriteable(filter);
}

@Override
Expand Down Expand Up @@ -357,7 +348,7 @@ public Combine(StreamInput in) throws IOException {
this.ordered = in.readBoolean();
this.subSources = in.readNamedWriteableList(IntervalsSourceProvider.class);
this.maxGaps = in.readInt();
this.filter = in.readOptionalWriteable(IntervalFilter::new);
this.filter = in.readOptionalNamedWriteable(IntervalFilter.class);
}

@Override
Expand Down Expand Up @@ -404,7 +395,7 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeBoolean(ordered);
out.writeNamedWriteableList(subSources);
out.writeInt(maxGaps);
out.writeOptionalWriteable(filter);
out.writeOptionalNamedWriteable(filter);
}

@Override
Expand Down Expand Up @@ -667,12 +658,12 @@ public static Wildcard fromXContent(XContentParser parser) throws IOException {
}
}

static class ScriptFilterSource extends FilteredIntervalsSource {
public static class ScriptFilterSource extends FilteredIntervalsSource {

final IntervalFilterScript script;
IntervalFilterScript.Interval interval = new IntervalFilterScript.Interval();

ScriptFilterSource(IntervalsSource in, String name, IntervalFilterScript script) {
public ScriptFilterSource(IntervalsSource in, String name, IntervalFilterScript script) {
super("FILTER(" + name + ")", in);
this.script = script;
}
Expand All @@ -683,133 +674,4 @@ protected boolean accept(IntervalIterator it) {
return script.execute(interval);
}
}

public static class IntervalFilter implements ToXContentObject, Writeable {

public static final String NAME = "filter";

private final String type;
private final IntervalsSourceProvider filter;
private final Script script;

public IntervalFilter(IntervalsSourceProvider filter, String type) {
this.filter = filter;
this.type = type.toLowerCase(Locale.ROOT);
this.script = null;
}

IntervalFilter(Script script) {
this.script = script;
this.type = "script";
this.filter = null;
}

public IntervalFilter(StreamInput in) throws IOException {
this.type = in.readString();
this.filter = in.readOptionalNamedWriteable(IntervalsSourceProvider.class);
if (in.readBoolean()) {
this.script = new Script(in);
}
else {
this.script = null;
}
}

public IntervalsSource filter(IntervalsSource input, QueryShardContext context, MappedFieldType fieldType) throws IOException {
if (script != null) {
IntervalFilterScript ifs = context.getScriptService().compile(script, IntervalFilterScript.CONTEXT).newInstance();
return new ScriptFilterSource(input, script.getIdOrCode(), ifs);
}
IntervalsSource filterSource = filter.getSource(context, fieldType);
switch (type) {
case "containing":
return Intervals.containing(input, filterSource);
case "contained_by":
return Intervals.containedBy(input, filterSource);
case "not_containing":
return Intervals.notContaining(input, filterSource);
case "not_contained_by":
return Intervals.notContainedBy(input, filterSource);
case "overlapping":
return Intervals.overlapping(input, filterSource);
case "not_overlapping":
return Intervals.nonOverlapping(input, filterSource);
case "before":
return Intervals.before(input, filterSource);
case "after":
return Intervals.after(input, filterSource);
default:
throw new IllegalArgumentException("Unknown filter type [" + type + "]");
}
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
IntervalFilter that = (IntervalFilter) o;
return Objects.equals(type, that.type) &&
Objects.equals(filter, that.filter);
}

@Override
public int hashCode() {
return Objects.hash(type, filter);
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(type);
out.writeOptionalNamedWriteable(filter);
if (script == null) {
out.writeBoolean(false);
}
else {
out.writeBoolean(true);
script.writeTo(out);
}
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(type);
builder.startObject();
filter.toXContent(builder, params);
builder.endObject();
builder.endObject();
return builder;
}

public static IntervalFilter fromXContent(XContentParser parser) throws IOException {
if (parser.nextToken() != XContentParser.Token.FIELD_NAME) {
throw new ParsingException(parser.getTokenLocation(), "Expected [FIELD_NAME] but got [" + parser.currentToken() + "]");
}
String type = parser.currentName();
if (Script.SCRIPT_PARSE_FIELD.match(type, parser.getDeprecationHandler())) {
Script script = Script.parse(parser);
if (parser.nextToken() != XContentParser.Token.END_OBJECT) {
throw new ParsingException(parser.getTokenLocation(), "Expected [END_OBJECT] but got [" + parser.currentToken() + "]");
}
return new IntervalFilter(script);
}
if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
throw new ParsingException(parser.getTokenLocation(), "Expected [START_OBJECT] but got [" + parser.currentToken() + "]");
}
if (parser.nextToken() != XContentParser.Token.FIELD_NAME) {
throw new ParsingException(parser.getTokenLocation(), "Expected [FIELD_NAME] but got [" + parser.currentToken() + "]");
}
IntervalsSourceProvider intervals = IntervalsSourceProvider.fromXContent(parser);
if (parser.nextToken() != XContentParser.Token.END_OBJECT) {
throw new ParsingException(parser.getTokenLocation(), "Expected [END_OBJECT] but got [" + parser.currentToken() + "]");
}
if (parser.nextToken() != XContentParser.Token.END_OBJECT) {
throw new ParsingException(parser.getTokenLocation(), "Expected [END_OBJECT] but got [" + parser.currentToken() + "]");
}
return new IntervalFilter(intervals, type);
}
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.index.query.intervals;

import org.apache.lucene.queries.intervals.Intervals;
import org.apache.lucene.queries.intervals.IntervalsSource;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.IntervalsSourceProvider;

import java.io.IOException;

public class AfterIntervalFilter extends SourceProviderIntervalFilter {
public static final String NAME = "after";

public AfterIntervalFilter(final IntervalsSourceProvider filter) {
super(NAME, filter);
}

public AfterIntervalFilter(final StreamInput in) throws IOException {
super(NAME, in);
}

@Override
public IntervalsSource getIntervalsSource(final IntervalsSource input, final IntervalsSource filterSource) throws IOException {
return Intervals.after(input, filterSource);
}

public static AfterIntervalFilter fromXContent(XContentParser parser) throws IOException {
return fromXContent(parser, AfterIntervalFilter::new);
}
}
Loading