Skip to content

Commit

Permalink
Merge branch 'master' into by-value-export-references
Browse files Browse the repository at this point in the history
  • Loading branch information
kibanamachine authored Feb 22, 2021
2 parents 688f73e + b1d76ce commit d2d2500
Show file tree
Hide file tree
Showing 40 changed files with 4,151,287 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface SearchSourceFields
| [source](./kibana-plugin-plugins-data-public.searchsourcefields.source.md) | <code>NameList</code> | |
| [terminate\_after](./kibana-plugin-plugins-data-public.searchsourcefields.terminate_after.md) | <code>number</code> | |
| [timeout](./kibana-plugin-plugins-data-public.searchsourcefields.timeout.md) | <code>string</code> | |
| [trackTotalHits](./kibana-plugin-plugins-data-public.searchsourcefields.tracktotalhits.md) | <code>boolean &#124; number</code> | |
| [type](./kibana-plugin-plugins-data-public.searchsourcefields.type.md) | <code>string</code> | |
| [version](./kibana-plugin-plugins-data-public.searchsourcefields.version.md) | <code>boolean</code> | |

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- 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; [SearchSourceFields](./kibana-plugin-plugins-data-public.searchsourcefields.md) &gt; [trackTotalHits](./kibana-plugin-plugins-data-public.searchsourcefields.tracktotalhits.md)

## SearchSourceFields.trackTotalHits property

<b>Signature:</b>

