-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[savedObjects] generate queries that are aware of root properties
- Loading branch information
Showing
5 changed files
with
129 additions
and
184 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
105 changes: 53 additions & 52 deletions
105
src/server/saved_objects/client/lib/search_dsl/experimental_filter/convert.js
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,70 +1,71 @@ | ||
import { parseAndValidateFromApi } from './parse_and_validate_from_api'; | ||
|
||
export function convertFilterToEsDsl(types, apiParam) { | ||
return convertFilter(types, parseAndValidateFromApi(apiParam)); | ||
function getExpandedFields(savedObjectTypes, rootAttributes, field) { | ||
if (rootAttributes.includes(field)) { | ||
return [field]; | ||
} | ||
|
||
return savedObjectTypes.reduce((acc, t) => [ | ||
...acc, | ||
`${t}.${field}` | ||
], []); | ||
} | ||
|
||
function getFilterFields(types, field) { | ||
switch (field) { | ||
case 'type': | ||
case 'updated_at': | ||
return [field]; | ||
export function convertFilterToEsDsl(savedObjectTypes, rootAttributes, apiParam) { | ||
const rootFilter = parseAndValidateFromApi(apiParam); | ||
|
||
default: | ||
return types.reduce((acc, t) => [ | ||
...acc, | ||
`${t}.${field}` | ||
], []); | ||
} | ||
} | ||
function recursivelyConvert(filter) { | ||
const fields = filter.field | ||
? getExpandedFields(savedObjectTypes, rootAttributes, filter.field) | ||
: undefined; | ||
|
||
function convertFilter(types, filter) { | ||
switch (filter.type) { | ||
case 'value': { | ||
return { | ||
multi_match: { | ||
type: 'phrase', | ||
query: filter.value, | ||
fields: filter.field | ||
? getFilterFields(types, filter.field) | ||
: undefined, | ||
} | ||
}; | ||
} | ||
switch (filter.type) { | ||
case 'value': { | ||
return { | ||
multi_match: { | ||
type: 'phrase', | ||
query: filter.value, | ||
fields, | ||
} | ||
}; | ||
} | ||
|
||
case 'range': { | ||
const filters = getFilterFields(types, filter.field).map(field => ({ | ||
range: { | ||
[field]: { | ||
gt: filter.gt, | ||
gte: filter.gte, | ||
lt: filter.lt, | ||
lte: filter.lte, | ||
case 'range': { | ||
const filters = fields.map(field => ({ | ||
range: { | ||
[field]: { | ||
gt: filter.gt, | ||
gte: filter.gte, | ||
lt: filter.lt, | ||
lte: filter.lte, | ||
} | ||
} | ||
})); | ||
|
||
if (filters.length > 1) { | ||
return { | ||
bool: { | ||
should: filters | ||
} | ||
}; | ||
} | ||
})); | ||
|
||
if (filters.length > 1) { | ||
return filters[0]; | ||
} | ||
|
||
case 'bool': | ||
return { | ||
bool: { | ||
should: filters | ||
must: filter.must.map(recursivelyConvert), | ||
must_not: filter.must_not.map(recursivelyConvert), | ||
should: filter.must_some.map(recursivelyConvert), | ||
} | ||
}; | ||
} | ||
|
||
return filters[0]; | ||
default: | ||
throw new Error(`unexpected filter.type "${filter.type}"`); | ||
} | ||
|
||
case 'bool': | ||
return { | ||
bool: { | ||
must: filter.must.map(filter => convertFilter(types, filter)), | ||
must_not: filter.must_not.map(filter => convertFilter(types, filter)), | ||
should: filter.must_some.map(filter => convertFilter(types, filter)), | ||
} | ||
}; | ||
|
||
default: | ||
throw new Error(`unexpected filter.type "${filter.type}"`); | ||
} | ||
|
||
return recursivelyConvert(rootFilter); | ||
} |
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.