-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[context view] Use courier when querying the context (#11127)
Instead of using a separate code path for querying the anchor and context documents, the context view actions now use a normal `SearchSource` and thereby scripted fields, source filtering and other features of a `SearchSource` without duplicated code. To facilitate this the `SearchSource` gained the `searchAfter` field, which is turned into a `search_after` clause on the request. Additionally, the `FetchStrategyForSearch` now honors the `timefilter.enabled` flag. When this flag is enabled, the index list will be expanded with respect to the current time interval configured in the `timefilter` (previous behaviour). When this flag is disabled, the `timefilter` values will be ignored when expanding the index list.
- Loading branch information
1 parent
b7d11c9
commit 0be7acc
Showing
13 changed files
with
224 additions
and
277 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,41 @@ | ||
import _ from 'lodash'; | ||
|
||
import { addComputedFields } from './utils/fields'; | ||
import { createAnchorQueryBody } from './utils/queries'; | ||
|
||
|
||
async function fetchAnchor(es, indexPattern, uid, sort) { | ||
const indices = await indexPattern.toIndexList(); | ||
const queryBody = addComputedFields(indexPattern, createAnchorQueryBody(uid, sort)); | ||
const response = await es.search({ | ||
index: indices, | ||
body: queryBody, | ||
}); | ||
|
||
if (_.get(response, ['hits', 'total'], 0) < 1) { | ||
throw new Error('Failed to load anchor document.'); | ||
} | ||
|
||
return Object.assign( | ||
{}, | ||
response.hits.hits[0], | ||
{ | ||
$$_isAnchor: true, | ||
}, | ||
); | ||
import { SearchSourceProvider } from 'ui/courier/data_source/search_source'; | ||
|
||
|
||
function fetchAnchorProvider(Private) { | ||
const SearchSource = Private(SearchSourceProvider); | ||
|
||
return async function fetchAnchor(indexPattern, uid, sort) { | ||
const searchSource = new SearchSource() | ||
.inherits(false) | ||
.set('index', indexPattern) | ||
.set('version', true) | ||
.set('size', 1) | ||
.set('query', { | ||
terms: { | ||
_uid: [uid], | ||
}, | ||
}) | ||
.set('sort', [sort, { _uid: 'asc' }]); | ||
|
||
const response = await searchSource.fetch(); | ||
|
||
if (_.get(response, ['hits', 'total'], 0) < 1) { | ||
throw new Error('Failed to load anchor document.'); | ||
} | ||
|
||
return Object.assign( | ||
{}, | ||
_.get(response, ['hits', 'hits', 0]), | ||
{ | ||
$$_isAnchor: true, | ||
}, | ||
); | ||
}; | ||
} | ||
|
||
|
||
export { | ||
fetchAnchor, | ||
fetchAnchorProvider, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,64 @@ | ||
import _ from 'lodash'; | ||
|
||
import { addComputedFields } from './utils/fields'; | ||
import { createSuccessorsQueryBody } from './utils/queries.js'; | ||
import { reverseQuerySort } from './utils/sorting'; | ||
import { SearchSourceProvider } from 'ui/courier/data_source/search_source'; | ||
|
||
import { reverseSortDirective } from './utils/sorting'; | ||
|
||
async function fetchSuccessors(es, indexPattern, anchorDocument, sort, size) { | ||
const successorsQueryBody = prepareQueryBody(indexPattern, anchorDocument, sort, size); | ||
const results = await performQuery(es, indexPattern, successorsQueryBody); | ||
return results; | ||
} | ||
|
||
async function fetchPredecessors(es, indexPattern, anchorDocument, sort, size) { | ||
const successorsQueryBody = prepareQueryBody(indexPattern, anchorDocument, sort, size); | ||
const predecessorsQueryBody = reverseQuerySort(successorsQueryBody); | ||
const reversedResults = await performQuery(es, indexPattern, predecessorsQueryBody); | ||
const results = reversedResults.slice().reverse(); | ||
return results; | ||
} | ||
function fetchContextProvider(Private) { | ||
const SearchSource = Private(SearchSourceProvider); | ||
|
||
return { | ||
fetchPredecessors, | ||
fetchSuccessors, | ||
}; | ||
|
||
function prepareQueryBody(indexPattern, anchorDocument, sort, size) { | ||
const successorsQueryBody = addComputedFields( | ||
indexPattern, | ||
createSuccessorsQueryBody(anchorDocument.sort, sort, size) | ||
); | ||
return successorsQueryBody; | ||
} | ||
async function fetchSuccessors(indexPattern, anchorDocument, contextSort, size) { | ||
const successorsSort = [contextSort, { _uid: 'asc' }]; | ||
const successorsSearchSource = createSearchSource( | ||
indexPattern, | ||
anchorDocument, | ||
successorsSort, | ||
size, | ||
); | ||
const results = await performQuery(successorsSearchSource); | ||
return results; | ||
} | ||
|
||
async function fetchPredecessors(indexPattern, anchorDocument, contextSort, size) { | ||
const predecessorsSort = [reverseSortDirective(contextSort), { _uid: 'desc' }]; | ||
const predecessorsSearchSource = createSearchSource( | ||
indexPattern, | ||
anchorDocument, | ||
predecessorsSort, | ||
size, | ||
); | ||
const reversedResults = await performQuery(predecessorsSearchSource); | ||
const results = reversedResults.slice().reverse(); | ||
return results; | ||
} | ||
|
||
async function performQuery(es, indexPattern, queryBody) { | ||
const indices = await indexPattern.toIndexList(); | ||
function createSearchSource(indexPattern, anchorDocument, sort, size) { | ||
return new SearchSource() | ||
.inherits(false) | ||
.set('index', indexPattern) | ||
.set('version', true) | ||
.set('size', size) | ||
.set('query', { | ||
match_all: {}, | ||
}) | ||
.set('searchAfter', anchorDocument.sort) | ||
.set('sort', sort); | ||
} | ||
|
||
const response = await es.search({ | ||
index: indices, | ||
body: queryBody, | ||
}); | ||
async function performQuery(searchSource) { | ||
const response = await searchSource.fetch(); | ||
|
||
return _.get(response, ['hits', 'hits'], []); | ||
return _.get(response, ['hits', 'hits'], []); | ||
} | ||
} | ||
|
||
|
||
export { | ||
fetchPredecessors, | ||
fetchSuccessors, | ||
fetchContextProvider, | ||
}; |
49 changes: 0 additions & 49 deletions
49
src/core_plugins/kibana/public/context/api/utils/__tests__/fields.js
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.