Dataview: Querying sections

Is it possible to query a section in dataview like section:() does in the Obsidian Search.
Or alternatively, is it possible to use an Obsidian search as a query in dataview?

My problem is as following:
I want to find all contacts where I mention a certain company in the section ### Employments, but not in the other sections. A simple search like section:("company" "employment") does the trick, but I would love to have a dataview table as a result.

1 Like

As far as I know, this is not yet possible with dataview. You can’t:

  • search by section
  • access note content

But you could write a dataviewjs codeblock to access the note content, and then use a regular expression to look for the company’s name after the ### employments heading.
Something like "### employments(.*?)(COMPANY-NAME)"

Definitely not as easy as using a regular codeblock

Thank you. The matching regex would not be no problem, but the dataviewjs is too much for me as a non-programmer. I could not manage to create a list with all pages where the regex matches.

Dataview only search things under sections if they’re tasks or bullet lists (“- this is a list”).

I tried to solve it differently.
I create Lists like:

  • Company1 (since 2022)
  • Company2 (2000 till 2021)

Now I want to find all Pages, where there exists a list item containing the company name and the word since.

But somehow this does not work:

List
WHERE regexmatch(".*Company1.*since.*", file.lists.text)

Whatever regex I enter, it always gives me a list of all existing pages. I can’t see why. Using cantains(file.lists.text, "Company1") works fine, but not the regex.

I don’t know why, but this works:

LIST 
WHERE any(file.lists.text, (l) => regexmatch(".*Company1.*since.*", string(l)))

Copied and adapted it from a GitHub discussion.

My suggestion (in table format, to see context):

TABLE rows.L.text AS "Company 1"
FROM "your-folder-path"
WHERE file.lists
FLATTEN file.lists AS L
WHERE contains(L.text, "Company1")
WHERE contains(L.text, "since")
GROUP BY file.link

I suggest the use of FROM to define the source (folder or tag) because performance optimization.

4 Likes

I don’t understand it. But it works like a charm :slight_smile:
I guess the two WHERE clauses only look in the same row, not in the whole file.

  • In each file file.lists is an array by default (i.e., a list, even if you have only one “bullet” list).
  • FLATTEN file.lists AS L allows to separate the array by single rows (one list per row). After that we can apply new filter per each list and exclude the unwanted in same file. That’s why the two “WHERE” are applied to each row.
  • two “WHERE” is similar to AND, i.e.
WHERE contains(L.text, "Company1") AND contains(L.text, "since")
  • at the end we grouped again the rows by file.link to present results per file; otherwise we see the repetitions by file (as many as the lists filtered in this file)
2 Likes

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