Skip to content

Commit

Permalink
[Discover] Change context query to prevent duplicates (#77014) (#79261)
Browse files Browse the repository at this point in the history
  • Loading branch information
kertal authored Oct 2, 2020
1 parent 882dd64 commit 460c033
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export function createContextSearchSourceStub(hits, timeField = '@timestamp') {
searchSourceStub.fetch = sinon.spy(() => {
const timeField = searchSourceStub._stubTimeField;
const lastQuery = searchSourceStub.setField.withArgs('query').lastCall.args[1];
const timeRange = lastQuery.query.constant_score.filter.range[timeField];
const timeRange = lastQuery.query.bool.must.constant_score.filter.range[timeField];
const lastSort = searchSourceStub.setField.withArgs('sort').lastCall.args[1];
const sortDirection = lastSort[0][timeField];
const sortFunction =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ describe('context app', function () {
).then((hits) => {
const intervals = mockSearchSource.setField.args
.filter(([property]) => property === 'query')
.map(([, { query }]) => get(query, ['constant_score', 'filter', 'range', '@timestamp']));
.map(([, { query }]) =>
get(query, ['bool', 'must', 'constant_score', 'filter', 'range', '@timestamp'])
);

expect(
intervals.every(({ gte, lte }) => (gte && lte ? moment(gte).isBefore(lte) : true))
Expand Down Expand Up @@ -160,7 +162,9 @@ describe('context app', function () {
).then((hits) => {
const intervals = mockSearchSource.setField.args
.filter(([property]) => property === 'query')
.map(([, { query }]) => get(query, ['constant_score', 'filter', 'range', '@timestamp']));
.map(([, { query }]) =>
get(query, ['bool', 'must', 'constant_score', 'filter', 'range', '@timestamp'])
);

// should have started at the given time
expect(intervals[0].gte).toEqual(moment(MS_PER_DAY * 1000).toISOString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ describe('context app', function () {
).then((hits) => {
const intervals = mockSearchSource.setField.args
.filter(([property]) => property === 'query')
.map(([, { query }]) => get(query, ['constant_score', 'filter', 'range', '@timestamp']));
.map(([, { query }]) =>
get(query, ['bool', 'must', 'constant_score', 'filter', 'range', '@timestamp'])
);

expect(
intervals.every(({ gte, lte }) => (gte && lte ? moment(gte).isBefore(lte) : true))
Expand Down Expand Up @@ -163,7 +165,9 @@ describe('context app', function () {
).then((hits) => {
const intervals = mockSearchSource.setField.args
.filter(([property]) => property === 'query')
.map(([, { query }]) => get(query, ['constant_score', 'filter', 'range', '@timestamp']));
.map(([, { query }]) =>
get(query, ['bool', 'must', 'constant_score', 'filter', 'range', '@timestamp'])
);

// should have started at the given time
expect(intervals[0].lte).toEqual(moment(MS_PER_DAY * 3000).toISOString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface EsHitRecord {
fields: Record<string, any>;
sort: number[];
_source: Record<string, any>;
_id: string;
}
export type EsHitRecordList = EsHitRecord[];

Expand Down Expand Up @@ -100,7 +101,8 @@ function fetchContextProvider(indexPatterns: IndexPatternsContract) {
interval,
searchAfter,
remainingSize,
nanos
nanos,
anchor._id
);

documents =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export async function fetchHitsInInterval(
interval: IntervalValue[],
searchAfter: EsQuerySearchAfter,
maxCount: number,
nanosValue: string
nanosValue: string,
anchorId: string
): Promise<EsHitRecordList> {
const range: RangeQuery = {
format: 'strict_date_optional_time',
Expand All @@ -61,10 +62,19 @@ export async function fetchHitsInInterval(
.setField('size', maxCount)
.setField('query', {
query: {
constant_score: {
filter: {
range: {
[timeField]: range,
bool: {
must: {
constant_score: {
filter: {
range: {
[timeField]: range,
},
},
},
},
must_not: {
ids: {
values: [anchorId],
},
},
},
Expand Down

0 comments on commit 460c033

Please sign in to comment.