Skip to content

Commit bc2319d

Browse files
committed
Add autocomplete hints for ESQL function parameters
1 parent f9c72c1 commit bc2319d

File tree

8 files changed

+100
-12
lines changed

8 files changed

+100
-12
lines changed

docs/reference/query-languages/esql/_snippets/functions/parameters/text_embedding.md

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/query-languages/esql/kibana/definition/functions/text_embedding.json

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/LookupJoinTypesIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,8 +1032,8 @@ private static void saveJoinTypes(Supplier<Set<DocsV3Support.TypeSignature>> sig
10321032
return;
10331033
}
10341034
ArrayList<EsqlFunctionRegistry.ArgSignature> args = new ArrayList<>();
1035-
args.add(new EsqlFunctionRegistry.ArgSignature("field from the left index", null, null, false, false));
1036-
args.add(new EsqlFunctionRegistry.ArgSignature("field from the lookup index", null, null, false, false));
1035+
args.add(new EsqlFunctionRegistry.ArgSignature("field from the left index", null, null, false, null, false));
1036+
args.add(new EsqlFunctionRegistry.ArgSignature("field from the lookup index", null, null, false, null, false));
10371037
DocsV3Support.CommandsDocsSupport docs = new DocsV3Support.CommandsDocsSupport(
10381038
"lookup-join",
10391039
LookupJoinTypesIT.class,

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/EsqlFunctionRegistry.java

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -577,24 +577,44 @@ public static String normalizeName(String name) {
577577
}
578578

579579
public static class ArgSignature {
580+
581+
public record AutocompleteHint(String entityType, Map<String, String> constraints) {}
582+
580583
protected final String name;
581584
protected final String[] type;
582585
protected final String description;
583586
protected final boolean optional;
584587
protected final boolean variadic;
585588
protected final DataType targetDataType;
586-
587-
public ArgSignature(String name, String[] type, String description, boolean optional, boolean variadic, DataType targetDataType) {
589+
protected final AutocompleteHint autocompleteHint;
590+
591+
public ArgSignature(
592+
String name,
593+
String[] type,
594+
String description,
595+
boolean optional,
596+
boolean variadic,
597+
AutocompleteHint autocompleteHint,
598+
DataType targetDataType
599+
) {
588600
this.name = name;
589601
this.type = type;
590602
this.description = description;
591603
this.optional = optional;
592604
this.variadic = variadic;
593605
this.targetDataType = targetDataType;
606+
this.autocompleteHint = autocompleteHint;
594607
}
595608

596-
public ArgSignature(String name, String[] type, String description, boolean optional, boolean variadic) {
597-
this(name, type, description, optional, variadic, UNSUPPORTED);
609+
public ArgSignature(
610+
String name,
611+
String[] type,
612+
String description,
613+
boolean optional,
614+
AutocompleteHint autocompleteHint,
615+
boolean variadic
616+
) {
617+
this(name, type, description, optional, variadic, autocompleteHint, UNSUPPORTED);
598618
}
599619

600620
public String name() {
@@ -650,7 +670,7 @@ public static class MapArgSignature extends ArgSignature {
650670
private final Map<String, MapEntryArgSignature> mapParams;
651671

652672
public MapArgSignature(String name, String description, boolean optional, Map<String, MapEntryArgSignature> mapParams) {
653-
super(name, new String[] { "map" }, description, optional, false);
673+
super(name, new String[] { "map" }, description, optional, null, false);
654674
this.mapParams = mapParams;
655675
}
656676

@@ -769,7 +789,25 @@ public static ArgSignature param(Param param, boolean variadic) {
769789
String[] type = removeUnderConstruction(param.type());
770790
String desc = param.description().replace('\n', ' ');
771791
DataType targetDataType = getTargetType(type);
772-
return new EsqlFunctionRegistry.ArgSignature(param.name(), type, desc, param.optional(), variadic, targetDataType);
792+
ArgSignature.AutocompleteHint autocompleteHint = null;
793+
if (param.autocompleteHint() != null && param.autocompleteHint().entityType() != Param.AutocompleteHint.ENTITY_TYPE.NONE) {
794+
Map<String, String> constraints = Arrays.stream(param.autocompleteHint().constraints())
795+
.collect(Collectors.toMap(Param.AutocompleteHint.Constraint::name, Param.AutocompleteHint.Constraint::value));
796+
autocompleteHint = new ArgSignature.AutocompleteHint(
797+
param.autocompleteHint().entityType().name().toLowerCase(Locale.ROOT),
798+
constraints
799+
);
800+
}
801+
802+
return new EsqlFunctionRegistry.ArgSignature(
803+
param.name(),
804+
type,
805+
desc,
806+
param.optional(),
807+
variadic,
808+
autocompleteHint,
809+
targetDataType
810+
);
773811
}
774812

775813
public static ArgSignature mapParam(MapParam mapParam) {
@@ -787,7 +825,7 @@ public static ArgSignature mapParam(MapParam mapParam) {
787825
}
788826

789827
public static ArgSignature paramWithoutAnnotation(String name) {
790-
return new EsqlFunctionRegistry.ArgSignature(name, new String[] { "?" }, "", false, false, UNSUPPORTED);
828+
return new EsqlFunctionRegistry.ArgSignature(name, new String[] { "?" }, "", false, false, null, UNSUPPORTED);
791829
}
792830

793831
/**

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/Param.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
package org.elasticsearch.xpack.esql.expression.function;
99

10+
import org.elasticsearch.core.Nullable;
11+
1012
import java.lang.annotation.ElementType;
1113
import java.lang.annotation.Retention;
1214
import java.lang.annotation.RetentionPolicy;
@@ -25,4 +27,28 @@
2527
String description() default "";
2628

2729
boolean optional() default false;
30+
31+
@Nullable
32+
AutocompleteHint autocompleteHint() default @AutocompleteHint(entityType = AutocompleteHint.ENTITY_TYPE.NONE);
33+
34+
@Retention(RetentionPolicy.RUNTIME)
35+
@Target(ElementType.PARAMETER)
36+
@interface AutocompleteHint {
37+
enum ENTITY_TYPE {
38+
NONE,
39+
INFERENCE_ENDPOINT,
40+
}
41+
42+
ENTITY_TYPE entityType();
43+
44+
Constraint[] constraints() default {};
45+
46+
@Retention(RetentionPolicy.RUNTIME)
47+
@Target(ElementType.PARAMETER)
48+
@interface Constraint {
49+
String name();
50+
51+
String value();
52+
}
53+
}
2854
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/inference/TextEmbedding.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ public TextEmbedding(
5656
@Param(
5757
name = InferenceFunction.INFERENCE_ID_PARAMETER_NAME,
5858
type = { "keyword" },
59-
description = "Identifier of the inference endpoint"
59+
description = "Identifier of the inference endpoint (must be of type text_embedding).",
60+
autocompleteHint = @Param.AutocompleteHint(
61+
entityType = Param.AutocompleteHint.ENTITY_TYPE.INFERENCE_ENDPOINT,
62+
constraints = { @Param.AutocompleteHint.Constraint(name = "task_type", value = "text_embedding") }
63+
)
6064
) Expression inferenceId
6165
) {
6266
super(source, List.of(inputText, inferenceId));

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/DocsV3Support.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,7 @@ private void renderDocs(FunctionDefinition definition) throws Exception {
622622
trueValue.type(),
623623
"The value that’s returned when no condition evaluates to `true`.",
624624
true,
625+
null,
625626
true
626627
);
627628
description = new EsqlFunctionRegistry.FunctionDescription(
@@ -1382,6 +1383,18 @@ void renderKibanaFunctionDefinition(
13821383
builder.field("optional", arg.optional());
13831384
String cleanedParamDesc = removeAppliesToBlocks(arg.description());
13841385
builder.field("description", cleanedParamDesc);
1386+
if (arg.autocompleteHint != null) {
1387+
builder.startObject("autocompleteHint");
1388+
builder.field("entityType", arg.autocompleteHint.entityType());
1389+
if (arg.autocompleteHint.constraints() != null && arg.autocompleteHint.constraints().size() > 0) {
1390+
builder.startObject("constraints");
1391+
for (Map.Entry<String, String> constraint : arg.autocompleteHint.constraints().entrySet()) {
1392+
builder.field(constraint.getKey(), constraint.getValue());
1393+
}
1394+
builder.endObject();
1395+
}
1396+
builder.endObject();
1397+
}
13851398
builder.endObject();
13861399
}
13871400
builder.endArray();

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/grouping/BucketTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ public static EsqlFunctionRegistry.ArgSignature patchKibanaSignature(EsqlFunctio
351351
arg.description(),
352352
false,
353353
arg.variadic(),
354+
null,
354355
arg.targetDataType()
355356
);
356357
}

0 commit comments

Comments
 (0)