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

Make lucene's IntervalQuery available via the Query DSL #32406

Closed
wants to merge 4 commits 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,92 @@
package org.elasticsearch.index.query;

import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.intervals.IntervalQuery;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.MappedFieldType;

import java.io.IOException;
import java.util.Objects;

import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;

public class IntervalQueryBuilder extends AbstractQueryBuilder<IntervalQueryBuilder> {

public static final String NAME = "intervals";

private final String field;
private final IntervalsSourceProvider sourceProvider;

public IntervalQueryBuilder(String field, IntervalsSourceProvider sourceProvider) {
this.field = field;
this.sourceProvider = sourceProvider;
}

public IntervalQueryBuilder(StreamInput in) throws IOException {
super(in);
this.field = in.readString();
this.sourceProvider = in.readNamedWriteable(IntervalsSourceProvider.class);
}

@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeString(field);
out.writeNamedWriteable(sourceProvider);
}

@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
builder.field("field", field);
builder.field("source", sourceProvider);
printBoostAndQueryName(builder);
builder.endObject();
}

private static final ConstructingObjectParser<IntervalQueryBuilder, Void> PARSER
= new ConstructingObjectParser<>(NAME, args -> new IntervalQueryBuilder((String) args[0], (IntervalsSourceProvider) args[1]));
static {
PARSER.declareString(constructorArg(), new ParseField("field"));
PARSER.declareObject(constructorArg(), (parser, c) -> IntervalsSourceProvider.fromXContent(parser), new ParseField("source"));
PARSER.declareFloat(IntervalQueryBuilder::boost, new ParseField("boost"));
PARSER.declareString(IntervalQueryBuilder::queryName, new ParseField("_name"));
}

public static IntervalQueryBuilder fromXContent(XContentParser parser) throws IOException {
return PARSER.apply(parser, null);
}

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
MappedFieldType fieldType = context.fieldMapper(field);
if (fieldType == null) {
throw new IllegalArgumentException("Cannot create IntervalQuery over non-existent field [" + field + "]");
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we check if the field is indexed with positions and fail the query if not ?

if (fieldType.tokenized() == false ||
fieldType.indexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0) {
throw new IllegalArgumentException("Cannot create IntervalQuery over field [" + field + "] with no indexed positions");
}
return new IntervalQuery(field, sourceProvider.getSource(fieldType));
}

@Override
protected boolean doEquals(IntervalQueryBuilder other) {
return Objects.equals(field, other.field) && Objects.equals(sourceProvider, other.sourceProvider);
}

@Override
protected int doHashCode() {
return Objects.hash(field, sourceProvider);
}

@Override
public String getWriteableName() {
return NAME;
}
}
Loading