Skip to content

Commit

Permalink
master conflicts solved
Browse files Browse the repository at this point in the history
  • Loading branch information
semd committed Aug 17, 2021
2 parents 97c4bed + 0eae57b commit 0e4f7e1
Show file tree
Hide file tree
Showing 310 changed files with 4,257 additions and 4,856 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
esKuery: {
nodeTypes: import("@kbn/es-query/target_types/kuery/node_types").NodeTypes;
fromKueryExpression: (expression: string | import("@elastic/elasticsearch/api/types").QueryDslQueryContainer, parseOptions?: Partial<import("@kbn/es-query/target_types/kuery/types").KueryParseOptions> | undefined) => import("@kbn/es-query").KueryNode;
toElasticsearchQuery: (node: import("@kbn/es-query").KueryNode, indexPattern?: import("@kbn/es-query").IndexPatternBase | undefined, config?: Record<string, any> | undefined, context?: Record<string, any> | undefined) => import("@kbn/utility-types").JsonObject;
toElasticsearchQuery: (node: import("@kbn/es-query").KueryNode, indexPattern?: import("@kbn/es-query").IndexPatternBase | undefined, config?: import("@kbn/es-query").KueryQueryOptions | undefined, context?: Record<string, any> | undefined) => import("@elastic/elasticsearch/api/types").QueryDslQueryContainer;
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ export interface IKibanaSearchResponse<RawResponse = any>
| [loaded](./kibana-plugin-plugins-data-public.ikibanasearchresponse.loaded.md) | <code>number</code> | If relevant to the search strategy, return a loaded number that represents how progress is indicated. |
| [rawResponse](./kibana-plugin-plugins-data-public.ikibanasearchresponse.rawresponse.md) | <code>RawResponse</code> | The raw response returned by the internal search method (usually the raw ES response) |
| [total](./kibana-plugin-plugins-data-public.ikibanasearchresponse.total.md) | <code>number</code> | If relevant to the search strategy, return a total number that represents how progress is indicated. |
| [warning](./kibana-plugin-plugins-data-public.ikibanasearchresponse.warning.md) | <code>string</code> | Optional warnings that should be surfaced to the end user |

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) &gt; [IKibanaSearchResponse](./kibana-plugin-plugins-data-public.ikibanasearchresponse.md) &gt; [warning](./kibana-plugin-plugins-data-public.ikibanasearchresponse.warning.md)

## IKibanaSearchResponse.warning property

Optional warnings that should be surfaced to the end user

<b>Signature:</b>

