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

Fix advanced search #2320

Merged
merged 1 commit into from
Nov 4, 2024
Merged
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
66 changes: 41 additions & 25 deletions app/subscriber/src/features/search-page/SearchPage.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { KnnSearchResponse } from '@elastic/elasticsearch/lib/api/types';
import { BasicSearch } from 'components/basic-search';
import { ContentList, ViewOptions } from 'components/content-list';
import { DateFilter } from 'components/date-filter';
import { FilterOptions } from 'components/media-type-filters';
import { PageSection } from 'components/section';
import { ContentListActionBar } from 'components/tool-bar';
import { useElastic } from 'features/my-searches/hooks';
import { castToSearchResult } from 'features/utils';
import { IContentSearchResult } from 'features/utils/interfaces';
import moment from 'moment';
import React from 'react';
import { FaBookmark } from 'react-icons/fa6';
import { useLocation, useParams } from 'react-router-dom';
Expand Down Expand Up @@ -80,26 +83,40 @@ export const SearchPage: React.FC<ISearchType> = ({ showAdvanced }) => {
}, [activeFilter, getFilter, filterId, init, storeFilter, storeSearchFilter, id]);

const groupResults = React.useCallback(
(res: any, currStartDate: Date, currEndDate: Date, groupStoredContent: boolean) => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Never use any

(
res: KnnSearchResponse<IContentModel>,
currStartDate: Date,
currEndDate: Date,
groupStoredContent: boolean,
) => {
const currDateResults: IContentSearchResult[] = [],
prevDateResults: IContentSearchResult[] = [];
res.hits.hits.forEach((h: { _source: IContentSearchResult }) => {
const resDate = new Date(h._source.publishedOn);
res.hits.hits.forEach((h) => {
if (!h._source) return;
const content = castToSearchResult(h._source);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Correct casting

const resDate = new Date(content.publishedOn);
if (
resDate.getTime() >= currStartDate.getTime() &&
resDate.getTime() <= currEndDate.getTime()
) {
// result occurred during currently selected date
currDateResults.push(h._source);
currDateResults.push(content);
} else {
prevDateResults.push(h._source);
prevDateResults.push(content);
}
});
setCurrDateResults(currDateResults);
setPrevDateResults(prevDateResults);
setTotalResults(currDateResults.length);
if (!groupStoredContent) {
if (res.hits.total.value === 0) toast.warn('No results found.');
if (
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Correctly handle different elasticsearch result types

(typeof res.hits.total === 'number' && res.hits.total === 0) ||
(res.hits.total !== undefined &&
typeof res.hits.total !== 'number' &&
'value' in res.hits.total &&
res.hits.total.value === 0)
)
toast.warn('No results found.');
if (currDateResults.length >= 500)
toast.warn(
'Search returned 500+ results, only showing first 500. Please consider refining your search.',
Expand Down Expand Up @@ -140,7 +157,7 @@ export const SearchPage: React.FC<ISearchType> = ({ showAdvanced }) => {
);

const fetchResults = React.useCallback(
async (filter: IFilterSettingsModel, storedContent?: any) => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Never use any

async (filter: IFilterSettingsModel, storedContent?: KnnSearchResponse<IContentModel>) => {
try {
setShowResults(false);
let newFilter = filter;
Expand All @@ -150,27 +167,26 @@ export const SearchPage: React.FC<ISearchType> = ({ showAdvanced }) => {
dateOffset: filter.dateOffset + 7,
};
}
const offSet = filter.dateOffset ? filter.dateOffset : 0;
setDateVisible(!(offSet > 1));
const dayInMs = 24 * 60 * 60 * 1000; // Hours*Minutes*Seconds*Milliseconds
let offSetDate = new Date();
offSetDate.setDate(offSetDate.getDate() - offSet);
offSetDate.setHours(0, 0, 0);
const offset = filter.dateOffset ?? 0;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fix the logic with offset and date values. Now the results should be correctly displayed.

setDateVisible(offset === 0);

let offsetDate =
offset <= 2 ? moment().add(offset * 24 * -1, 'hour') : moment().add(offset * -1, 'day');
if (offset > 2 || offset === 0) offsetDate.startOf('day');

// if no date offset/ start date this means the user is looking for all content
// unix epoch used as a default start date for all content
const currStartDate = filter.startDate
? new Date(filter.startDate)
? moment(filter.startDate)
: filter.dateOffset !== undefined
? offSetDate
: new Date(1970, 0, 1);
const prevStartDate = new Date(currStartDate.getTime() - 7 * dayInMs);
const currEndDate = filter.endDate
? new Date(filter.endDate)
: filter.dateOffset !== undefined
? new Date(currStartDate.getTime() + offSet * dayInMs - 1)
: new Date();
currEndDate.setHours(23, 59, 59);
setStartDate(currStartDate);
? moment(offsetDate)
: moment(1432252800); // 1970
const prevStartDate = moment(currStartDate).add(-7, 'day');

let currEndDate = filter.endDate ? moment(filter.endDate) : moment();
currEndDate.endOf('day');

setStartDate(currStartDate.toDate());
if (filter.startDate && filter.endDate) {
newFilter = {
...filter,
Expand All @@ -190,7 +206,7 @@ export const SearchPage: React.FC<ISearchType> = ({ showAdvanced }) => {
res = storedContent;
groupStoredContent = true;
}
groupResults(res, currStartDate, currEndDate, groupStoredContent);
groupResults(res, currStartDate.toDate(), currEndDate.toDate(), groupStoredContent);
} catch {
} finally {
setShowResults(true);
Expand Down
Loading