2020-06-30 15:24:30 +02:00
|
|
|
import {
|
2020-07-01 18:13:32 +02:00
|
|
|
SearchQuery,
|
|
|
|
QueryResult
|
2020-10-13 17:05:57 +02:00
|
|
|
} from '@oceanprotocol/lib/dist/node/metadatacache/MetadataCache'
|
|
|
|
import { MetadataCache, Logger } from '@oceanprotocol/lib'
|
2020-05-07 08:03:30 +02:00
|
|
|
|
2020-06-30 15:24:30 +02:00
|
|
|
export function getSearchQuery(
|
2020-11-03 14:57:40 +01:00
|
|
|
text?: string,
|
|
|
|
owner?: string,
|
|
|
|
tags?: string,
|
|
|
|
categories?: string,
|
|
|
|
page?: string,
|
|
|
|
offset?: string
|
2020-06-30 15:24:30 +02:00
|
|
|
): SearchQuery {
|
2020-11-14 15:02:54 +01:00
|
|
|
const searchTerm = owner
|
|
|
|
? `(publicKey.owner:${owner})`
|
|
|
|
: tags
|
|
|
|
? // eslint-disable-next-line no-useless-escape
|
|
|
|
`(service.attributes.additionalInformation.tags:\"${tags}\")`
|
|
|
|
: categories
|
|
|
|
? // eslint-disable-next-line no-useless-escape
|
|
|
|
`(service.attributes.additionalInformation.categories:\"${categories}\")`
|
|
|
|
: text || ''
|
|
|
|
|
2020-06-30 15:24:30 +02:00
|
|
|
return {
|
|
|
|
page: Number(page) || 1,
|
2020-10-28 12:33:53 +01:00
|
|
|
offset: Number(offset) || 21,
|
2020-06-30 15:24:30 +02:00
|
|
|
query: {
|
2020-11-14 15:02:54 +01:00
|
|
|
nativeSearch: 1,
|
|
|
|
query_string: {
|
|
|
|
query: `${searchTerm} -isInPurgatory:true`
|
|
|
|
}
|
|
|
|
// ...(owner && { 'publicKey.owner': [owner] }),
|
|
|
|
// ...(tags && { tags: [tags] }),
|
|
|
|
// ...(categories && { categories: [categories] })
|
2020-06-30 15:24:30 +02:00
|
|
|
},
|
|
|
|
sort: {
|
|
|
|
created: -1
|
|
|
|
}
|
|
|
|
|
2020-10-24 17:39:51 +02:00
|
|
|
// Something in ocean.js is weird when using 'tags: [tag]'
|
2020-06-30 15:24:30 +02:00
|
|
|
// which is the only way the query actually returns desired results.
|
|
|
|
// But it doesn't follow 'SearchQuery' interface so we have to assign
|
|
|
|
// it here.
|
2020-11-14 15:02:54 +01:00
|
|
|
// } as SearchQuery
|
|
|
|
|
|
|
|
// And the next hack,
|
|
|
|
// nativeSearch is not implmeneted on ocean.js typings
|
|
|
|
} as any
|
2020-06-30 15:24:30 +02:00
|
|
|
}
|
|
|
|
|
2020-08-11 15:56:13 +02:00
|
|
|
export async function getResults(
|
2020-10-24 17:39:51 +02:00
|
|
|
params: {
|
|
|
|
text?: string
|
2020-11-03 14:57:40 +01:00
|
|
|
owner?: string
|
2020-10-24 17:39:51 +02:00
|
|
|
tags?: string
|
|
|
|
categories?: string
|
|
|
|
page?: string
|
|
|
|
offset?: string
|
|
|
|
},
|
2020-10-13 17:05:57 +02:00
|
|
|
metadataCacheUri: string
|
2020-08-11 15:56:13 +02:00
|
|
|
): Promise<QueryResult> {
|
2020-11-03 14:57:40 +01:00
|
|
|
const { text, owner, tags, page, offset, categories } = params
|
2020-06-30 15:24:30 +02:00
|
|
|
|
2020-10-13 17:05:57 +02:00
|
|
|
const metadataCache = new MetadataCache(metadataCacheUri, Logger)
|
2020-11-05 14:43:13 +01:00
|
|
|
const searchQuery = getSearchQuery(
|
|
|
|
text,
|
|
|
|
owner,
|
|
|
|
tags,
|
|
|
|
categories,
|
|
|
|
page,
|
|
|
|
offset
|
2020-05-07 08:03:30 +02:00
|
|
|
)
|
2020-11-05 14:43:13 +01:00
|
|
|
const queryResult = await metadataCache.queryMetadata(searchQuery)
|
2020-06-30 15:24:30 +02:00
|
|
|
|
|
|
|
return queryResult
|
2020-05-07 08:03:30 +02:00
|
|
|
}
|