Query for multiple tags

For example, I have two pages with the following tags in the first block:
page1:
tags:: tag1
page2:
tags:: tag1, tag2

I want to get the pages with both tag1 and tag2:
{{query (page-property tags tag1 tag2)}} returns page1 and page2, meaning that pages with tag1 or tag2.

{{query (page-property tags and(tag1 tag2))}} report an error.

Is there any way to return pages with page1 and page2?

Welcome to the community @gaoyang_liu! :wave:

You need to first select each property independently, and then pass it into the (and) filter. Think about queries as if they were mathematical equations; first the inner formulas are calculated, then the ones in the outer parentheses. You can read more about it in the query documentation.

Here’s what your query should look like:
{{query (and (page-property tags tag1) (page-property tags tag2)) }}

Because you use the tags:: property, you should also be able to use the page-tags query filter directly:

{{query (and (page-tags tag1) (page-tags tag2)) }}
2 Likes

Solved! Thank you so much for such a quick answer, Ramses. By the way, your videos on Youtube are very helpful too.

I read the query documentation before asking the question. I tried some workaround like create block with [[tag1]], [[tag2]], and query with {{query and( [[tag1]] [[tag2]] )}}. But I did not want the returned blocks. I just want the page title and page-properties, and stuck there. Now it works very well with your suggestion.

I’m tired of jumping from Zotero to Logseq back and forth, again and again. Now I try to complete my academic workflow all in Logseq. Use query to imitate the folder structure in Zotero. Like:

Another minor question, is it possible to show the block Query: (and (page-tags SHAP) (page-tags ES)) simply as SHAP, ES? I tried [SHAP, ES]({{query (and (page-tags SHAP) (page-tags ES)) }}), but it did not work.

1 Like

Did you try (page-tags SHAP ES) ? That should work. Do keep in mind that page-tags only works in combination with the tags:: property; for non-standard properties you need to use the (page-properties) filter.

If you’re somewhat familiar with programming, think of page-tags (or any other query filter) as a function that you give an argument/input (SHAP and ES in this case).

Also, keep an eye on your parentheses; and ([[tag 1]] [[tag 2]]) isn’t valid syntax. Make sure that the opening bracket is always before the filter, like:
(and [[tag 1]] [[tag 2]])

Thanks, Ramses. Maybe I didn’t explain myself clearly. The query works perfectly now. What I am looking for is just to give the Query title an alias for conciseness and readability. Like [alias](query command).

I am trying to replace:


with something like:

Ah I see! This is more the domain of Datalog queries, which lets you add HTML as headers. Unfortunately, it’s outside of my scope of knowledge but maybe these Datalog resources might be useful.

Super simple advanced query because you can actually use a simple query from inside an advanced query, thus getting access to things like breadcrumbs collapsing and result transform, but for your use case, we just need to add the title key.

it’s exactly what you’ve mentioned but just a few lines extra.

#+BEGIN_QUERY
 {:title [:b"SHAP, ES"]
 :query (and (page-tags SHAP) (page-tags ES) )
}
 #+END_QUERY
2 Likes

Thanks, I should check it out.

It is awesome! Works exactly as I wanted. Thanks!