LIST
WHERE contains(file.outlinks, this.file.link) AND contains(file.lists.text, "#yourtag")
FROM ""
FLATTEN file.lists AS list
WHERE contains(list.text, "#yourtag") and list.section = "list-item" and list.outlinks = this.file.link
SORT file.name
Explanation and Breakdown:
-
LIST: This tells Dataview to output a list of the found list items. You can also use TABLE for a tabular format if preferred.
-
WHERE contains(file.outlinks, this.file.link) AND contains(file.lists.text, "#yourtag"): This initial WHERE clause drastically improves performance by filtering whole files first.
contains(file.outlinks, this.file.link): This checks if the current file (being processed by Dataview) links to the current page. this.file.link dynamically gets the link to the note where the Dataview query is running (e.g., [[Python]]). This ensures that only files that reference the current page are considered. Using file.outlinks is crucial because it directly references the links at the file level.
contains(file.lists.text, "#yourtag"): This checks if any list item in the file contains the tag "#yourtag". This does a broader initial filter to cut down on the number of individual lines Dataview needs to process.
AND: Both conditions must be true for the file to be included.
-
FROM "": This specifies the source of the files to search. "" means search the entire vault. You can narrow this down (e.g., FROM "MyFolder"). Important: If you put a folder name, remember to put it in quotes. Also, folders are case-sensitive.
-
FLATTEN file.lists AS list: This is crucial. file.lists is an array of all the list items (bullet points, numbered lists, etc.) in each file. FLATTEN expands this array into individual rows for each list item. We alias each file.list item as list.
-
WHERE contains(list.text, "#yourtag") and list.section = "list-item" and list.outlinks = this.file.link: This is where we refine the search to the specific list item we want.
contains(list.text, "#yourtag"): This makes sure the specific list item (represented by list) contains your tag #yourtag. This is essential to narrow down the results from just any list item in the file.
list.section = "list-item": Ensures the identified list is an actual list item (bullet point, number).
list.outlinks = this.file.link: This is the most important part. It checks if the specific list item (list) specifically links to the current page (e.g., [[Python]]). Because list.outlinks returns an array, we need to search for the specific matching link.
-
SORT file.name: Sorts the output by file name. You can change this to sort by other fields, such as list.lineNumber for the order in the file, or remove it entirely for no sorting.
How to use it:
- Replace
#yourtag with the actual tag you're looking for. Make sure it's typed correctly, including the #.
- Create or open the page you want to reference. For example, if you want to find all lines with
#mytag that reference the "Python" page, create or open a page called "Python".
- Paste the code into that page.
- Enter Dataview's reading mode/preview mode to see the results.
Important Considerations for Performance:
-
Specificity: The more specific your criteria, the faster the query will run. A broad query across the entire vault will take longer.
-
FROM Clause: Use the FROM clause to limit the search to specific folders if possible. This is the single biggest factor in Dataview performance.
-
contains(file.outlinks, this.file.link): This initial filter is critical for performance. Make sure it's in the query.
-
Indexing: Dataview maintains an index of your vault. If you've made significant changes to your notes, it might be helpful to trigger a re-index (usually found in Dataview's settings).
Example Scenario:
Let's say you have a page called Python.md and the following notes:
File: Note1.md
- This is a list item about something else.
- This is another list item.
- This has the tag #important.
File: Note2.md
- This list item links to [[Python]] and has the tag #mytag. This is relevant.
- This list item links to [[Python]] but has a different tag.
- This is just a random list item.
File: Note3.md
- This list item has #mytag but doesn't link to [[Python]].
- This has #mytag and links to [[Python]]
File: Note4.md
- This links to [[Python]]
- This is nested with #mytag
If you put the Dataview query (with #mytag substituted for #yourtag) into the Python.md file, it will only return the lines from Note2.md, Note3.md and Note4.md that contain both #mytag and link to the Python page. The first result in Note3.md is only at the file level not at the list level so is not returned. The nested list also satisfies both the condition.
This revised answer is much more efficient, accurate, and addresses the common performance issues encountered with Dataview. It directly targets the list item level for both tag and link matching.