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.

#+BEGIN_QUERY
{:title "number of links"
 :query [:find ?topic (count ?link)
   :keys name number
   :where
     [?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])]
           ])
   ]])
}
#+END_QUERY

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. https://docs.logseq.com/#/page/64076691-bea5-429c-a43c-dc9ea8e31e01
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!

It should have type:: topic for it to work.
Based on how you have it, you will need to change the following.

[?p :block/properties ?prop]
[(get ?prop :type) ?type]
[(= ?type "topic")]

To

[?p :block/properties-text-values ?prop]
[(get ?prop :type) ?type]
[(= ?type "[[topics]]")]

Please be aware I’m doing this from the top of my head lol.

Hi @Siferiax - your queries are so helpful. I’ve been struggling with this precise one. I want to make it a query for pages which have a property that is a link to the current page. (In this case child projects which reference a parent project).

I can make it work with:

[?p :block/properties ?prop]
[(get ?prop :parent) ?parent]
[(= ?parent #{"House & Family"})]

But I can’t seem to get it to work with the using current-page or query-page as an input (which I want to do to make it a template). I had an idea to try the properties-text-values and came up with the equivalent code to what you’ve put here but it doesn’t seem to work.

[?p :block/properties-text-values ?prop]
[(get ?prop :parent) ?parent]
[(= ?parent "[[House & Family]]")] ; NB I've tried upper and lower case.

I’ve looked in the block properties and it’s got that precise text so I’m a bit stumped. Any help gratefully received.

I’ve now got something to work in a different way. I get the original name of the input page:

[?page :block/name ?query-page]
[?page :block/original-name ?page-name]

and then use clojure.string/includes? to match to the properties-text-values:

[?p :block/properties-text-values ?prop]
[(get ?prop :parent) ?parent]
[(clojure.string/includes? ?parent ?page-name)] 

A bit messy, but it works.

Yeah you run into a text vs reference sort of issue.
I also learned better how to get the set data to work when properties use page references.
The translation from name to original-name is necessary as in your property value it is the original-name and in the input value it is the name.
Instead of clojure.string/includes? you can also use contains? and not use text-values.

[?p :block/properties ?prop]
[(get ?prop :parent) ?parent]
[(contains? ?parent ?page-name)]

Thanks - that’s much neater. Still struggling with some of this as some of these nuances (e.g. sets rather than plain text) are not obvious but I’m getting there.

1 Like