String search by prefix

15 views
Skip to first unread message

Andreas Jung

unread,
May 15, 2019, 5:59:18 AM5/15/19
to ArangoDB
As part of a CMS migration we store all content as JSON in ArangoDB. Content objects have to path properties

_path = '/path/to/my/content/foo.html'
_path2 = ['path', 'to', 'my', 'content', 'foo.html']

Basic question: what is the best way to query in AQL for all documents by where _path matches a particular prefix like '/path' or '/path/to/my/content'.
I can use FILTER doc._path like '/path/%' but that's not very efficient...any better idea?



Wilfried Gösgens

unread,
May 15, 2019, 6:24:06 AM5/15/19
to ArangoDB
Hi,
maybe adding a 3rd attribute that has a part of the path, which is pretty distinguishable but can be indexed with a good selectivity and used to compare in AQL with `==`is a good addition performance wise?

So you would use the `==` to reduce the result set perfomant with the help of the index, and afterwards filter the reduced result set by the left bound string match.

Cheers,
Willi

Andreas Jung

unread,
May 15, 2019, 9:54:31 AM5/15/19
to ArangoDB
SOLVED

For each document I pre-compute an array of possible prefixes and store it as attribute _all_paths - in this case:

doc._all_paths = [
'/path',
'/path/to',
'/path/to/my',
'/path/to/my/content'
]

Then I can query this field using the IN operator:
for doc in mycollection
    FILTER '/some/path' in doc._all_paths

Andreas


On Wednesday, May 15, 2019 at 11:59:18 AM UTC+2, Andreas Jung wrote:
Reply all
Reply to author
Forward
0 new messages