Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('SearchSource#normalizeSortRequest', function () {

it('should append default sort options', function () {
const defaultSortOptions = {
unmapped_type: 'boolean',
unmapped_type: 'boolean' as 'boolean',
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Export type FieldSortOptions and use it here to avoid casting:

    const defaultSortOptions: FieldSortOptions = {
      unmapped_type: 'boolean',
    };

};
const result = normalizeSortRequest(
[{ someField: SortDirection.desc }],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@
* Side Public License, v 1.
*/

import { estypes } from '@elastic/elasticsearch';
import type { DataView } from '@kbn/data-views-plugin/common';
import { EsQuerySortValue, SortOptions } from './types';
import { EsQuerySortValue } from './types';

type FieldSortOptions = estypes.FieldSort &
estypes.ScoreSort &
estypes.GeoDistanceSort &
Omit<estypes.ScriptSort, 'script'> & {
script?: estypes.ScriptSort['script'];
};

export function normalizeSortRequest(
sortObject: EsQuerySortValue | EsQuerySortValue[],
indexPattern: DataView | string | undefined,
defaultSortOptions: SortOptions = {}
defaultSortOptions: FieldSortOptions | string = {}
) {
const sortArray: EsQuerySortValue[] = Array.isArray(sortObject) ? sortObject : [sortObject];
return sortArray.map(function (sortable) {
Expand All @@ -28,7 +36,7 @@ export function normalizeSortRequest(
function normalize(
sortable: EsQuerySortValue,
indexPattern: DataView | string | undefined,
defaultSortOptions: any
defaultSortOptions: FieldSortOptions | string
) {
const [[sortField, sortOrder]] = Object.entries(sortable);
const order = typeof sortOrder === 'object' ? sortOrder : { order: sortOrder };
Expand All @@ -52,13 +60,16 @@ function normalize(
// FIXME: for unknown reason on the server this setting is serialized
// https://github.com/elastic/kibana/issues/89902
if (typeof defaultSortOptions === 'string') {
defaultSortOptions = JSON.parse(defaultSortOptions);
defaultSortOptions = JSON.parse(defaultSortOptions) as FieldSortOptions;
}
// Don't include unmapped_type for _score field
// eslint-disable-next-line @typescript-eslint/naming-convention
const { unmapped_type, ...otherSortOptions } = defaultSortOptions;
return {
[sortField]: { ...order, ...(sortField === '_score' ? otherSortOptions : defaultSortOptions) },
[sortField]: {
...order,
...(sortField === '_score' ? otherSortOptions : defaultSortOptions),
},
};
}

Expand Down
52 changes: 0 additions & 52 deletions src/plugins/data/common/search/search_source/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ export interface ISearchStartSearchSource
createEmpty: () => ISearchSource;
}

/**
* @deprecated use {@link estypes.SortResults} instead.
*/
export type EsQuerySearchAfter = [string | number, string | number];

export enum SortDirection {
asc = 'asc',
desc = 'desc',
Expand Down Expand Up @@ -178,53 +173,6 @@ export interface SearchSourceOptions {
callParentStartHandlers?: boolean;
}

export interface SortOptions {
mode?: 'min' | 'max' | 'sum' | 'avg' | 'median';
type?: 'double' | 'long' | 'date' | 'date_nanos';
nested?: object;
unmapped_type?: string;
distance_type?: 'arc' | 'plane';
unit?: string;
ignore_unmapped?: boolean;
_script?: object;
}

export interface Request {
docvalue_fields: string[];
_source: unknown;
query: unknown;
script_fields: unknown;
sort: unknown;
stored_fields: string[];
}

export interface ResponseWithShardFailure {
_shards: {
failed: number;
failures: ShardFailure[];
skipped: number;
successful: number;
total: number;
};
}

export interface ShardFailure {
index: string;
node: string;
reason: {
caused_by: {
reason: string;
type: string;
};
reason: string;
lang?: estypes.ScriptLanguage;
script?: string;
script_stack?: string[];
type: string;
};
shard: number;
}

export function isSerializedSearchSource(
maybeSerializedSearchSource: unknown
): maybeSerializedSearchSource is SerializedSearchSourceFields {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { estypes } from '@elastic/elasticsearch';
import { lastValueFrom } from 'rxjs';
import { ISearchSource, EsQuerySortValue, SortDirection } from '@kbn/data-plugin/public';
import { EsQuerySearchAfter } from '@kbn/data-plugin/common';
import { buildDataTableRecord } from '@kbn/discover-utils';
import type { DataTableRecord } from '@kbn/discover-utils/types';
import {
Expand Down Expand Up @@ -39,7 +39,7 @@ export async function fetchHitsInInterval(
sort: [EsQuerySortValue, EsQuerySortValue],
sortDir: SortDirection,
interval: IntervalValue[],
searchAfter: EsQuerySearchAfter,
searchAfter: estypes.SortResults,
maxCount: number,
nanosValue: string,
anchorId: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import type { EsQuerySearchAfter } from '@kbn/data-plugin/common';
import { estypes } from '@elastic/elasticsearch';
import type { DataTableRecord } from '@kbn/discover-utils/types';
import { SurrDocType } from '../services/context';

Expand All @@ -19,7 +19,7 @@ export function getEsQuerySearchAfter(
type: SurrDocType,
rows: DataTableRecord[],
anchor: DataTableRecord
): EsQuerySearchAfter {
): estypes.SortResults {
if (rows.length) {
// already surrounding docs -> first or last record is used
const afterTimeRecIdx = type === SurrDocType.SUCCESSORS && rows.length ? rows.length - 1 : 0;
Expand Down