As a follow on to question: MarkLogic - getting distinct values
I have a document structure like this:
<document>
<question_item>
<question>What is your name?</question>
<answer>Barney Rubble</answer>
</question_item>
<question_item>
<question>What is your address?</question>
<answer>Bedrock</answer>
</question_item>
...
</document>
Thanks to answer received on the other question I can now list all distinct questions in order of frequency as such:
for $v in cts:element-values(xs:QName('question'), (), (),
cts:element-word-query(xs:QName('question'), 'name'))
where cts:contains($v, cts:word-query('name'))
order by cts:frequency($v)
return concat($v,concat("-",cts:frequency($v)))
I would love to able to also include with each distinct question, the top x most common answers and their count, e.g. Barney Rubble (100), Fred Flintstone (59) etc.
Is there a way to do this reasonably efficiently? I know another option is to change the document format to have one document per question_item but I would prefer to avoid that for the moment, if possible.
Any help is greatly appreciated. Thanks!