Skip to content

Commit

Permalink
ES|QL Add serialization to fulltext predicate types (#109819)
Browse files Browse the repository at this point in the history
This commit adds serialization to fulltext predicate types, so that queries build atop these types can be sent to data nodes.
  • Loading branch information
ChrisHegarty committed Jun 18, 2024
1 parent 55431dd commit 3a41d8c
Show file tree
Hide file tree
Showing 9 changed files with 272 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,28 @@
*/
package org.elasticsearch.xpack.esql.core.expression.predicate.fulltext;

import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.xpack.esql.core.expression.Expression;
import org.elasticsearch.xpack.esql.core.expression.Nullability;
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.core.type.DataType;
import org.elasticsearch.xpack.esql.core.util.PlanStreamInput;
import org.elasticsearch.xpack.esql.core.util.PlanStreamOutput;

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

public abstract class FullTextPredicate extends Expression {

public static List<NamedWriteableRegistry.Entry> getNamedWriteables() {
return List.of(MatchQueryPredicate.ENTRY, MultiMatchQueryPredicate.ENTRY, StringQueryPredicate.ENTRY);
}

public enum Operator {
AND,
OR;
Expand All @@ -32,7 +43,7 @@ public org.elasticsearch.index.query.Operator toEs() {
// common properties
private final String analyzer;

FullTextPredicate(Source source, String query, String options, List<Expression> children) {
FullTextPredicate(Source source, String query, @Nullable String options, List<Expression> children) {
super(source, children);
this.query = query;
this.options = options;
Expand All @@ -41,6 +52,15 @@ public org.elasticsearch.index.query.Operator toEs() {
this.analyzer = optionMap.get("analyzer");
}

protected FullTextPredicate(StreamInput in) throws IOException {
this(
Source.readFrom((StreamInput & PlanStreamInput) in),
in.readString(),
in.readOptionalString(),
in.readCollectionAsList(input -> ((PlanStreamInput) in).readExpression())
);
}

public String query() {
return query;
}
Expand All @@ -67,6 +87,14 @@ public DataType dataType() {
return DataType.BOOLEAN;
}

@Override
public void writeTo(StreamOutput out) throws IOException {
source().writeTo(out);
out.writeString(query);
out.writeOptionalString(options);
out.writeCollection(children(), (o, v) -> ((PlanStreamOutput) o).writeExpression(v));
}

@Override
public int hashCode() {
return Objects.hash(query, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,39 @@
*/
package org.elasticsearch.xpack.esql.core.expression.predicate.fulltext;

import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.xpack.esql.core.expression.Expression;
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
import org.elasticsearch.xpack.esql.core.tree.Source;

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

import static java.util.Collections.singletonList;

public class MatchQueryPredicate extends FullTextPredicate {

public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(
Expression.class,
"MatchQueryPredicate",
MatchQueryPredicate::new
);

private final Expression field;

public MatchQueryPredicate(Source source, Expression field, String query, String options) {
super(source, query, options, singletonList(field));
this.field = field;
}

MatchQueryPredicate(StreamInput in) throws IOException {
super(in);
assert super.children().size() == 1;
field = super.children().get(0);
}

@Override
protected NodeInfo<MatchQueryPredicate> info() {
return NodeInfo.create(this, MatchQueryPredicate::new, field, query(), options());
Expand Down Expand Up @@ -51,4 +66,9 @@ public boolean equals(Object obj) {
}
return false;
}

@Override
public String getWriteableName() {
return ENTRY.name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
*/
package org.elasticsearch.xpack.esql.core.expression.predicate.fulltext;

import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.xpack.esql.core.expression.Expression;
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
import org.elasticsearch.xpack.esql.core.tree.Source;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -18,6 +22,12 @@

public class MultiMatchQueryPredicate extends FullTextPredicate {

public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(
Expression.class,
"MultiMatchQueryPredicate",
MultiMatchQueryPredicate::new
);

private final String fieldString;
private final Map<String, Float> fields;

Expand All @@ -28,6 +38,14 @@ public MultiMatchQueryPredicate(Source source, String fieldString, String query,
this.fields = FullTextUtils.parseFields(fieldString, source);
}

MultiMatchQueryPredicate(StreamInput in) throws IOException {
super(in);
assert super.children().isEmpty();
fieldString = in.readString();
// inferred
this.fields = FullTextUtils.parseFields(fieldString, source());
}

@Override
protected NodeInfo<MultiMatchQueryPredicate> info() {
return NodeInfo.create(this, MultiMatchQueryPredicate::new, fieldString, query(), options());
Expand All @@ -46,6 +64,12 @@ public Map<String, Float> fields() {
return fields;
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeString(fieldString);
}

@Override
public int hashCode() {
return Objects.hash(fieldString, super.hashCode());
Expand All @@ -59,4 +83,9 @@ public boolean equals(Object obj) {
}
return false;
}

@Override
public String getWriteableName() {
return ENTRY.name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,26 @@
*/
package org.elasticsearch.xpack.esql.core.expression.predicate.fulltext;

import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.xpack.esql.core.expression.Expression;
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
import org.elasticsearch.xpack.esql.core.tree.Source;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import static java.util.Collections.emptyList;

public final class StringQueryPredicate extends FullTextPredicate {

public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(
Expression.class,
"StringQueryPredicate",
StringQueryPredicate::new
);

private final Map<String, Float> fields;

public StringQueryPredicate(Source source, String query, String options) {
Expand All @@ -26,6 +35,12 @@ public StringQueryPredicate(Source source, String query, String options) {
this.fields = FullTextUtils.parseFields(optionMap(), source);
}

StringQueryPredicate(StreamInput in) throws IOException {
super(in);
assert super.children().isEmpty();
this.fields = FullTextUtils.parseFields(optionMap(), source());
}

@Override
protected NodeInfo<StringQueryPredicate> info() {
return NodeInfo.create(this, StringQueryPredicate::new, query(), options());
Expand All @@ -39,4 +54,9 @@ public Expression replaceChildren(List<Expression> newChildren) {
public Map<String, Float> fields() {
return fields;
}

@Override
public String getWriteableName() {
return ENTRY.name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.elasticsearch.xpack.esql.core.expression.NamedExpression;
import org.elasticsearch.xpack.esql.core.expression.Order;
import org.elasticsearch.xpack.esql.core.expression.function.scalar.ScalarFunction;
import org.elasticsearch.xpack.esql.core.expression.predicate.fulltext.FullTextPredicate;
import org.elasticsearch.xpack.esql.core.expression.predicate.logical.And;
import org.elasticsearch.xpack.esql.core.expression.predicate.logical.BinaryLogic;
import org.elasticsearch.xpack.esql.core.expression.predicate.logical.Not;
Expand Down Expand Up @@ -368,6 +369,9 @@ public static List<PlanNameRegistry.Entry> namedTypeEntries() {
for (NamedWriteableRegistry.Entry e : NamedExpression.getNamedWriteables()) {
entries.add(of(Expression.class, e));
}
for (NamedWriteableRegistry.Entry e : FullTextPredicate.getNamedWriteables()) {
entries.add(of(Expression.class, e));
}
entries.add(of(Expression.class, UnsupportedAttribute.ENTRY));
entries.add(of(Expression.class, Literal.ENTRY));
entries.add(of(Expression.class, org.elasticsearch.xpack.esql.expression.Order.ENTRY));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.esql.expression.predicate.operator.fulltext;

import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.xpack.esql.core.expression.predicate.fulltext.FullTextPredicate;
import org.elasticsearch.xpack.esql.expression.AbstractExpressionSerializationTests;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public abstract class AbstractFulltextSerializationTests<T extends FullTextPredicate> extends AbstractExpressionSerializationTests<T> {

static final String OPTION_DELIMITER = ";";

@Override
protected List<NamedWriteableRegistry.Entry> getNamedWriteables() {
return FullTextPredicate.getNamedWriteables();
}

String randomOptionOrNull() {
if (randomBoolean()) {
return null;
}
HashMap<String, String> options = new HashMap<>();
int maxOptions = randomInt(8);
for (int i = 0; i < maxOptions; i++) {
var opt = randomIndividualOption();
options.computeIfAbsent(opt.getKey(), k -> opt.getValue()); // no duplicate options
}
return options.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining(OPTION_DELIMITER));
}

Map.Entry<String, String> randomIndividualOption() {
return Map.entry(randomAlphaOfLength(randomIntBetween(1, 4)), randomAlphaOfLength(randomIntBetween(1, 4)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.esql.expression.predicate.operator.fulltext;

import org.elasticsearch.xpack.esql.core.expression.predicate.fulltext.MatchQueryPredicate;
import org.elasticsearch.xpack.esql.expression.AbstractExpressionSerializationTests;

import java.io.IOException;

public class MatchQuerySerializationTests extends AbstractFulltextSerializationTests<MatchQueryPredicate> {

@Override
protected final MatchQueryPredicate createTestInstance() {
return new MatchQueryPredicate(randomSource(), randomChild(), randomAlphaOfLength(randomIntBetween(1, 16)), randomOptionOrNull());
}

@Override
protected MatchQueryPredicate mutateInstance(MatchQueryPredicate instance) throws IOException {
var field = instance.field();
var query = instance.query();
var options = instance.options();
switch (between(0, 2)) {
case 0 -> field = randomValueOtherThan(field, AbstractExpressionSerializationTests::randomChild);
case 1 -> query = randomValueOtherThan(query, () -> randomAlphaOfLength(randomIntBetween(1, 16)));
case 2 -> options = randomValueOtherThan(options, this::randomOptionOrNull);
}
return new MatchQueryPredicate(instance.source(), field, query, options);
}
}
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
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.esql.expression.predicate.operator.fulltext;

import org.elasticsearch.xpack.esql.core.expression.predicate.fulltext.MultiMatchQueryPredicate;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class MultiMatchQuerySerializationTests extends AbstractFulltextSerializationTests<MultiMatchQueryPredicate> {

@Override
protected final MultiMatchQueryPredicate createTestInstance() {
return new MultiMatchQueryPredicate(
randomSource(),
randomFieldString(),
randomAlphaOfLength(randomIntBetween(1, 16)),
randomOptionOrNull()
);
}

@Override
protected MultiMatchQueryPredicate mutateInstance(MultiMatchQueryPredicate instance) throws IOException {
var fieldString = instance.fieldString();
var query = instance.query();
var options = instance.options();
switch (between(0, 2)) {
case 0 -> fieldString = randomValueOtherThan(fieldString, this::randomFieldString);
case 1 -> query = randomValueOtherThan(query, () -> randomAlphaOfLength(randomIntBetween(1, 16)));
case 2 -> options = randomValueOtherThan(options, this::randomOptionOrNull);
}
return new MultiMatchQueryPredicate(instance.source(), fieldString, query, options);
}

String randomFieldString() {
if (randomBoolean()) {
return ""; // empty, no fields
}
HashMap<String, Float> fields = new HashMap<>();
int maxOptions = randomInt(4);
for (int i = 0; i < maxOptions; i++) {
var opt = randomIndividualField();
fields.computeIfAbsent(opt.getKey(), k -> opt.getValue()); // no duplicate fields
}
return fields.entrySet().stream().map(e -> e.getKey() + "^" + e.getValue()).collect(Collectors.joining(","));
}

Map.Entry<String, Float> randomIndividualField() {
return Map.entry(randomAlphaOfLength(randomIntBetween(1, 4)), randomFloat());
}
}
Loading

0 comments on commit 3a41d8c

Please sign in to comment.