0

Posting this here because I have found no note or solution elsewhere on the web.

I am currently experiencing a discrepancy between search results when using the drive search toolbar and the DriveApp.searchFiles() functionality of apps scripts as follows.

The drive search bar always returns complete results for a given query [looking at all text within a document and in the title], but DriveApp.searchFiles() does not. Certain files are missing/not returned.

Upon finding a given file that is returned from the drive search bar ONLY (one that was not being returned by the DriveApp search) and opening it, it then starts appearing/being returned by DriveApp.searchFiles().

This issue further seems to be a user-specific one. The script we are developing is used by multiple users and new users (ones that are added to an extant file system and then given our tool) experience this issue for a majority of files. After which, when they open a given 'missing' file, it begins appearing in the search results once more.

For reference, my code is as follows:

var targetParam = 'title contains "'+target+'" or fullText contains "'+target+'"';
var searchResults = DriveApp.searchFiles(targetParam);

In all instances of this issue, the drive search bar returns a complete list and opening a given file 'fixes' its issue. Given the scale of what we are trying to do it is not a possibility to have every user open every file.

For clarification, these files are within a large file system in either team drives or traditional G suite. Users are given access by being added to the highest level file, to the team drive, or by being added to a user group that has access to the file system already. All users are within our domain.

Is this a known discrepancy? Is there something that I may be doing wrong in my search query to cause this? I am interested in any potential solutions or ideas.

Rubén
  • 34,714
  • 9
  • 70
  • 166
Nick G
  • 1
  • 2
  • Who owns the missing files? How are they shared with the users of your script? Are the files shared by using the users' email address, by using anyone with the link or a by using an email group? Are your users using consumer accounts or G Suite accounts? Have you read the docs? – Rubén Jun 01 '18 at 18:10
  • they are files within G suite team drives and/or drive files in large file systems that are shared with email user groups within our instant of g suite business, those are good questions and I will update my post accordingly. – Nick G Jun 01 '18 at 19:02

2 Answers2

0

Just to mention one of the things that are causing this, from Use Groups to share content (emphasis mine)

If you later add new members to the group, they'll be able to access the document only via the document's URL. To make the document appear in the Shared with Me view of a new member's Google Drive, you must reshare the document with the group or share the document with the new member individually.

Rubén
  • 34,714
  • 9
  • 70
  • 166
0

A possible solution to this (I don't have a domain to test on) is to enable and then use the Drive "advanced service" and not the DriveApp implementation. If one reviews the Drive REST API for Files#list, one will note that the default corpus used when querying for files is files that the user has accessed. This corpus includes any file the user has created themselves (by UI or by script).

Thus, modifying the search corpora could be the answer. There's a lot of extra stuff that has to be added if you want to search Team Drive items, so I'll leave that to the reader.

function searchDomain(query) {
  const listOptions = {
    q: query,
    corpora: 'domain',
  };
  const results = [];
  do {
    var search = Drive.Files.list(listOptions);
    listOptions.pageToken = search.nextPageToken;
    if (search.items)
      Array.prototype.push.apply(results, search.items);
    else
      console.log({message: "No results for search", search: query, options: listOptions});
  } while (listOptions.pageToken);
  return results;
}

Given that you mention DriveApp.searchFiles you already know the general structure for the query string, but for anyone who is finding this, you will want to review the documentation on its format here

tehhowch
  • 9,645
  • 4
  • 24
  • 42