Skip to content

Commit d59dbe4

Browse files
Merge branch 'main' into skip-can-match-on-batched
2 parents 9e840c7 + 0700b24 commit d59dbe4

File tree

21 files changed

+2675
-38
lines changed

21 files changed

+2675
-38
lines changed

qa/smoke-test-http/src/internalClusterTest/java/org/elasticsearch/http/SearchRestCancellationIT.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
import org.apache.http.nio.entity.NByteArrayEntity;
1313
import org.apache.logging.log4j.LogManager;
1414
import org.apache.lucene.util.SetOnce;
15-
import org.elasticsearch.action.admin.cluster.node.info.NodeInfo;
16-
import org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse;
1715
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse;
1816
import org.elasticsearch.action.bulk.BulkRequestBuilder;
1917
import org.elasticsearch.action.search.MultiSearchRequest;
@@ -46,7 +44,6 @@
4644
import java.util.ArrayList;
4745
import java.util.Collection;
4846
import java.util.Collections;
49-
import java.util.HashMap;
5047
import java.util.List;
5148
import java.util.Map;
5249
import java.util.concurrent.CancellationException;
@@ -94,7 +91,7 @@ public void testAutomaticCancellationMultiSearchDuringQueryPhase() throws Except
9491
}
9592

9693
void verifyCancellationDuringQueryPhase(String searchAction, Request searchRequest) throws Exception {
97-
Map<String, String> nodeIdToName = readNodesInfo();
94+
Map<String, String> nodeIdToName = nodeIdsToNames();
9895

9996
List<ScriptedBlockPlugin> plugins = initBlockFactory();
10097
indexTestData();
@@ -137,7 +134,7 @@ public void testAutomaticCancellationMultiSearchDuringFetchPhase() throws Except
137134
}
138135

