0

I have a graph like this:

// [user] -answer-> [question]

for u in user
    filter u._id in ['user/foo', 'user/bar']
    for v, e in 1 outbound u graph 'qaGraph'
        return keep(e, '_from', '_to', 'chosen')

Output:

[
  {
    "_from": "user/foo",
    "_to": "question/A",
    "chosen": 0
  },
  {
    "_from": "user/foo",
    "_to": "question/B",
    "chosen": 0
  },
  {
    "_from": "user/foo",
    "_to": "question/C",
    "chosen": 1
  },
  {
    "_from": "user/bar",
    "_to": "question/A",
    "chosen": 0
  },
  {
    "_from": "user/bar",
    "_to": "question/C",
    "chosen": 0
  }
]

That means foo and bar have answered two questions in common (A & C) but they gave the same answer to only one question (A).

How can I write an AQL to return the same information in the following format?

{
  "questions": 2,
  "match": 1
}

I'm struggling here but without success, so any help would be appreciated.

Thanks!

  • Edit: I forgot to mention that all questions are multiple choice, with only two alternatives: 0 or 1. So answer.chosen represent the user's choice.
André
  • 12,497
  • 6
  • 42
  • 44

1 Answers1

0

I ended up with this query:

let data = (
    for u in user
        filter u._id in ['user/foo', 'user/bar']
        for v, e in 1 outbound u graph 'qaGraph'
            collect question = e._to into answers = e.chosen
            filter count(answers) == 2 // questions answered by both users
            let match = sum(answers) != 1 ? 1 : 0 // flags same answer by both users
            return {question, match}
)
return { 
    questions: count(data), 
    matches: sum(data[*].match) 
}

However I'd like to find a solution simpler than that.

André
  • 12,497
  • 6
  • 42
  • 44