Skip to content

Commit a76655e

Browse files
Update new index mappings if selecting from existing index (#670)
Signed-off-by: Tyler Ohlsen <ohltyler@amazon.com> (cherry picked from commit 101472b) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent fe61162 commit a76655e

File tree

3 files changed

+45
-32
lines changed

3 files changed

+45
-32
lines changed

public/pages/workflow_detail/workflow_inputs/ingest_inputs/source_data.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ export function SourceData(props: SourceDataProps) {
218218
// helper fn to parse out some useful info from the ML ingest processor config, if applicable
219219
// takes on the assumption the first processor is an ML inference processor, and should
220220
// only be executed for workflows coming from preset vector search use cases.
221-
export function getProcessorInfo(
221+
function getProcessorInfo(
222222
uiConfig: WorkflowConfig,
223223
values: WorkflowFormValues
224224
): {

public/pages/workflow_detail/workflow_inputs/ingest_inputs/source_data_modal.tsx

+34-27
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ import {
2525
} from '@elastic/eui';
2626
import { JsonLinesField } from '../input_fields';
2727
import {
28+
customStringify,
2829
customStringifySingleLine,
2930
FETCH_ALL_QUERY_LARGE,
3031
IConfigField,
3132
IndexMappings,
3233
IngestDocsFormValues,
33-
isVectorSearchUseCase,
3434
JSONLINES_LINK,
3535
MAX_BYTES_FORMATTED,
3636
MAX_DOCS_TO_IMPORT,
@@ -48,10 +48,10 @@ import {
4848
} from '../../../../store';
4949
import {
5050
getDataSourceId,
51+
getExistingVectorField,
5152
getFieldSchema,
5253
getInitialValue,
5354
} from '../../../../utils';
54-
import { getProcessorInfo } from './source_data';
5555
import '../../../../global-styles.scss';
5656

5757
interface SourceDataProps {
@@ -71,6 +71,7 @@ export function SourceDataModal(props: SourceDataProps) {
7171
const dataSourceId = getDataSourceId();
7272
const { values, setFieldValue } = useFormikContext<WorkflowFormValues>();
7373
const indices = useSelector((state: AppState) => state.opensearch.indices);
74+
const indexMappingsPath = 'ingest.index.mappings';
7475

7576
// sub-form values/schema
7677
const docsFormValues = {
@@ -110,33 +111,39 @@ export function SourceDataModal(props: SourceDataProps) {
110111
// 1. Update the form with the temp docs
111112
setFieldValue('ingest.docs', tempDocs);
112113

113-
// 2. Update several form values if an index is selected (and if vector search)
114+
// 2. Update several form values if an index is selected. Persist any preset/existing
115+
// embedding fields in the mappings, if found
114116
if (selectedIndex !== undefined) {
115-
if (isVectorSearchUseCase(props.workflow?.ui_metadata?.type)) {
116-
dispatch(getMappings({ index: selectedIndex, dataSourceId }))
117-
.unwrap()
118-
.then((resp: IndexMappings) => {
119-
const { processorId, inputMapEntry } = getProcessorInfo(
120-
props.uiConfig,
121-
values
122-
);
123-
if (processorId !== undefined && inputMapEntry !== undefined) {
124-
// set/overwrite default text field for the input map. may be empty.
125-
if (inputMapEntry !== undefined) {
126-
const textFieldFormPath = `ingest.enrich.${processorId}.input_map.0.0.value`;
127-
const curTextField = getIn(values, textFieldFormPath) as string;
128-
if (!Object.keys(resp.properties).includes(curTextField)) {
129-
const defaultTextField =
130-
Object.keys(resp.properties).find((fieldName) => {
131-
return resp.properties[fieldName]?.type === 'text';
132-
}) || '';
133-
setFieldValue(textFieldFormPath, defaultTextField);
134-
setIsUpdating(false);
135-
}
117+
dispatch(getMappings({ index: selectedIndex, dataSourceId }))
118+
.unwrap()
119+
.then((resp: IndexMappings) => {
120+
if (!isEmpty(resp)) {
121+
let updatedMappings = resp;
122+
try {
123+
let existingMappingsObj = JSON.parse(
124+
getIn(values, indexMappingsPath)
125+
);
126+
const existingEmbeddingField = getExistingVectorField(
127+
existingMappingsObj
128+
);
129+
const existingEmbeddingFieldValue = getIn(
130+
existingMappingsObj,
131+
`properties.${existingEmbeddingField}`
132+
);
133+
if (
134+
existingEmbeddingField !== undefined &&
135+
existingEmbeddingFieldValue !== undefined
136+
) {
137+
updatedMappings.properties = {
138+
...updatedMappings.properties,
139+
[existingEmbeddingField]: existingEmbeddingFieldValue,
140+
};
136141
}
137-
}
138-
});
139-
}
142+
} catch {}
143+
setFieldValue(indexMappingsPath, customStringify(updatedMappings));
144+
}
145+
setIsUpdating(false);
146+
});
140147
} else {
141148
setIsUpdating(false);
142149
}

public/utils/utils.tsx

+10-4
Original file line numberDiff line numberDiff line change
@@ -897,10 +897,7 @@ export function removeVectorFieldFromIndexMappings(
897897
): string {
898898
try {
899899
let existingMappingsObj = JSON.parse(existingMappings);
900-
const existingEmbeddingField = findKey(
901-
existingMappingsObj?.properties,
902-
(field) => field.type === 'knn_vector'
903-
);
900+
const existingEmbeddingField = getExistingVectorField(existingMappingsObj);
904901
if (existingEmbeddingField !== undefined) {
905902
unset(existingMappingsObj?.properties, existingEmbeddingField);
906903
}
@@ -910,6 +907,15 @@ export function removeVectorFieldFromIndexMappings(
910907
}
911908
}
912909

910+
export function getExistingVectorField(
911+
existingMappings: any
912+
): string | undefined {
913+
return findKey(
914+
existingMappings?.properties,
915+
(field) => field.type === 'knn_vector'
916+
);
917+
}
918+
913919
// Parse out any hidden errors within a 2xx ingest response
914920
export function parseErrorsFromIngestResponse(
915921
ingestResponse: any

0 commit comments

Comments
 (0)