@@ -2,12 +2,10 @@ import { aql } from 'arangojs'
2
2
import { query , collection , term } from './lib/structs'
3
3
import { parseQuery } from './parse'
4
4
5
-
6
5
export function buildSearch ( query : query ) : any {
7
6
/* parse string query */
8
- query . terms = typeof query . terms == 'string'
9
- ? parseQuery ( query . terms )
10
- : query . terms
7
+ query . terms =
8
+ typeof query . terms == 'string' ? parseQuery ( query . terms ) : query . terms
11
9
12
10
/* build boolean pieces */
13
11
let ANDS = buildOPS ( query . collections , query . terms , '+' , query . key )
@@ -22,7 +20,9 @@ export function buildSearch(query: query): any {
22
20
if ( ! ! NOTS ) {
23
21
NOTS = aql `${ ANDS || ORS ? aql . literal ( ' AND ' ) : undefined }
24
22
${ NOTS . phrases ? aql . literal ( ' NOT ' ) : undefined } ${ NOTS . phrases }
25
- ${ NOTS . phrases && NOTS . tokens ? aql . literal ( ' AND ' ) : undefined } ${ NOTS . tokens } `
23
+ ${ NOTS . phrases && NOTS . tokens ? aql . literal ( ' AND ' ) : undefined } ${
24
+ NOTS . tokens
25
+ } `
26
26
}
27
27
28
28
/* if an empty query.terms string or array is passed, SEARCH true, bringing
@@ -33,19 +33,24 @@ export function buildSearch(query: query): any {
33
33
${ ORS }
34
34
${ NOTS }
35
35
${ ( ! ANDS && ! ORS && ! NOTS ) || undefined }
36
- OPTIONS ${ { collections : query . collections . map ( c => c . name ) } }
36
+ OPTIONS ${ { collections : query . collections . map ( ( c ) => c . name ) } }
37
37
SORT TFIDF(doc) DESC`
38
38
}
39
39
40
- function buildOPS ( collections : collection [ ] , terms : term [ ] , op : string , key :
41
- string = 'text' ) : any {
40
+ function buildOPS (
41
+ collections : collection [ ] ,
42
+ terms : term [ ] ,
43
+ op : string ,
44
+ key : string = 'text' ,
45
+ ) : any {
42
46
const opWord : string = op == '+' ? ' AND ' : ' OR '
43
47
44
48
let queryTerms : any = terms . filter ( ( t : term ) => t . op == op )
45
49
if ( ! queryTerms . length ) return
46
50
47
51
/* phrases */
48
- let phrases = queryTerms . filter ( ( qT : term ) => qT . type == 'phr' )
52
+ let phrases = queryTerms
53
+ . filter ( ( qT : term ) => qT . type == 'phr' )
49
54
. map ( ( phrase : any ) => buildPhrase ( phrase , collections , key ) )
50
55
if ( ! phrases . length ) {
51
56
phrases = undefined
@@ -60,17 +65,25 @@ function buildOPS(collections: collection[], terms: term[], op: string, key:
60
65
if ( ! phrases && ! tokens ) return
61
66
if ( op == '-' ) return { phrases, tokens }
62
67
if ( phrases && tokens ) return aql . join ( [ phrases , tokens ] , opWord )
63
- return ( tokens || phrases )
68
+ return tokens || phrases
64
69
}
65
70
66
- function buildPhrase ( phrase : term , collections : collection [ ] , key : string ) : any {
67
- const phrases = collections . map ( coll => {
71
+ function buildPhrase (
72
+ phrase : term ,
73
+ collections : collection [ ] ,
74
+ key : string ,
75
+ ) : any {
76
+ const phrases = collections . map ( ( coll ) => {
68
77
return aql `PHRASE(doc.${ key } , ${ phrase . val . slice ( 1 , - 1 ) } , ${ coll . analyzer } )`
69
78
} )
70
79
return aql `(${ aql . join ( phrases , ' OR ' ) } )`
71
80
}
72
81
73
- function buildTokens ( tokens : term [ ] , collections : collection [ ] , key : string ) : any {
82
+ function buildTokens (
83
+ tokens : term [ ] ,
84
+ collections : collection [ ] ,
85
+ key : string ,
86
+ ) : any {
74
87
if ( ! tokens . length ) return
75
88
76
89
const opWordMap = {
@@ -85,19 +98,24 @@ function buildTokens(tokens: term[], collections: collection[], key: string): an
85
98
return a
86
99
} , { } )
87
100
88
- const makeTokenAnalyzers = ( tokens : term [ ] , op : string , analyzer : string ,
89
- key : string ) => {
101
+ const makeTokenAnalyzers = (
102
+ tokens : term [ ] ,
103
+ op : string ,
104
+ analyzer : string ,
105
+ key : string ,
106
+ ) => {
90
107
return aql `
91
108
ANALYZER(
92
109
TOKENS(${ tokens } , ${ analyzer } )
93
110
${ aql . literal ( op ) } IN doc.${ key } , ${ analyzer } )`
94
111
}
95
112
96
113
let remapped = [ ]
97
- collections . forEach ( coll => {
114
+ collections . forEach ( ( coll ) => {
98
115
remapped . push (
99
- ...Object . keys ( mapped ) . map ( op => makeTokenAnalyzers ( mapped [ op ] , op ,
100
- coll . analyzer , key ) )
116
+ ...Object . keys ( mapped ) . map ( ( op ) =>
117
+ makeTokenAnalyzers ( mapped [ op ] , op , coll . analyzer , key ) ,
118
+ ) ,
101
119
)
102
120
} )
103
121
0 commit comments