Search code examples
google-apigoogle-drive-apigoogle-api-python-clientgoogle-api-console

Google Drive API: "contains" does not match string in middle in files().list(q="name contains 'SMITH'")?


I have a folder named SMITH_JOHN TAYLER_DAVID, I can only search SMITH or TAYLER but not any other ways, i.e.:

files().list(q="name contains 'SMITH'")  => OK
files().list(q="name contains 'TAYLER'") => OK
files().list(q="name contains 'JOHN'")   => No match!
files().list(q="name contains 'DAVID'")  => No match!
files().list(q="name contains 'MITH'")   => No match!

So looks like that I can only search word from the beginning of the folder name or after a space. This happens to you as well? What does "contains" mean in the REST API and how does the Python implemented this (perhaps using "match" instead of "search")?


Solution

  • So looks like that I can only search word from the beginning of the folder name or after a space. This happens to you as well?

    In my environment, I confirmed the same situation with you. In your situation, the folder name is SMITH_JOHN TAYLER_DAVID. I thought that in this case, _ might be the reason for this issue. For example, when the folder name is SMITH JOHN TAYLER DAVID, your all search queries can retrieve the folder. It seems that the top letter and the letter after a space can be searched. I thought that this might be the current specification.

    What does "contains" mean in the REST API and how does the Python implemented this (perhaps using "match" instead of "search")?

    This is used as the search query. Ref This search query is used for the method of "Files: list".

    For example, if you want to retrieve the folder of the folder name of SMITH_JOHN TAYLER_DAVID by searching the values of 'SMITH','TAYLER','JOHN','DAVID','MITH' instead of the search query of name='SMITH_JOHN TAYLER_DAVID', you can use the following flow.

    1. Retrieve the folder list using the search query of name contains 'SMITH' and mimeType='application/vnd.google-apps.folder' and trashed=false.
      • By this, the folder list includes SMITH.
    2. Retrieve the folder by searching the values of 'SMITH','TAYLER','JOHN','DAVID','MITH' from the retrieved folder list.

    By this flow, the folder of SMITH_JOHN TAYLER_DAVID can be retrieved by one API call. When this flow is reflected in a sample script using googleapis for python, it becomes as follows.

    Sample script:

    drive_service = build('drive', 'v3', credentials=creds) # Please use your credential.
    
    q = "name contains 'SMITH' and mimeType='application/vnd.google-apps.folder' and trashed=false"
    response = drive_service.files().list(q=q, fields='files(id, name)', pageSize=1000).execute()
    search = ['TAYLER', 'JOHN', 'DAVID', 'MITH']
    for file in response.get('files'):
        name = file.get('name')
        if all(e for e in search if e in name):
            print(name)
    
    • When this script is run, the folder of SMITH_JOHN TAYLER_DAVID can be obtained.

    • In this sample, it supposes that the number of folders including the value of SMITH is less than 1000. Please be careful about this.

    References: