From 716ab2374dde594a1eea2af05622d099544ccf59 Mon Sep 17 00:00:00 2001 From: Jason Stoltzfus Date: Wed, 20 Jan 2021 12:37:44 -0500 Subject: [PATCH] Implemented actions and selectors --- .../components/relevance_tuning/index.ts | 1 + .../relevance_tuning_logic.test.ts | 296 ++++++++++++++++++ .../relevance_tuning_logic.ts | 155 +++++++++ .../components/relevance_tuning/types.ts | 26 ++ 4 files changed, 478 insertions(+) create mode 100644 x-pack/plugins/enterprise_search/public/applications/app_search/components/relevance_tuning/relevance_tuning_logic.test.ts create mode 100644 x-pack/plugins/enterprise_search/public/applications/app_search/components/relevance_tuning/relevance_tuning_logic.ts create mode 100644 x-pack/plugins/enterprise_search/public/applications/app_search/components/relevance_tuning/types.ts diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/relevance_tuning/index.ts b/x-pack/plugins/enterprise_search/public/applications/app_search/components/relevance_tuning/index.ts index 55070255ac81b..d61834b73c6dd 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/components/relevance_tuning/index.ts +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/relevance_tuning/index.ts @@ -6,3 +6,4 @@ export { RELEVANCE_TUNING_TITLE } from './constants'; export { RelevanceTuning } from './relevance_tuning'; +export { RelevanceTuningLogic } from './relevance_tuning_logic'; diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/relevance_tuning/relevance_tuning_logic.test.ts b/x-pack/plugins/enterprise_search/public/applications/app_search/components/relevance_tuning/relevance_tuning_logic.test.ts new file mode 100644 index 0000000000000..4d64e82ca534b --- /dev/null +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/relevance_tuning/relevance_tuning_logic.test.ts @@ -0,0 +1,296 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { LogicMounter } from '../../../__mocks__'; + +import { IBoostType } from './types'; + +import { RelevanceTuningLogic } from './relevance_tuning_logic'; + +describe('RelevanceTuningLogic', () => { + const searchSettings = { + boosts: { + foo: [ + { + type: 'value' as IBoostType, + factor: 5, + }, + ], + }, + search_fields: {}, + }; + const schema = {}; + const schemaConflicts = {}; + const searchSettingProps = { + searchSettings, + schema, + schemaConflicts, + }; + const searchResults = [{}, {}]; + + const { mount } = new LogicMounter(RelevanceTuningLogic); + + const DEFAULT_VALUES = { + dataLoading: true, + schema: {}, + schemaConflicts: {}, + searchSettings: {}, + unsavedChanges: false, + filterInputValue: '', + query: '', + resultsLoading: false, + searchResults: null, + showSchemaConflictCallout: true, + engineHasSchemaFields: false, + schemaFields: [], + schemaFieldsWithConflicts: [], + filteredSchemaFields: [], + filteredSchemaFieldsWithConflicts: [], + }; + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('has expected default values', () => { + mount(); + expect(RelevanceTuningLogic.values).toEqual(DEFAULT_VALUES); + }); + + describe('actions', () => { + describe('onInitializeSearchSettings', () => { + it('should set state', () => { + mount({ + dataLoading: true, + }); + RelevanceTuningLogic.actions.onInitializeSearchSettings(searchSettingProps); + + expect(RelevanceTuningLogic.values).toEqual({ + ...DEFAULT_VALUES, + searchSettings, + schema, + dataLoading: false, + schemaConflicts, + }); + }); + }); + + describe('setSearchSettings', () => { + it('should set state', () => { + mount({ + unsavedChanges: false, + }); + RelevanceTuningLogic.actions.setSearchSettings(searchSettings); + + expect(RelevanceTuningLogic.values).toEqual({ + ...DEFAULT_VALUES, + searchSettings, + unsavedChanges: true, + }); + }); + }); + + describe('setFilterValue', () => { + it('should set state', () => { + mount(); + RelevanceTuningLogic.actions.setFilterValue('foo'); + + expect(RelevanceTuningLogic.values).toEqual({ + ...DEFAULT_VALUES, + filterInputValue: 'foo', + }); + }); + }); + + describe('setSearchQuery', () => { + it('should set state', () => { + mount(); + RelevanceTuningLogic.actions.setSearchQuery('foo'); + + expect(RelevanceTuningLogic.values).toEqual({ + ...DEFAULT_VALUES, + query: 'foo', + }); + }); + }); + + describe('setSearchResults', () => { + it('should set state', () => { + mount({ + resultsLoading: true, + }); + RelevanceTuningLogic.actions.setSearchResults(searchResults); + + expect(RelevanceTuningLogic.values).toEqual({ + ...DEFAULT_VALUES, + searchResults, + resultsLoading: false, + }); + }); + }); + + describe('setResultsLoading', () => { + it('should set state', () => { + mount({ + resultsLoading: false, + }); + RelevanceTuningLogic.actions.setResultsLoading(true); + + expect(RelevanceTuningLogic.values).toEqual({ + ...DEFAULT_VALUES, + resultsLoading: true, + }); + }); + }); + + describe('clearSearchResults', () => { + it('should set state', () => { + mount({ + searchResults: [{}], + }); + RelevanceTuningLogic.actions.clearSearchResults(); + + expect(RelevanceTuningLogic.values).toEqual({ + ...DEFAULT_VALUES, + searchResults: null, + }); + }); + }); + + describe('resetSearchSettingsState', () => { + it('should set state', () => { + mount({ + dataLoading: false, + }); + RelevanceTuningLogic.actions.resetSearchSettingsState(); + + expect(RelevanceTuningLogic.values).toEqual({ + ...DEFAULT_VALUES, + dataLoading: true, + }); + }); + }); + + describe('dismissSchemaConflictCallout', () => { + it('should set state', () => { + mount({ + showSchemaConflictCallout: true, + }); + RelevanceTuningLogic.actions.dismissSchemaConflictCallout(); + + expect(RelevanceTuningLogic.values).toEqual({ + ...DEFAULT_VALUES, + showSchemaConflictCallout: false, + }); + }); + }); + }); + + describe('selectors', () => { + describe('engineHasSchemaFields', () => { + it('should false if there is only a single field in a schema', () => { + // I believe this is because schemas *always* have an ID, and we don't consider that + // a tunable field + mount({ + schema: { + id: 'foo', + }, + }); + expect(RelevanceTuningLogic.values.engineHasSchemaFields).toEqual(false); + }); + + it('should return true otherwise', () => { + mount({ + schema: { + id: 'foo', + bar: 'bar', + }, + }); + expect(RelevanceTuningLogic.values.engineHasSchemaFields).toEqual(true); + }); + }); + + describe('schemaFields', () => { + it('should return the list of field names from the schema', () => { + mount({ + schema: { + id: 'foo', + bar: 'bar', + }, + }); + expect(RelevanceTuningLogic.values.schemaFields).toEqual(['id', 'bar']); + }); + }); + + describe('schemaFieldsWithConflicts', () => { + it('should return the list of field names that have schema conflicts', () => { + mount({ + schemaConflicts: { + foo: { + text: ['source_engine_1'], + number: ['source_engine_2'], + }, + }, + }); + expect(RelevanceTuningLogic.values.schemaFieldsWithConflicts).toEqual(['foo']); + }); + }); + + describe('filteredSchemaFields', () => { + it('should return a list of schema field names that contain the text from filterInputValue ', () => { + mount({ + filterInputValue: 'ba', + schema: { + id: 'string', + foo: 'string', + bar: 'string', + baz: 'string', + }, + }); + expect(RelevanceTuningLogic.values.filteredSchemaFields).toEqual(['bar', 'baz']); + }); + + it('should return all schema fields if there is no filter applied', () => { + mount({ + filterTerm: '', + schema: { + id: 'string', + foo: 'string', + bar: 'string', + baz: 'string', + }, + }); + expect(RelevanceTuningLogic.values.filteredSchemaFields).toEqual([ + 'id', + 'foo', + 'bar', + 'baz', + ]); + }); + }); + + describe('filteredSchemaFieldsWithConflicts', () => { + it('should return a list of schema field names that contain the text from filterInputValue, and if that field has a schema conflict', () => { + mount({ + filterInputValue: 'ba', + schema: { + id: 'string', + foo: 'string', + bar: 'string', + baz: 'string', + }, + schemaConflicts: { + bar: { + text: ['source_engine_1'], + number: ['source_engine_2'], + }, + }, + }); + expect(RelevanceTuningLogic.values.filteredSchemaFieldsWithConflicts).toEqual(['bar']); + }); + }); + }); +}); diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/relevance_tuning/relevance_tuning_logic.ts b/x-pack/plugins/enterprise_search/public/applications/app_search/components/relevance_tuning/relevance_tuning_logic.ts new file mode 100644 index 0000000000000..00947f7d0fb00 --- /dev/null +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/relevance_tuning/relevance_tuning_logic.ts @@ -0,0 +1,155 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { kea, MakeLogicType } from 'kea'; + +import { Schema, SchemaConflicts } from '../../../shared/types'; +import { ISearchSettings } from './types'; + +interface ISearchSettingsProps { + searchSettings: ISearchSettings; + schema: Schema; + schemaConflicts: SchemaConflicts; +} + +interface ISearchSettingsActions { + onInitializeSearchSettings(props: ISearchSettingsProps): ISearchSettingsProps; + setSearchSettings(searchSettings: ISearchSettings): { searchSettings: ISearchSettings }; + setFilterValue(value: string): string; + setSearchQuery(value: string): string; + setSearchResults(searchResults: object[]): object[]; + setResultsLoading(resultsLoading: boolean): boolean; + clearSearchResults(): void; + resetSearchSettingsState(): void; + dismissSchemaConflictCallout(): void; +} + +interface ISearchSettingsValues { + searchSettings: Partial; + schema: Schema; + dataLoading: boolean; + schemaConflicts: SchemaConflicts; + unsavedChanges: boolean; + filterInputValue: string; + query: string; + searchResults: object[] | null; + resultsLoading: boolean; + showSchemaConflictCallout: boolean; + engineHasSchemaFields: boolean; + schemaFields: string[]; + schemaFieldsWithConflicts: string[]; + filteredSchemaFields: string[]; + filteredSchemaFieldsWithConflicts: string[]; +} + +// If the user hasn't entered a filter, then we can skip filtering the array entirely +const filterIfTerm = (array: string[], filterTerm: string): string[] => { + return filterTerm === '' ? array : array.filter((item) => item.includes(filterTerm)); +}; + +export const RelevanceTuningLogic = kea< + MakeLogicType +>({ + path: ['enterprise_search', 'app_search', 'relevance_tuning_logic'], + actions: () => ({ + onInitializeSearchSettings: (props) => props, + setSearchSettings: (searchSettings) => ({ searchSettings }), + setFilterValue: (value) => value, + setSearchQuery: (query) => query, + setSearchResults: (searchResults) => searchResults, + setResultsLoading: (resultsLoading) => resultsLoading, + clearSearchResults: true, + resetSearchSettingsState: true, + dismissSchemaConflictCallout: true, + }), + reducers: () => ({ + searchSettings: [ + {}, + { + onInitializeSearchSettings: (_, { searchSettings }) => searchSettings, + setSearchSettings: (_, { searchSettings }) => searchSettings, + }, + ], + schema: [ + {}, + { + onInitializeSearchSettings: (_, { schema }) => schema, + }, + ], + dataLoading: [ + true, + { + onInitializeSearchSettings: () => false, + resetSearchSettingsState: () => true, + }, + ], + schemaConflicts: [ + {}, + { + onInitializeSearchSettings: (_, { schemaConflicts }) => schemaConflicts, + }, + ], + unsavedChanges: [ + false, + { + setSearchSettings: () => true, + }, + ], + filterInputValue: [ + '', + { + setFilterValue: (_, filterInputValue) => filterInputValue, + }, + ], + query: [ + '', + { + setSearchQuery: (_, query) => query, + }, + ], + resultsLoading: [ + false, + { + setResultsLoading: (_, resultsLoading) => resultsLoading, + setSearchResults: () => false, + }, + ], + searchResults: [ + null, + { + clearSearchResults: () => null, + setSearchResults: (_, searchResults) => searchResults, + }, + ], + showSchemaConflictCallout: [ + true, + { + dismissSchemaConflictCallout: () => false, + }, + ], + }), + selectors: ({ selectors }) => ({ + engineHasSchemaFields: [ + () => [selectors.schema], + (schema: Schema): boolean => Object.keys(schema).length >= 2, + ], + schemaFields: [() => [selectors.schema], (schema: Schema) => Object.keys(schema)], + schemaFieldsWithConflicts: [ + () => [selectors.schemaConflicts], + (schemaConflicts: SchemaConflicts) => Object.keys(schemaConflicts), + ], + filteredSchemaFields: [ + () => [selectors.schemaFields, selectors.filterInputValue], + (schemaFields: string[], filterInputValue: string): string[] => + filterIfTerm(schemaFields, filterInputValue), + ], + filteredSchemaFieldsWithConflicts: [ + () => [selectors.schemaFieldsWithConflicts, selectors.filterInputValue], + (schemaFieldsWithConflicts: string[], filterInputValue: string): string[] => + filterIfTerm(schemaFieldsWithConflicts, filterInputValue), + ], + }), +}); diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/relevance_tuning/types.ts b/x-pack/plugins/enterprise_search/public/applications/app_search/components/relevance_tuning/types.ts new file mode 100644 index 0000000000000..caf7ffd31aefd --- /dev/null +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/relevance_tuning/types.ts @@ -0,0 +1,26 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +export type IBoostType = 'value' | 'functional' | 'proximity'; + +export interface IBoost { + type: IBoostType; + operation?: string; + function?: string; + newBoost?: boolean; + center?: string | number; + value?: string | number | string[] | number[]; + factor: number; +} + +export interface IBoostObject { + [key: string]: IBoost[]; +} + +export interface ISearchSettings { + boosts: IBoostObject; + search_fields: object; +}