-
Notifications
You must be signed in to change notification settings - Fork 7
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
feat: add custom filter function #632
base: development
Are you sure you want to change the base?
Changes from 1 commit
1868ebc
c2819f9
401b690
303f283
302b545
581edd0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,38 @@ | ||
const FILTER_VALUES = { | ||
MUST_EXIST: 'MUST_EXIST', | ||
moritzkirstein marked this conversation as resolved.
Show resolved
Hide resolved
|
||
MUST_EXISTS_AND_NON_EMPTY: 'MUST_EXISTS_AND_NON_EMPTY' | ||
//... | ||
moritzkirstein marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
module.exports = { | ||
filters: [], | ||
filters: [ | ||
{ | ||
id: 'gaiax', | ||
label: 'Gaia-X Service', | ||
type: 'filterList', | ||
options: [ | ||
// a new filter value for a MUST_EXIST type could be added to handle this new functionality | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. todo(blocking): This seems to be a leftover. Remove it or enhance for clarity. |
||
{ | ||
label: 'Service SD', | ||
value: FILTER_VALUES.MUST_EXISTS_AND_NON_EMPTY, | ||
queryPath: | ||
'metadata.additionalInformation.gaiaXInformation.serviceSD.url' | ||
}, | ||
{ | ||
label: 'Terms and Conditions', | ||
value: FILTER_VALUES.MUST_EXIST, | ||
queryPath: | ||
'metadata.additionalInformation.gaiaXInformation.termsAndConditions' | ||
}, | ||
// options can have their own queryPath defined that gets validated against the defined value | ||
moritzkirstein marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
label: 'Verified', | ||
value: FILTER_VALUES.MUST_EXIST, | ||
queryPath: | ||
'metadata.additionalInformation.gaiaXInformation.serviceSd.isValid' | ||
} | ||
] | ||
} | ||
], | ||
filterSets: {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,18 @@ | |
*/ | ||
type TFilterValue = string | number | boolean | number[] | string[] | ||
type TFilterKey = 'terms' | 'term' | 'match' | 'match_phrase' | ||
type Query = { | ||
moritzkirstein marked this conversation as resolved.
Show resolved
Hide resolved
|
||
must: { | ||
exists: { | ||
field: string | ||
} | ||
} | ||
must_not?: { | ||
term: { | ||
[key: string]: string | ||
} | ||
} | ||
moritzkirstein marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
export function getFilterTerm( | ||
filterField: string, | ||
|
@@ -56,6 +68,39 @@ | |
} | ||
} | ||
|
||
export function getFilter(...args: any[]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue(blocking): we should try not to use |
||
let filters = [] | ||
if (typeof args[0] === 'object') { | ||
args[0].forEach((arg) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion(blocking): In general prefer usage of This comment applies to all places in the code where |
||
const filter = arg.split('=') | ||
filters = [...filters, filter] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion(blocking): move this code to a named function and reuse it to increase readability |
||
}) | ||
} else { | ||
const filter = args[0].split('=') | ||
filters = [...filters, filter] | ||
} | ||
|
||
let filter: Query[] = [] | ||
filters.forEach((filterItem) => { | ||
let query: Query = { | ||
must: { | ||
exists: { field: filterItem[0] } | ||
} | ||
} | ||
if (filterItem[1] === 'MUST_EXISTS_AND_NON_EMPTY') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion(blocking): Move FILTER_VALUES const into this file and reference it to prevent errors: |
||
query = { | ||
...query, | ||
must_not: { | ||
term: { [filterItem[0] + '.keyword']: '' } | ||
} | ||
} | ||
} | ||
filter = [...filter, query] | ||
}) | ||
|
||
return filter | ||
} | ||
|
||
export function parseFilters( | ||
filtersList: Filters, | ||
filterSets: { [key: string]: string[] } | ||
|
@@ -77,6 +122,9 @@ | |
? getFilterTerm(filterQueryPath[key], uniqueTags) | ||
: undefined | ||
} | ||
if (key === 'gaiax') { | ||
return undefined | ||
} | ||
if (filtersList[key].length > 0) | ||
return getFilterTerm(filterQueryPath[key], filtersList[key]) | ||
|
||
|
@@ -90,7 +138,7 @@ | |
const { whitelists } = addressConfig | ||
|
||
const whitelistFilterTerms = Object.entries(whitelists) | ||
.filter(([field, whitelist]) => whitelist.length > 0) | ||
.map(([field, whitelist]) => | ||
whitelist.map((address) => getFilterTerm(field, address, 'match')) | ||
) | ||
|
@@ -160,7 +208,8 @@ | |
...(baseQueryParams.showSaas === false ? [saasFieldExists] : []) | ||
] | ||
} | ||
} | ||
}, | ||
...(baseQueryParams.bool || []) | ||
] | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import { LoggerInstance } from '@oceanprotocol/lib' | |
import { | ||
escapeEsReservedCharacters, | ||
generateBaseQuery, | ||
getFilter, | ||
getFilterTerm, | ||
parseFilters, | ||
queryMetadata | ||
|
@@ -16,6 +17,21 @@ import { | |
} from '../../@types/aquarius/SearchQuery' | ||
import { filterSets, getInitialFilters } from './Filter' | ||
|
||
export interface boolFilter { | ||
bool: { | ||
must: { | ||
exists: { | ||
field: string | ||
} | ||
} | ||
must_not?: { | ||
term: { | ||
[key: string]: string | ||
} | ||
} | ||
} | ||
} | ||
|
||
export function updateQueryStringParameter( | ||
uri: string, | ||
key: string, | ||
|
@@ -43,15 +59,27 @@ export function getSearchQuery( | |
serviceType?: string | string[], | ||
accessType?: string | string[], | ||
filterSet?: string | string[], | ||
showSaas?: boolean | ||
showSaas?: boolean, | ||
gaiax?: string | string[] | ||
): SearchQuery { | ||
text = escapeEsReservedCharacters(text) | ||
const emptySearchTerm = text === undefined || text === '' | ||
const filters: FilterTerm[] = [] | ||
const bool: boolFilter[] = [] | ||
let searchTerm = text || '' | ||
let nestedQuery | ||
if (tags) { | ||
filters.push(getFilterTerm('metadata.tags.keyword', tags)) | ||
} else if (gaiax) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question(blocking): Is this query not compatible with tags or why is it only applied if |
||
const filter = getFilter(gaiax) | ||
filter.forEach((term) => { | ||
const query = { | ||
bool: { | ||
...term | ||
} | ||
} | ||
bool.push(query) | ||
}) | ||
} else { | ||
searchTerm = searchTerm.trim() | ||
const modifiedSearchTerm = searchTerm.split(' ').join(' OR ').trim() | ||
|
@@ -120,8 +148,8 @@ export function getSearchQuery( | |
} | ||
|
||
const filtersList = getInitialFilters( | ||
{ accessType, serviceType, filterSet }, | ||
['accessType', 'serviceType', 'filterSet'] | ||
{ accessType, serviceType, filterSet, gaiax }, | ||
['accessType', 'serviceType', 'filterSet', 'gaiax'] | ||
) | ||
parseFilters(filtersList, filterSets).forEach((term) => filters.push(term)) | ||
|
||
|
@@ -134,6 +162,7 @@ export function getSearchQuery( | |
}, | ||
sortOptions: { sortBy: sort, sortDirection }, | ||
filters, | ||
bool, | ||
showSaas | ||
} as BaseQueryParams | ||
|
||
|
@@ -154,6 +183,7 @@ export async function getResults( | |
serviceType?: string | string[] | ||
accessType?: string | string[] | ||
filterSet?: string[] | ||
gaiax?: string | string[] | ||
}, | ||
chainIds: number[], | ||
cancelToken?: CancelToken | ||
|
@@ -168,7 +198,8 @@ export async function getResults( | |
sortOrder, | ||
serviceType, | ||
accessType, | ||
filterSet | ||
filterSet, | ||
gaiax | ||
} = params | ||
|
||
const showSaas = | ||
|
@@ -199,7 +230,8 @@ export async function getResults( | |
sanitizedServiceType, | ||
accessType, | ||
filterSet, | ||
showSaas | ||
showSaas, | ||
gaiax | ||
) | ||
|
||
const queryResult = await queryMetadata(searchQuery, cancelToken) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Move the FILTER_VALUES const into
aquarius/index.ts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made the const into an enum and added it to the other methods in the searchQuery.ts is this ok?