-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Search][Playground] Support Multiple Context Fields #210703
Merged
TattdCodeMonkey
merged 9 commits into
elastic:main
from
TattdCodeMonkey:playground/multi-context-fields
Feb 21, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9bd765c
chore(search_playground): improve client type usage to be accurate wi…
TattdCodeMonkey 705f8fd
feat(search_playground): support multiple context fields per index wh…
TattdCodeMonkey 4762d28
test(search_playground): support combobox context field selection
TattdCodeMonkey d709b5d
refactor(search_playground): dont lose order of selected context fields
TattdCodeMonkey 99b33b7
feat(search_playground): default context to all source_fields
TattdCodeMonkey 2c8793c
refactor(search_playground): updated to exclude empty field values fr…
TattdCodeMonkey 7d618de
feat(search_playground): update python code example to support multip…
TattdCodeMonkey 0923f78
Update langchain example to support multiple context fields
TattdCodeMonkey 6f5b1ac
Merge branch 'main' into playground/multi-context-fields
elasticmachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
...search/plugins/search_playground/public/components/edit_context/context_fields_select.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React, { useMemo, useCallback } from 'react'; | ||
|
||
import { EuiCallOut, EuiComboBox, EuiComboBoxOptionOption } from '@elastic/eui'; | ||
import { i18n } from '@kbn/i18n'; | ||
|
||
import { QuerySourceFields } from '../../types'; | ||
|
||
export interface ContextFieldsSelectProps { | ||
indexName: string; | ||
indexFields: QuerySourceFields; | ||
selectedContextFields?: string[]; | ||
updateSelectedContextFields: (index: string, value: string[]) => void; | ||
} | ||
|
||
export const ContextFieldsSelect = ({ | ||
indexName, | ||
indexFields, | ||
selectedContextFields, | ||
updateSelectedContextFields, | ||
}: ContextFieldsSelectProps) => { | ||
const { options: selectOptions, selectedOptions } = useMemo(() => { | ||
if (!indexFields.source_fields?.length) return { options: [], selectedOptions: [] }; | ||
|
||
const options: Array<EuiComboBoxOptionOption<unknown>> = indexFields.source_fields.map( | ||
(field) => ({ | ||
label: field, | ||
'data-test-subj': `contextField-${field}`, | ||
}) | ||
); | ||
const selected: Array<EuiComboBoxOptionOption<unknown>> = | ||
selectedContextFields | ||
?.map((field) => options.find((opt) => opt.label === field)) | ||
?.filter( | ||
( | ||
val: EuiComboBoxOptionOption<unknown> | undefined | ||
): val is EuiComboBoxOptionOption<unknown> => val !== undefined | ||
) ?? []; | ||
return { | ||
options, | ||
selectedOptions: selected, | ||
}; | ||
}, [indexFields.source_fields, selectedContextFields]); | ||
const onSelectFields = useCallback( | ||
(updatedSelectedOptions: Array<EuiComboBoxOptionOption<unknown>>) => { | ||
// always require at least 1 selected field | ||
if (updatedSelectedOptions.length === 0) return; | ||
updateSelectedContextFields( | ||
indexName, | ||
updatedSelectedOptions.map((opt) => opt.label) | ||
); | ||
}, | ||
[indexName, updateSelectedContextFields] | ||
); | ||
|
||
if (selectOptions.length === 0) { | ||
return ( | ||
<EuiCallOut | ||
title={i18n.translate('xpack.searchPlayground.editContext.noSourceFieldWarning', { | ||
defaultMessage: 'No source fields found', | ||
})} | ||
color="warning" | ||
iconType="warning" | ||
size="s" | ||
/> | ||
); | ||
} | ||
|
||
return ( | ||
<EuiComboBox | ||
data-test-subj={`contextFieldsSelectable-${indexName}`} | ||
options={selectOptions} | ||
selectedOptions={selectedOptions} | ||
onChange={onSelectFields} | ||
isClearable={false} | ||
fullWidth | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,24 +5,16 @@ | |
* 2.0. | ||
*/ | ||
|
||
import { | ||
EuiFlexGroup, | ||
EuiFlexItem, | ||
EuiFormRow, | ||
EuiPanel, | ||
EuiSelect, | ||
EuiSuperSelect, | ||
EuiText, | ||
EuiCallOut, | ||
} from '@elastic/eui'; | ||
import { EuiFlexGroup, EuiFlexItem, EuiFormRow, EuiPanel, EuiSelect, EuiText } from '@elastic/eui'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { FormattedMessage } from '@kbn/i18n-react'; | ||
import React from 'react'; | ||
import React, { useCallback } from 'react'; | ||
import { useController } from 'react-hook-form'; | ||
import { useSourceIndicesFields } from '../../hooks/use_source_indices_field'; | ||
import { useUsageTracker } from '../../hooks/use_usage_tracker'; | ||
import { ChatForm, ChatFormFields } from '../../types'; | ||
import { AnalyticsEvents } from '../../analytics/constants'; | ||
import { ContextFieldsSelect } from './context_fields_select'; | ||
|
||
export const EditContextPanel: React.FC = () => { | ||
const usageTracker = useUsageTracker(); | ||
|
@@ -40,13 +32,16 @@ export const EditContextPanel: React.FC = () => { | |
name: ChatFormFields.sourceFields, | ||
}); | ||
|
||
const updateSourceField = (index: string, field: string) => { | ||
onChangeSourceFields({ | ||
...sourceFields, | ||
[index]: [field], | ||
}); | ||
usageTracker?.click(AnalyticsEvents.editContextFieldToggled); | ||
}; | ||
const updateSourceField = useCallback( | ||
(index: string, contextFields: string[]) => { | ||
onChangeSourceFields({ | ||
...sourceFields, | ||
[index]: contextFields, | ||
}); | ||
usageTracker?.click(AnalyticsEvents.editContextFieldToggled); | ||
}, | ||
[onChangeSourceFields, sourceFields, usageTracker] | ||
); | ||
|
||
const handleDocSizeChange = (e: React.ChangeEvent<HTMLSelectElement>) => { | ||
usageTracker?.click(AnalyticsEvents.editContextDocSizeChanged); | ||
|
@@ -64,6 +59,7 @@ export const EditContextPanel: React.FC = () => { | |
fullWidth | ||
> | ||
<EuiSelect | ||
data-test-subj="contextPanelDocumentNumberSelect" | ||
options={[ | ||
{ | ||
value: 1, | ||
|
@@ -100,32 +96,15 @@ export const EditContextPanel: React.FC = () => { | |
</h5> | ||
</EuiText> | ||
</EuiFlexItem> | ||
{Object.entries(fields).map(([index, group], indexNum) => ( | ||
{Object.entries(fields).map(([index, group]) => ( | ||
<EuiFlexItem grow={false} key={index}> | ||
<EuiFormRow label={index} fullWidth> | ||
{!!group.source_fields?.length ? ( | ||
<EuiSuperSelect | ||
data-test-subj={`contextFieldsSelectable-${indexNum}`} | ||
options={group.source_fields.map((field) => ({ | ||
value: field, | ||
inputDisplay: field, | ||
'data-test-subj': 'contextField', | ||
}))} | ||
valueOfSelected={sourceFields[index]?.[0]} | ||
onChange={(value) => updateSourceField(index, value)} | ||
fullWidth | ||
/> | ||
) : ( | ||
<EuiCallOut | ||
title={i18n.translate( | ||
'xpack.searchPlayground.editContext.noSourceFieldWarning', | ||
{ defaultMessage: 'No source fields found' } | ||
)} | ||
color="warning" | ||
iconType="warning" | ||
size="s" | ||
/> | ||
)} | ||
<ContextFieldsSelect | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. neat! |
||
indexName={index} | ||
indexFields={group} | ||
selectedContextFields={sourceFields[index] ?? []} | ||
updateSelectedContextFields={updateSourceField} | ||
/> | ||
</EuiFormRow> | ||
</EuiFlexItem> | ||
))} | ||
|
105 changes: 100 additions & 5 deletions
105
...layground/public/components/view_code/examples/__snapshots__/py_lang_client.test.tsx.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't we avoid defining
unknown
andany
as a type?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should avoid
any
,uknown
is perfectly fine.Using
unknown
TS will make you do type checks to validate what the type is. But in this case thats the option data type and what we need to access is for theEuiComboBoxOptionOption
. So the option type is not needed in this callback currently.