@@ -3,11 +3,14 @@ import { search } from '@ecomplus/client'
3
3
export default ( self , isSimpleSearch , axiosConfig ) => {
4
4
// mount axios req options for complex or simpĺe search
5
5
const reqOptions = {
6
- url : '/items.json'
6
+ url : '/items.json' ,
7
+ axiosConfig
7
8
}
9
+
8
10
if ( isSimpleSearch === true ) {
9
11
// https://www.elastic.co/guide/en/elasticsearch/reference/6.3/search-uri-request.html
10
12
const { query } = self . dsl
13
+ reqOptions . url += '?q='
11
14
if ( query && query . bool && Array . isArray ( query . bool . filter ) ) {
12
15
// parse query filters to string
13
16
let queryString = ''
@@ -19,11 +22,17 @@ export default (self, isSimpleSearch, axiosConfig) => {
19
22
if ( condition ) {
20
23
const field = Object . keys ( condition ) [ 0 ]
21
24
const value = condition [ field ]
22
- queryString += `${ field } :${ ( Array . isArray ( value ) ? `("${ value . join ( '" OR "' ) } ")` : value ) } `
25
+ queryString += `${ field } :${ ( Array . isArray ( value ) ? `("${ value . join ( '" "' ) } ")` : value ) } `
23
26
}
24
27
} )
25
- reqOptions . url += `?q= ${ encodeURIComponent ( queryString ) } `
28
+ reqOptions . url += encodeURIComponent ( queryString )
26
29
}
30
+ // handle pagination
31
+ ; [ 'from' , 'size' ] . forEach ( field => {
32
+ if ( self . dsl [ field ] ) {
33
+ reqOptions . url += `&${ field } =${ self . dsl [ field ] } `
34
+ }
35
+ } )
27
36
} else {
28
37
reqOptions . method = 'post'
29
38
reqOptions . data = self . dsl
@@ -32,9 +41,6 @@ export default (self, isSimpleSearch, axiosConfig) => {
32
41
reqOptions . axiosConfig = isSimpleSearch
33
42
}
34
43
}
35
- if ( axiosConfig ) {
36
- reqOptions . axiosConfig = axiosConfig
37
- }
38
44
39
45
// request Search API and return promise
40
46
return search ( reqOptions ) . then ( ( { data } ) => {
0 commit comments