[ML] Fixing model testing with index field selection (#146862)

Fixes issue where changing the selected field or pressing the reload
examples button causes the default field name to be added to the
pipeline.
The inference still works as the field is also renamed in the supplied
docs. but it does mean the pipeline doesn't really match the real index
field name and so can't be used to ingest data.

The fix is to ensure `inferrer.setInputField(..)` is called whenever the
field select changes or examples are reloaded.
This commit is contained in:
James Gowdy 2022-12-05 17:37:09 +00:00 committed by GitHub
parent 91db82a055
commit f7b74d0c4e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 11 deletions

View file

@ -103,8 +103,12 @@ export function useIndexInput({ inferrer }: { inferrer: InferrerType }) {
const [selectedDataViewId, setSelectedDataViewId] = useState<string | undefined>(undefined);
const [selectedDataView, setSelectedDataView] = useState<DataView | null>(null);
const [fieldNames, setFieldNames] = useState<Array<{ value: string; text: string }>>([]);
const [selectedField, setSelectedField] = useState<string | undefined>(undefined);
const selectedField = useObservable(inferrer.getInputField$(), inferrer.getInputField());
const setSelectedField = useCallback(
(fieldName: string) => inferrer.setInputField(fieldName),
[inferrer]
);
useEffect(
function loadDataViewListItems() {
dataViews.getIdsWithTitle().then((items) => {
@ -122,7 +126,6 @@ export function useIndexInput({ inferrer }: { inferrer: InferrerType }) {
function loadSelectedDataView() {
inferrer.reset();
setFieldNames([]);
setSelectedField(undefined);
if (selectedDataViewId !== undefined) {
dataViews.get(selectedDataViewId).then((dv) => setSelectedDataView(dv));
}
@ -158,14 +161,14 @@ export function useIndexInput({ inferrer }: { inferrer: InferrerType }) {
inferrer.setInputText(tempExamples);
});
}
}, [inferrer, selectedField, selectedDataView, search]);
}, [inferrer, selectedDataView, search, selectedField]);
useEffect(
function loadFieldNames() {
if (selectedDataView !== null) {
const tempFieldNames = selectedDataView.fields
.filter(
({ displayName, esTypes, count }) =>
({ displayName, esTypes }) =>
esTypes && esTypes.includes('text') && !['_id', '_index'].includes(displayName)
)
.sort((a, b) => a.displayName.localeCompare(b.displayName))
@ -174,11 +177,9 @@ export function useIndexInput({ inferrer }: { inferrer: InferrerType }) {
text: displayName,
}));
setFieldNames(tempFieldNames);
if (tempFieldNames.length === 1) {
const fieldName = tempFieldNames[0].value;
setSelectedField(fieldName);
inferrer.setInputField(fieldName);
}
const fieldName = tempFieldNames.length === 1 ? tempFieldNames[0].value : undefined;
inferrer.setInputField(fieldName);
}
},
[selectedDataView, inferrer]
@ -188,7 +189,9 @@ export function useIndexInput({ inferrer }: { inferrer: InferrerType }) {
function loadExamplesAfterFieldChange() {
loadExamples();
},
[selectedField, loadExamples]
// only load examples if selectedField changes
// eslint-disable-next-line react-hooks/exhaustive-deps
[selectedField]
);
function reloadExamples() {

View file

@ -151,7 +151,6 @@ export abstract class InferenceBase<TInferResponse> {
}
public reset() {
this.setInputField(undefined);
this.inputText$.next([]);
this.inferenceResult$.next(null);
this.inferenceError$.next(null);
@ -159,6 +158,7 @@ export abstract class InferenceBase<TInferResponse> {
}
public setInputField(field: string | undefined) {
// if the field is not set, change to be the same as the model input field
this.inputField$.next(field === undefined ? this.modelInputField : field);
}
@ -166,6 +166,10 @@ export abstract class InferenceBase<TInferResponse> {
return this.inputField$.getValue();
}
public getInputField$() {
return this.inputField$.asObservable();
}
public setInputText(text: string[]) {
this.inputText$.next(text);
}