-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.ts
202 lines (171 loc) · 5.04 KB
/
search.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { expect } from 'chai'
import { buildSearch } from '../src/search'
describe('search.ts', () => {
it('should export a function named buildSearch', () => {
expect(buildSearch).to.be.a('function')
})
it(`should return SEARCH true
when terms: is an empty string or array`, () => {
/* empty string */
let empty_string_query = {
view: 'search_view',
collections: [{ name: 'coll', analyzer: 'text_en' }],
terms: '',
}
const builtSearch = buildSearch(empty_string_query)
expect(builtSearch).to.be.an('object')
expect(Object.keys(builtSearch.bindVars)).to.have.length(2)
expect(builtSearch.bindVars.value0).to.equal(true)
expect(builtSearch.bindVars.value1).to.deep.equal(['coll'])
/* empty array */
let empty_array_query = {
view: 'search_view',
collections: [{ name: 'coll', analyzer: 'text_en' }],
terms: [],
}
const builtSearch_from_array = buildSearch(empty_array_query)
expect(builtSearch_from_array.bindVars.value0).to.equal(true)
})
it(`should handle single phrase queries`, () => {
const query = {
view: 'search_view',
collections: [
{
name: 'coll',
analyzer: 'text_en',
keys: ['text'],
},
],
terms: '"complex phrase"',
/* key: ['text'], */
}
const builtSearch = buildSearch(query)
expect(builtSearch.query).to.equal(`
SEARCH
(PHRASE(doc.@value0, @value1, @value2))
OPTIONS { collections: @value3 }
SORT TFIDF(doc) DESC`)
})
it(`should return an array of aql objects
when a complex query is passed`, () => {
const query = {
view: 'search_view',
collections: [
{
name: 'coll',
analyzer: 'text_en',
keys: ['text'],
},
],
terms: '-a +"query string" ?token',
key: ['text'],
}
const builtSearch = buildSearch(query)
expect(Object.keys(builtSearch.bindVars)).to.have.length(7)
expect(builtSearch.bindVars.value0).to.deep.equal('text')
expect(builtSearch.bindVars.value1).to.deep.equal('query string')
expect(builtSearch.bindVars.value2).to.deep.equal('text_en')
expect(builtSearch.bindVars.value4).to.deep.equal(1)
expect(builtSearch.bindVars.value5).to.deep.equal('a')
expect(builtSearch.bindVars.value6).to.deep.equal([
query.collections[0].name,
])
expect(builtSearch.query).to.equal(`
SEARCH
(PHRASE(doc.@value0, @value1, @value2)) OR ((PHRASE(doc.@value0, @value1, @value2)) AND MIN_MATCH(
ANALYZER(
TOKENS(@value3, @value2)
ANY IN doc.@value0, @value2),
@value4))
AND
MIN_MATCH(
ANALYZER(
TOKENS(@value5, @value2)
NONE IN doc.@value0, @value2),
@value4)
OPTIONS { collections: @value6 }
SORT TFIDF(doc) DESC`)
})
it('should return an array of aql objects', () => {
const query = {
view: 'search_view',
collections: [
{
name: 'coll',
analyzer: 'text_en',
keys: ['text'],
},
],
terms: '+mandatory -exclude ?"optional phrase"',
key: ['text'],
}
const builtSearch = buildSearch(query)
expect(builtSearch.query).to.equal(`
SEARCH
MIN_MATCH(
ANALYZER(
TOKENS(@value0, @value1)
ALL IN doc.@value2, @value1),
@value3) OR (MIN_MATCH(
ANALYZER(
TOKENS(@value0, @value1)
ALL IN doc.@value2, @value1),
@value3) AND (PHRASE(doc.@value2, @value4, @value1)))
AND
MIN_MATCH(
ANALYZER(
TOKENS(@value5, @value1)
NONE IN doc.@value2, @value1),
@value3)
OPTIONS { collections: @value6 }
SORT TFIDF(doc) DESC`)
})
it('should handle exclude phrase', () => {
const query = {
view: 'search_view',
collections: [
{
name: 'coll',
analyzer: 'text_en',
keys: ['text'],
},
],
terms: '-"exclude"',
key: ['text'],
}
const builtSearch = buildSearch(query)
expect(builtSearch.query).to.equal(`
SEARCH
NOT (PHRASE(doc.@value0, @value1, @value2))
OPTIONS { collections: @value3 }
SORT TFIDF(doc) DESC`)
})
it('should handle exclude token', () => {
const query = {
view: 'search_view',
collections: [
{
name: 'coll',
analyzer: 'text_en',
keys: ['text'],
},
],
terms: '-exclude',
key: ['text'],
}
const builtSearch = buildSearch(query)
expect(builtSearch.bindVars.value0).to.equal('exclude')
expect(builtSearch.bindVars.value1).to.equal('text_en')
expect(builtSearch.bindVars.value2).to.equal('text')
expect(builtSearch.bindVars.value3).to.equal(1)
expect(builtSearch.query).to.equal(`
SEARCH
MIN_MATCH(
ANALYZER(
TOKENS(@value0, @value1)
NONE IN doc.@value2, @value1),
@value3)
OPTIONS { collections: @value4 }
SORT TFIDF(doc) DESC`)
})
})