简体   繁体   中英

ArangoDB getting all unique tags from results

I've a document in ArangoDB. { title: "title 12345", tags: ["tagx", "tagy", "tagz"}

  1. I've an full text index on title and another hash index on tags[*] .

  2. I've a query where I want to use full text search on title and apply some filtering via tags and get skip x limit 10 in results. I'm able to achieve this. This will help me in pagination. In one API call, I can return the user 10 items.

  3. However, I also want to get all the unique tags which are present in the result(without skip and limit constraint) without hitting all the documents present in the result. This will help me to show the tags which user can further select to narrow down the search.

We can assume that there will be a small number of unique tags(around 30-40) in the database. Is there an efficient way to achieve this in ArangoDB? Maybe, we can create some new indexes or change the schema to achieve this.

Assuming that you are going to create a collection with documents named test with the following content

在此处输入图像描述

Just to be reproducible, the data that I am inserting is:

[{"_key":"342","_id":"test/342","_rev":"504","tags":["tagx","tagy","tagz"],"title":"title 12345"},{"_key":"564","_id":"test/564","_rev":"591","tags":["tagx","tagy","tagt"],"title":"title another"},{"_key":"510","_id":"test/510","_rev":"538","tags":["tagh","tagk","tagz"],"title":"title 56789"}]

If you run the following AQL query

let tags = (for i in test
return i.tags)[**]

return unique(tags)

This will return

[
  [
    "tagk",
    "tagh",
    "tagt",
    "tagz",
    "tagy",
    "tagx"
  ]
]

As you can see there are different tags repeated across multiple documents and the results are showing an array with just the unique ones

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM