Using python driver to search and return all documents where index matches a string value

I have a collection named users.
Documents inside users have a fieldname named usertype.
There are two user types: “Engineering Talent”, “Manager”.
Assuming I have an index that allows searching users by usertype:
What is the best way to query all documents containing a particular usertype?

It seems to work well on the gui:
Screen Shot 2022-08-18 at 1.20.24 PM

Currently, I am using

talent_data = client.query(q.get(q.match(q.index("userType_index"), "Engineering Talent")))

However, this only returns the first match.

I have been doing some reading, and it seems one can achieve what I need using a combination of the built in functions: q.map, q.paginate, q.lambda.

However, I am struggling to implement this cleanly.

Thanks for your help in advance!

However, this only returns the first match.

Indeed, Get fetches a single document.

For multiple documents, you need to use Paginate to fetch a page of results. Map can be used to iterate over each item in a page (or set). Your query should look something like:

talent_data = client.query(
  q.map_(
    q.lambda_("ref", q.get(q.var("ref"))),
    q.paginate(
      q.match(q.index("userType_index"), "Engineering Talent")
    )
  )
)

The q.lambda_ is an anonymous function that is called for every item in the q.paginate, and the function calls q.get on each reference that it receives.

If you used an index that specified fields in its values definition, the parameter signature of the q.lambda_ would have to match the number of fields.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.