diff --git a/public/components/common/search/query_area.tsx b/public/components/common/search/query_area.tsx index 2345f0b2cb..b176f45596 100644 --- a/public/components/common/search/query_area.tsx +++ b/public/components/common/search/query_area.tsx @@ -37,7 +37,7 @@ export function QueryArea({ const memoizedGetAvailableFields = useMemo(() => getAvailableFields, []); const memoizedHandleQueryChange = useMemo(() => handleQueryChange, []); useEffect(() => { - const indexQuery = `source = ${selectedIndex[0].label}`; + const indexQuery = `source = ${selectedIndex[0]?.label || ''}`; memoizedHandleQueryChange(indexQuery); memoizedGetAvailableFields(indexQuery); }, [selectedIndex, memoizedGetAvailableFields, memoizedHandleQueryChange]); diff --git a/public/components/common/search/search.tsx b/public/components/common/search/search.tsx index a99e02763e..5bf9671ca1 100644 --- a/public/components/common/search/search.tsx +++ b/public/components/common/search/search.tsx @@ -282,9 +282,7 @@ export const Search = (props: any) => { }; // STATE FOR LANG PICKER AND INDEX PICKER - const [selectedIndex, setSelectedIndex] = useState([ - { label: 'opensearch_dashboards_sample_data_logs' }, - ]); + const [selectedIndex, setSelectedIndex] = useState([]); const { data: indices, loading: indicesLoading } = useCatIndices(); const { data: indexPatterns, loading: indexPatternsLoading } = useGetIndexPatterns(); const indicesAndIndexPatterns = @@ -292,9 +290,22 @@ export const Search = (props: any) => { ? [...indexPatterns, ...indices].filter( (v1, index, array) => array.findIndex((v2) => v1.label === v2.label) === index ) - : undefined; + : []; const loading = indicesLoading || indexPatternsLoading; + useEffect(() => { + if (selectedIndex.length || !indicesAndIndexPatterns.length) return; + // pre-fill selected index with sample logs or other sample data index + const sampleLogOption = indicesAndIndexPatterns.find( + (option) => option.label === 'opensearch_dashboards_sample_data_logs' + ); + if (sampleLogOption) setSelectedIndex([sampleLogOption]); + const sampleDataOption = indicesAndIndexPatterns.find((option) => + option.label.startsWith('opensearch_dashboards_sample_data_') + ); + if (sampleDataOption) setSelectedIndex([sampleDataOption]); + }, [indicesAndIndexPatterns]); + const onLanguagePopoverClick = () => { setLanguagePopoverOpen(!_isLanguagePopoverOpen); }; @@ -352,7 +363,7 @@ export const Search = (props: any) => { Index} singleSelection={{ asPlainText: true }} isLoading={loading} diff --git a/public/components/event_analytics/explorer/query_assist/__tests__/__snapshots__/callouts.test.tsx.snap b/public/components/event_analytics/explorer/query_assist/__tests__/__snapshots__/callouts.test.tsx.snap index 4bd92a4253..6989a255d8 100644 --- a/public/components/event_analytics/explorer/query_assist/__tests__/__snapshots__/callouts.test.tsx.snap +++ b/public/components/event_analytics/explorer/query_assist/__tests__/__snapshots__/callouts.test.tsx.snap @@ -4,7 +4,7 @@ exports[`Callouts spec EmptyQueryCallOut should match snapshot 1`] = `
= (props) /> ); +export const EmptyIndexCallOut: React.FC = (props) => ( + +); + export const EmptyQueryCallOut: React.FC = (props) => ( > = (props const summaryData = useSelector(selectQueryAssistantSummarization)[props.tabId]; const loading = summaryData.loading; const inputRef = useRef(null); + const selectedIndex = props.selectedIndex[0]?.label || ''; useEffect(() => { if ( @@ -162,7 +163,7 @@ export const QueryAssistInput: React.FC> = (props const generatedPPL = await getOSDHttp().post(QUERY_ASSIST_API.GENERATE_PPL, { body: JSON.stringify({ question: props.nlqInput, - index: props.selectedIndex[0].label, + index: selectedIndex, }), }); await props.handleQueryChange(generatedPPL); @@ -182,7 +183,10 @@ export const QueryAssistInput: React.FC> = (props const generatePPL = async () => { dispatch(reset({ tabId: props.tabId })); dispatch(resetSummary({ tabId: props.tabId })); - if (!props.selectedIndex.length) return; + if (!selectedIndex) { + props.setCallOut(); + return; + } if (props.nlqInput.trim().length === 0) { props.setCallOut(); return; @@ -208,7 +212,7 @@ export const QueryAssistInput: React.FC> = (props const isError = summaryData.responseForSummaryStatus === 'failure'; const summarizationContext: SummarizationContext = { question: props.nlqInput, - index: props.selectedIndex[0].label, + index: selectedIndex, isError, query: queryRedux.rawQuery, response: isError @@ -273,7 +277,10 @@ export const QueryAssistInput: React.FC> = (props const runAndSummarize = async () => { dispatch(reset({ tabId: props.tabId })); dispatch(resetSummary({ tabId: props.tabId })); - if (!props.selectedIndex.length) return; + if (!selectedIndex) { + props.setCallOut(); + return; + } if (props.nlqInput.trim().length === 0) { props.setCallOut(); return; @@ -309,8 +316,8 @@ export const QueryAssistInput: React.FC> = (props > = (props }} > - {HARDCODED_SUGGESTIONS[props.selectedIndex[0]?.label]?.map((question) => ( + {HARDCODED_SUGGESTIONS[selectedIndex]?.map((question) => ( { props.setNlqInput(question);