-
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.
- Loading branch information
Showing
27 changed files
with
282 additions
and
107 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
10 changes: 10 additions & 0 deletions
10
x-pack/packages/ml/query_utils/src/__mocks__/simple_query.ts
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { SearchQueryVariant } from '../types'; | ||
|
||
export const simpleQueryMock: SearchQueryVariant = { query_string: { query: 'airline:AAL' } }; |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { simpleQueryMock } from './__mocks__/simple_query'; | ||
import { isDefaultQuery } from './default_query'; | ||
import { matchAllQuery } from './match_all_query'; | ||
import { defaultSimpleQuery } from './simple_query'; | ||
|
||
describe('isDefaultQuery', () => { | ||
it("should return if it's a default query", () => { | ||
expect(isDefaultQuery(defaultSimpleQuery)).toBe(true); | ||
expect(isDefaultQuery(matchAllQuery)).toBe(true); | ||
expect(isDefaultQuery(simpleQueryMock)).toBe(false); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { isFilterBasedSimpleQuery } from './filter_based_simple_query'; | ||
import { isMatchAllQuery } from './match_all_query'; | ||
import { isSimpleQuery } from './simple_query'; | ||
import type { SearchQueryVariant } from './types'; | ||
|
||
export function isDefaultQuery(query: SearchQueryVariant): boolean { | ||
return ( | ||
isMatchAllQuery(query) || | ||
(isSimpleQuery(query) && query.query_string.query === '*') || | ||
(isFilterBasedSimpleQuery(query) && | ||
(query.bool.filter[0].query_string.query === '*' || isMatchAllQuery(query.bool.filter[0]))) | ||
); | ||
} |
49 changes: 49 additions & 0 deletions
49
x-pack/packages/ml/query_utils/src/filter_based_default_query.test.ts
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { isFilterBasedDefaultQuery } from './filter_based_default_query'; | ||
import { simpleQueryMock } from './__mocks__/simple_query'; | ||
import { matchAllQuery } from './match_all_query'; | ||
import { defaultSimpleQuery } from './simple_query'; | ||
|
||
describe('isFilterBasedDefaultQuery', () => { | ||
it('should identify filter based default queries', () => { | ||
expect(isFilterBasedDefaultQuery(defaultSimpleQuery)).toBe(false); | ||
expect(isFilterBasedDefaultQuery(matchAllQuery)).toBe(false); | ||
expect(isFilterBasedDefaultQuery(simpleQueryMock)).toBe(false); | ||
expect( | ||
isFilterBasedDefaultQuery({ | ||
bool: { filter: [simpleQueryMock], must: [], must_not: [], should: [] }, | ||
}) | ||
).toBe(false); | ||
expect( | ||
isFilterBasedDefaultQuery({ | ||
bool: { filter: [], must: [], must_not: [], should: [] }, | ||
}) | ||
).toBe(true); | ||
expect( | ||
isFilterBasedDefaultQuery({ | ||
bool: { filter: [matchAllQuery], must: [], must_not: [], should: [] }, | ||
}) | ||
).toBe(true); | ||
expect( | ||
isFilterBasedDefaultQuery({ | ||
bool: { filter: [], must: [matchAllQuery], must_not: [], should: [simpleQueryMock] }, | ||
}) | ||
).toBe(false); | ||
expect( | ||
isFilterBasedDefaultQuery({ | ||
bool: { filter: [], must: [matchAllQuery], must_not: [simpleQueryMock] }, | ||
}) | ||
).toBe(false); | ||
expect( | ||
isFilterBasedDefaultQuery({ | ||
bool: { filter: [], must: [matchAllQuery], must_not: [] }, | ||
}) | ||
).toBe(true); | ||
}); | ||
}); |
35 changes: 35 additions & 0 deletions
35
x-pack/packages/ml/query_utils/src/filter_based_default_query.ts
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { isPopulatedObject } from '@kbn/ml-is-populated-object'; | ||
|
||
import type { FilterBasedSimpleQuery } from './types'; | ||
import { isDefaultQuery } from './default_query'; | ||
|
||
const boolRequiredAttributes = ['filter', 'must', 'must_not']; | ||
// const boolAttributes = ['filter', 'must', 'must_not', 'should']; | ||
|
||
// should identify variants of | ||
// `{ bool: { filter: [{ match_all: {} }], must: [], must_not: [], should: [] } }` | ||
// `{ bool: { filter: [], must: [{ match_all: {} }], must_not: [] } }` | ||
export function isFilterBasedDefaultQuery(arg: unknown): arg is FilterBasedSimpleQuery { | ||
return ( | ||
isPopulatedObject(arg, ['bool']) && | ||
isPopulatedObject(arg.bool, boolRequiredAttributes) && | ||
Object.keys(arg.bool).every( | ||
// should be either an empty array or an array with just 1 default query | ||
(d) => { | ||
if (!isPopulatedObject(arg.bool, [d])) return false; | ||
const attr = arg.bool[d]; | ||
return ( | ||
Array.isArray(attr) && | ||
(attr.length === 0 || (attr.length === 1 && isDefaultQuery(attr[0]))) | ||
); | ||
} | ||
) | ||
); | ||
} |
21 changes: 21 additions & 0 deletions
21
x-pack/packages/ml/query_utils/src/filter_based_simple_query.ts
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { isPopulatedObject } from '@kbn/ml-is-populated-object'; | ||
|
||
import { isSimpleQuery } from './simple_query'; | ||
import type { FilterBasedSimpleQuery } from './types'; | ||
|
||
export function isFilterBasedSimpleQuery(arg: unknown): arg is FilterBasedSimpleQuery { | ||
return ( | ||
isPopulatedObject(arg, ['bool']) && | ||
isPopulatedObject(arg.bool, ['filter']) && | ||
Array.isArray(arg.bool.filter) && | ||
arg.bool.filter.length === 1 && | ||
isSimpleQuery(arg.bool.filter[0]) | ||
); | ||
} |
16 changes: 16 additions & 0 deletions
16
x-pack/packages/ml/query_utils/src/match_all_query.test.ts
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { isMatchAllQuery, matchAllQuery } from './match_all_query'; | ||
|
||
describe('isMatchAllQuery', () => { | ||
it("should return if it's a match_all query", () => { | ||
expect(isMatchAllQuery(matchAllQuery)).toBe(true); | ||
expect(isMatchAllQuery({ query_string: { query: '*' } })).toBe(false); | ||
expect(isMatchAllQuery({ query_string: { query: 'airline:AAL' } })).toBe(false); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { isPopulatedObject } from '@kbn/ml-is-populated-object'; | ||
|
||
export const matchAllQuery = { match_all: {} }; | ||
|
||
export function isMatchAllQuery(query: unknown): boolean { | ||
return ( | ||
isPopulatedObject(query, ['match_all']) && | ||
typeof query.match_all === 'object' && | ||
query.match_all !== null && | ||
Object.keys(query.match_all).length === 0 | ||
); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { simpleQueryMock } from './__mocks__/simple_query'; | ||
import { defaultSimpleQuery, isSimpleQuery } from './simple_query'; | ||
import { matchAllQuery } from './match_all_query'; | ||
|
||
describe('isSimpleQuery', () => { | ||
it("should return if it's a simple query", () => { | ||
expect(isSimpleQuery(defaultSimpleQuery)).toBe(true); | ||
expect(isSimpleQuery(matchAllQuery)).toBe(false); | ||
expect(isSimpleQuery(simpleQueryMock)).toBe(true); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { isPopulatedObject } from '@kbn/ml-is-populated-object'; | ||
|
||
import type { SimpleQuery } from './types'; | ||
|
||
export const defaultSimpleQuery: SimpleQuery = { query_string: { query: '*' } }; | ||
|
||
export function isSimpleQuery(arg: unknown): arg is SimpleQuery { | ||
return isPopulatedObject(arg, ['query_string']); | ||
} |
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
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
Oops, something went wrong.