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
3
4
+ ##### !! warning !! experimental and unstable
5
+
4
6
## overview
5
7
ArangoSearch provides a high-level API for interacting with Arango Search Views
6
8
through the Arango Query Language (AQL). This library aims to provide a query
@@ -47,11 +49,83 @@ provided, optional terms are considered required, so as not to retrieve all
47
49
documents.
48
50
49
51
## setup
52
+
53
+ 1 ) running generated AQL queries will require a working arangodb instance. in
54
+ the future, it is hoped that this package can be imported and used in the
55
+ ` arangosh ` , as well as client and server side. Currently there is only limited
56
+ support for server-side use.
57
+
50
58
## installation
59
+
60
+ !! packaging and export behavior is not stable, and is likely to change
61
+ !! significantly in the short-term
62
+ 1 ) clone this repository in your es6 compatible project.
63
+ 2 ) run ` yarn install ` from the project directory.
64
+
51
65
## usage
66
+ for better documentation, run ` yarn doc && serve docs/ ` from the project
67
+ directory root.
68
+
69
+ AQLqueryBuilder aims to provide collection-agnostic and language-agnostic
70
+ boolean search capabilities to the library's user. Currently, this library
71
+ makes a number of assumptions about the way your data is stored and indexed,
72
+ but these are hopefully compatible with a wide range of setups.
73
+
74
+ The primary assumption this library makes is that the data you are trying to
75
+ query against is indexed by an ArangoSearch View, and that all documents index
76
+ the same exact field. This field can be indexed by any number of analyzers,
77
+ and the search be will run against all supplied collections simultaneously. This
78
+ allows for true multi-language search provided that each collection is
79
+ restricted to just one language and all documents index the same key as all
80
+ other documents in the view. While there are plans to expand on this
81
+ functionality to provide multi-key search, this library is primarily built for
82
+ academic and textual searches, and is ideally suited for documents like books,
83
+ articles, and other media where most of the data resides in a single place.
84
+
85
+ This works best as a document query tool. Leveraging ArangoSearch's built-in
86
+ language stemming analyzers allows for complex search phrases to be run
87
+ against any number of language-specific collections simultaneously.
88
+
89
+ For an example of a multi-lingual document ingest/parser, please see
90
+ [ ptolemy's curator] ( https://gitlab.com/HP4k1h5/nineveh/-/tree/master/ptolemy/dimitri/curator.js )
91
+
92
+ __ Example:__
93
+ ``` javascript
94
+ import {buildAQL } from ' path/to/AQLqueryBuilder'
95
+ const queryObject =
96
+ {
97
+ " view" : " the_arango-search_view-name" ,
98
+ " collections" : [{
99
+ " name" : " collection_name" ,
100
+ " analyzer" : " analyzer_name"
101
+ }],
102
+ " query" : " +'query string' -for +parseQuery ?to parse"
103
+ }
104
+ const aqlQuery = buildAQL (queryObject)
105
+ // ... const cursor = await db.query(aqlQuery)
106
+ ```
107
+ ` collections ` is an array of ` collection ` objects. This allows searching and
108
+ filtering across collections impacted by the search.
109
+
52
110
### query object
53
111
54
112
` buildAQL ` accepts an object with the following properties:
113
+
114
+ • ** view** : * string* (required): the name of the ArangoSearch view the query
115
+ will be run against
116
+
117
+ • ** collections** (required): the names of the collections indexed by @view to query
118
+
119
+ • ** terms** (required): either an array of @term interfaces or a string to be
120
+ parsed by @parseQuery
121
+
122
+ • ** key** (optional | default: "text"): the name of the Arango document key to search
123
+ within.
124
+
125
+ • ** filters** (optional): a list of @filter interfaces
126
+
127
+ ___
128
+
55
129
Example:
56
130
``` json
57
131
{
@@ -63,18 +137,72 @@ Example:
63
137
" analyzer_name"
64
138
}
65
139
],
140
+ "key" : " text" ,
66
141
"query" : " either a +query ?\" string for parseQuery to parse\" " ,
67
142
"query" : [
68
143
{"type" : " phr" , "op" : " ?" , "val" : " \" or a list of query objects\" " },
69
144
{"type" : " tok" , "op" : " -" , "val" : " tokens" }
70
145
],
146
+ "filters" : [
147
+ {
148
+ "field" : " field_name" ,
149
+ "op" : " >" ,
150
+ "val" : 0
151
+ }
152
+ ],
71
153
"limit" :
72
154
{
73
155
"start" : 0 ,
74
156
"end" : 20 ,
75
157
}
76
158
}
77
159
```
160
+
161
+ ### boolean search logic
162
+ Quoting [ mit's Database Search Tips] ( https://libguides.mit.edu/c.php?g=175963&p=1158594 ) :
163
+ > Boolean operators form the basis of mathematical sets and database logic.
164
+ They connect your search words together to either narrow or broaden your
165
+ set of results. The three basic boolean operators are: AND, OR, and NOT.
166
+
167
+ #### ` + ` AND
168
+ * Mandatory terms and phrases. All results MUST INCLUDE these terms and
169
+ * phrases.
170
+ #### ` ? ` OR
171
+ * Optional terms and phrases. If there are ANDS or NOTS, these serve as
172
+ * match score "boosters". If there are no ANDS or NOTS, ORS become required
173
+ * in results.
174
+ #### ` - ` NOT
175
+ * Search results MUST NOT INCLUDE these terms and phrases. If a result that
176
+ * would otherwise have matched, contains one or more terms or phrases, it
177
+ * will not be included in the result set.
178
+
78
179
### default query syntax
180
+ for more information on boolean search logic see
181
+ [ above] ( #boolean-search-logic )
182
+
183
+ The default syntax accepted by ` AQLqueryBuilder ` 's ` query ` object's ` terms `
184
+ key is as follows:
185
+
186
+ 1 ) Everything inside single or double quotes is considered a ` PHRASE `
187
+ 2 ) Everything else is considered a word to be analyzed by ` TOKENS `
188
+ 3 ) Every individual search word and quoted phrase may be optionally prefixed
189
+ by one of the following symbols ` + ? - ` , or the plus-sign, the question-mark,
190
+ and the minus-sign. If a word has no operator prefix, it is considered
191
+ optional and is counted as an ` OR ` .
192
+
193
+ Example:
194
+ input ` one +two -"buckle my shoe" ` and the queryParser will interpret as
195
+ follows:
196
+
197
+ | | ANDS | ORS | NOTS |
198
+ | - | - | - | - |
199
+ | PHRASE | | | "buckle my shoe" |
200
+ | TOKENS | two | one | |
201
+
202
+ The generated AQL query, when run will bring back only results that contain
203
+ "two", that do not contain variations on the phrase "buckle my shoe", and that
204
+ optionally contain "one". In this case, documents that contain "one" will be
205
+ likely to score higher than those that do not.
206
+
79
207
## bugs
80
208
## contributing
0 commit comments