@@ -30,11 +30,11 @@ export default function createSearchResolver(
3030 ) ;
3131 }
3232
33- // if (!(tc instanceof TypeComposer)) {
34- // throw new Error(
35- // 'Second arg for Resolver search() should be instance of TypeComposer.'
36- // );
37- // }
33+ if ( ! ( sourceTC instanceof TypeComposer ) ) {
34+ throw new Error (
35+ 'Second arg for Resolver search() should be instance of TypeComposer.'
36+ ) ;
37+ }
3838
3939 const prefix = opts . prefix || 'Es' ;
4040
@@ -48,36 +48,62 @@ export default function createSearchResolver(
4848 type : 'cv' ,
4949 } ) ;
5050
51- const args = Object . assign ( { } , searchFC . args , {
51+ const searchITC = getSearchBodyITC ( {
52+ prefix,
53+ fieldMap,
54+ } ) ;
55+
56+ searchITC . removeField ( [ 'size' , 'from' , '_source' , 'explain' , 'version' ] ) ;
57+
58+ const argsConfigMap = Object . assign ( { } , searchFC . args , {
5259 body : {
53- type : getSearchBodyITC ( {
54- prefix,
55- fieldMap,
56- } ) . getType ( ) ,
60+ type : searchITC . getType ( ) ,
5761 } ,
5862 } ) ;
5963
60- delete args . index ; // index can not be changed, it hardcoded in searchFC
61- delete args . type ; // type can not be changed, it hardcoded in searchFC
62- delete args . explain ; // added automatically if requested _shard, _node, _explanation
63- delete args . version ; // added automatically if requested _version
64- delete args . _source ; // added automatically due projection
65- delete args . _sourceExclude ; // added automatically due projection
66- delete args . _sourceInclude ; // added automatically due projection
64+ delete argsConfigMap . index ; // index can not be changed, it hardcoded in searchFC
65+ delete argsConfigMap . type ; // type can not be changed, it hardcoded in searchFC
66+ delete argsConfigMap . explain ; // added automatically if requested _shard, _node, _explanation
67+ delete argsConfigMap . version ; // added automatically if requested _version
68+ delete argsConfigMap . _source ; // added automatically due projection
69+ delete argsConfigMap . _sourceExclude ; // added automatically due projection
70+ delete argsConfigMap . _sourceInclude ; // added automatically due projection
71+
72+ delete argsConfigMap . size ;
73+ delete argsConfigMap . from ;
74+ argsConfigMap . limit = 'Int' ;
75+ argsConfigMap . skip = 'Int' ;
76+
77+ const type = getSearchOutputTC ( { prefix, fieldMap, sourceTC } ) ;
78+ // $FlowFixMe
79+ type . addFields ( {
80+ count : 'Int' ,
81+ max_score : 'Float' ,
82+ } ) ;
6783
6884 // $FlowFixMe
6985 return new Resolver ( {
70- // $FlowFixMe
71- type : sourceTC ? getSearchOutputTC ( { prefix, fieldMap, sourceTC } ) : 'JSON' ,
86+ type,
7287 name : 'search' ,
7388 kind : 'query' ,
74- args,
75- resolve : ( rp : ResolveParams < * , * > ) => {
76- if ( rp . args && rp . args . body ) {
77- rp . args . body = prepareBodyInResolve ( rp . args . body , fieldMap ) ;
89+ args : argsConfigMap ,
90+ resolve : async ( rp : ResolveParams < * , * > ) => {
91+ const { args = { } , projection = { } } = rp ;
92+
93+ if ( args . body ) {
94+ args . body = prepareBodyInResolve ( args . body , fieldMap ) ;
95+ }
96+
97+ if ( { } . hasOwnProperty . call ( args , 'limit' ) ) {
98+ args . size = args . limit ;
99+ delete args . limit ;
100+ }
101+
102+ if ( { } . hasOwnProperty . call ( args , 'skip' ) ) {
103+ args . from = args . skip ;
104+ delete args . skip ;
78105 }
79106
80- const { projection = { } } = rp ;
81107 const { hits = { } } = projection ;
82108 // $FlowFixMe
83109 const { hits : hitsHits } = hits ;
@@ -86,25 +112,28 @@ export default function createSearchResolver(
86112 // Turn on explain if in projection requested this fields:
87113 if ( hitsHits . _shard || hitsHits . _node || hitsHits . _explanation ) {
88114 // $FlowFixMe
89- rp . args . body . explain = true ;
115+ args . body . explain = true ;
90116 }
91117
92118 if ( hitsHits . _version ) {
93119 // $FlowFixMe
94- rp . args . body . version = true ;
120+ args . body . version = true ;
95121 }
96122
97123 if ( ! hitsHits . _source ) {
98124 // $FlowFixMe
99- rp . args . body . _source = false ;
125+ args . body . _source = false ;
100126 } else {
101127 // $FlowFixMe
102- rp . args . body . _source = toDottedList ( hitsHits . _source ) ;
128+ args . body . _source = toDottedList ( hitsHits . _source ) ;
103129 }
104130 }
105131
106132 // $FlowFixMe
107- const res = searchFC . resolve ( rp . source , rp . args , rp . context , rp . info ) ;
133+ const res = await searchFC . resolve ( rp . source , args , rp . context , rp . info ) ;
134+
135+ res . count = res . hits . total ;
136+ res . max_score = res . hits . max_score ;
108137
109138 return res ;
110139 } ,
0 commit comments