139136
void verifyCancellationDuringFetchPhase(String searchAction, Request searchRequest) throws Exception {
140-
Map<String, String> nodeIdToName = readNodesInfo();
137+
Map<String, String> nodeIdToName = nodeIdsToNames();
141138

142139
List<ScriptedBlockPlugin> plugins = initBlockFactory();
143140
indexTestData();
@@ -153,16 +150,6 @@ void verifyCancellationDuringFetchPhase(String searchAction, Request searchReque
153150
expectThrows(CancellationException.class, future::actionGet);
154151
}
155152

156-
private static Map<String, String> readNodesInfo() {
157-
Map<String, String> nodeIdToName = new HashMap<>();
158-
NodesInfoResponse nodesInfoResponse = clusterAdmin().prepareNodesInfo().get();
159-
assertFalse(nodesInfoResponse.hasFailures());
160-
for (NodeInfo node : nodesInfoResponse.getNodes()) {
161-
nodeIdToName.put(node.getNode().getId(), node.getNode().getName());
162-
}
163-
return nodeIdToName;
164-
}
165-
166153
private static void ensureSearchTaskIsCancelled(String transportAction, Function<String, String> nodeIdToName) throws Exception {
167154
SetOnce<TaskInfo> searchTask = new SetOnce<>();
168155
ListTasksResponse listTasksResponse = clusterAdmin().prepareListTasks().get();

test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@
193193
import java.util.Arrays;
194194
import java.util.Collection;
195195
import java.util.Collections;
196+
import java.util.HashMap;
196197
import java.util.HashSet;
197198
import java.util.IdentityHashMap;
198199
import java.util.List;
@@ -1207,6 +1208,19 @@ public static String getNodeId(String nodeName) {
12071208
return internalCluster().getInstance(ClusterService.class, nodeName).localNode().getId();
12081209
}
12091210

1211+
/**
1212+
* @return A map of the cluster node Ids to their node names.
1213+
*/
1214+
public static Map<String, String> nodeIdsToNames() {
1215+
var names = internalCluster().getNodeNames();
1216+
Map<String, String> nodeIdsToNames = new HashMap<>();
1217+
for (var name : names) {
1218+
nodeIdsToNames.put(getNodeId(name), name);
1219+
}
1220+
return nodeIdsToNames;
1221+
1222+
}
1223+
12101224
/**
12111225
* Waits until at least a give number of document is visible for searchers
12121226
*

test/framework/src/main/java/org/elasticsearch/test/InternalTestCluster.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2314,6 +2314,11 @@ public int numMasterNodes() {
23142314
return filterNodes(nodes, NodeAndClient::isMasterEligible).size();
23152315
}
23162316

2317+
public Set<String> masterEligibleNodeNames() {
2318+
var masterEligibleNodes = filterNodes(nodes, NodeAndClient::isMasterEligible);
2319+
return masterEligibleNodes.stream().map(nodeAndClient -> nodeAndClient.name).collect(Collectors.toSet());
2320+
}
2321+
23172322
public void setDisruptionScheme(ServiceDisruptionScheme scheme) {
23182323
assert activeDisruptionScheme == null
23192324
: "there is already and active disruption [" + activeDisruptionScheme + "]. call clearDisruptionScheme first";

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/common/MapPathExtractor.java

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
import org.elasticsearch.common.Strings;
1111

1212
import java.util.ArrayList;
13+
import java.util.LinkedHashSet;
1314
import java.util.List;
1415
import java.util.Map;
16+
import java.util.Set;
1517
import java.util.regex.Pattern;
1618

1719
/**
@@ -78,6 +80,8 @@
7880
* [1, 2]
7981
* ]
8082
* }
83+
*
84+
* The array field names would be {@code ["embeddings", "embedding"}
8185
* </pre>
8286
*
8387
* This implementation differs from JSONPath when handling a list of maps. JSONPath will flatten the result and return a single array.
@@ -123,10 +127,28 @@ public class MapPathExtractor {
123127
private static final String DOLLAR = "$";
124128

125129
// default for testing
126-
static final Pattern dotFieldPattern = Pattern.compile("^\\.([^.\\[]+)(.*)");
127-
static final Pattern arrayWildcardPattern = Pattern.compile("^\\[\\*\\](.*)");
130+
static final Pattern DOT_FIELD_PATTERN = Pattern.compile("^\\.([^.\\[]+)(.*)");
131+
static final Pattern ARRAY_WILDCARD_PATTERN = Pattern.compile("^\\[\\*\\](.*)");
132+
public static final String UNKNOWN_FIELD_NAME = "unknown";
133+
134+
/**
135+
* A result object that tries to match up the field names parsed from the passed in path and the result
136+
* extracted from the passed in map.
137+
* @param extractedObject represents the extracted result from the map
138+
* @param traversedFields a list of field names in order as they're encountered while navigating through the nested objects
139+
*/
140+
public record Result(Object extractedObject, List<String> traversedFields) {
141+
public String getArrayFieldName(int index) {
142+
// if the index is out of bounds we'll return a default value
143+
if (traversedFields.size() <= index || index < 0) {
144+
return UNKNOWN_FIELD_NAME;
145+
}
146+
147+
return traversedFields.get(index);
148+
}
149+
}
128150

129-
public static Object extract(Map<String, Object> data, String path) {
151+
public static Result extract(Map<String, Object> data, String path) {
130152
if (data == null || data.isEmpty() || path == null || path.trim().isEmpty()) {
131153
return null;
132154
}
@@ -139,16 +161,41 @@ public static Object extract(Map<String, Object> data, String path) {
139161
throw new IllegalArgumentException(Strings.format("Path [%s] must start with a dollar sign ($)", cleanedPath));
140162
}
141163

142-
return navigate(data, cleanedPath);
164+
var fieldNames = new LinkedHashSet<String>();
165+
166+
return new Result(navigate(data, cleanedPath, new FieldNameInfo("", "", fieldNames)), fieldNames.stream().toList());
143167
}
144168

145-
private static Object navigate(Object current, String remainingPath) {
146-
if (current == null || remainingPath == null || remainingPath.isEmpty()) {
169+
private record FieldNameInfo(String currentPath, String fieldName, Set<String> traversedFields) {
170+
void addTraversedField(String fieldName) {
171+
traversedFields.add(createPath(fieldName));
172+
}
173+
174+
void addCurrentField() {
175+
traversedFields.add(currentPath);
176+
}
177+
178+
FieldNameInfo descend(String newFieldName) {
179+
var newLocation = createPath(newFieldName);
180+
return new FieldNameInfo(newLocation, newFieldName, traversedFields);
181+
}
182+
183+
private String createPath(String newFieldName) {
184+
if (Strings.isNullOrEmpty(currentPath)) {
185+
return newFieldName;
186+
} else {
187+
return currentPath + "." + newFieldName;
188+
}
189+
}
190+
}
191+
192+
private static Object navigate(Object current, String remainingPath, FieldNameInfo fieldNameInfo) {
193+
if (current == null || Strings.isNullOrEmpty(remainingPath)) {
147194
return current;
148195
}
149196

150-
var dotFieldMatcher = dotFieldPattern.matcher(remainingPath);
151-
var arrayWildcardMatcher = arrayWildcardPattern.matcher(remainingPath);
197+
var dotFieldMatcher = DOT_FIELD_PATTERN.matcher(remainingPath);
198+
var arrayWildcardMatcher = ARRAY_WILDCARD_PATTERN.matcher(remainingPath);
152199

153200
if (dotFieldMatcher.matches()) {
154201
String field = dotFieldMatcher.group(1);
@@ -168,7 +215,12 @@ private static Object navigate(Object current, String remainingPath) {
168215
throw new IllegalArgumentException(Strings.format("Unable to find field [%s] in map", field));
169216
}
170217

171-
return navigate(currentMap.get(field), nextPath);
218+
// Handle the case where the path was $.result.text or $.result[*].key
219+
if (Strings.isNullOrEmpty(nextPath)) {
220+
fieldNameInfo.addTraversedField(field);
221+
}
222+
223+
return navigate(currentMap.get(field), nextPath, fieldNameInfo.descend(field));
172224
} else {
173225
throw new IllegalArgumentException(
174226
Strings.format(
@@ -182,10 +234,12 @@ private static Object navigate(Object current, String remainingPath) {
182234
} else if (arrayWildcardMatcher.matches()) {
183235
String nextPath = arrayWildcardMatcher.group(1);
184236
if (current instanceof List<?> list) {
237+
fieldNameInfo.addCurrentField();
238+
185239
List<Object> results = new ArrayList<>();
186240

187241
for (Object item : list) {
188-
Object result = navigate(item, nextPath);
242+
Object result = navigate(item, nextPath, fieldNameInfo);
189243
if (result != null) {
190244
results.add(result);
191245
}

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/external/http/retry/ErrorResponse.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,16 @@ public String getErrorMessage() {
3434
public boolean errorStructureFound() {
3535
return errorStructureFound;
3636
}
37+
38+
@Override
39+
public boolean equals(Object o) {
40+
if (o == null || getClass() != o.getClass()) return false;
41+
ErrorResponse that = (ErrorResponse) o;
42+
return errorStructureFound == that.errorStructureFound && Objects.equals(errorMessage, that.errorMessage);
43+
}
44+
45+
@Override
46+
public int hashCode() {
47+
return Objects.hash(errorMessage, errorStructureFound);
48+
}
3749
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.inference.services.custom;
9+
10+
public class CustomServiceSettings {
11+
public static final String NAME = "custom_service_settings";
12+
public static final String URL = "url";
13+
public static final String HEADERS = "headers";
14+
public static final String REQUEST = "request";
15+
public static final String REQUEST_CONTENT = "content";
16+
public static final String RESPONSE = "response";
17+
public static final String JSON_PARSER = "json_parser";
18+
public static final String ERROR_PARSER = "error_parser";
19+
}

0 commit comments

Comments
 (0)