Skip to content

Added switch based mapping for filters in searchbar #448

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
27 changes: 24 additions & 3 deletions frontend/src/api/fetchAlgoliaData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SearchResponse } from 'algoliasearch'
import { AlgoliaRequestType, AlgoliaResponseType } from 'types/algolia'
import { ENVIRONMENT } from 'utils/credentials'
import { getParamsForFilters } from 'utils/filterMapping'
import { client } from 'utils/helpers/algoliaClient'

import { getParamsForIndexName } from 'utils/paramsMapping'
Expand All @@ -19,17 +20,37 @@ export const fetchAlgoliaData = async <T>(
}
try {
const params = getParamsForIndexName(indexName)

let filters = ''
let searchQuery = query
Copy link
Collaborator

@Rajgupta36 Rajgupta36 Jan 15, 2025

Choose a reason for hiding this comment

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

Could you please define the types for this? Otherwise, we may unintentionally mutate the variable from a string to another type, which could lead to unexpected behavior.


const filterRegex = /(\w+)([:><=!]+)([^ ]+)/g
Copy link
Collaborator

Choose a reason for hiding this comment

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

can we put this logic in another function

let match

while ((match = filterRegex.exec(query)) !== null) {
const [fullMatch, attribute, operator, value] = match
filters += `${attribute}${operator}${value} AND `
searchQuery = searchQuery.replace(fullMatch, '').trim()
}

filters = filters.trim().replace(/AND$/, '')
filters = getParamsForFilters(indexName, filters)

if (filterKey) {
filters = filters ? `(${filters}) AND idx_key:${filterKey}` : `idx_key:${filterKey}`
}

const request: AlgoliaRequestType = {
attributesToHighlight: [],
hitsPerPage: hitsPerPage,
indexName: `${ENVIRONMENT}_${indexName}`,
page: currentPage - 1,
query: query,
query: searchQuery,
removeWordsIfNoResults: 'allOptional',
...params,
}
if (filterKey) {
request.filters = `idx_key: ${filterKey}`
if (filters) {
request.filters = filters
}

const { results } = await client.search({
Expand Down
20 changes: 20 additions & 0 deletions frontend/src/utils/filterMapping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const getParamsForFilters = (indexName: string, filter: string) => {
filter = filter.trim()
switch (indexName) {
case 'projects':
Copy link
Collaborator

Choose a reason for hiding this comment

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

What about contribute page (issues), will it be addressed in a separate PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have gone through the issues index, its most of the attributes are seachable,
only attribute we can use was comments<10 or is:open is:close
Even there is no attribute for open and close

Copy link
Collaborator

@Rajgupta36 Rajgupta36 Jan 15, 2025

Choose a reason for hiding this comment

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

I think we can also add languages and topics based filtering in contribute page

switch (true) {
case /^contributors([><]=?|=)(\d+)$/.test(filter):
return filter.replace(/^contributors([><]=?|=)(\d+)$/, 'idx_contributors_count$1$2')
case /^stars([><]=?|=)(\d+)$/.test(filter):
return filter.replace(/^stars([><]=?|=)(\d+)$/, 'idx_stars_count$1$2')
case /^forks([><]=?|=)(\d+)$/.test(filter):
return filter.replace(/^forks([><]=?|=)(\d+)$/, 'idx_forks_count$1$2')
case /^is:([a-zA-Z]+)$/.test(filter):
return filter.replace(/^is:([a-zA-Z]+)$/, 'idx_type:$1')
default:
return filter
Comment on lines +6 to +15
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we extend it to datetime fields (created/updated) too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@arkid15r We can do that, how the user will input the dates?
Is it like 1sep24 or 01092024 or ?
As the index contains the that flied in UNIX timestamp

Copy link
Collaborator

Choose a reason for hiding this comment

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

@ShashaankS we can always convert the dates into desirable format.

}
default:
return filter
}
}
Loading