```typescript
trackTotalHits?: boolean | number;
```
3 changes: 2 additions & 1 deletion examples/search_examples/public/search/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ export const SearchExamplesApp = ({
.setField('index', indexPattern)
.setField('filter', filters)
.setField('query', query)
.setField('fields', selectedFields.length ? selectedFields.map((f) => f.name) : ['*']);
.setField('fields', selectedFields.length ? selectedFields.map((f) => f.name) : ['*'])
.setField('trackTotalHits', 100);

if (selectedNumericField) {
searchSource.setField('aggs', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import { UI_SETTINGS } from '../../../constants';
import { GetConfigFn } from '../../../types';
import { getSearchParams } from './get_search_params';
import { getSearchParams, getSearchParamsFromRequest } from './get_search_params';

function getConfigStub(config: any = {}): GetConfigFn {
return (key) => config[key];
Expand All @@ -23,4 +23,26 @@ describe('getSearchParams', () => {
const searchParams = getSearchParams(config);
expect(searchParams.preference).toBe('aaa');
});

test('extracts track total hits', () => {
const getConfig = getConfigStub({
[UI_SETTINGS.COURIER_SET_REQUEST_PREFERENCE]: 'custom',
[UI_SETTINGS.COURIER_CUSTOM_REQUEST_PREFERENCE]: 'aaa',
});
const searchParams = getSearchParamsFromRequest(
{
index: 'abc',
body: {
query: 123,
track_total_hits: true,
},
},
{ getConfig }
);
expect(searchParams.index).toBe('abc');
expect(searchParams.track_total_hits).toBe(true);
expect(searchParams.body).toStrictEqual({
query: 123,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ export function getSearchParamsFromRequest(
): ISearchRequestParams {
const { getConfig } = dependencies;
const searchParams = getSearchParams(getConfig);
// eslint-disable-next-line @typescript-eslint/naming-convention
const { track_total_hits, ...body } = searchRequest.body;

return {
index: searchRequest.index.title || searchRequest.index,
body: searchRequest.body,
body,
track_total_hits,
...searchParams,
};
}
2 changes: 2 additions & 0 deletions src/plugins/data/common/search/search_source/search_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,8 @@ export class SearchSource {
return key && data[key] == null && addToRoot(key, val);
case 'searchAfter':
return addToBody('search_after', val);
case 'trackTotalHits':
return addToBody('track_total_hits', val);
case 'source':
return addToBody('_source', val);
case 'sort':
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/common/search/search_source/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export interface SearchSourceFields {
sort?: EsQuerySortValue | EsQuerySortValue[];
highlight?: any;
highlightAll?: boolean;
trackTotalHits?: boolean | number;
/**
* {@link AggConfigs}
*/
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/data/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2447,6 +2447,8 @@ export interface SearchSourceFields {
// (undocumented)
timeout?: string;
// (undocumented)
trackTotalHits?: boolean | number;
// (undocumented)
type?: string;
// (undocumented)
version?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,20 @@ export const CURATIONS_TITLE = i18n.translate(
'xpack.enterpriseSearch.appSearch.engine.curations.title',
{ defaultMessage: 'Curations' }
);
export const CURATIONS_OVERVIEW_TITLE = i18n.translate(
'xpack.enterpriseSearch.appSearch.engine.curations.overview.title',
{ defaultMessage: 'Curated results' }
);
export const CREATE_NEW_CURATION_TITLE = i18n.translate(
'xpack.enterpriseSearch.appSearch.engine.curations.create.title',
{ defaultMessage: 'Create new curation' }
);

export const DELETE_MESSAGE = i18n.translate(
'xpack.enterpriseSearch.appSearch.engine.curations.deleteConfirmation',
{ defaultMessage: 'Are you sure you want to remove this curation?' }
);
export const SUCCESS_MESSAGE = i18n.translate(
'xpack.enterpriseSearch.appSearch.engine.curations.deleteSuccessMessage',
{ defaultMessage: 'Successfully removed curation.' }
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { LogicMounter, mockHttpValues, mockFlashMessageHelpers } from '../../../__mocks__';
import '../../__mocks__/engine_logic.mock';

import { nextTick } from '@kbn/test/jest';

import { DEFAULT_META } from '../../../shared/constants';

import { CurationsLogic } from './';

describe('CurationsLogic', () => {
const { mount } = new LogicMounter(CurationsLogic);
const { http } = mockHttpValues;
const { clearFlashMessages, setSuccessMessage, flashAPIErrors } = mockFlashMessageHelpers;

const MOCK_CURATIONS_RESPONSE = {
meta: {
page: {
current: 1,
size: 10,
total_results: 1,
total_pages: 1,
},
},
results: [
{
id: 'some-curation-id',
last_updated: 'January 1, 1970 at 12:00PM',
queries: ['some query'],
promoted: [],
hidden: [],
organic: [],
},
],
};

const DEFAULT_VALUES = {
dataLoading: true,
curations: [],
meta: DEFAULT_META,
};

beforeEach(() => {
jest.clearAllMocks();
});

it('has expected default values', () => {
mount();
expect(CurationsLogic.values).toEqual(DEFAULT_VALUES);
});

describe('actions', () => {
describe('onCurationsLoad', () => {
it('should set curations and meta state, & dataLoading to false', () => {
mount();

CurationsLogic.actions.onCurationsLoad(MOCK_CURATIONS_RESPONSE);

expect(CurationsLogic.values).toEqual({
...DEFAULT_VALUES,
curations: MOCK_CURATIONS_RESPONSE.results,
meta: MOCK_CURATIONS_RESPONSE.meta,
dataLoading: false,
});
});
});

describe('onPaginate', () => {
it('should set meta.page.current state', () => {
mount();

CurationsLogic.actions.onPaginate(3);

expect(CurationsLogic.values).toEqual({
...DEFAULT_VALUES,
meta: { page: { ...DEFAULT_VALUES.meta.page, current: 3 } },
});
});
});
});

describe('listeners', () => {
describe('loadCurations', () => {
it('should set dataLoading state', () => {
mount({ dataLoading: false });

CurationsLogic.actions.loadCurations();

expect(CurationsLogic.values).toEqual({
...DEFAULT_VALUES,
dataLoading: true,
});
});

it('should make an API call and set curations & meta state', async () => {
http.get.mockReturnValueOnce(Promise.resolve(MOCK_CURATIONS_RESPONSE));
mount();
jest.spyOn(CurationsLogic.actions, 'onCurationsLoad');

CurationsLogic.actions.loadCurations();
await nextTick();

expect(http.get).toHaveBeenCalledWith('/api/app_search/engines/some-engine/curations', {
query: {
'page[current]': 1,
'page[size]': 10,
},
});
expect(CurationsLogic.actions.onCurationsLoad).toHaveBeenCalledWith(
MOCK_CURATIONS_RESPONSE
);
});

it('handles errors', async () => {
http.get.mockReturnValueOnce(Promise.reject('error'));
mount();

CurationsLogic.actions.loadCurations();
await nextTick();

expect(flashAPIErrors).toHaveBeenCalledWith('error');
});
});

describe('deleteCurationSet', () => {
const confirmSpy = jest.spyOn(window, 'confirm');

beforeEach(() => {
confirmSpy.mockImplementation(jest.fn(() => true));
});

it('should make an API call and show a success message', async () => {
http.delete.mockReturnValueOnce(Promise.resolve({}));
mount();
jest.spyOn(CurationsLogic.actions, 'loadCurations');

CurationsLogic.actions.deleteCurationSet('some-curation-id');
expect(clearFlashMessages).toHaveBeenCalled();
await nextTick();

expect(http.delete).toHaveBeenCalledWith(
'/api/app_search/engines/some-engine/curations/some-curation-id'
);
expect(CurationsLogic.actions.loadCurations).toHaveBeenCalled();
expect(setSuccessMessage).toHaveBeenCalledWith('Successfully removed curation.');
});

it('handles errors', async () => {
http.delete.mockReturnValueOnce(Promise.reject('error'));
mount();

CurationsLogic.actions.deleteCurationSet('some-curation-id');
expect(clearFlashMessages).toHaveBeenCalled();
await nextTick();

expect(flashAPIErrors).toHaveBeenCalledWith('error');
});

it('does nothing if the user cancels the confirmation prompt', async () => {
confirmSpy.mockImplementationOnce(() => false);
mount();

CurationsLogic.actions.deleteCurationSet('some-curation-id');
expect(clearFlashMessages).toHaveBeenCalled();
await nextTick();

expect(http.delete).not.toHaveBeenCalled();
});
});
});
});
Loading

0 comments on commit d2d2500

Please sign in to comment.