ALI AFSHAR: Hello, everyone.
Welcome to Google Developers Live.
Today we're going to be talking about searching for
files using the Google Drive API.
Now, Google, obviously we do search.
Hopefully we do it well.
That's no exception with Google Drive.
So I'm just going to open my Google Drive now and show you
what it looks like for the user.
And then we'll do that for a couple of minutes, and then
we'll go on to how it is for the developer.
So I've pre-created a couple of files here--
Fruits and vegetables and fruitsandvegetables.pdf.
Let's have a look at one of them.
So the first one is just a list of some fruits and
vegetables.
I'm want you to notice a couple of things.
The first one is the title.
Is fruits and vegetables.
And secondly, there's a list--
banana, apple, orange, pair, lemon, mango, cucumber.
Now, what I did earlier is I exported this file as a PDF,
except it's not the whole file.
I created just five fruits in there first and
then exported that.
So this is actually a PDF also in Google Drive, and this has
only got banana, apple, orange, pair, lemon, and it's
got a similar kind of title, OK?
So if I go to Google Drive, and if I search for banana, I
find lots of stuff.
Actually, I like bananas.
But one of them is this--
fruitsandvegetables.pdf.
We see we opened that.
So interesting to see, Google Drive has scanned that PDF,
and it knows the content, so that when you type one of the
textual content in this binary file, it looks it up for you,
and it finds it.
So that's great, and it found our other one as well.
Here it is-- fruitsandvegetables.
OK, now if I just search for one of the terms that's only
in the Google Doc, not in the PDF, so that's going to be
cucumber here, we see we only get the one file.
Obviously, that's how it works.
So that's the general flow for the user.
So Google Drive API aims to be able to do anything the user
can do in Google Drive and be able to do it via a
programmatic interface, the Google Drive API.
So let's switch immediately to our Python shell, and let's
have a try on that.
So this is bPython.
I've got the Python client library installed, and I've
got an authorized HTTP instance ready to be imported.
That's just saving us a couple of minutes of time.
So the first thing we're going to do is import the HTTP
instance called authorization.
That's in my library called Auth that I can share with you
if anyone's interested.
Secondly, I want to create a Drive instance.
This is standard for you all.
Let's import from apiclient.discovery.
Import Build and Drive, which is going
to our Drive service--
Drive equals build, then the name of the API, and the
version of the API.
This goes off to the discovery service, finds the schema for
this API, and builds a service dynamically in Python
that can use that.
And let's not forget to use our authorized HTTP instance
so we don't need to parse that ever time on every call.
So let's create that, and let's make
sure we've got something.
So Drive, there it is.
Now, when you want to search files in the Google Drive API,
you can apply a query to any list
operation involving files.
So that's really files.list, or children.list, you can
apply these queries.
We're just going to use files.list for now, and we're
going to create some queries to see.
So the first one I'd like to do is the basic query
that we just did.
So we're going to search for banana, or apple, or something
like this, so drive.files.list.
So we could just run that for now, and that would just list
all of them.
But by passing the queue parameter is a query created
by whatever parameters you want.
Let's just have a quick look at these parameters in our
documentation.
So these are the fields available--
Title, Full Text, MIME Type, Modified Date, Last Viewed By
Me Date, Trash, Start, Hidden, Parent.
So all of these you can see them, and that's in our
documentation--
developers.google.com/drive--
and under the tag of search parameters.
That's where you can find that.
So we have these parameters.
We also have some operators, depending on how you want to
apply it-- "in" for collections, "contains" for
strings within others strings. "Equals," obviously, is
equality, "less than" and "greater than" for dates.
And you can combine these terms with "and" and you can
negate these terms with "not." So that's pretty much whatever
you want to do.
So back to the shell.
So we want to say, OK, we want fruit in
the title, for example.
I'm going to use double quotes here, because the query
language uses single quotes.
So fruit, and the field title contains fruit.
How about that?
So we'll do that, and we'll execute it, and we
see loads of stuff.
OK, so this just shows us that we've found a search.
It's not an empty result.
So what I'm going to do now is I'm going to write a quick
function to be able to run these queries and return us
more useful information that fits on a page.
So we're going to call it "execute query," and it's
going to take a query that we'll call "q." That will be a
string that's parsed in.
And what's it going to do?
Well, let's have a look.
Items equals drive.files.list.
q equals q.execute.
Let's graph the items deck.
There will be a list of items that have been returned.
And then let's just print the length of that.
We don't really want to see all the individual items, so
print length.
Results found.
And a full stop to be good.
And let's just test that out.
So execute query, and let's do the same one.
So fruit, title contains fruit.
Title contains fruit.
Here's where it doesn't work.
There we go, two results found.
So those are our two files, and that's great.
So let's play about with that.
So we have title.
Title here is the field that we're going to be looking for.
So if you want to search within the full text, you just
say full text equals fruit.
And full text obviously includes the title and the
body of the file itself.
So we found our two results.
Now, let's do the query for a type of fruit that we know is
in the body.
So let's try banana, and we know there are more results.
There were eight.
Remember, we found the those eight results earlier.
And let's do a mango that is only in the doc,
and not in the PDF.
And there you go.
It's only in one result found.
So what I really want to show you here is that
it's treating PDFs--
and it does the same for images that have text in them.
It's treating them as if they're plain text files or
text files that can be indexed.
Massively useful.
Essentially doing OCR on your stuff and being able to return
the results with the queries that you provide.
So that's that one.
And there are other query parameters.
So we're going to do a few, and we're going to show you a
few more operators.
So MIME type, that's a pretty cool one.
So you can query by MIME type.
I just want mime type equals--
and let's think of an easy one to type-- application PDF.
This is all the PDFs in my Google Drive.
That's 22 results.
It's pretty great.
Another thing we can do is we can look
at the parent hierarchy.
So I can say I want files that are only
within a certain directory.
So let's do another query here.
Let's say I want files that are in the root directory.
Now, root is a special alias for the base directory in your
Google Drive.
And so this means files that are in that are not in any
other folders.
So root in parents, and we've got six results.
So if we go back to Google Drive, we can see that there
are others there.
So it's looking at these four and a couple of others that
I've hidden away.
So that's that.
So that's the parents.
And again, we can look at in children, which looks at which
files are children of the folder.
So we have that case covered.
Combining these, that's pretty cool.
You can combine these as well.
So you can say root in parents and full text contains mango.
That should return one result, and it has done that.
So this is looking for files that are in the root
directory, and the full text contains mango.
And you can just combine these.
So let's see, and not full text contains mango.
So this wants stuff in the directory that is not
containing the word mango.
And we see the six files there.
So using this, you can combine these search queries.
We also have a few little tweaks here and there that
we've added to make the stuff more useful.
One thing we just launched yesterday, actually, is a
query that people have been asking for on Stack Overflow,
and personally I think something
that's very, very useful.
It's called Shared with Me.
So that's the complete query is you just execute "shared
with me."
And that returns files in the Google Drive that have been
shared with me.
If you wanted to do that query otherwise, you'd have to say,
well, I'm not the owner of the file, but I am in the readers,
or I am in the writers, something like that.
You'd have to create that query, and it's not exactly
perfect, and people have been having trouble.
So we've just launched this--
Shared with Me--
and that returns those 28 results.
Really, really useful if you want to separate what your
files are and what someone else's files are, which leads
me to searching for permissions on files.
So you can say I want files where I am a writer.
So you would say me, and that is another alias for my
username, but I can just aafshar@gmail.com in writers.
So these are files that I can write to, I have the
permission to write to.
And then we get 99 results.
Actually, it's bigger than that.
99 is the page size.
So if you wanted the next set of pages, you'd do that.
And these files are all my images.
I have thousands of images in my Google Drive photographs.
So this is just looking at them.
Again, if I do readers, we should again get 99 results.
That's all the files I can read.
And the last one is owners.
OK And that's going to take a bigger query, and it's taking
a bit of time.
100 results found.
So let's combine this query with the "shared with me"
query to see what happens.
So this is files which are shared with me, and
I'm an owner of.
It's quite an interesting case.
It means someone has shared me the file and made me
the owner of it.
Zero results.
It's not going to happen very often that someone shares you
a file and makes you an owner.
It's more likely they'll share the file, allow you to write
or comment on it, and will read it, but maintain
ownership in it for themselves.
So that's searching.
As I say, we've got documentation here.
We have a number of examples that I encourage you to try.
Title equals hello, MIME type we showed you.
Title contains, negation, we showed you.
Using the full text, which is the body and the description,
and the title.
We showed you how to search by permission, as in using
in-writers, in-readers.
We showed you how to test for the parent hierarchy, as in
using the in-parents query.
We showed you how to combine queries together using the
"and" operator, and we showed you how to negate queries with
the "not" operator.
I see at the end we've got just one more query, which is
the date style queries.
And that's where you can use greater than, less than,
greater than or equal to to specify what the relationship
is with the date.
So greater than a date means after a date.
It kind of makes sense.
So, hang on.
Let's just copy paste that query and run it.
Execute query.
Oops.
So this is going to look for files with a modified date
after the fourth and the sixth of this year.
And that's going to be a lot--
100 results found.
Let's find files that are modified before then.
And that might be zero, actually.
I think I cleaned out my Google Drive.
Oh, no.
That's more as well.
So I do have some old files and some new files.
Let's put an earlier date in here, so we
can kind of get this.
I don't know, 2007.
There you go.
So no files in my Drive were modified before then.
As usual, we're keen to get you up and running on this.
If you find something you want to search for but you can't do
immediately, if you find that any particular queries are
slow, these are things we really need to know about.
So please let us know.
Let us know on Stack Overflow.
I'm always available.
You can contact me on Google+ or on Gmail, things we
absolutely, absolutely, need to know about
If you find anything good, or you want something expanded,
also let us know.
So that's searching in the Google Drive API.
Thank you very much, and we look forward to
hearing from you.
[MUSIC PLAYING]