Return files whose name match any current page tag

Hi all – I am looking for a query that returns all pages whose name matches any of the tags of the current page. The following code is not working:

LIST
WHERE contains(file.name, this.file.tags)

Any help is greatly appreciated!

The contains() functions first parameter needs to be an array, and not a single text, and the second parameter needs to be the thing to check against.

Test file used in explanation
---
Tags: matching, no-matching, nothing, another
---

Some files in my vault: `another`, `matching` , `no`, and `a`

## Arguments switched around
```dataview
list
where contains(this.file.tags, file.name)
```

## Let's list the tags
```dataview
table without id t, replace(t, "#", "")
flatten this.file.tags as t
where file.name = this.file.name
```

## Is this what you want?
```dataview
list
where filter(this.file.tags, (t) => replace(t, "#", "") = file.name)
```

Switch arguments, not enough

So for starters you need to switch your arguments around, but it’ll still not do exactly what you want. In my case, the upper part displays as follows:

image

As can be seen the files no and a does match when using contains against the #matching and #another tags.

What’s behind the tags

Another issue with your code, is that in the query tags are usually prefixed with the #, as can be seen by the second query in my test file:
image

This show that we need to work a little with the tags, to make them into just the text part, and now we can consider how to match them properly against file names

A proper match against the text of the tag

So we want to do an exact match between the file.name and the text of a tag. There are multiple ways to achieve this, and one is through the use of filter, as in my last query: filter(this.file.tags, (t) => replace(t, "#", "") = file.name)

This will filter all the elements out of this.file.tags, and for each of them assign them to a variable t and do something with it, =>. The something we’re doing is match the text part of that tag with the file.name. If this is a match, it filters as true, and if any of the tags matches, this will produce a line in our list.

And now we get an output without the a and the no file:
image

But is this what you really want?

It is a query which returns all pages which matches any of the tags of the current page, but why do you want this? Wouldn’t it be better to just list the files directly in another field?

Or do you want to locate files having the same set of tags as the current file? Or do you want to locate files having any of the tags as the current file?

In short, I think I’ve solved the exact question of your request, but I’m not sure if that is what you really want. If I’m mistaken, please provide some examples with files with/without tags, and what you expect for your query to do.

1 Like

Thank you very much for your reply – it is exactly what I was looking for!

I use a “related” callout in my notes templates with a dataview query that is intended to display all notes that share tags with the current note, or link to it, or are linked from it, or – this was the missing piece that you have solved – that have a name which matches one or the note’s tags. For example, I tag members of my team (e.g., “AME” as an internal short name) who work on a particular file in that file’s notes, and I want their note “AME” to show up as related on the basis of the file note’s tag AME for their name. Now with the switched arguments it works brilliantly.

The only thing I see in order to improve the related-query is sorting the results on the basis of how many of the OR arguments given in the query are matched by the resulting notes, per my separate topic. Not sure that’s possible. For now I sort the results by type (note, statute, court case, literature etc.) which provides a fairly workable overview.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.