@@ -25,12 +25,12 @@ import {
25
25
} from '@elastic/eui' ;
26
26
import { JsonLinesField } from '../input_fields' ;
27
27
import {
28
+ customStringify ,
28
29
customStringifySingleLine ,
29
30
FETCH_ALL_QUERY_LARGE ,
30
31
IConfigField ,
31
32
IndexMappings ,
32
33
IngestDocsFormValues ,
33
- isVectorSearchUseCase ,
34
34
JSONLINES_LINK ,
35
35
MAX_BYTES_FORMATTED ,
36
36
MAX_DOCS_TO_IMPORT ,
@@ -48,10 +48,10 @@ import {
48
48
} from '../../../../store' ;
49
49
import {
50
50
getDataSourceId ,
51
+ getExistingVectorField ,
51
52
getFieldSchema ,
52
53
getInitialValue ,
53
54
} from '../../../../utils' ;
54
- import { getProcessorInfo } from './source_data' ;
55
55
import '../../../../global-styles.scss' ;
56
56
57
57
interface SourceDataProps {
@@ -71,6 +71,7 @@ export function SourceDataModal(props: SourceDataProps) {
71
71
const dataSourceId = getDataSourceId ( ) ;
72
72
const { values, setFieldValue } = useFormikContext < WorkflowFormValues > ( ) ;
73
73
const indices = useSelector ( ( state : AppState ) => state . opensearch . indices ) ;
74
+ const indexMappingsPath = 'ingest.index.mappings' ;
74
75
75
76
// sub-form values/schema
76
77
const docsFormValues = {
@@ -110,33 +111,43 @@ export function SourceDataModal(props: SourceDataProps) {
110
111
// 1. Update the form with the temp docs
111
112
setFieldValue ( 'ingest.docs' , tempDocs ) ;
112
113
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
114
116
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
+ } ;
136
145
}
137
- }
138
- } ) ;
139
- }
146
+ } catch { }
147
+ setFieldValue ( indexMappingsPath , customStringify ( updatedMappings ) ) ;
148
+ }
149
+ setIsUpdating ( false ) ;
150
+ } ) ;
140
151
} else {
141
152
setIsUpdating ( false ) ;
142
153
}
0 commit comments