Skip to content

Commit

Permalink
bwca for unified search suggestions route (elastic#158796)
Browse files Browse the repository at this point in the history
## Summary

Makes unified search field suggestion route bwca

resolves elastic#157085

- [x] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
ppisljar and kibanamachine authored Jun 2, 2023
1 parent e3da05c commit e3c3ba8
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const setupValueSuggestionProvider = (
) => {
usageCollector?.trackRequest();
return core.http
.fetch<T>(`/api/kibana/suggestions/values/${index}`, {
.fetch<T>(`/internal/kibana/suggestions/values/${index}`, {
method: 'POST',
body: JSON.stringify({
query,
Expand All @@ -75,6 +75,7 @@ export const setupValueSuggestionProvider = (
method,
}),
signal,
version: '1',
})
.then((r) => {
usageCollector?.trackResult();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,55 +16,62 @@ import { termsEnumSuggestions } from './terms_enum';
import { termsAggSuggestions } from './terms_agg';

export function registerValueSuggestionsRoute(router: IRouter, config$: Observable<ConfigSchema>) {
router.post(
{
path: '/api/kibana/suggestions/values/{index}',
validate: {
params: schema.object(
{
index: schema.string(),
},
{ unknowns: 'allow' }
),
body: schema.object(
{
field: schema.string(),
query: schema.string(),
filters: schema.maybe(schema.any()),
fieldMeta: schema.maybe(schema.any()),
method: schema.maybe(
schema.oneOf([schema.literal('terms_agg'), schema.literal('terms_enum')])
router.versioned
.post({
path: '/internal/kibana/suggestions/values/{index}',
access: 'internal',
})
.addVersion(
{
version: '1',
validate: {
request: {
params: schema.object(
{
index: schema.string(),
},
{ unknowns: 'allow' }
),
body: schema.object(
{
field: schema.string(),
query: schema.string(),
filters: schema.maybe(schema.any()),
fieldMeta: schema.maybe(schema.any()),
method: schema.maybe(
schema.oneOf([schema.literal('terms_agg'), schema.literal('terms_enum')])
),
},
{ unknowns: 'allow' }
),
},
{ unknowns: 'allow' }
),
},
},
},
async (context, request, response) => {
const config = await firstValueFrom(config$);
const { field: fieldName, query, filters, fieldMeta, method } = request.body;
const { index } = request.params;
const abortSignal = getRequestAbortedSignal(request.events.aborted$);
const { savedObjects, elasticsearch } = await context.core;
async (context, request, response) => {
const config = await firstValueFrom(config$);
const { field: fieldName, query, filters, fieldMeta, method } = request.body;
const { index } = request.params;
const abortSignal = getRequestAbortedSignal(request.events.aborted$);
const { savedObjects, elasticsearch } = await context.core;

try {
const fn = method === 'terms_agg' ? termsAggSuggestions : termsEnumSuggestions;
const body = await fn(
config,
savedObjects.client,
elasticsearch.client.asCurrentUser,
index,
fieldName,
query,
filters,
fieldMeta,
abortSignal
);
return response.ok({ body });
} catch (e) {
const kbnErr = getKbnServerError(e);
return reportServerError(response, kbnErr);
try {
const fn = method === 'terms_agg' ? termsAggSuggestions : termsEnumSuggestions;
const body = await fn(
config,
savedObjects.client,
elasticsearch.client.asCurrentUser,
index,
fieldName,
query,
filters,
fieldMeta,
abortSignal
);
return response.ok({ body });
} catch (e) {
const kbnErr = getKbnServerError(e);
return reportServerError(response, kbnErr);
}
}
}
);
);
}
37 changes: 25 additions & 12 deletions test/api_integration/apis/suggestions/suggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/
import expect from '@kbn/expect';
import { ELASTIC_HTTP_VERSION_HEADER } from '@kbn/core-http-common';

export default function ({ getService }) {
const esArchiver = getService('esArchiver');
Expand All @@ -32,7 +33,8 @@ export default function ({ getService }) {
});
it('should return 200 without a query', () =>
supertest
.post('/api/kibana/suggestions/values/basic_index')
.post('/internal/kibana/suggestions/values/basic_index')
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.send({
field: 'baz.keyword',
query: '',
Expand All @@ -45,7 +47,8 @@ export default function ({ getService }) {

it('should return 200 without a query and with method set to terms_agg', () =>
supertest
.post('/api/kibana/suggestions/values/basic_index')
.post('/internal/kibana/suggestions/values/basic_index')
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.send({
field: 'baz.keyword',
method: 'terms_agg',
Expand All @@ -59,7 +62,8 @@ export default function ({ getService }) {

it('should return 200 without a query and with method set to terms_enum', () =>
supertest
.post('/api/kibana/suggestions/values/basic_index')
.post('/internal/kibana/suggestions/values/basic_index')
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.send({
field: 'baz.keyword',
method: 'terms_enum',
Expand All @@ -73,7 +77,8 @@ export default function ({ getService }) {

it('should return 200 with special characters', () =>
supertest
.post('/api/kibana/suggestions/values/basic_index')
.post('/internal/kibana/suggestions/values/basic_index')
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.send({
field: 'baz.keyword',
query: '<something?with:lots&of^ bad characters',
Expand All @@ -85,7 +90,8 @@ export default function ({ getService }) {

it('should support nested fields', () =>
supertest
.post('/api/kibana/suggestions/values/basic_index')
.post('/internal/kibana/suggestions/values/basic_index')
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.send({
field: 'nestedField.child',
query: 'nes',
Expand All @@ -94,7 +100,8 @@ export default function ({ getService }) {

it('should return 404 if index is not found', () =>
supertest
.post('/api/kibana/suggestions/values/not_found')
.post('/internal/kibana/suggestions/values/not_found')
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.send({
field: 'baz.keyword',
query: '1',
Expand All @@ -103,15 +110,17 @@ export default function ({ getService }) {

it('should return 400 without a query', () =>
supertest
.post('/api/kibana/suggestions/values/basic_index')
.post('/internal/kibana/suggestions/values/basic_index')
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.send({
field: 'baz.keyword',
})
.expect(400));

it('should return 400 with a bad method', () =>
supertest
.post('/api/kibana/suggestions/values/basic_index')
.post('/internal/kibana/suggestions/values/basic_index')
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.send({
field: 'baz.keyword',
query: '',
Expand All @@ -138,7 +147,8 @@ export default function ({ getService }) {

it('filter is applied on a document level with terms_agg', () =>
supertest
.post('/api/kibana/suggestions/values/logstash-*')
.post('/internal/kibana/suggestions/values/logstash-*')
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.send({
field: 'extension.raw',
query: '',
Expand All @@ -163,7 +173,8 @@ export default function ({ getService }) {

it('filter returns all results because it was applied on an index level with terms_enum', () =>
supertest
.post('/api/kibana/suggestions/values/logstash-*')
.post('/internal/kibana/suggestions/values/logstash-*')
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.send({
field: 'extension.raw',
query: '',
Expand All @@ -188,7 +199,8 @@ export default function ({ getService }) {

it('filter is applied on an index level with terms_enum - find in range', () =>
supertest
.post('/api/kibana/suggestions/values/logstash-*')
.post('/internal/kibana/suggestions/values/logstash-*')
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.send({
field: 'request.raw',
query: '/uploads/anatoly-art',
Expand All @@ -212,7 +224,8 @@ export default function ({ getService }) {

it('filter is applied on an index level with terms_enum - DONT find in range', () => {
supertest
.post('/api/kibana/suggestions/values/logstash-*')
.post('/internal/kibana/suggestions/values/logstash-*')
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.send({
field: 'request.raw',
query: '/uploads/anatoly-art',
Expand Down
3 changes: 2 additions & 1 deletion test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"@kbn/dev-proc-runner",
"@kbn/enterprise-search-plugin",
"@kbn/core-saved-objects-server",
"@kbn/discover-plugin"
"@kbn/discover-plugin",
"@kbn/core-http-common"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { ApmSynthtraceEsClient } from '@kbn/apm-synthtrace';
import expect from '@kbn/expect';
import { APM_STATIC_DATA_VIEW_ID } from '@kbn/apm-plugin/common/data_view_constants';
import { DataView } from '@kbn/data-views-plugin/common';
import { ELASTIC_HTTP_VERSION_HEADER } from '@kbn/core-http-common';
import request from 'superagent';
import { FtrProviderContext } from '../../common/ftr_provider_context';
import { SupertestReturnType, ApmApiError } from '../../common/apm_api_supertest';
Expand Down Expand Up @@ -46,8 +47,9 @@ export default function ApiTest({ getService }: FtrProviderContext) {

function getDataViewSuggestions(field: string) {
return supertest
.post(`/api/kibana/suggestions/values/${dataViewPattern}`)
.post(`/internal/kibana/suggestions/values/${dataViewPattern}`)
.set('kbn-xsrf', 'foo')
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.send({ query: '', field, method: 'terms_agg' });
}

Expand Down

0 comments on commit e3c3ba8

Please sign in to comment.