1
1
# AQLqueryBuilder.js
2
2
> a typescript query builder for [ arangodb] ( https://www.arangodb.com ) 's [ ArangoSearch] ( https://www.arangodb.com/docs/stable/arangosearch.html )
3
- See working demo at [ hp4k1h5.github.io] ( https://hp4k1h5.github.io/#search-box ) .
3
+
4
+ See working demo at [ hp4k1h5.github.io] ( https://hp4k1h5.github.io/#search-box ) .
4
5
5
6
** !Note** AQLqueryBuilder.js does NOT contain any code for the search bar, only the
6
7
query string parser and AQL builder. Unabstracted code for the searchbar is
7
- located
8
- [ here ] ( https://github.com/HP4k1h5/hp4k1h5.github.io/tree/main/demos/src/components/search ) .
8
+ located [ here ] ( https://github.com/HP4k1h5/hp4k1h5.github.io/tree/main/demos/src/components/search ) .
9
+
9
10
![ search bar demonstration with schematic query
10
11
interface] ( ./img/searchbar_demo.png )
11
12
12
- - [ overview ] ( #overview )
13
- - [ setup] ( #setup )
14
- - [ installation] ( #installation )
15
- - [ usage] ( #usage )
16
- - [ query object] ( #query-object )
17
- - [ boolean search logic] ( #boolean-search-logic )
18
- - [ default query syntax] ( #default-query-syntax )
19
- - [ Example] ( #Example )
20
- - [ bugs] ( #bugs )
21
- - [ contributing] ( #contributing )
13
+ - [ Patch Notes] ( #patch-notes )
14
+ - [ overview] ( #overview )
15
+ - [ setup] ( #setup )
16
+ - [ installation] ( #installation )
17
+ - [ usage] ( #usage )
18
+ - [ ` buildAQL() ` ] ( #buildaql() )
19
+ - [ boolean search logic] ( #boolean-search-logic )
20
+ - [ default query syntax] ( #default-query-syntax )
21
+ - [ Example] ( #example )
22
+ - [ bugs] ( #bugs )
23
+ - [ contributing] ( #contributing )
24
+
25
+ ## Patch Notes
26
+
27
+ - v0.1.1
28
+ - ❌ Breaking change!
29
+ ` buildAQL() ` 's ` limit ` parameter no longer accepts key
30
+ ` end ` , which has been renamed, per Arango spec, to ` count ` . The functionality
31
+ remains the same, which is why the patch bump. Please accept my apologies.
32
+ - 🔑🗝 multi-key search
33
+ this can be useful if you
34
+ have multiple fields with textual information. Theoretically, each
35
+ chapter of a book could be stored on its own key. Or a document could
36
+ have be translated into several languages, each stored on its own key.
37
+ - ** There are two ways to provide multiple keys to relevant functions.**
38
+ 1 ) ` query.key ` now accepts in addition to a string value, an array of
39
+ strings over which the query is to be run. All keys listed here will be
40
+ run combined with all collections provided to ` query.collections `
41
+ ** unless** a collection has a ` keys ` property of its own, in which case
42
+ ** only** those keys are searched against.
43
+ 2 ) ` query.collections[].keys ` is an optional array of key names that are
44
+ indexed by ` query.collections[].analyzer ` . ** Note** It is important that
45
+ any key listed in ` query.collections[].keys ` be indexed by the analyzer
46
+ as it will impact results if such a key does not exist.
47
+
48
+
22
49
23
50
## overview
24
51
@@ -87,7 +114,8 @@ also accommodate multiple key searches.
87
114
___
88
115
## setup
89
116
90
- 1) running generated AQL queries will require a running arangodb instance.
117
+ 1) running generated AQL queries will require a running ArangoDB v3.6+
118
+ instance. This package is tested against v3.6.5 🥑
91
119
92
120
## installation
93
121
currently there is only support for server-side use.
@@ -96,7 +124,8 @@ currently there is only support for server-side use.
96
124
or `npm install --save @hp4k1h5/AQLqueryBuilder.js`
97
125
in a directory containing a `package.json` file.
98
126
__or__
99
- clone this repository in your node compatible project.
127
+ clone this repository in your node compatible project. And run `yarn` from
128
+ inside the directory.
100
129
101
130
2) import/require the exported functions
102
131
```js
@@ -113,22 +142,15 @@ const {buildAQL} = require('@hp4k1h5/AQLqueryBuilder.js')
113
142
__ for up-to-date documentation, run ` yarn doc && serve docs/ ` from the project
114
143
directory root.__
115
144
116
- AQLqueryBuilder aims to provide cross-collection and cross-language boolean
117
- search capabilities to the library's user. Currently, this library makes a
118
- number of assumptions about the way your data is stored and indexed, but these
119
- are hopefully compatible with a wide range of setups.
120
-
121
- The primary assumption this library makes is that the data you are trying to
122
- query against is indexed by an ArangoSearch View, and that all documents index
123
- the same exact field. This field, passed to the builder as a key on the
124
- ` query ` object passed to e.g. ` buildAQL() ` , can be indexed by any number of
125
- analyzers, and the query will target all supplied collections simultaneously.
126
- This allows for true multi-language search, provided all analyzers and
127
- indexers index the same key as all other documents in the view. While there
128
- are plans to expand on this functionality to provide multi-key search, this
129
- library is primarily built for academic and textual searches, and is ideally
130
- suited for documents like books, articles, and other media where most of the
131
- data resides in a single place, i.e. document ` key ` , or ` field ` .
145
+ AQLqueryBuilder aims to provide cross-collection and cross-language, multi-key
146
+ boolean search capabilities to the library's user.
147
+
148
+ Please ensure that the data you are trying to query against is indexed by an
149
+ ArangoSearch View. The query will target all combinations of provided
150
+ collections, analyzers and keys an simultaneously. This allows for granular
151
+ multi-language search. This library is primarily built for academic and
152
+ textual searches, and is ideally suited for documents like books, articles,
153
+ and other text-heavy media.
132
154
133
155
AQLqueryBuilder works best as a document query tool. Leveraging ArangoSearch's
134
156
built-in language stemming analyzers allows for complex search phrases to be
@@ -143,55 +165,112 @@ const queryObject = {
143
165
" view" : " the_arango-search_view-name" ,
144
166
" collections" : [{
145
167
" name" : " collection_name" ,
146
- " analyzer" : " analyzer_name"
168
+ " analyzer" : " analyzer_name" ,
169
+ " keys" : [" text" ]
147
170
}],
148
171
" query" : " +'query string' -for +parseQuery ?to parse"
149
172
}
150
173
151
- const aqlQuery = buildAQL (queryObject)
174
+ const limit = {start: 0 , count: 20 }
175
+
176
+ const aqlQuery = buildAQL (queryObject, limit)
152
177
153
178
// ... const cursor = await db.query(aqlQuery)
154
- // ... const cursor = await db.query(buildAQL(queryObject, {start:21, end:40 })
179
+ // ... const cursor = await db.query(buildAQL(queryObject, {start:20, count:20 })
155
180
```
156
181
157
182
Generate documenation with ` yarn doc && serve docs/ ` or see more examples in
158
183
e.g. [ tests/search.ts] ( tests/search.ts )
159
184
160
- ### query object
185
+ ___
186
+
187
+ ### ` buildAQL() `
161
188
162
- ` buildAQL ` accepts an object with the following properties:
189
+ ` buildAQL ` accepts two parameters: ` query ` and ` limit `
163
190
191
+ #### query
164
192
• ** view** : * string* (required): the name of the ArangoSearch view the query
165
193
will be run against
166
194
167
- • ** collections** (required): the names of the collections indexed by @view to query
168
-
169
- • ** terms** (required): either an array of ` term ` interfaces or a string to be
170
- parsed by ` parseQuery `
171
-
172
- • ** key** (optional | default: "text"): the name of the Arango document key to search
173
- within.
195
+ • ** collections** (required): an array of objects of the indexed collections
196
+ to query. Objects have the following shape:
197
+ ``` json
198
+ {
199
+ "name" : " collection-name" ,
200
+ "analyzer" : " analyzer_name" ,
201
+ "keys" : [" text" , " summary" , " notes" ]
202
+ }
203
+ ```
204
+ ` keys ` are optional, though if key names are provided to ` query.key ` , and
205
+ not all those keys are indexed by the collection, it is advisable to
206
+ explicitly list only those keys on documents in that collection that are
207
+ indexed by the analyzer. If a collection is indexed by multiple analyzers
208
+ please list each collection-analyzer combination along with their relevant
209
+ keys, unless a unified set of keys is provided to ` query.key ` .
210
+
211
+ • ** query** (required): either an array of ` term ` interfaces or a query string
212
+ to be parsed by ` parseQuery ` .
213
+ - ** term** (optional): a JSON object representing the search.
214
+ - ** val** * string* (required): a search term or phrase. For PHRASE
215
+ searches, include one "quoted phrase" per val. For TOKENS you may combine
216
+ multiple terms under a single analyzer or use one ` term ` per token.
217
+ - ** op** * string* (required): boolean operator; ** one of ` + ? - ` ,**
218
+ representing ` AND OR NOT ` respectively
219
+ - ** type** * string* (required): ** one of ` "phr" ` ** for PHRASES ** or ` "tok" ` **
220
+ for TOKENS.
221
+
222
+ • ** key** (optional | default: "text"): the name of the Arango document key to
223
+ search within.
224
+
225
+ • ** filters** (optional): a list of filter interfaces. See [ arango FILTER
226
+ operations] ( https://www.arangodb.com/docs/stable/aql/operations-filter.html )
227
+ for more details. All [ Arango
228
+ operators] ( https://www.arangodb.com/docs/3.6/aql/operators.html ) Filters have
229
+ the following shape:
230
+ ``` json
231
+ {
232
+ "field" : " the name of the field, i.e. Arango document key to filter on" ,
233
+ "op" : " one of: == != < > <= >= IN NOT IN LIKE NOT LIKE =~ !~ " ,
234
+ "val" : " the "
235
+ }
236
+ ```
174
237
175
- • ** filters** (optional): a list of @filter interfaces
238
+ #### limit
239
+ an object with the following keys:
240
+ - ` start ` (integer) 0-based result pagination lower bound.
241
+ - ` count ` (integer) total number of results to return. ⚠ see CHANGELOG v0.1.1
176
242
177
- ___
243
+ to bring back up to the first 10 results
244
+ ``` json
245
+ {"start" :0 , "count" :10 }
246
+ ```
247
+ and the next page would be
248
+ ``` json
249
+ {"start" :10 , "count" :10 }
250
+ ```
178
251
179
- #### ` query ` Example
252
+ #### ` query ` example
180
253
``` json
181
254
{
182
255
"view" : " the_arango-search_view-name" ,
183
256
"collections" : [
184
257
{
185
- "name" :
186
- " collection_name" , "analyzer" :
187
- " analyzer_name"
258
+ "name" :" collection_name" ,
259
+ "analyzer" : " analyzer_name" ,
260
+ "keys" : [" text" , " summary" , " notes" ]
261
+ },
262
+ {
263
+ "name" :" collection_es" ,
264
+ "analyzer" : " analyzer_es" ,
265
+ "keys" : [" texto" , " resumen" , " notas" ]
188
266
}
189
267
],
190
- "key" : " text" ,
191
- "query" : " either a +query ?\" string for parseQuery to parse\" " ,
268
+ "query" : " either a +query ?\" string for parseQuery to parse\" texto en español" ,
192
269
"query" : [
193
270
{"type" : " phr" , "op" : " ?" , "val" : " \" !!! OR a list of query objects\" " },
194
- {"type" : " tok" , "op" : " -" , "val" : " tokens" }
271
+ {"type" : " phr" , "op" : " ?" , "val" : " optional phrases" },
272
+ {"type" : " tok" , "op" : " -" , "val" : " excluded tokens" },
273
+ {"type" : " tok" , "op" : " +" , "val" : " mandatory tokens" }
195
274
],
196
275
"filters" : [
197
276
{
203
282
}
204
283
```
205
284
206
-
207
285
___
208
286
### boolean search logic
209
-
210
287
Quoting [ mit's Database Search Tips] ( https://libguides.mit.edu/c.php?g=175963&p=1158594 ) :
211
-
212
288
> Boolean operators form the basis of mathematical sets and database logic.
213
289
They connect your search words together to either narrow or broaden your
214
290
set of results. The three basic boolean operators are: AND, OR, and NOT.
289
365
please see [ bugs] ( https://github.com/HP4k1h5/AQLqueryBuilder.js/issues/new?assignees=HP4k1h5&labels=bug&template=bug_report.md&title=basic )
290
366
291
367
## contributing
292
- please see [ ./.github/CONTRIBUTING.md]
293
-
368
+ please see [ CONTRIBUTING] ( ./.github/CONTRIBUTING.md )
0 commit comments