Back

Multiple search for different attributes

  • 0
  • Web
rohan
2 May, 2023, 17:35

Hi i want a search a user either by his name or phone number... How do i do that using Query.equal?

TL;DR
To search a user either by their name or phone number using `Query.equal`, you can follow the steps below: 1. Create a search attribute that combines the name and phone number fields. 2. Use `Query.search()` to dynamically search the combined attribute. 3. If you're using a custom collection, modify the code to query the database instead. 4. If Appwrite doesn't support the OR operation, you can use a workaround by making two API calls. 5. In the first API call, search by email using `Query.equal("email", ["john@appwrite.io"])`. 6. If no results are found, make
Binyamin
2 May, 2023, 17:41

Searching of user can work only with the Users API with Server side SDK. And that means you'll need either deploy Appwrite function or in any backend code you're using.

So, this function will let you to run the query against the users https://appwrite.io/docs/server/users?sdk=nodejs-default#usersList

And the final code can be something similar to this (JavaScript snippet)

TypeScript
client
    .setEndpoint('https://cloud.appwrite.io/v1') // Your API Endpoint
    .setProject('5df5acd0d48c2') // Your project ID
    .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
;

try {
  const users = await users.list([
    Query.equal("email", ["john@appwrite.io"]),
    Query.equal("phone", ["+1234567890"]),
  ]);

} catch (e) {
  // TODO: handle error
}

All query-able attributes can be found here in the user model https://appwrite.io/docs/models/user

rohan
2 May, 2023, 17:43

but i want to search a string in a attribute

Binyamin
2 May, 2023, 17:43

Meaning?

rohan
2 May, 2023, 17:44

for example i want to start searching for either name for phone number

rohan
2 May, 2023, 17:45

it should work like Query.search("name", search) **OR** Query.search("phoneNumber", search)

Binyamin
2 May, 2023, 17:45

Mmm. Got you

Binyamin
2 May, 2023, 17:45

There is no option from what I know to do in Appwrite

rohan
2 May, 2023, 17:49

hmm 😦

Binyamin
2 May, 2023, 17:49

I think there some issue regards this one.

Guille
2 May, 2023, 17:52

appwrite doesn't support OR operation in that case.

But the there is a workaround making two API calls

TypeScript
let searchUser = null;

try {
  // search by email
  searchUser = await users.list([
    Query.equal("email", ["john@appwrite.io"]),
  });

  // or search by phone
  if(!searchUser || searchUser.documents.length === 0) {
   searchUser = await users.list([
      Query.equal("phone", ["+1234567890"])
  }
} catch (e) {
  // handle error
}
Drake
2 May, 2023, 17:55

do you specifically want to search through Users or your custom Collection?

Binyamin
2 May, 2023, 18:01
rohan
2 May, 2023, 18:30

its custom collection

Binyamin
2 May, 2023, 18:31

Ohh Then just change the code to query the database instead

rohan
2 May, 2023, 18:32

yeah but thats not an efficient approach i guess

Binyamin
2 May, 2023, 18:32
TypeScript
client
  .setEndpoint('https://your-server-domain-or-ip/v1')
  .setProject('5df5acd0d48c2');

try {
  const users = await databases.listDocuments('[DATABASE_ID]', '[COLLECTION_ID]',
    [
      Query.search('email', 'john@appwrite.io'),
      Query.search('phone', '+1234567890')
    ]
  );

} catch (e) {
  // TODO: handle error
}
rohan
2 May, 2023, 18:33

im trying Query.search()

Binyamin
2 May, 2023, 18:33

Updated it It work the same

rohan
2 May, 2023, 18:33

as i type the search box.. it should dynamically search name and phone number

Drake
2 May, 2023, 18:34

This would be the related feature request then

Drake
2 May, 2023, 18:34

as a workaround, you can create search attribute that has all the text you want to search against

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more