|
1 | 1 | # AQLqueryBuilder.js
|
2 |
| -a typescript query builder for [arangodb](https://www.arangodb.com)'s [ArangoSearch](https://www.arangodb.com/docs/stable/arangosearch.html) |
| 2 | +> a typescript query builder for [arangodb](https://www.arangodb.com)'s [ArangoSearch](https://www.arangodb.com/docs/stable/arangosearch.html) |
| 3 | +
|
| 4 | +##### !! warning !! experimental and unstable |
| 5 | + |
| 6 | +## overview |
| 7 | +ArangoSearch provides a high-level API for interacting with Arango Search Views |
| 8 | +through the Arango Query Language (AQL). This library aims to provide a query |
| 9 | +parser and AQL query builder to enable full boolean search operations across |
| 10 | +all available Arango Search View capabilities, including, `PHRASE` and |
| 11 | +`TOKENS` operations. With minimal syntax overhead the user can generate |
| 12 | +multi-lingual and language-specific, complex phrase, proximity and tokenized |
| 13 | +search terms. |
| 14 | + |
| 15 | +For example, passing a search phrase like: `+mandatory -exclude ?"optional |
| 16 | +phrase"` to `buildAQL`, will produce the following query: |
| 17 | +```aql |
| 18 | +FOR doc IN search_view |
| 19 | +
|
| 20 | + SEARCH |
| 21 | + MIN_MATCH( |
| 22 | + ANALYZER( |
| 23 | + TOKENS(@value0, @value1) |
| 24 | + ALL IN doc.@value2, @value1), |
| 25 | + @value3) OR (MIN_MATCH( |
| 26 | + ANALYZER( |
| 27 | + TOKENS(@value0, @value1) |
| 28 | + ALL IN doc.@value2, @value1), |
| 29 | + @value3) AND @value4) |
| 30 | +
|
| 31 | + AND |
| 32 | +
|
| 33 | + MIN_MATCH( |
| 34 | + ANALYZER( |
| 35 | + TOKENS(@value5, @value1) |
| 36 | + NONE IN doc.@value2, @value1), |
| 37 | + @value3) |
| 38 | +
|
| 39 | + OPTIONS @value6 |
| 40 | + SORT TFIDF(doc) DESC |
| 41 | +
|
| 42 | + LIMIT @value7, @value8 |
| 43 | + RETURN doc |
| 44 | +``` |
| 45 | +This query will retrieve all documents that __include__ the term "mandatory" |
| 46 | +AND __do not include__ the term "exclude", AND whose ranking will be boosted by the |
| 47 | +presence of the phrase "optional phrase". If no mandatory or exclude terms are |
| 48 | +provided, optional terms are considered required, so as not to retrieve all |
| 49 | +documents. |
| 50 | + |
| 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 | + |
| 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 | + |
| 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 | + |
| 110 | +### query object |
| 111 | + |
| 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 | + |
| 129 | +Example: |
| 130 | +```json |
| 131 | +{ |
| 132 | + "view": "the_arango-search_view-name", |
| 133 | + "collections": [ |
| 134 | + { |
| 135 | + "name": |
| 136 | + "collection_name", "analyzer": |
| 137 | + "analyzer_name" |
| 138 | + } |
| 139 | + ], |
| 140 | + "key": "text", |
| 141 | + "query": "either a +query ?\"string for parseQuery to parse\"", |
| 142 | + "query": [ |
| 143 | + {"type": "phr", "op": "?", "val": "\"or a list of query objects\""}, |
| 144 | + {"type": "tok", "op": "-", "val": "tokens"} |
| 145 | + ], |
| 146 | + "filters": [ |
| 147 | + { |
| 148 | + "field": "field_name", |
| 149 | + "op": ">", |
| 150 | + "val": 0 |
| 151 | + } |
| 152 | + ], |
| 153 | + "limit": |
| 154 | + { |
| 155 | + "start": 0, |
| 156 | + "end": 20, |
| 157 | + } |
| 158 | +} |
| 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 | + |
| 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 | + |
| 207 | +## bugs |
| 208 | +## contributing |
0 commit comments