Skip to content
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

Refactoring the quries into a single one to get the search results on Home Page #4372

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions datahub-web-react/src/Mocks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2967,4 +2967,69 @@ export const mocks = [
},
},
},
{
request: {
query: GetSearchResultsForMultipleDocument,
variables: {
input: {
types: [],
query: '*',
start: 0,
count: 20,
filters: [],
},
},
},
result: {
data: {
__typename: 'Query',
searchAcrossEntities: {
__typename: 'SearchResults',
start: 0,
count: 1,
total: 1,
searchResults: [
{
entity: {
__typename: 'Dataset',
...dataset3,
},
matchedFields: [],
insights: [],
},
{
entity: {
__typename: 'Dataset',
...dataset4,
},
matchedFields: [],
insights: [],
},
],
facets: [
{
field: 'origin',
displayName: 'origin',
aggregations: [
{
value: 'PROD',
count: 3,
entity: null,
},
],
},
{
field: 'platform',
displayName: 'platform',
aggregations: [
{ value: 'hdfs', count: 1, entity: null },
{ value: 'mysql', count: 1, entity: null },
{ value: 'kafka', count: 1, entity: null },
],
},
],
},
} as GetSearchResultsForMultipleQuery,
},
},
];
80 changes: 27 additions & 53 deletions datahub-web-react/src/app/home/HomePageHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import { useGetAuthenticatedUser } from '../useGetAuthenticatedUser';
import { useEntityRegistry } from '../useEntityRegistry';
import { navigateToSearchUrl } from '../search/utils/navigateToSearchUrl';
import { SearchBar } from '../search/SearchBar';
import { GetSearchResultsQuery, useGetAutoCompleteMultipleResultsLazyQuery } from '../../graphql/search.generated';
import { useGetAllEntitySearchResults } from '../../utils/customGraphQL/useGetAllEntitySearchResults';
import {
useGetAutoCompleteMultipleResultsLazyQuery,
useGetSearchResultsForMultipleQuery,
} from '../../graphql/search.generated';
import { EntityType } from '../../types.generated';
import analytics, { EventType } from '../analytics';
import { AdminHeaderLinks } from '../shared/admin/AdminHeaderLinks';
Expand Down Expand Up @@ -96,28 +98,6 @@ const SearchBarContainer = styled.div`
text-align: center;
`;

function getSuggestionFieldsFromResult(result: GetSearchResultsQuery | undefined): string[] {
return (
(result?.search?.searchResults
?.map((searchResult) => searchResult.entity)
?.map((entity) => {
switch (entity.__typename) {
case 'Dataset':
return entity.name.split('.').slice(-1)[0];
case 'CorpUser':
return entity.username;
case 'Chart':
return entity.properties?.name;
case 'Dashboard':
return entity.properties?.name;
default:
return undefined;
}
})
.filter(Boolean) as string[]) || []
);
}

function truncate(input, length) {
if (input.length > length) {
return `${input.substring(0, length)}...`;
Expand Down Expand Up @@ -166,38 +146,32 @@ export const HomePageHeader = () => {
}
};

// fetch some results from each entity to display search suggestions
const allSearchResultsByType = useGetAllEntitySearchResults({
query: '*',
start: 0,
count: 20,
filters: [],
// Fetch results
const { data: searchResultsData } = useGetSearchResultsForMultipleQuery({
variables: {
input: {
types: [],
query: '*',
start: 0,
count: 20,
filters: [],
},
},
});

const suggestionsLoading = Object.keys(allSearchResultsByType).some((type) => {
return allSearchResultsByType[type].loading;
});
const searchResultsToShow = useMemo(() => {
let result: string[] | undefined = [];
if (searchResultsData) {
const entities = searchResultsData?.searchAcrossEntities?.searchResults.map((searchResult) => {
return searchResult?.entity;
});

const suggestionsToShow = useMemo(() => {
let result: string[] = [];
if (!suggestionsLoading) {
// TODO: Make this more dynamic.
// Add a ticket.
// Colored Tags: Feature Request...
// ...
[EntityType.Dashboard, EntityType.Chart, EntityType.Dataset].forEach((type) => {
const suggestionsToShowForEntity = getSuggestionFieldsFromResult(
allSearchResultsByType[type]?.data,
).sort(sortRandom);
const suggestionToAddToFront = suggestionsToShowForEntity?.pop();
result = [...result, ...suggestionsToShowForEntity];
if (suggestionToAddToFront) {
result.splice(0, 0, suggestionToAddToFront);
}
result = entities?.map((entity) => {
return entityRegistry.getDisplayName(entity.type, entity);
});
}
return result;
}, [suggestionsLoading, allSearchResultsByType]);
return result?.sort(sortRandom);
}, [searchResultsData, entityRegistry]);

return (
<Background>
Expand Down Expand Up @@ -232,11 +206,11 @@ export const HomePageHeader = () => {
autoCompleteStyle={styles.searchBox}
entityRegistry={entityRegistry}
/>
{suggestionsToShow && suggestionsToShow.length > 0 && (
{searchResultsToShow && searchResultsToShow.length > 0 && (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: we can simplify to

searchResultsToShow?.length > 0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As We are using

let result: string[] | undefined = [];

That's why added check searchResultsToShow && searchResultsToShow.length > 0.

<SuggestionsContainer>
<SuggestedQueriesText strong>Try searching for</SuggestedQueriesText>
<SuggestionTagContainer>
{suggestionsToShow.slice(0, 3).map((suggestion) => (
{searchResultsToShow.slice(0, 3).map((suggestion) => (
<SuggestionButton
key={suggestion}
type="link"
Expand Down