Skip to content

Commit

Permalink
Merge pull request #222 from smartprocure/feature/enable_partial_resu…
Browse files Browse the repository at this point in the history
…lts_config_and_logging

Add Partial Results Logging and Request Options
  • Loading branch information
positiveVibes4all authored Jun 14, 2024
2 parents 8f98560 + 5015cf2 commit 396595f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-ties-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'contexture-elasticsearch': minor
---

Enable control of Elastic partial results config and ability to log partial results
28 changes: 26 additions & 2 deletions packages/provider-elasticsearch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,23 @@ let ElasticsearchProvider = (config = { request: {} }) => ({
async runSearch({ requestOptions = {} } = {}, node, schema, filters, aggs) {
let hoistedFromFilters = hoistOnTree(filters)
let hoistedFromAggs = hoistOnTree(aggs)
let { searchWrapper } = config
let {
searchWrapper,
configOptions = {},
logger,
clusterDefaultTimeout,
} = config
let { scroll, scrollId } = node
let request = scrollId
? // If we have scrollId then keep scrolling, no query needed
{
...configOptions,
scroll: scroll === true ? '60m' : hoistedFromFilters,
body: { scroll_id: scrollId },
}
: // Deterministic ordering of JSON keys for request cache optimization
{
...configOptions,
index: schema.elasticsearch.index,
// Scroll support (used for bulk export)
...(scroll && { scroll: scroll === true ? '2m' : scroll }),
Expand Down Expand Up @@ -93,13 +100,30 @@ let ElasticsearchProvider = (config = { request: {} }) => ({

let metaObj = { request, requestOptions }

// Log Request
node._meta.requests.push(metaObj)
let count = counter.inc()
debug('(%s) Request: %O\nOptions: %O', count, request, requestOptions)
let { body } = await search(request, requestOptions)

// If body has timed_out set to true, log that partial results were returned,
// if partial is turned off an error will be thrown instead.
// https://www.elastic.co/guide/en/elasticsearch/guide/current/_search_options.html#_timeout_2
if (body?.timed_out)
logger &&
logger(
`Returned partial search results, took ${body.took}ms
Timeout Threshold: ${
configOptions.timeout ||
clusterDefaultTimeout ||
// Could grab from cluster settings if not provided with a query
// but would add overhead so N/A presented if not available from call site.
'N/A'
}`
)

metaObj.response = body
debug('(%s) Response: %O', count, body)
// Log Request

return metaObj.response
},
Expand Down

0 comments on commit 396595f

Please sign in to comment.