Skip to content
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

Add Partial Results Logging and Request Options #222

Merged
Merged
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
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
Loading