Skip to content

Commit

Permalink
Filter out nested fields and their subfields from the relevance UI sc…
Browse files Browse the repository at this point in the history
…reen (#136290)
  • Loading branch information
afoucret authored Jul 18, 2022
1 parent 31f351f commit 974c286
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ describe('RelevanceTuningForm', () => {
filteredSchemaFields: ['foo', 'bar'],
filteredSchemaFieldsWithConflicts: [],
schema: {
foo: 'text',
bar: 'number',
foo: { type: 'text' },
bar: { type: 'number' },
},
searchSettings: {
boosts: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const RelevanceTuningForm: React.FC = () => {
buttonContent={
<RelevanceTuningItem
name={fieldName}
type={schema[fieldName]}
type={schema[fieldName].type}
boosts={searchSettings.boosts && searchSettings.boosts[fieldName]}
field={searchSettings.search_fields[fieldName]}
/>
Expand All @@ -90,7 +90,7 @@ export const RelevanceTuningForm: React.FC = () => {
>
<RelevanceTuningItemContent
name={fieldName}
type={schema[fieldName]}
type={schema[fieldName].type}
boosts={searchSettings.boosts && searchSettings.boosts[fieldName]}
field={searchSettings.search_fields[fieldName]}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ describe('RelevanceTuningLogic', () => {
it('will parse the provided provided value and set the center to that parsed value', () => {
mount({
schema: {
foo: 'number',
foo: { type: 'number', capabilities: { boost: true } },
},
searchSettings: searchSettingsWithBoost({
factor: 1,
Expand Down Expand Up @@ -1046,7 +1046,7 @@ describe('RelevanceTuningLogic', () => {
// consider a tunable field.
mount({
schema: {
id: 'foo',
id: { type: 'text', capabilities: {} },
},
});
expect(RelevanceTuningLogic.values.engineHasSchemaFields).toEqual(false);
Expand All @@ -1055,8 +1055,8 @@ describe('RelevanceTuningLogic', () => {
it('should return true otherwise', () => {
mount({
schema: {
id: 'foo',
bar: 'bar',
id: { type: 'text', capabilities: {} },
text_field: { type: 'text', capabilities: { fulltext: true } },
},
});
expect(RelevanceTuningLogic.values.engineHasSchemaFields).toEqual(true);
Expand All @@ -1067,11 +1067,39 @@ describe('RelevanceTuningLogic', () => {
it('should return the list of field names from the schema', () => {
mount({
schema: {
id: 'foo',
bar: 'bar',
fulltext_only_field: {
type: 'text',
capabilities: {
fulltext: true,
},
},
boost_only_field: {
type: 'text',
capabilities: {
boost: true,
},
},
fulltext_and_boost_field: {
type: 'text',
capabilities: {
fulltext: true,
boost: true,
},
},
ignored_field: {
type: 'text',
capabilities: {
fulltext: false,
boost: false,
},
},
},
});
expect(RelevanceTuningLogic.values.schemaFields).toEqual(['id', 'bar']);
expect(RelevanceTuningLogic.values.schemaFields).toEqual([
'fulltext_only_field',
'boost_only_field',
'fulltext_and_boost_field',
]);
});
});

Expand All @@ -1092,28 +1120,48 @@ describe('RelevanceTuningLogic', () => {
describe('filteredSchemaFields', () => {
it('should return a list of schema field names that contain the text from filterInputValue ', () => {
mount({
filterInputValue: 'ba',
filterInputValue: 'fu',
schema: {
id: 'string',
foo: 'string',
bar: 'string',
baz: 'string',
fulltext_only_field: {
type: 'text',
capabilities: {
fulltext: true,
},
},
boost_only_field: {
type: 'text',
capabilities: {
boost: true,
},
},
fulltext_and_boost_field: {
type: 'text',
capabilities: {
fulltext: true,
boost: true,
},
},
ignored_field: {
type: 'text',
capabilities: {
fulltext: false,
boost: false,
},
},
},
});
expect(RelevanceTuningLogic.values.filteredSchemaFields).toEqual(['bar', 'baz']);
expect(RelevanceTuningLogic.values.filteredSchemaFields).toEqual([
'fulltext_only_field',
'fulltext_and_boost_field',
]);
});
});

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',
},
schema: {},
schemaConflicts: {
bar: {
text: ['source_engine_1'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
clearFlashMessages,
} from '../../../shared/flash_messages';
import { HttpLogic } from '../../../shared/http';
import { Schema, SchemaConflicts } from '../../../shared/schema/types';
import { AdvancedSchema, SchemaConflicts } from '../../../shared/schema/types';

import { EngineLogic } from '../engine';

Expand All @@ -39,7 +39,7 @@ import {

interface RelevanceTuningProps {
searchSettings: SearchSettings;
schema: Schema;
schema: AdvancedSchema;
schemaConflicts?: SchemaConflicts;
}

Expand Down Expand Up @@ -94,7 +94,7 @@ interface RelevanceTuningActions {

interface RelevanceTuningValues {
searchSettings: SearchSettings;
schema: Schema;
schema: AdvancedSchema;
schemaFields: string[];
schemaFieldsWithConflicts: string[];
filteredSchemaFields: string[];
Expand Down Expand Up @@ -221,7 +221,18 @@ export const RelevanceTuningLogic = kea<
],
}),
selectors: ({ selectors }) => ({
schemaFields: [() => [selectors.schema], (schema: Schema) => Object.keys(schema)],
schemaFields: [
() => [selectors.schema],
(schema: AdvancedSchema) =>
Object.entries(schema).reduce(
(fields: string[], [fieldName, { capabilities: fieldCapabilities }]) => {
return fieldCapabilities.fulltext || fieldCapabilities.boost
? [...fields, fieldName]
: fields;
},
[]
),
],
schemaFieldsWithConflicts: [
() => [selectors.schemaConflicts],
(schemaConflicts: SchemaConflicts) => Object.keys(schemaConflicts),
Expand All @@ -237,8 +248,8 @@ export const RelevanceTuningLogic = kea<
filterIfTerm(schemaFieldsWithConflicts, filterInputValue),
],
engineHasSchemaFields: [
() => [selectors.schema],
(schema: Schema): boolean => Object.keys(schema).length >= 2,
() => [selectors.schemaFields],
(schemaFields: string[]): boolean => schemaFields.length > 0,
],
isPrecisionTuningEnabled: [
() => [selectors.searchSettings],
Expand Down Expand Up @@ -453,7 +464,7 @@ export const RelevanceTuningLogic = kea<
const { searchSettings } = values;
const { boosts } = searchSettings;
const updatedBoosts = cloneDeep(boosts[name]);
const fieldType = values.schema[name];
const fieldType = values.schema[name].type;
updatedBoosts[boostIndex].center = parseBoostCenter(fieldType, value);

actions.setSearchSettings({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type { SearchResult } from '@elastic/search-ui';
import { i18n } from '@kbn/i18n';

import { KibanaLogic } from '../../../shared/kibana';
import { Schema } from '../../../shared/schema/types';
import { AdvancedSchema, Schema } from '../../../shared/schema/types';

import { ENGINE_DOCUMENT_DETAIL_PATH } from '../../routes';
import { generateEncodedPath } from '../../utils/encode_path_params';
Expand All @@ -32,7 +32,7 @@ interface Props {
showScore?: boolean;
resultPosition?: number;
shouldLinkToDetailPage?: boolean;
schemaForTypeHighlights?: Schema;
schemaForTypeHighlights?: Schema | AdvancedSchema;
actions?: ResultAction[];
dragHandleProps?: DraggableProvidedDragHandleProps;
showClick?: boolean;
Expand Down Expand Up @@ -61,8 +61,20 @@ export const Result: React.FC<Props> = ({
[result]
);
const numResults = resultFields.length;
const isAdvancedSchema = (schema: Schema | AdvancedSchema): schema is AdvancedSchema => {
return (
schema &&
Object.values(schema).reduce((isAdvanced, schemaField) => {
return isAdvanced && typeof schemaField !== 'string';
}, true)
);
};
const typeForField = (fieldName: string) => {
if (schemaForTypeHighlights) return schemaForTypeHighlights[fieldName];
if (schemaForTypeHighlights) {
return isAdvancedSchema(schemaForTypeHighlights)
? schemaForTypeHighlights[fieldName].type
: schemaForTypeHighlights[fieldName];
}
};

const documentLink = shouldLinkToDetailPage
Expand Down

0 comments on commit 974c286

Please sign in to comment.