Skip to content

Commit e784706

Browse files
authored
Allow typed_keys for search application Search API (#108007)
* Allow typed_keys for search application Search API * Update docs/changelog/108007.yaml * Use RestSearchAction.TYPED_KEYS_PARAM
1 parent f4fac1e commit e784706

File tree

6 files changed

+110
-0
lines changed

6 files changed

+110
-0
lines changed

docs/changelog/108007.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 108007
2+
summary: Allow `typed_keys` for search application Search API
3+
area: Application
4+
type: feature
5+
issues: []

docs/reference/search-application/apis/search-application-search.asciidoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ Unspecified template parameters will be assigned their default values (if applic
2323
Requires read privileges on the backing alias of the search application.
2424

2525
[[search-application-search-path-params]]
26+
==== {api-path-parms-title}
27+
28+
`typed_keys`::
29+
(Optional, Boolean) If `true`, aggregation and suggester names are prefixed
30+
by their respective types in the response. Defaults to `false`.
2631

2732
[[search-application-search-request-body]]
2833
==== {api-request-body-title}

rest-api-spec/src/main/resources/rest-api-spec/api/search_application.search.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@
3434
"body": {
3535
"description": "Search parameters, including template parameters that override defaults",
3636
"required": false
37+
},
38+
"params": {
39+
"typed_keys":{
40+
"type":"boolean",
41+
"default":false,
42+
"description": "Specify whether aggregation and suggester names should be prefixed by their respective types in the response"
43+
}
3744
}
3845
}
3946
}

x-pack/plugin/ent-search/qa/rest/roles.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ user:
2525
"test-index1",
2626
"test-search-application",
2727
"test-search-application-1",
28+
"test-search-application-with-aggs",
2829
"test-search-application-with-list",
2930
"test-search-application-with-list-invalid",
3031
".elastic-connectors-v1",

x-pack/plugin/ent-search/qa/rest/src/yamlRestTest/resources/rest-api-spec/test/entsearch/search/55_search_application_search.yml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,34 @@ setup:
111111
boost: 3
112112
lang: "mustache"
113113

114+
- do:
115+
search_application.put:
116+
name: test-search-application-with-aggs
117+
body:
118+
indices: [ "test-search-index1", "test-search-index2" ]
119+
analytics_collection_name: "test-analytics"
120+
template:
121+
script:
122+
source:
123+
query:
124+
term:
125+
"{{field_name}}": "{{field_value}}"
126+
aggs:
127+
my_agg:
128+
value_count:
129+
field: "field1.keyword"
130+
params:
131+
field_name: field1
132+
field_value: value1
133+
dictionary:
134+
additionalProperties: false
135+
required: [ "field_name" ]
136+
properties:
137+
field_name:
138+
type: string
139+
field_value:
140+
type: string
141+
114142
- do:
115143
index:
116144
index: test-search-index1
@@ -151,6 +179,11 @@ teardown:
151179
name: test-search-application-with-list-invalid
152180
ignore: 404
153181

182+
- do:
183+
search_application.delete:
184+
name: test-search-application-with-aggs
185+
ignore: 404
186+
154187
- do:
155188
indices.delete:
156189
index: test-search-index1
@@ -318,3 +351,54 @@ teardown:
318351
- name: field3
319352
boost: 3
320353

354+
---
355+
"Search Application search with typed keys includes type prefix in aggregation names":
356+
- skip:
357+
features: headers
358+
359+
- do:
360+
headers: { Authorization: "Basic ZW50c2VhcmNoLXVzZXI6ZW50c2VhcmNoLXVzZXItcGFzc3dvcmQ=" } # user
361+
search_application.search:
362+
name: test-search-application-with-aggs
363+
typed_keys: true
364+
body:
365+
params:
366+
field_name: field2
367+
368+
- match: { hits.total.value: 1 }
369+
- match: { hits.hits.0._id: "doc1" }
370+
- match: { aggregations.value_count#my_agg.value: 1 }
371+
372+
---
373+
"Search Application search with typed keys set to false returns aggregations without type prefix":
374+
- skip:
375+
features: headers
376+
377+
- do:
378+
headers: { Authorization: "Basic ZW50c2VhcmNoLXVzZXI6ZW50c2VhcmNoLXVzZXItcGFzc3dvcmQ=" } # user
379+
search_application.search:
380+
name: test-search-application-with-aggs
381+
body:
382+
params:
383+
field_name: field2
384+
385+
- match: { hits.total.value: 1 }
386+
- match: { hits.hits.0._id: "doc1" }
387+
- match: { aggregations.my_agg.value: 1 }
388+
389+
---
390+
"Search Application search without typed keys returns aggregations without type prefix":
391+
- skip:
392+
features: headers
393+
394+
- do:
395+
headers: { Authorization: "Basic ZW50c2VhcmNoLXVzZXI6ZW50c2VhcmNoLXVzZXItcGFzc3dvcmQ=" } # user
396+
search_application.search:
397+
name: test-search-application-with-aggs
398+
body:
399+
params:
400+
field_name: field2
401+
402+
- match: { hits.total.value: 1 }
403+
- match: { hits.hits.0._id: "doc1" }
404+
- match: { aggregations.my_agg.value: 1 }

x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/search/action/RestQuerySearchApplicationAction.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414
import org.elasticsearch.rest.ServerlessScope;
1515
import org.elasticsearch.rest.action.RestCancellableNodeClient;
1616
import org.elasticsearch.rest.action.RestRefCountedChunkedToXContentListener;
17+
import org.elasticsearch.rest.action.search.RestSearchAction;
1718
import org.elasticsearch.xpack.application.EnterpriseSearch;
1819
import org.elasticsearch.xpack.application.EnterpriseSearchBaseRestHandler;
1920
import org.elasticsearch.xpack.application.utils.LicenseUtils;
2021

2122
import java.io.IOException;
2223
import java.util.List;
24+
import java.util.Set;
2325

2426
import static org.elasticsearch.rest.RestRequest.Method.GET;
2527
import static org.elasticsearch.rest.RestRequest.Method.POST;
@@ -31,6 +33,7 @@ public RestQuerySearchApplicationAction(XPackLicenseState licenseState) {
3133
}
3234

3335
public static final String ENDPOINT_PATH = "/" + EnterpriseSearch.SEARCH_APPLICATION_API_ENDPOINT + "/{name}" + "/_search";
36+
public static final Set<String> RESPONSE_PARAMS = Set.of(RestSearchAction.TYPED_KEYS_PARAM);
3437

3538
@Override
3639
public String getName() {
@@ -56,4 +59,9 @@ protected RestChannelConsumer innerPrepareRequest(RestRequest restRequest, NodeC
5659
cancelClient.execute(QuerySearchApplicationAction.INSTANCE, request, new RestRefCountedChunkedToXContentListener<>(channel));
5760
};
5861
}
62+
63+
@Override
64+
protected Set<String> responseParams() {
65+
return RESPONSE_PARAMS;
66+
}
5967
}

0 commit comments

Comments
 (0)