With Google Drive API, how do I find a filename that contains a string of digits?

Viewed 665

With the Drive API I can find matching filenames by calling Files: list with query q set to name contains 'SO8393'. I get back matching files like this:

{
 "files": [
  {
   "id": "111111",
   "name": "-SO8393_8041HE.pdf",
  },
  {
   "id": "222222",
   "name": "-SO8393_8041VENDORX.pdf",
  }
 ]
}

However, if I modify my query q to have only digits and no alpha characters, like name contains '8393', then I get back no matching files:

{
 "files": []
}

I also tried double quotes, i.e., name contains "8393", but got the same empty list in return. This is very surprising to not match on strings that are just digits.

How do you match filenames when your name contains argument is only digits?

2 Answers

Answer

Use fullText instead of name: fullText contains '8393'

Explanation

  • name returns files whose name has exactly the same word as the searched word
  • fullText returns files whose name contains the searched word even if it is included in another word

Reference

Search for files and folders

With further investigation, this is not an issue specific to digits in the name, as my original question asked about. Instead, it has to do with how the file's name is tokenized/indexed and can be matched, if at all. These footnotes on the contains operator explain some of the behavior I'm seeing: https://developers.google.com/drive/api/v3/reference/query-ref#fn1 and https://developers.google.com/drive/api/v3/reference/query-ref#fn2

@fullfine your suggestion to use fullText is a good one and gets us closer, but does not cover the general case for any substring in the file's name, like z123. The table below shows some examples.

Here is the experiment setup, with these four files in a Shared Drive. Each of these files are identical in content -- a blank PDF file. So this leaves only the name of the file to match on, as the body of the PDF contains nothing.

four files in a google drive folder

Here are several variations on queries using name contains and fullText contains, and the resulting files that match. (Tell me if I've missed some queries to try, and I'll run them and add them to the table.)

Query q using name Files matched and returned
name contains '123' Abc-123-test.pdf Xyz-123-test.pdf
name contains "123" Abc-123-test.pdf Xyz-123-test.pdf
name contains 'z123'
name contains "z123"
name contains 'xyz123' Xyz123test.pdf
name contains "xyz123" Xyz123test.pdf
name contains 'xyz test'
name contains "xyz test"
name contains 'z 123'
name contains "z 123"
name contains 'xyz 123'
name contains "xyz 123"
Query q using fullText Files matched and returned
fullText contains '123' Abc-123-test.pdf Abc123test.pdf Xyz-123-test.pdf Xyz123test.pdf
fullText contains "123" Abc-123-test.pdf Abc123test.pdf Xyz-123-test.pdf Xyz123test.pdf
fullText contains 'z123'
fullText contains "z123"
fullText contains 'xyz123'
fullText contains "xyz123"
fullText contains 'xyz test' Xyz-123-test.pdf Xyz123test.pdf
fullText contains "xyz test" Xyz-123-test.pdf Xyz123test.pdf
fullText contains 'z 123'
fullText contains "z 123"
fullText contains 'xyz 123' Xyz-123-test.pdf Xyz123test.pdf
fullText contains "xyz 123" Xyz-123-test.pdf Xyz123test.pdf

Matching "xyz test" seems inconsistent with the documentation footnote 3:

For example, if the fullText of a doc contains the string "Hello there world", then the query fullText contains '"Hello there"' will return a result, but the query fullText contains '"Hello world"' will not.

...unless it it tokenizes 123 and drops it as if it were not alphanumeric.

Also, in footnote [2]:

The contains operator only performs matching on entire string tokens for fullText.

Given the experiment, it looks like it breaks up tokens on [a-zA-Z] and [0-9] separately, given that a query for z123 does not match, since that would span separate tokens and not match tokens in their entirety.

It appears to be impossible to query for a file name that matches substring z123.

Related