Skip to content

Commit

Permalink
feat: Support for Elasticsearch "Collapse" query
Browse files Browse the repository at this point in the history
* add collapse field

* Update SearchBody.ts

* Update SearchHitItem.ts
  • Loading branch information
Michael-372 authored Jan 15, 2022
1 parent 4fdfcec commit a06664f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/elasticDSL/SearchBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export function getSearchBodyITC<TContext>(
description,
fields: {
query: { type: () => getQueryITC(opts) },
collapse: 'JSON',
aggs: { type: () => getAggsITC(opts) },
size: 'Int',
from: 'Int',
Expand Down
10 changes: 8 additions & 2 deletions src/resolvers/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ export default function createSearchResolver<TSource, TContext>(
argsConfigMap.query = searchITC.getField('query');
argsConfigMap.aggs = searchITC.getField('aggs');
argsConfigMap.sort = searchITC.getField('sort');
argsConfigMap.collapse = searchITC.getField('collapse');
argsConfigMap.highlight = searchITC.getField('highlight');

const topLevelArgs = ['q', 'query', 'sort', 'limit', 'skip', 'aggs', 'highlight', 'opts'];
const topLevelArgs = ['q', 'query', 'collapse', 'sort', 'limit', 'skip', 'aggs', 'highlight', 'opts'];
argsConfigMap.opts = schemaComposer
.createInputTC({
name: `${sourceTC.getTypeName()}Opts`,
Expand Down Expand Up @@ -145,6 +146,11 @@ export default function createSearchResolver<TSource, TContext>(
args.body.query = args.query;
delete args.query;
}

if (args.collapse) {
args.body.collapse = args.collapse;
delete args.collapse;
}

if (args.aggs) {
args.body.aggs = args.aggs;
Expand Down Expand Up @@ -191,7 +197,7 @@ export default function createSearchResolver<TSource, TContext>(
return res;
},
})
.reorderArgs(['q', 'query', 'sort', 'limit', 'skip', 'aggs']);
.reorderArgs(['q', 'query', 'collapse', 'sort', 'limit', 'skip', 'aggs']);
}

export function toDottedList(projection: ProjectionType, prev?: string[]): string[] | boolean {
Expand Down
5 changes: 5 additions & 0 deletions src/types/SearchHitItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export function getSearchHitItemTC<TContext>(

_version: 'Int',

inner_hits: {
type: 'JSON',
description: 'Returns data only if `args.collapse` is provided',
},

highlight: {
type: 'JSON',
description: 'Returns data only if `args.highlight` is provided',
Expand Down

0 comments on commit a06664f

Please sign in to comment.