logseq.DB.q - case insensitive search using logseq API

I’m using the logseq api and trying to search for text and get the resultant blocks.
I’m using the logseq.DB.q api to search and noticed that it is case sensitive, so its not giving me the results when if the search query is not an exact case match.

How can I get a case insensitive search using logseq.DB.q

Should use instead datascriptQuery and regular expressions.

1 Like

@mentaloid, can you give me an example of the datascriptQuery?
Not sure what add in the input argument

Apparently the input argument expects zero or more inputs, those that go inside :inputs [] of an Advanced Query. See how "home" is passed in this post, in order to become ?name.

1 Like

Thank you for the reference.
Also, I remember seeing something like logseq.App.search being used to search, but I don’t see it in the documentation.

  • Is this a valid api?
  • If yes, would that be useful in this case for case insensitive search?

Yes, search is part of the API and:

  • represents the search-box
    • i.e. expects a search string
  • performs case-insensitive search only
    • but the results are always given in lower-case
  • of the whole content
    • i.e. cannot be filtered like queries
1 Like

So in summary,

  • if I want a simple case insensitive search targeting the entire graph, then I can use logseq.App.Search (this is not documented as of now)
  • if I want advanced case insensitive search to search only on a specific page or tag etc… then I need to use logseq.DB.datascriptQuery along with regular expressions

example datascriptQuery (reference):


const tags: [string, string, BlockEntity][] = await logseq.DB.datascriptQuery(`
[:find ?id :in $ ?name :where [?id :block/name ?name]],
`, "\"home\"");

Example 2 (reference):

const page = await logseq.Editor.getCurrentPage();
const currentPageName = await page?.name;

const tags: [string, string, BlockEntity][] = await logseq.DB.datascriptQuery(`
  [:find ?content ?tag (pull ?b [*])
    :where
    [?b :block/refs ?page-ref]
    [?b :block/content ?content]
    [?page-ref :block/name ?tag]  
    [?page-ref :block/page ${currentPageName}]
  ]
`);

Hi everybody, I think this block is a very helpful entry point.

I am trying to retrieve blocks based on a python query with a block uuid as parameter.

The use case is to be able to export a group of blocks and have their properties be shown as independent columns. Since some of the properties have references to other blocks as values, it would be also very useful to be able to choose what to show in the property (the content or a property of the referenced block, instead of having its uuid).

The issues I am facing are different, but this is the main one:
when I use the following query, I manage to retrieve the content of a block:

query = {        
    "method": "logseq.DB.datascriptQuery",
    "args": [f"""
        [:find ?b :in $ ?id :where [?id :block/content ?b]]
        """, block_id]
}

However, when I try running this query, I get the error “invalid number: UUID

query = {        
    "method": "logseq.DB.datascriptQuery",
    "args": [f"""
        [:find ?b :in $ ?uuid :where [?b :block/uuid ?uuid]]
        """, uuid]
}

I have seen, that it might be an issue in the formatting of the uuid. Unfortunately I get either the same error whe I use f"{uuid}“, or an empty result when I use f”‘{uuid}’". The same happens when trying to format in the query via “[(str ?uuid) ?str] [(= ?str ?uuid)]”

Any Idea on why this happen or alternative ways to achieve my objective?

Welcome. Try replacing:

  • this:
    [?b :block/uuid ?uuid]
    
  • with this:
    [?b :block/uuid ?tagged-literal]
    [(str ?tagged-literal) ?str]
    [(= ?str ?uuid)]
    

Hi metaloid, thank you very much! I finally managed to make it work! Now that I look at your version, I understand that my mistake was to use “uuid” for both the uuid from the logseq database and the input variable.

Some additional information for future reference:

  • The working query ended up looking like this:
    query = {        
        "method": "logseq.DB.datascriptQuery",
        "args": [f"""
            [:find ?b :in $ ?uuid :where [?b :block/uuid ?tagged-literal]
            [(str ?tagged-literal) ?str]
            [(= ?str ?uuid)]]
            """, f'"{uuid}"']
    }
    
  • the order of the quotation marks for the input variable (f'"{uuid}"' - meaning ' single on the outside and " double in the inside) is important. No other combination worked.
  • As additional information, this worked with both these request variations:
    • response = requests.post(endpoint, json=query, headers=headers)
    • response = requests.post(endpoint, data=json.dumps(query), headers=headers)