How can I calculate how many back links referenced to a particular page?

Hi I wonder how I can calculate number of back links in certain page. Say I collect Links (articles, tweets) and tags them with properties include one called Topics, where I spell out which topic this links is referenced to. Then I have a query to pull out all Topics : {{Query (page-property type [[topics]]}} . But I can’t see number of Links each Topic is back linked to.
Under All Pages, I can see number of back links but I don’t know how to filter by pages that are only topic.

Please shine some lights on this . Thanks!

1 Like

I think this is possible with an advanced query. Can you post some sample data so I might have a look?

Thanks, here you go! I guess this involved in Aggregating function.
" I collect articles as reference links (“Links”) and assign them to Topics (“Topics”). I need to calculate how many links each topic has collected as an indicator which topic is the priority to work on (the one with the most reference links). Currently the Links’s property include "Type:: Link , Topic:: Topic 1, Topic 2 " ; a topic’s property include “Type:: topic ; "
I guess I can use Sum or Max function but I have no experience in using them. Maybe I can add a property like " Reference-count:1 " so the query can count the value ('1”) if so whats the full query look like ? Thank so much!

Ok, I hope this serves you well.

{:title "number of links"
 :query [:find ?topic (count ?link)
   :keys name number
     [?p :block/properties ?prop]
     [(get ?prop :type) ?type]
     [(= ?type "topic")]
     [?p :block/original-name ?topic]
     [?link :block/properties-text-values ?ptv]
     [(get ?ptv :type) ?lt]
     [(= ?lt "Link")]
     [(get ?ptv :topic) ?t]
     [(clojure.string/includes? ?t ?topic)]
 :result-transform (fn [result] (sort-by (fn [r] (get-in r [:number])) > result))
 :view (fn [rows] [:table
   [:thead [:tr [:th "Topic"] [:th "Links"] ] ]
   [:tbody (for [r rows] [:tr
             [:td [:a {:href 
     (str "#/page/" (clojure.string/replace (get-in r [:name]) "/" "%2F"))}
 (get-in r [:name])]]
             [:td (get-in r [:number])]

Here’s what I used in my test:

wow this is amazing. I will do a test and let you know. (I am new to logseq and coding but will certainly let you know!) Thanks so much!

1 Like

Hi there ! Somehow it didn’t return any result. While I am RE-exam the prospect to fit into your scrip, I just learned Logseq does have a SUM function.
Do you think in my case , I can include property “qty” for each link and because each link gets to count once, so by default qty::1 . Then use SUM to calculate the total Links for each Topic. If so do you know how should I write the query? And how can I have the count of total links showed up like under All Pages , back links automatically showed up in the table as a column? I hope I made myself clear . :-p ) if not I can do a few rendering or screenshot to explain. :slight_smile:

As you can see in the example on the site, you would need a query per topic.
Because it would do a sum across the table.
I’m more curious as to why you do not get any results from my query.
Here’s a screenshot of what the article page looks like:

If you recreate this page as shown, the query should give you results. Two topics with both a count of 1 to be specific.

You may be interested in

1 Like

I will run your query again. Will do screenshots if needed ( yes I realized the Count function doesn’t return what I really wanted.) thanks!

Hi I am still trying using the query here. Just so you know under a Topic , which is a page, I have property type such as type:: [[topics]] but I don’t have a property as topic:: ( title of the topic) . Should I add the later in order to run your query?
Thank you!