Get the number of referenced documents inside contents

Enonic version: 7.3.1
OS: Ubuntu

Hello guys, I am making a search using the query function of the content lib, and I am looking for a way to count the number of pdf documents that are referenced by these contents that I am getting as the search result.
Is there a way to do that?

Thank you ;]

Have you tried to use aggregations? You could do some terms aggregation in the PDF relation field. Eg:
{
“aggregations”: {
“pdfs”: {
“terms”: {
“field”: “data.pdf”,
“order”: “_count desc”,
“size”: 100
}
}
}
}

That way the query result will bring in the “pdfs” aggregation the aggregated buckets for these related pdfs.

https://developer.enonic.com/docs/xp/stable/storage/aggregations

I’m sorry, I was not clear enough, the pdf documents are inside HtmlArea fields, so I want the total of pdf files inside these filtered contents.
@PVMerlo thank you for the response anyway.

With _references I can get the contents that make a reference to some pdf document (content->document), but I don’t believe that there is some search tag to get the reference in the other direction (with the content, get the files that are referenced by it).

I just saw that there is a function that do partially what I want, but just for one content at a time, and it does not allow to use filters. The function is the getOutboundDependencies from the contentLib. Would be nice to have this kind of search in the query function.

Do you only want to list a pdf reference count when listing other content items, or something else?

If you first Query to get all pdf’s, you could use the pdf ids in a new Query on the _references field to only get content referencing at least one pdf for instance?

I would like the count of referenced pdf and spreadsheet documents. This referenced documents should be referenced by a list of filtered contents that I am getting using a query.

The query is something like:

ContentLib.query({
    query: " _path LIKE '*/path1/*' OR path LIKE '*/path2/*' "
    contentTypes: [ `${app.name}:article`]
})

So this query would get me a list of article, and I want the amount of pdf and spreadsheet documents that these articles make reference to.

If we could use a SQL approach to solve this problem, I would do a LEFT JOIN between the filtered articles and the documents, then I would get the DISTINCT count of the documents ID’s.

This way I will be getting the count of contents that make a reference to a pdf document, right !? But I want the count of documents that are referenced.

Thank you for the replay @tsi ;]

up!

There is no solution for this problem I believe, could enonic implement this feature in the future?

1 Like

@rhuancaetano
Well, if we can’t get that data on-the-fly from ebonic, at least we can pre-store and use it later.
I can think only in 2 possible approaches:

  1. Use events and watch for publishing of contents you want to use in the query.
  2. Use cron and, from time to time, read and update the data you want to.

After that, the data will be already on the contents from your query, so you can filter it.

1 Like

You can easily solve this today.

If you just intend to show the count in a presenation UI (i.e. where the number of references are listed per item), you can always count the references per item, or simply make a query per item.

If this causes performance issues, you could always cache or batch update as @ff9will says.