Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

contains() doesn't return hits for matched fields that have only one value? #88

Closed
davecan opened this issue Apr 8, 2021 · 8 comments
Closed
Labels
bug Something isn't working.

Comments

@davecan
Copy link

davecan commented Apr 8, 2021

File A:

---
field: foo
---

File B:

---
field:
- foo
- bar
---

This query returns only A as expected, because it is matching a string vs a list:

list from "" where field = foo

This query only returns B which is unexpected:

list from "" where contains(field, foo)

Intuitively I would expect the above query to return both A and B. But maybe I'm missing something in how it works.

@blacksmithgu
Copy link
Owner

As a side note, you don't need FROM "" anymore (it is implied if not present).

Does

LIST WHERE contains(field, "foo")

work? I feel like it should work (since a string trivially contains itself).

@blacksmithgu blacksmithgu added the bug Something isn't working. label Apr 9, 2021
@davecan
Copy link
Author

davecan commented Apr 9, 2021

Negative. The actual query (without FROM) looks more like this: (in file foo)

list where contains(field, this.file.link)
sort file.name

"foo" etc in the example above are actually [[links]] so they really look more like this:

File A:

---
field: [[foo]]
---

File B:

---
field:
- [[foo]]
- [[bar]]
---

The query in foo should (intuitively, I was thinking the same as you) return the following:

- File A
- File B

What the query in foo actually returns is:

- File B

If I change File A to look like this:

---
field: 
- [[foo]]
---

Then the query in foo returns this:

- File A
- File B

As expected. But it implies that if I expect to add more than a single value to any field in the future then I should make all fields - list style so I can standardize on contains() instead of equality operators in searches. Which I take it is not the intent.

Hope that is more clear. Thanks.

@blacksmithgu
Copy link
Owner

Ah, is this operating over links? I understand what's going on then, I think - contains(link, <thing>) does the same thing that contains(object, <thing>) does - it retrives the metadata from [[link]] and then checks if the thing is in that metadata.

I will modify this so that contains() does an equality check BEFORE doing any other magic, so this case should work.

@davecan
Copy link
Author

davecan commented Apr 11, 2021

Yes it is operating over links. Thanks.

@blacksmithgu
Copy link
Owner

Fixed in upcoming release 0.2.10.

@davecan
Copy link
Author

davecan commented Apr 25, 2021

I'm experiencing this behavior again in the current version 0.2.14.

Some files have:

topic: [[note]]

Others have:

topic:
- [[note]]
- [[note 2]]

This query only returns notes that have the list, and does not return notes that have topic: [[note]]:

table status
where contains(topic, this.file.link)
sort file.name

This query only returns notes that have the strict equality topic: [[note]] and not the ones where [[note]] is contained in a list:

table status
where topic = this.file.link
sort file.name

@the-tanvan
Copy link

For v0.5.47, I also had a similar issue and found that adding quotes around the link allowed it to be queried by the contains function.

Try putting double quotes around the link for the case where there is a single link:
topic: "[[note]]"

The query with the contains function should then return both cases.

In the documentation, it says

If you reference a link in frontmatter, you need to quote it, as so: key: "[[Link]]". This is default Obsidian-supported behavior. Please be aware that this is only a link for dataview, but not for Obsidian anymore - that ultimatively means it won't show up in the Outgoing links and won't be updated on i.e. a rename.

@AB1908
Copy link
Collaborator

AB1908 commented Jan 1, 2023

@davecan OR the two ideas together? I wonder if it's a type mismatch. If so, you can use array() to cast it before you use contains().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working.
Projects
None yet
Development

No branches or pull requests

4 participants