Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[canvas] Create Expression Factories; remove legacy search service #107353

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 88 additions & 64 deletions x-pack/plugins/canvas/canvas_plugin_src/functions/browser/escount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,86 +9,110 @@ import {
ExpressionFunctionDefinition,
ExpressionValueFilter,
} from 'src/plugins/expressions/common';
import { i18n } from '@kbn/i18n';

import { StartDeps } from '../../../canvas_plugin_src/plugin';
import { ExpressionFunctionDefinitionFactory } from '../../../types';
import { ELASTICSEARCH, LUCENE } from '../../../i18n/constants';

// @ts-expect-error untyped local
import { buildESRequest } from '../../../common/lib/request/build_es_request';

import { searchService } from '../../../public/services';

import { getFunctionHelp } from '../../../i18n';

interface Arguments {
export interface Arguments {
index: string | null;
query: string;
}

export function escount(): ExpressionFunctionDefinition<
'escount',
ExpressionValueFilter,
Arguments,
any
> {
const { help, args: argHelp } = getFunctionHelp().escount;

return {
name: 'escount',
type: 'number',
context: {
types: ['filter'],
const strings = {
help: i18n.translate('xpack.canvas.functions.escountHelpText', {
defaultMessage: 'Query {ELASTICSEARCH} for the number of hits matching the specified query.',
values: {
ELASTICSEARCH,
},
help,
args: {
query: {
types: ['string'],
aliases: ['_', 'q'],
help: argHelp.query,
default: '"-_index:.kibana"',
}),
args: {
query: i18n.translate('xpack.canvas.functions.escount.args.queryHelpText', {
defaultMessage: 'A {LUCENE} query string.',
values: {
LUCENE,
},
index: {
types: ['string'],
default: '_all',
help: argHelp.index,
}),
index: i18n.translate('xpack.canvas.functions.escount.args.indexHelpText', {
defaultMessage: 'An index or index pattern. For example, {example}.',
values: {
example: '`"logstash-*"`',
},
},
fn: (input, args, handlers) => {
input.and = input.and.concat([
{
type: 'filter',
filterType: 'luceneQueryString',
query: args.query,
and: [],
}),
},
};

type Fn = ExpressionFunctionDefinition<'escount', ExpressionValueFilter, Arguments, any>;

export const escountFactory: ExpressionFunctionDefinitionFactory<StartDeps, Fn> = ({
startPlugins,
}) => {
return function escount(): Fn {
const { help, args: argHelp } = strings;

return {
name: 'escount',
type: 'number',
context: {
types: ['filter'],
},
help,
args: {
query: {
types: ['string'],
aliases: ['_', 'q'],
help: argHelp.query,
default: '"-_index:.kibana"',
},
]);
index: {
types: ['string'],
default: '_all',
help: argHelp.index,
},
},
fn: (input, args, _handlers) => {
input.and = input.and.concat([
{
type: 'filter',
filterType: 'luceneQueryString',
query: args.query,
and: [],
},
]);

const esRequest = buildESRequest(
{
index: args.index,
body: {
track_total_hits: true,
size: 0,
query: {
bool: {
must: [{ match_all: {} }],
const esRequest = buildESRequest(
{
index: args.index,
body: {
track_total_hits: true,
size: 0,
query: {
bool: {
must: [{ match_all: {} }],
},
},
},
},
},
input
);
input
);

const search = searchService.getService().search;
const req = {
params: {
...esRequest,
},
};
const { search } = startPlugins.data.search;
const req = {
params: {
...esRequest,
},
};

return search
.search(req)
.toPromise()
.then((resp: any) => {
return resp.rawResponse.hits.total;
});
},
return search(req)
.toPromise()
.then((resp: any) => {
return resp.rawResponse.hits.total;
});
},
};
};
}
};
Loading