Bing Chat failed me :)

I’m trying to find a query to list all pages in a graph with only a single backlink in them. I used Bing Chat and it suggested this solution:

{:title “Pages with at least one backlink”
:query [:find (pull ?p [*])
[?p :block/marker ?marker]
[(join [?p] [?e]
[?e :block/refs ?p])]
[(join [?p] [?e]
[?e :block/refs-page ?p])]]}

Unfortunately it doesn’t work. I get the following error:
Query failed:
Unknown predicate 'join in [(join [?p] [?e] [?e :block/refs ?p])]

Ideally I would also like to exclude certain pages from the query, but let’s say that’s a stage 2 requirement!

Any help with this please?

EDIT: I did some more research with Bing Chat and I got this query:
{:query [:find (pull ?page [*])
:in $ ?backlink-count
[?page :block/page true]
[(count-backlinks ?page) ?backlink-count]]
:inputs [1]}

Again, no success. I get a Query error about an unknown function “count-backlinks”

Bing clearly doesn’t know datalog or the Logseq database. So you’ll probably get nowhere fast :smiley:

I’m not sure what you are trying to find with your query?
What purpose does it have? What is your data like and what data are you trying to find?

If it is “give me pages that only link to 1 other page”, that’s going to be difficult.
You are asking for a where based on a count, that’s the problem.
There is no counting in the where clause of a query.

I don’t know your technical background/knowledge so I’ll refrain from going into the technical details of it.

Thank you for even considering my query request! I don’t have any technical background regarding programming and especially anything related to Logseq’s programming.

What I’m trying to do is to manage a situation where a fairly large number of pages are created and each one of them has one (common) backlink to another specific page. If for any reason I would want to delete these pages, the only recourse for me would be to find their created at or perhaps their modified at date in All Pages, select them one by one and then delete them. This could be inefficient because there may be 20 to 30 pages (backlinks) that I may have to delete every time.

This is why I thought that maybe a query would give me a listing and then maybe I could select them easily and delete them (or even automate the selection-delete part as well).

You would just get a list and still need to delete them one by one. But at least you can get a list and don’t need to search.

For finding all pages that reference a specific page, use this query.

{:title [:h3 "Pages with page reference"]
 :query [:find (pull ?p [*])
   [?r :block/name "name"] ; name is always lowercase
   [?b :block/refs ?r]
   [?b :block/page ?p]

Thank you for the suggestion. I’m going to try a test with a small data sample because I’m getting inconsistent results and I don’t understand why this is so.

Here’s an example of what I’m trying to do.

I create a page named Summer and another one named Autumn.
Page Summer has two blocks with the property fruit (fruit:: #[[watermelon]] and fruit:: #[[figs]]).
Page Autumn also has two blocks with the property fruit (fruit:: #[[grapes]] and fruit:: #[[figs]]).

In “All pages” both Summer and Autumn have zero backlinks. Again in “All pages”, “fruit” has two backlinks and when I open this page (fruit) I see two Linked References each of them to Summer and Autumn and two Unlinked References to each of these pages too.

What I want is: if I decide to delete page Summer, to also detect and/or delete #[[watermelon]] provided that #[[watermelon]] is not used anywhere else in the graph, which is the case in my example. By contrast #[[figs]] should not be detected and/or deleted since it’s still relevant to the page Autumn.

Since you ultimately want to delete the results of the query, have you considered the option Remove orphaned pages at the top-right of All pages ?

1 Like

Exactly what I was going to say :smiley:

@Freddie for your understanding see the below screenshots:

made the pages:

deleting page Summer:

The option @mentaloid pointed at:

1 Like

Firstly, I’m happy for me that I finally made it clear enough for you to understand what I meant. I’m not very good at explaining myself, so this was a success!

Secondly, the solution that you propose should be fine in my case. The popup dialog brings up other orphaned pages that I might not want to delete, but in practice I hope that it shouldn’t be a problem. Ideally the list of orphaned pages should have a checkmark selection mechanism as in “All pages”, but that would be asking for too much.

P.S. I marked mentaloid’s answer as the solution because he mentioned it first, but you both deserve credit for it. :smiley:

1 Like

It should only show pages without content and without links. So effectively pages that are not being used anywhere in your graph.
Therefore all should be safe to delete.

1 Like