Skip to content

Commit

Permalink
fix(ui) Fix filtering logic for everwhere generating OR filters (#9116)
Browse files Browse the repository at this point in the history
  • Loading branch information
chriscollins3456 authored Oct 27, 2023
1 parent cc75115 commit 07a5e4c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
12 changes: 3 additions & 9 deletions datahub-web-react/src/app/search/useGetSearchQueryInputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useLocation, useParams } from 'react-router';
import { useMemo } from 'react';
import { FacetFilterInput, EntityType } from '../../types.generated';
import { useEntityRegistry } from '../useEntityRegistry';
import { ENTITY_FILTER_NAME, FILTER_DELIMITER, UnionType } from './utils/constants';
import { ENTITY_FILTER_NAME, UnionType } from './utils/constants';
import { useUserContext } from '../context/useUserContext';
import useFilters from './utils/useFilters';
import { generateOrFilters } from './utils/generateOrFilters';
Expand All @@ -27,12 +27,6 @@ export default function useGetSearchQueryInputs(excludedFilterFields?: Array<str
const sortInput = useSortInput();

const filters: Array<FacetFilterInput> = useFilters(params);
const nonNestedFilters = filters.filter(
(f) => !f.field.includes(FILTER_DELIMITER) && !excludedFilterFields?.includes(f.field),
);
const nestedFilters = filters.filter(
(f) => f.field.includes(FILTER_DELIMITER) && !excludedFilterFields?.includes(f.field),
);
const entityFilters: Array<EntityType> = useMemo(
() =>
filters
Expand All @@ -43,8 +37,8 @@ export default function useGetSearchQueryInputs(excludedFilterFields?: Array<str
);

const orFilters = useMemo(
() => generateOrFilters(unionType, nonNestedFilters, nestedFilters),
[nonNestedFilters, nestedFilters, unionType],
() => generateOrFilters(unionType, filters, excludedFilterFields),
[filters, excludedFilterFields, unionType],
);

return { entityFilters, query, unionType, filters, orFilters, viewUrn, page, activeType, sortInput };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
DOMAINS_FILTER_NAME,
ENTITY_SUB_TYPE_FILTER_NAME,
ENTITY_TYPE_FILTER_NAME,
ENTITY_FILTER_NAME,
TAGS_FILTER_NAME,
UnionType,
} from '../constants';
Expand All @@ -10,7 +10,7 @@ import { generateOrFilters } from '../generateOrFilters';
describe('generateOrFilters', () => {
it('should generate orFilters with UnionType.AND', () => {
const filters = [
{ field: ENTITY_TYPE_FILTER_NAME, values: ['DATASET', 'CONTAINER'] },
{ field: ENTITY_FILTER_NAME, values: ['DATASET', 'CONTAINER'] },
{ field: TAGS_FILTER_NAME, values: ['urn:li:tag:tag1'] },
];
const orFilters = generateOrFilters(UnionType.AND, filters);
Expand All @@ -24,7 +24,7 @@ describe('generateOrFilters', () => {

it('should generate orFilters with UnionType.OR', () => {
const filters = [
{ field: ENTITY_TYPE_FILTER_NAME, values: ['DATASET', 'CONTAINER'] },
{ field: ENTITY_FILTER_NAME, values: ['DATASET', 'CONTAINER'] },
{ field: TAGS_FILTER_NAME, values: ['urn:li:tag:tag1'] },
];
const orFilters = generateOrFilters(UnionType.OR, filters);
Expand All @@ -43,17 +43,23 @@ describe('generateOrFilters', () => {
const filters = [
{ field: TAGS_FILTER_NAME, values: ['urn:li:tag:tag1'] },
{ field: DOMAINS_FILTER_NAME, values: ['urn:li:domains:domain1'] },
{ field: ENTITY_SUB_TYPE_FILTER_NAME, values: ['CONTAINER', 'DATASET␞table'] },
];
const nestedFilters = [{ field: ENTITY_SUB_TYPE_FILTER_NAME, values: ['CONTAINER', 'DATASET␞table'] }];
const orFilters = generateOrFilters(UnionType.AND, filters, nestedFilters);
// const nestedFilters = [{ field: ENTITY_SUB_TYPE_FILTER_NAME, values: ['CONTAINER', 'DATASET␞table'] }];
const orFilters = generateOrFilters(UnionType.AND, filters);

expect(orFilters).toMatchObject([
{
and: [...filters, { field: '_entityType', values: ['CONTAINER'] }],
and: [
{ field: TAGS_FILTER_NAME, values: ['urn:li:tag:tag1'] },
{ field: DOMAINS_FILTER_NAME, values: ['urn:li:domains:domain1'] },
{ field: '_entityType', values: ['CONTAINER'] },
],
},
{
and: [
...filters,
{ field: TAGS_FILTER_NAME, values: ['urn:li:tag:tag1'] },
{ field: DOMAINS_FILTER_NAME, values: ['urn:li:domains:domain1'] },
{ field: '_entityType', values: ['DATASET'] },
{ field: 'typeNames', values: ['table'] },
],
Expand All @@ -65,9 +71,9 @@ describe('generateOrFilters', () => {
const filters = [
{ field: TAGS_FILTER_NAME, values: ['urn:li:tag:tag1'] },
{ field: DOMAINS_FILTER_NAME, values: ['urn:li:domains:domain1'] },
{ field: ENTITY_SUB_TYPE_FILTER_NAME, values: ['CONTAINER', 'DATASET␞table'] },
];
const nestedFilters = [{ field: ENTITY_SUB_TYPE_FILTER_NAME, values: ['CONTAINER', 'DATASET␞table'] }];
const orFilters = generateOrFilters(UnionType.OR, filters, nestedFilters);
const orFilters = generateOrFilters(UnionType.OR, filters);

expect(orFilters).toMatchObject([
{
Expand All @@ -87,4 +93,18 @@ describe('generateOrFilters', () => {
},
]);
});

it('should generate orFilters and exclude filters with a provided exclude field', () => {
const filters = [
{ field: ENTITY_FILTER_NAME, values: ['DATASET', 'CONTAINER'] },
{ field: TAGS_FILTER_NAME, values: ['urn:li:tag:tag1'] },
];
const orFilters = generateOrFilters(UnionType.AND, filters, [ENTITY_FILTER_NAME]);

expect(orFilters).toMatchObject([
{
and: [{ field: TAGS_FILTER_NAME, values: ['urn:li:tag:tag1'] }],
},
]);
});
});
16 changes: 11 additions & 5 deletions datahub-web-react/src/app/search/utils/generateOrFilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,34 @@ function generateInputWithNestedFilters(filters: FacetFilterInput[], nestedFilte
export function generateOrFilters(
unionType: UnionType,
filters: FacetFilterInput[],
nestedFilters: FacetFilterInput[] = [],
excludedFilterFields: string[] = [],
): AndFilterInput[] {
if ((filters?.length || 0) === 0 && nestedFilters.length === 0) {
if ((filters?.length || 0) === 0) {
return [];
}
const nonNestedFilters = filters.filter(
(f) => !f.field.includes(FILTER_DELIMITER) && !excludedFilterFields?.includes(f.field),
);
const nestedFilters = filters.filter(
(f) => f.field.includes(FILTER_DELIMITER) && !excludedFilterFields?.includes(f.field),
);

if (unionType === UnionType.OR) {
const orFiltersWithNestedFilters = generateInputWithNestedFilters([], nestedFilters);
const orFilters = filters.map((filter) => ({
const orFilters = nonNestedFilters.map((filter) => ({
and: [filter],
}));
return [...orFilters, ...orFiltersWithNestedFilters];
}
const andFiltersWithNestedFilters = generateInputWithNestedFilters(filters, nestedFilters);
const andFiltersWithNestedFilters = generateInputWithNestedFilters(nonNestedFilters, nestedFilters);

if (andFiltersWithNestedFilters.length) {
return andFiltersWithNestedFilters;
}

return [
{
and: filters,
and: nonNestedFilters,
},
];
}

0 comments on commit 07a5e4c

Please sign in to comment.