Skip to content

Commit

Permalink
[App Search] Fixed 2 relevance tuning bugs (#94312) (#94357)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Stoltzfus <[email protected]>
  • Loading branch information
kibanamachine and JasonStoltz authored Mar 10, 2021
1 parent b8ca02d commit f37eafd
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { EuiButton, EuiFormRow, EuiPanel, EuiRange, EuiSpacer } from '@elastic/e
import { i18n } from '@kbn/i18n';

import { RelevanceTuningLogic } from '../..';
import { Boost, BoostType } from '../../types';
import { Boost, BoostType, FunctionalBoost, ProximityBoost, ValueBoost } from '../../types';

import { FunctionalBoostForm } from './functional_boost_form';
import { ProximityBoostForm } from './proximity_boost_form';
Expand All @@ -32,11 +32,11 @@ export const BoostItemContent: React.FC<Props> = ({ boost, index, name }) => {
const getBoostForm = () => {
switch (type) {
case BoostType.Value:
return <ValueBoostForm boost={boost} index={index} name={name} />;
return <ValueBoostForm boost={boost as ValueBoost} index={index} name={name} />;
case BoostType.Functional:
return <FunctionalBoostForm boost={boost} index={index} name={name} />;
return <FunctionalBoostForm boost={boost as FunctionalBoost} index={index} name={name} />;
case BoostType.Proximity:
return <ProximityBoostForm boost={boost} index={index} name={name} />;
return <ProximityBoostForm boost={boost as ProximityBoost} index={index} name={name} />;
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import { shallow, ShallowWrapper } from 'enzyme';

import { EuiSelect } from '@elastic/eui';

import { Boost, BoostOperation, BoostType, FunctionalBoostFunction } from '../../types';
import { FunctionalBoost, BoostOperation, BoostType, FunctionalBoostFunction } from '../../types';

import { FunctionalBoostForm } from './functional_boost_form';

describe('FunctionalBoostForm', () => {
const boost: Boost = {
const boost: FunctionalBoost = {
value: undefined,
factor: 2,
type: 'functional' as BoostType,
function: 'logarithmic' as FunctionalBoostFunction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ import {
FUNCTIONAL_BOOST_FUNCTION_DISPLAY_MAP,
} from '../../constants';
import {
Boost,
FunctionalBoost,
BoostFunction,
BoostOperation,
BoostType,
FunctionalBoostFunction,
} from '../../types';

interface Props {
boost: Boost;
boost: FunctionalBoost;
index: number;
name: string;
}
Expand All @@ -39,7 +39,7 @@ const functionOptions = Object.values(FunctionalBoostFunction).map((boostFunctio

const operationOptions = Object.values(BoostOperation).map((boostOperation) => ({
value: boostOperation,
text: BOOST_OPERATION_DISPLAY_MAP[boostOperation as BoostOperation],
text: BOOST_OPERATION_DISPLAY_MAP[boostOperation],
}));

export const FunctionalBoostForm: React.FC<Props> = ({ boost, index, name }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import { shallow, ShallowWrapper } from 'enzyme';

import { EuiFieldText, EuiSelect } from '@elastic/eui';

import { Boost, BoostType, ProximityBoostFunction } from '../../types';
import { ProximityBoost, BoostType, ProximityBoostFunction } from '../../types';

import { ProximityBoostForm } from './proximity_boost_form';

describe('ProximityBoostForm', () => {
const boost: Boost = {
const boost: ProximityBoost = {
value: undefined,
operation: undefined,
factor: 2,
type: 'proximity' as BoostType,
function: 'linear' as ProximityBoostFunction,
Expand Down Expand Up @@ -46,8 +48,8 @@ describe('ProximityBoostForm', () => {

describe('various boost values', () => {
const renderWithBoostValues = (boostValues: {
center?: Boost['center'];
function?: Boost['function'];
center?: ProximityBoost['center'];
function?: ProximityBoost['function'];
}) => {
return shallow(
<ProximityBoostForm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import { i18n } from '@kbn/i18n';

import { RelevanceTuningLogic } from '../..';
import { PROXIMITY_BOOST_FUNCTION_DISPLAY_MAP } from '../../constants';
import { Boost, BoostType, ProximityBoostFunction } from '../../types';
import { BoostType, ProximityBoost, ProximityBoostFunction } from '../../types';

interface Props {
boost: Boost;
boost: ProximityBoost;
index: number;
name: string;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import { shallow, ShallowWrapper } from 'enzyme';

import { EuiButton, EuiButtonIcon, EuiFieldText } from '@elastic/eui';

import { Boost, BoostType } from '../../types';
import { ValueBoost, BoostType } from '../../types';

import { ValueBoostForm } from './value_boost_form';

describe('ValueBoostForm', () => {
const boost: Boost = {
const boost: ValueBoost = {
operation: undefined,
function: undefined,
factor: 2,
type: 'value' as BoostType,
value: ['bar', '', 'baz'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import {
import { i18n } from '@kbn/i18n';

import { RelevanceTuningLogic } from '../..';
import { Boost } from '../../types';
import { ValueBoost } from '../../types';

interface Props {
boost: Boost;
boost: ValueBoost;
index: number;
name: string;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,24 @@ describe('Boosts', () => {
expect(select.prop('options').map((o: any) => o.value)).toEqual(['add-boost', 'proximity']);
});

it('will not render functional option if "type" prop is "date"', () => {
const wrapper = shallow(
<Boosts
{...{
...props,
type: 'date' as SchemaTypes,
}}
/>
);

const select = wrapper.find(EuiSuperSelect);
expect(select.prop('options').map((o: any) => o.value)).toEqual([
'add-boost',
'proximity',
'value',
]);
});

it('will add a boost of the selected type when a selection is made', () => {
const wrapper = shallow(<Boosts {...props} />);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { EuiFlexGroup, EuiFlexItem, EuiPanel, EuiTitle, EuiSuperSelect } from '@

import { i18n } from '@kbn/i18n';

import { GEOLOCATION, TEXT } from '../../../../shared/constants/field_types';
import { GEOLOCATION, TEXT, DATE } from '../../../../shared/constants/field_types';
import { SchemaTypes } from '../../../../shared/types';

import { BoostIcon } from '../boost_icon';
Expand Down Expand Up @@ -70,6 +70,8 @@ const filterInvalidOptions = (value: BoostType, type: SchemaTypes) => {
if (type === TEXT && [BoostType.Proximity, BoostType.Functional].includes(value)) return false;
// Value and Functional boost types are not valid for geolocation fields
if (type === GEOLOCATION && [BoostType.Functional, BoostType.Value].includes(value)) return false;
// Functional boosts are not valid for date fields
if (type === DATE && value === BoostType.Functional) return false;
return true;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import { i18n } from '@kbn/i18n';
import {
BoostOperation,
BoostType,
FunctionalBoost,
FunctionalBoostFunction,
ProximityBoost,
ProximityBoostFunction,
ValueBoost,
} from './types';

export const FIELD_FILTER_CUTOFF = 10;
Expand Down Expand Up @@ -77,6 +80,38 @@ export const BOOST_TYPE_TO_ICON_MAP = {
[BoostType.Proximity]: 'tokenGeo',
};

const EMPTY_VALUE_BOOST: ValueBoost = {
type: BoostType.Value,
factor: 1,
newBoost: true,
function: undefined,
operation: undefined,
};

const EMPTY_FUNCTIONAL_BOOST: FunctionalBoost = {
value: undefined,
type: BoostType.Functional,
factor: 1,
newBoost: true,
function: FunctionalBoostFunction.Logarithmic,
operation: BoostOperation.Multiply,
};

const EMPTY_PROXIMITY_BOOST: ProximityBoost = {
value: undefined,
type: BoostType.Proximity,
factor: 1,
newBoost: true,
operation: undefined,
function: ProximityBoostFunction.Gaussian,
};

export const BOOST_TYPE_TO_EMPTY_BOOST = {
[BoostType.Value]: EMPTY_VALUE_BOOST,
[BoostType.Functional]: EMPTY_FUNCTIONAL_BOOST,
[BoostType.Proximity]: EMPTY_PROXIMITY_BOOST,
};

export const ADD_DISPLAY = i18n.translate(
'xpack.enterpriseSearch.appSearch.engine.relevanceTuning.boosts.addOperationDropDownOptionLabel',
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,9 @@ describe('RelevanceTuningLogic', () => {
factor: 1,
newBoost: true,
type: BoostType.Functional,
function: 'logarithmic',
operation: 'multiply',
value: undefined,
},
],
},
Expand All @@ -744,6 +747,9 @@ describe('RelevanceTuningLogic', () => {
factor: 1,
newBoost: true,
type: BoostType.Functional,
function: 'logarithmic',
operation: 'multiply',
value: undefined,
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,9 @@ import {
RESET_CONFIRMATION_MESSAGE,
DELETE_SUCCESS_MESSAGE,
DELETE_CONFIRMATION_MESSAGE,
BOOST_TYPE_TO_EMPTY_BOOST,
} from './constants';
import {
BaseBoost,
Boost,
BoostFunction,
BoostOperation,
BoostType,
SearchSettings,
} from './types';
import { Boost, BoostFunction, BoostOperation, BoostType, SearchSettings } from './types';
import {
filterIfTerm,
parseBoostCenter,
Expand Down Expand Up @@ -87,12 +81,12 @@ interface RelevanceTuningActions {
updateBoostSelectOption(
name: string,
boostIndex: number,
optionType: keyof BaseBoost,
optionType: keyof Pick<Boost, 'operation' | 'function'>,
value: BoostOperation | BoostFunction
): {
name: string;
boostIndex: number;
optionType: keyof BaseBoost;
optionType: keyof Pick<Boost, 'operation' | 'function'>;
value: string;
};
updateSearchValue(query: string): string;
Expand Down Expand Up @@ -376,7 +370,7 @@ export const RelevanceTuningLogic = kea<
addBoost: ({ name, type }) => {
const { searchSettings } = values;
const { boosts } = searchSettings;
const emptyBoost = { type, factor: 1, newBoost: true };
const emptyBoost = BOOST_TYPE_TO_EMPTY_BOOST[type];
let boostArray;

if (Array.isArray(boosts[name])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,38 @@ export enum BoostOperation {
Add = 'add',
Multiply = 'multiply',
}

export interface BaseBoost {
export interface Boost {
type: BoostType;
operation?: BoostOperation;
function?: BoostFunction;
newBoost?: boolean;
center?: string | number;
factor: number;
value?: string[];
}

// A boost that comes from the server, before we normalize it has a much looser schema
export interface RawBoost extends BaseBoost {
type: BoostType;
newBoost?: boolean;
center?: string | number;
export interface RawBoost extends Omit<Boost, 'value'> {
value?: string | number | boolean | object | Array<string | number | boolean | object>;
factor: number;
}

// We normalize raw boosts to make them safer and easier to work with
export interface Boost extends RawBoost {
export interface ValueBoost extends Boost {
value?: string[];
operation: undefined;
function: undefined;
}

export interface FunctionalBoost extends Boost {
value: undefined;
operation: BoostOperation;
function: FunctionalBoostFunction;
}

export interface ProximityBoost extends Boost {
value: undefined;
operation: undefined;
function: ProximityBoostFunction;
}
export interface SearchField {
weight: number;
}
Expand Down

0 comments on commit f37eafd

Please sign in to comment.