Skip to content

Commit

Permalink
Merge pull request #695 from oceanprotocol/feature/add-elasticdb-search
Browse files Browse the repository at this point in the history
Feature/add elasticdb search
  • Loading branch information
bogdanfazakas authored Sep 23, 2024
2 parents 782ad64 + fc6ef3a commit 5fb46ae
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,4 @@ html-report.html

# databases
*.sqlite
databases/*
databases/*
72 changes: 47 additions & 25 deletions src/components/database/ElasticSearchDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,40 +602,62 @@ export class ElasticsearchDdoDatabase extends AbstractDdoDatabase {
}
}

async search(
query: Record<string, any>,
maxResultsPerPage?: number,
pageNumber?: number
): Promise<any> {
async search(query: Record<string, any>): Promise<any> {
const results = []
try {
const maxPerPage = maxResultsPerPage || 100
const from = (pageNumber || 1) * maxPerPage - maxPerPage
for (const schema of this.getSchemas()) {
const maxPerPage = query.size || 100
const from = (query.from || 1) * maxPerPage - maxPerPage

if (query.index) {
const { index, ...queryWithoutIndex } = query
try {
const response = await this.client.search({
index: schema.index,
index,
body: {
...query,
...queryWithoutIndex,
from,
size: maxPerPage
}
})
results.push(response.hits.hits)
if (response.hits?.hits.length > 0) {
results.push(response)
}
} catch (error) {
const schemaErrorMsg = `Error for schema ${query.index}: ${error.message}`
DATABASE_LOGGER.logMessageWithEmoji(
schemaErrorMsg,
true,
GENERIC_EMOJIS.EMOJI_CROSS_MARK,
LOG_LEVELS_STR.LEVEL_WARN
)
}
} else {
for (const schema of this.getSchemas()) {
try {
const response = await this.client.search({
index: schema.index,
body: {
...query,
from,
size: maxPerPage
}
})
if (response.hits?.hits.length > 0) {
results.push(response)
}
} catch (error) {
const schemaErrorMsg = `Error for schema ${schema.index}: ${error.message}`
DATABASE_LOGGER.logMessageWithEmoji(
schemaErrorMsg,
true,
GENERIC_EMOJIS.EMOJI_CROSS_MARK,
LOG_LEVELS_STR.LEVEL_WARN
)
continue
}
}

return results
} catch (error) {
const errorMsg = `Error when searching by query ${JSON.stringify(query)}: ${
error.message
}`
DATABASE_LOGGER.logMessageWithEmoji(
errorMsg,
true,
GENERIC_EMOJIS.EMOJI_CROSS_MARK,
LOG_LEVELS_STR.LEVEL_ERROR
)
return results
}

return results
}

async create(ddo: Record<string, any>): Promise<any> {
Expand Down

0 comments on commit 5fb46ae

Please sign in to comment.