```typescript
warning?: string;
```
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
esKuery: {
nodeTypes: import("@kbn/es-query/target_types/kuery/node_types").NodeTypes;
fromKueryExpression: (expression: string | import("@elastic/elasticsearch/api/types").QueryDslQueryContainer, parseOptions?: Partial<import("@kbn/es-query/target_types/kuery/types").KueryParseOptions> | undefined) => import("@kbn/es-query").KueryNode;
toElasticsearchQuery: (node: import("@kbn/es-query").KueryNode, indexPattern?: import("@kbn/es-query").IndexPatternBase | undefined, config?: Record<string, any> | undefined, context?: Record<string, any> | undefined) => import("@kbn/utility-types").JsonObject;
toElasticsearchQuery: (node: import("@kbn/es-query").KueryNode, indexPattern?: import("@kbn/es-query").IndexPatternBase | undefined, config?: import("@kbn/es-query").KueryQueryOptions | undefined, context?: Record<string, any> | undefined) => import("@elastic/elasticsearch/api/types").QueryDslQueryContainer;
}
```
98 changes: 92 additions & 6 deletions examples/search_examples/public/search/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,46 @@ export const SearchExamplesApp = ({
setSelectedNumericField(fields?.length ? getNumeric(fields)[0] : null);
}, [fields]);

const doAsyncSearch = async (strategy?: string, sessionId?: string) => {
const doAsyncSearch = async (
strategy?: string,
sessionId?: string,
addWarning: boolean = false,
addError: boolean = false
) => {
if (!indexPattern || !selectedNumericField) return;

// Construct the query portion of the search request
const query = data.query.getEsQuery(indexPattern);

if (addWarning) {
query.bool.must.push({
// @ts-ignore
error_query: {
indices: [
{
name: indexPattern.title,
error_type: 'warning',
message: 'Watch out!',
},
],
},
});
}
if (addError) {
query.bool.must.push({
// @ts-ignore
error_query: {
indices: [
{
name: indexPattern.title,
error_type: 'exception',
message: 'Watch out!',
},
],
},
});
}

// Construct the aggregations portion of the search request by using the `data.search.aggs` service.
const aggs = [{ type: 'avg', params: { field: selectedNumericField!.name } }];
const aggsDsl = data.search.aggs.createAggConfigs(indexPattern, aggs).toDsl();
Expand Down Expand Up @@ -193,14 +227,23 @@ export const SearchExamplesApp = ({
}
);
searchSubscription$.unsubscribe();
if (res.warning) {
notifications.toasts.addWarning({
title: 'Warning',
text: mountReactNode(res.warning),
});
}
} else if (isErrorResponse(res)) {
// TODO: Make response error status clearer
notifications.toasts.addWarning('An error has occurred');
notifications.toasts.addDanger('An error has occurred');
searchSubscription$.unsubscribe();
}
},
error: () => {
notifications.toasts.addDanger('Failed to run search');
error: (e) => {
notifications.toasts.addDanger({
title: 'Failed to run search',
text: e.message,
});
},
});
};
Expand Down Expand Up @@ -270,6 +313,14 @@ export const SearchExamplesApp = ({
doAsyncSearch('myStrategy');
};

const onWarningSearchClickHandler = () => {
doAsyncSearch(undefined, undefined, true);
};

const onErrorSearchClickHandler = () => {
doAsyncSearch(undefined, undefined, false, true);
};

const onPartialResultsClickHandler = () => {
setSelectedTab(1);
const req = {
Expand Down Expand Up @@ -299,8 +350,11 @@ export const SearchExamplesApp = ({
searchSubscription$.unsubscribe();
}
},
error: () => {
notifications.toasts.addDanger('Failed to run search');
error: (e) => {
notifications.toasts.addDanger({
title: 'Failed to run search',
text: e.message,
});
},
});
};
Expand Down Expand Up @@ -530,6 +584,38 @@ export const SearchExamplesApp = ({
</EuiText>
</EuiText>
<EuiSpacer />
<EuiTitle size="xs">
<h3>Handling errors & warnings</h3>
</EuiTitle>
<EuiText>
When fetching data from Elasticsearch, there are several different ways warnings and
errors may be returned. In general, it is recommended to surface these in the UX.
<EuiSpacer />
<EuiButtonEmpty
size="xs"
onClick={onWarningSearchClickHandler}
iconType="play"
data-test-subj="searchWithWarning"
>
<FormattedMessage
id="searchExamples.searchWithWarningButtonText"
defaultMessage="Request with a warning in response"
/>
</EuiButtonEmpty>
<EuiText />
<EuiButtonEmpty
size="xs"
onClick={onErrorSearchClickHandler}
iconType="play"
data-test-subj="searchWithError"
>
<FormattedMessage
id="searchExamples.searchWithErrorButtonText"
defaultMessage="Request with an error in response"
/>
</EuiButtonEmpty>
</EuiText>
<EuiSpacer />
<EuiTitle size="xs">
<h3>Handling partial results</h3>
</EuiTitle>
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
"@elastic/charts": "33.2.2",
"@elastic/datemath": "link:bazel-bin/packages/elastic-datemath",
"@elastic/elasticsearch": "npm:@elastic/elasticsearch-canary@^8.0.0-canary.14",
"@elastic/ems-client": "7.14.0",
"@elastic/ems-client": "7.15.0",
"@elastic/eui": "37.1.1",
"@elastic/filesaver": "1.1.2",
"@elastic/good": "^9.0.1-kibana3",
Expand Down Expand Up @@ -632,7 +632,6 @@
"@types/strong-log-transformer": "^1.0.0",
"@types/styled-components": "^5.1.0",
"@types/supertest": "^2.0.5",
"@types/supertest-as-promised": "^2.0.38",
"@types/tapable": "^1.0.6",
"@types/tar": "^4.0.3",
"@types/tar-fs": "^1.16.1",
Expand Down Expand Up @@ -824,7 +823,6 @@
"stylelint-scss": "^3.18.0",
"superagent": "^3.8.2",
"supertest": "^3.1.0",
"supertest-as-promised": "^4.0.2",
"supports-color": "^7.0.0",
"tape": "^5.0.1",
"tar-fs": "^2.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-es-query/src/es_query/from_kuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function buildQuery(
indexPattern: IndexPatternBase | undefined,
queryASTs: KueryNode[],
config: SerializableRecord = {}
) {
): BoolQuery {
const compoundQueryAST = nodeTypes.function.buildNode('and', queryASTs);
const kueryQuery = toElasticsearchQuery(compoundQueryAST, indexPattern, config);

Expand Down
7 changes: 4 additions & 3 deletions packages/kbn-es-query/src/kuery/ast/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { JsonObject } from '@kbn/utility-types';
import { estypes } from '@elastic/elasticsearch';
import { nodeTypes } from '../node_types/index';
import { KQLSyntaxError } from '../kuery_syntax_error';
import { KueryNode, KueryParseOptions } from '../types';
import { KueryNode, KueryParseOptions, KueryQueryOptions } from '../types';

import { parse as parseKuery } from '../grammar';
import { IndexPatternBase } from '../..';
Expand Down Expand Up @@ -62,18 +62,19 @@ export const fromKueryExpression = (
*
* IndexPattern isn't required, but if you pass one in, we can be more intelligent
* about how we craft the queries (e.g. scripted fields)
*
*/
export const toElasticsearchQuery = (
node: KueryNode,
indexPattern?: IndexPatternBase,
config?: Record<string, any>,
config: KueryQueryOptions = {},
context?: Record<string, any>
): JsonObject => {
if (!node || !node.type || !nodeTypes[node.type]) {
return toElasticsearchQuery(nodeTypes.function.buildNode('and', []), indexPattern);
}

// TODO: the return type of this function might be incorrect and it works only because of this casting
const nodeType = (nodeTypes[node.type] as unknown) as any;

return nodeType.toElasticsearchQuery(node, indexPattern, config, context);
};
9 changes: 5 additions & 4 deletions packages/kbn-es-query/src/kuery/functions/exists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* Side Public License, v 1.
*/

import { IndexPatternFieldBase, IndexPatternBase, KueryNode } from '../..';
import { estypes } from '@elastic/elasticsearch';
import { IndexPatternFieldBase, IndexPatternBase, KueryNode, KueryQueryOptions } from '../..';
import * as literal from '../node_types/literal';

export function buildNodeParams(fieldName: string) {
Expand All @@ -18,17 +19,17 @@ export function buildNodeParams(fieldName: string) {
export function toElasticsearchQuery(
node: KueryNode,
indexPattern?: IndexPatternBase,
config: Record<string, any> = {},
config: KueryQueryOptions = {},
context: Record<string, any> = {}
) {
): estypes.QueryDslQueryContainer {
const {
arguments: [fieldNameArg],
} = node;
const fullFieldNameArg = {
...fieldNameArg,
value: context?.nested ? `${context.nested.path}.${fieldNameArg.value}` : fieldNameArg.value,
};
const fieldName = literal.toElasticsearchQuery(fullFieldNameArg);
const fieldName = literal.toElasticsearchQuery(fullFieldNameArg) as string;
const field = indexPattern?.fields?.find((fld: IndexPatternFieldBase) => fld.name === fieldName);

if (field?.scripted) {
Expand Down
25 changes: 19 additions & 6 deletions packages/kbn-es-query/src/kuery/functions/geo_bounding_box.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { fields } from '../../filters/stubs';
import { IndexPatternBase } from '../..';

import * as geoBoundingBox from './geo_bounding_box';
import { JsonObject } from '@kbn/utility-types';

jest.mock('../grammar');

Expand Down Expand Up @@ -78,24 +79,36 @@ describe('kuery functions', () => {
const result = geoBoundingBox.toElasticsearchQuery(node, indexPattern);

expect(result).toHaveProperty('geo_bounding_box');
expect(result.geo_bounding_box.geo).toHaveProperty('top_left', '73.12, -174.37');
expect(result.geo_bounding_box.geo).toHaveProperty('bottom_right', '50.73, -135.35');
expect((result.geo_bounding_box as JsonObject).geo).toHaveProperty(
'top_left',
'73.12, -174.37'
);
expect((result.geo_bounding_box as JsonObject).geo).toHaveProperty(
'bottom_right',
'50.73, -135.35'
);
});

test('should return an ES geo_bounding_box query without an index pattern', () => {
const node = nodeTypes.function.buildNode('geoBoundingBox', 'geo', params);
const result = geoBoundingBox.toElasticsearchQuery(node);

expect(result).toHaveProperty('geo_bounding_box');
expect(result.geo_bounding_box.geo).toHaveProperty('top_left', '73.12, -174.37');
expect(result.geo_bounding_box.geo).toHaveProperty('bottom_right', '50.73, -135.35');
expect((result.geo_bounding_box as JsonObject).geo).toHaveProperty(
'top_left',
'73.12, -174.37'
);
expect((result.geo_bounding_box as JsonObject).geo).toHaveProperty(
'bottom_right',
'50.73, -135.35'
);
});

test('should use the ignore_unmapped parameter', () => {
const node = nodeTypes.function.buildNode('geoBoundingBox', 'geo', params);
const result = geoBoundingBox.toElasticsearchQuery(node, indexPattern);

expect(result.geo_bounding_box.ignore_unmapped).toBe(true);
expect(result.geo_bounding_box!.ignore_unmapped).toBe(true);
});

test('should throw an error for scripted fields', () => {
Expand All @@ -116,7 +129,7 @@ describe('kuery functions', () => {
);

expect(result).toHaveProperty('geo_bounding_box');
expect(result.geo_bounding_box['nestedField.geo']).toBeDefined();
expect((result.geo_bounding_box as JsonObject)['nestedField.geo']).toBeDefined();
});
});
});
Expand Down
7 changes: 4 additions & 3 deletions packages/kbn-es-query/src/kuery/functions/geo_bounding_box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
*/

import _ from 'lodash';
import { estypes } from '@elastic/elasticsearch';
import { nodeTypes } from '../node_types';
import * as ast from '../ast';
import { IndexPatternBase, KueryNode, LatLon } from '../..';
import { IndexPatternBase, KueryNode, KueryQueryOptions, LatLon } from '../..';

export function buildNodeParams(fieldName: string, params: any) {
params = _.pick(params, 'topLeft', 'bottomRight');
Expand All @@ -27,9 +28,9 @@ export function buildNodeParams(fieldName: string, params: any) {
export function toElasticsearchQuery(
node: KueryNode,
indexPattern?: IndexPatternBase,
config: Record<string, any> = {},
config: KueryQueryOptions = {},
context: Record<string, any> = {}
) {
): estypes.QueryDslQueryContainer {
const [fieldNameArg, ...args] = node.arguments;
const fullFieldNameArg = {
...fieldNameArg,
Expand Down
12 changes: 6 additions & 6 deletions packages/kbn-es-query/src/kuery/functions/geo_polygon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ describe('kuery functions', () => {
const result = geoPolygon.toElasticsearchQuery(node, indexPattern);

expect(result).toHaveProperty('geo_polygon');
expect(result.geo_polygon.geo).toHaveProperty('points');
expect((result.geo_polygon as any).geo).toHaveProperty('points');

(result.geo_polygon.geo as any).points.forEach((point: any, index: number) => {
(result.geo_polygon as any).geo.points.forEach((point: any, index: number) => {
const expectedLatLon = `${points[index].lat}, ${points[index].lon}`;

expect(point).toBe(expectedLatLon);
Expand All @@ -93,9 +93,9 @@ describe('kuery functions', () => {
const result = geoPolygon.toElasticsearchQuery(node);

expect(result).toHaveProperty('geo_polygon');
expect(result.geo_polygon.geo).toHaveProperty('points');
expect((result.geo_polygon as any).geo).toHaveProperty('points');

(result.geo_polygon.geo as any).points.forEach((point: any, index: number) => {
(result.geo_polygon as any).geo.points.forEach((point: any, index: number) => {
const expectedLatLon = `${points[index].lat}, ${points[index].lon}`;

expect(point).toBe(expectedLatLon);
Expand All @@ -106,7 +106,7 @@ describe('kuery functions', () => {
const node = nodeTypes.function.buildNode('geoPolygon', 'geo', points);
const result = geoPolygon.toElasticsearchQuery(node, indexPattern);

expect(result.geo_polygon.ignore_unmapped).toBe(true);
expect((result.geo_polygon as any).ignore_unmapped).toBe(true);
});

test('should throw an error for scripted fields', () => {
Expand All @@ -126,7 +126,7 @@ describe('kuery functions', () => {
);

expect(result).toHaveProperty('geo_polygon');
expect(result.geo_polygon['nestedField.geo']).toBeDefined();
expect((result.geo_polygon as any)['nestedField.geo']).toBeDefined();
});
});
});
Expand Down
Loading

0 comments on commit 0e4f7e1

Please sign in to comment.