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}]
  ]
`);