Skip to content

Commit

Permalink
feat(#1317036, #1317038): [Example app] Use 'In' operator in facets f…
Browse files Browse the repository at this point in the history
…or date and location attributes
  • Loading branch information
botisSmile committed Oct 28, 2024
1 parent 03423a0 commit 79c3e27
Showing 1 changed file with 4 additions and 70 deletions.
74 changes: 4 additions & 70 deletions front/example-app/src/services/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@ import {
} from '@elastic-suite/gally-admin-shared'

import { IActiveFilters } from '../types'
import { add, format, parse } from 'date-fns'

type DurationKey = 'y' | 'M' | 'd'

const durationFormat: Record<DurationKey, string> = {
y: 'years',
M: 'months',
d: 'days',
}

/* eslint-disable no-underscore-dangle */
export function getProductFilters(
Expand Down Expand Up @@ -44,7 +35,10 @@ export function getProductFilters(
acc[activeFilter.filter.field as keyof IProductFieldFilterInput] = {
lte: Number(activeFilter.value),
} as IEntityIntegerTypeFilterInput
} else if (activeFilter.filter.type === AggregationType.CHECKBOX) {
} else if (activeFilter.filter.type === AggregationType.CHECKBOX
|| activeFilter.filter.type === AggregationType.HISTOGRAM_DATE
|| activeFilter.filter.type === AggregationType.HISTOGRAM
) {
if (!(activeFilter.filter.field in acc)) {
acc[activeFilter.filter.field as keyof IProductFieldFilterInput] = {
in: [],
Expand All @@ -55,66 +49,6 @@ export function getProductFilters(
activeFilter.filter.field as keyof IProductFieldFilterInput
] as ISelectTypeDefaultFilterInputType
).in.push(activeFilter.value)
} else if (activeFilter.filter.type === AggregationType.HISTOGRAM_DATE) {
if (!acc.boolFilter?._should) {
acc.boolFilter = { _should: [] }
}
const field = activeFilter.filter
.field as keyof IProductFieldFilterInput
const date = parse(
activeFilter.value,
activeFilter.filter.date_format,
new Date()
)

const incrementString = activeFilter.filter.date_range_interval
const incrementNumber = Number(
incrementString.substring(0, incrementString.length - 1)
)
const incrementType = incrementString.substring(
incrementString.length - 1
) as DurationKey

const gt = format(date, activeFilter.filter.date_format)
const lt = format(
add(date, {
[durationFormat[incrementType]]: incrementNumber,
}),
activeFilter.filter.date_format
)

acc.boolFilter._should.push({
[field]: {
gt,
lt,
},
})
} else if (activeFilter.filter.type === AggregationType.HISTOGRAM) {
if (!acc.boolFilter?._should) {
acc.boolFilter = { _should: [] }
}
const field = activeFilter.filter
.field as keyof IProductFieldFilterInput
const arrayValue = activeFilter.value.split('-')
const [firstValue, secondValue] = arrayValue

if (arrayValue.length > 1) {
const isFirstValueIsAsterisk = firstValue === '*'
const isSecondValueIsAsterisk = secondValue === '*'
let data: Record<string, number> = {}

if (isFirstValueIsAsterisk && !isSecondValueIsAsterisk) {
data = { lt: Number(secondValue) }
} else if (!isFirstValueIsAsterisk && !isSecondValueIsAsterisk) {
data = { gte: Number(firstValue), lte: Number(secondValue) }
} else {
data = { gt: Number(firstValue) }
}

acc.boolFilter._should.push({
[field]: data,
})
}
}

return data
Expand Down

0 comments on commit 79c3e27

Please sign in to comment.