Skip to content

Commit 69fb523

Browse files
committed
Port over index mappings if selecting from existing index
Signed-off-by: Tyler Ohlsen <ohltyler@amazon.com>
1 parent 3ff7b00 commit 69fb523

File tree

3 files changed

+49
-32
lines changed

3 files changed

+49
-32
lines changed

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

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

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

+38-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,43 @@ 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 = findKey(
127+
// existingMappingsObj?.properties,
128+
// (field) => field.type === 'knn_vector'
129+
// );
130+
const existingEmbeddingField = getExistingVectorField(
131+
existingMappingsObj
132+
);
133+
const existingEmbeddingFieldValue = getIn(
134+
existingMappingsObj,
135+
`properties.${existingEmbeddingField}`
136+
);
137+
if (
138+
existingEmbeddingField !== undefined &&
139+
existingEmbeddingFieldValue !== undefined
140+
) {
141+
updatedMappings.properties = {
142+
...updatedMappings.properties,
143+
[existingEmbeddingField]: existingEmbeddingFieldValue,
144+
};
136145
}
137-
}
138-
});
139-
}
146+
} catch {}
147+
setFieldValue(indexMappingsPath, customStringify(updatedMappings));
148+
}
149+
setIsUpdating(false);
150+
});
140151
} else {
141152
setIsUpdating(false);
142153
}

public/utils/utils.tsx

+10-4
Original file line numberDiff line numberDiff line change
@@ -890,10 +890,7 @@ export function removeVectorFieldFromIndexMappings(
890890
): string {
891891
try {
892892
let existingMappingsObj = JSON.parse(existingMappings);
893-
const existingEmbeddingField = findKey(
894-
existingMappingsObj?.properties,
895-
(field) => field.type === 'knn_vector'
896-
);
893+
const existingEmbeddingField = getExistingVectorField(existingMappingsObj);
897894
if (existingEmbeddingField !== undefined) {
898895
unset(existingMappingsObj?.properties, existingEmbeddingField);
899896
}
@@ -903,6 +900,15 @@ export function removeVectorFieldFromIndexMappings(
903900
}
904901
}
905902

903+
export function getExistingVectorField(
904+
existingMappings: any
905+
): string | undefined {
906+
return findKey(
907+
existingMappings?.properties,
908+
(field) => field.type === 'knn_vector'
909+
);
910+
}
911+
906912
// Parse out any hidden errors within a 2xx ingest response
907913
export function parseErrorsFromIngestResponse(
908914
ingestResponse: any

0 commit comments

Comments
 (0)