Skip to content

Commit

Permalink
Category graphql/searchable aggregate functions (aws-amplify#7764)
Browse files Browse the repository at this point in the history
* feat(graphql-searchable): Aggregate support

* added enum type for aggregate type

* updated aggregate types to lowercase

* correct lint fix

* remove unused comments

* remove unwanted comments

* updated test snapshots

* update tests to include aggregate

* created new function for v2 searchTemplate

* organize imports

* Update packages/amplify-graphql-searchable-transformer/src/definitions.ts

Co-authored-by: Colin Ihrig <cjihrig@gmail.com>

* added e2e tests for aggregates

* remove unused imports

* update error handling

Co-authored-by: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
2 people authored and akshbhu committed Aug 15, 2021
1 parent fe10668 commit 3b6787f
Show file tree
Hide file tree
Showing 9 changed files with 795 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,49 @@ input SearchableEmployeeSortInput {
direction: SearchableSortDirection
}
input SearchableEmployeeAggregationInput {
name: String!
type: SearchableAggregateType!
field: String!
}
enum SearchableAggregateType {
terms
avg
min
max
sum
}
type SearchableEmployeeConnection {
items: [Employee]
nextToken: String
total: Int
aggregateItems: [SearchableAggregateResult]
}
type SearchableAggregateResult {
name: String!
result: SearchableAggregateGenericResult
}
type SearchableAggregateScalarResult {
value: Float!
}
type SearchableAggregateBucketResult {
buckets: [SearchableAggregateBucketResultItem]
}
type SearchableAggregateBucketResultItem {
key: String!
doc_count: Int!
}
union SearchableAggregateGenericResult = SearchableAggregateScalarResult | SearchableAggregateBucketResult
type Query {
searchEmployees(filter: SearchableEmployeeFilterInput, sort: [SearchableEmployeeSortInput], limit: Int, nextToken: String, from: Int): SearchableEmployeeConnection
searchEmployees(filter: SearchableEmployeeFilterInput, sort: [SearchableEmployeeSortInput], limit: Int, nextToken: String, from: Int, aggregates: [SearchableEmployeeAggregationInput]): SearchableEmployeeConnection
getEmployee(id: ID!): Employee
listEmployees(filter: ModelEmployeeFilterInput, limit: Int, nextToken: String): ModelEmployeeConnection
}
Expand Down Expand Up @@ -357,14 +392,49 @@ input SearchablePostSortInput {
direction: SearchableSortDirection
}
input SearchablePostAggregationInput {
name: String!
type: SearchableAggregateType!
field: String!
}
enum SearchableAggregateType {
terms
avg
min
max
sum
}
type SearchablePostConnection {
items: [Post]
nextToken: String
total: Int
aggregateItems: [SearchableAggregateResult]
}
type SearchableAggregateResult {
name: String!
result: SearchableAggregateGenericResult
}
type SearchableAggregateScalarResult {
value: Float!
}
type SearchableAggregateBucketResult {
buckets: [SearchableAggregateBucketResultItem]
}
type SearchableAggregateBucketResultItem {
key: String!
doc_count: Int!
}
union SearchableAggregateGenericResult = SearchableAggregateScalarResult | SearchableAggregateBucketResult
type Query {
searchPosts(filter: SearchablePostFilterInput, sort: [SearchablePostSortInput], limit: Int, nextToken: String, from: Int): SearchablePostConnection
searchPosts(filter: SearchablePostFilterInput, sort: [SearchablePostSortInput], limit: Int, nextToken: String, from: Int, aggregates: [SearchablePostAggregationInput]): SearchablePostConnection
getPost(id: ID!): Post
listPosts(filter: ModelPostFilterInput, limit: Int, nextToken: String): ModelPostConnection
}
Expand Down Expand Up @@ -888,6 +958,7 @@ $util.toJson($ListRequest)
"Query.searchPosts.req.vtl": "#set( $indexPath = \\"/post/doc/_search\\" )
#set( $nonKeywordFields = [] )
#set( $sortValues = [] )
#set( $aggregateValues = {} )
#set( $primaryKey = \\"id\\" )
#if( $util.isNullOrEmpty($context.args.sort) )
#if( $nonKeywordFields.contains($primaryKey) )
Expand Down Expand Up @@ -916,6 +987,13 @@ $util.toJson($ListRequest)
$util.qr($sortValues.add(\\"{$sortField: $sortDirection}\\"))
#end
#end
#foreach( $aggItem in $context.args.aggregates )
#if( $nonKeywordFields.contains($aggItem.field) )
$util.qr($aggregateValues.put(\\"$aggItem.name\\", {\\"$aggItem.type\\": {\\"field\\": \\"$aggItem.field\\"}}))
#else
$util.qr($aggregateValues.put(\\"$aggItem.name\\", {\\"$aggItem.type\\": {\\"field\\": \\"\${aggItem.field}.keyword\\"}}))
#end
#end
{
\\"version\\": \\"2018-05-29\\",
\\"operation\\": \\"GET\\",
Expand All @@ -933,21 +1011,41 @@ $util.transform.toElasticsearchQueryDSL($ctx.args.filter)
{
\\"match_all\\": {}
}
#end
#end,
\\"aggs\\": $util.toJson($aggregateValues)
}
}
}",
"Query.searchPosts.res.vtl": "#set( $es_items = [] )
#set( $aggregateValues = [] )
#foreach( $entry in $context.result.hits.hits )
#if( !$foreach.hasNext )
#set( $nextToken = $util.base64Encode($util.toJson($entry.sort)) )
#end
$util.qr($es_items.add($entry.get(\\"_source\\")))
#end
#foreach( $aggItem in $context.result.aggregations.keySet() )
#set( $aggResult = {} )
#set( $aggResultValue = {} )
$util.qr($aggResult.put(\\"name\\", $aggItem))
#if( !$util.isNullOrEmpty($context.result.aggregations) )
#if( !$util.isNullOrEmpty($context.result.aggregations.get($aggItem).buckets) )
$util.qr($aggResultValue.put(\\"__typename\\", \\"SearchableAggregateBucketResult\\"))
$util.qr($aggResultValue.put(\\"buckets\\", $context.result.aggregations.get($aggItem).buckets))
#end
#if( !$util.isNullOrEmpty($context.result.aggregations.get($aggItem).value) )
$util.qr($aggResultValue.put(\\"__typename\\", \\"SearchableAggregateScalarResult\\"))
$util.qr($aggResultValue.put(\\"value\\", $context.result.aggregations.get($aggItem).value))
#end
#end
$util.qr($aggResult.put(\\"result\\", $aggResultValue))
$util.qr($aggregateValues.add($aggResult))
#end
$util.toJson({
\\"items\\": $es_items,
\\"total\\": $ctx.result.hits.total,
\\"nextToken\\": $nextToken
\\"total\\": $ctx.result.hits.total.value,
\\"nextToken\\": $nextToken,
\\"aggregateItems\\": $aggregateValues
})",
"Subscription.onCreatePost.req.vtl": "## [Start] Subscription Request template. **
$util.toJson({
Expand Down Expand Up @@ -1081,15 +1179,50 @@ input SearchablePostSortInput {
direction: SearchableSortDirection
}
input SearchablePostAggregationInput {
name: String!
type: SearchableAggregateType!
field: String!
}
enum SearchableAggregateType {
terms
avg
min
max
sum
}
type SearchablePostConnection {
items: [Post]
nextToken: String
total: Int
aggregateItems: [SearchableAggregateResult]
}
type SearchableAggregateResult {
name: String!
result: SearchableAggregateGenericResult
}
type SearchableAggregateScalarResult {
value: Float!
}
type SearchableAggregateBucketResult {
buckets: [SearchableAggregateBucketResultItem]
}
type SearchableAggregateBucketResultItem {
key: String!
doc_count: Int!
}
union SearchableAggregateGenericResult = SearchableAggregateScalarResult | SearchableAggregateBucketResult
type Query {
searchPosts(filter: SearchablePostFilterInput, sort: [SearchablePostSortInput], limit: Int, nextToken: String, from: Int): SearchablePostConnection
searchUsers(filter: SearchableUserFilterInput, sort: [SearchableUserSortInput], limit: Int, nextToken: String, from: Int): SearchableUserConnection
searchPosts(filter: SearchablePostFilterInput, sort: [SearchablePostSortInput], limit: Int, nextToken: String, from: Int, aggregates: [SearchablePostAggregationInput]): SearchablePostConnection
searchUsers(filter: SearchableUserFilterInput, sort: [SearchableUserSortInput], limit: Int, nextToken: String, from: Int, aggregates: [SearchableUserAggregationInput]): SearchableUserConnection
getPost(id: ID!): Post
listPosts(filter: ModelPostFilterInput, limit: Int, nextToken: String): ModelPostConnection
getUser(id: ID!): User
Expand All @@ -1114,10 +1247,17 @@ input SearchableUserSortInput {
direction: SearchableSortDirection
}
input SearchableUserAggregationInput {
name: String!
type: SearchableAggregateType!
field: String!
}
type SearchableUserConnection {
items: [User]
nextToken: String
total: Int
aggregateItems: [SearchableAggregateResult]
}
input ModelStringInput {
Expand Down Expand Up @@ -1400,14 +1540,49 @@ input SearchablePostSortInput {
direction: SearchableSortDirection
}
input SearchablePostAggregationInput {
name: String!
type: SearchableAggregateType!
field: String!
}
enum SearchableAggregateType {
terms
avg
min
max
sum
}
type SearchablePostConnection {
items: [Post]
nextToken: String
total: Int
aggregateItems: [SearchableAggregateResult]
}
type SearchableAggregateResult {
name: String!
result: SearchableAggregateGenericResult
}
type SearchableAggregateScalarResult {
value: Float!
}
type SearchableAggregateBucketResult {
buckets: [SearchableAggregateBucketResultItem]
}
type SearchableAggregateBucketResultItem {
key: String!
doc_count: Int!
}
union SearchableAggregateGenericResult = SearchableAggregateScalarResult | SearchableAggregateBucketResult
type Query {
searchPosts(filter: SearchablePostFilterInput, sort: [SearchablePostSortInput], limit: Int, nextToken: String, from: Int): SearchablePostConnection
searchPosts(filter: SearchablePostFilterInput, sort: [SearchablePostSortInput], limit: Int, nextToken: String, from: Int, aggregates: [SearchablePostAggregationInput]): SearchablePostConnection
getPost(id: ID!): Post
listPosts(filter: ModelPostFilterInput, limit: Int, nextToken: String): ModelPostConnection
}
Expand Down Expand Up @@ -1651,14 +1826,49 @@ input SearchablePostSortInput {
direction: SearchableSortDirection
}
input SearchablePostAggregationInput {
name: String!
type: SearchableAggregateType!
field: String!
}
enum SearchableAggregateType {
terms
avg
min
max
sum
}
type SearchablePostConnection {
items: [Post]
nextToken: String
total: Int
aggregateItems: [SearchableAggregateResult]
}
type SearchableAggregateResult {
name: String!
result: SearchableAggregateGenericResult
}
type SearchableAggregateScalarResult {
value: Float!
}
type SearchableAggregateBucketResult {
buckets: [SearchableAggregateBucketResultItem]
}
type SearchableAggregateBucketResultItem {
key: String!
doc_count: Int!
}
union SearchableAggregateGenericResult = SearchableAggregateScalarResult | SearchableAggregateBucketResult
type Query {
customSearchPost(filter: SearchablePostFilterInput, sort: [SearchablePostSortInput], limit: Int, nextToken: String, from: Int): SearchablePostConnection
customSearchPost(filter: SearchablePostFilterInput, sort: [SearchablePostSortInput], limit: Int, nextToken: String, from: Int, aggregates: [SearchablePostAggregationInput]): SearchablePostConnection
getPost(id: ID!): Post
listPosts(filter: ModelPostFilterInput, limit: Int, nextToken: String): ModelPostConnection
}
Expand Down Expand Up @@ -1902,14 +2112,49 @@ input SearchablePostSortInput {
direction: SearchableSortDirection
}
input SearchablePostAggregationInput {
name: String!
type: SearchableAggregateType!
field: String!
}
enum SearchableAggregateType {
terms
avg
min
max
sum
}
type SearchablePostConnection {
items: [Post]
nextToken: String
total: Int
aggregateItems: [SearchableAggregateResult]
}
type SearchableAggregateResult {
name: String!
result: SearchableAggregateGenericResult
}
type SearchableAggregateScalarResult {
value: Float!
}
type SearchableAggregateBucketResult {
buckets: [SearchableAggregateBucketResultItem]
}
type SearchableAggregateBucketResultItem {
key: String!
doc_count: Int!
}
union SearchableAggregateGenericResult = SearchableAggregateScalarResult | SearchableAggregateBucketResult
type Query {
searchPosts(filter: SearchablePostFilterInput, sort: [SearchablePostSortInput], limit: Int, nextToken: String, from: Int): SearchablePostConnection
searchPosts(filter: SearchablePostFilterInput, sort: [SearchablePostSortInput], limit: Int, nextToken: String, from: Int, aggregates: [SearchablePostAggregationInput]): SearchablePostConnection
getPost(id: ID!): Post
listPosts(filter: ModelPostFilterInput, limit: Int, nextToken: String): ModelPostConnection
}
Expand Down
Loading

0 comments on commit 3b6787f

Please sign in to comment.