Back

[SOLVED] Query.Search and multiples attributes

  • 0
  • Databases
  • Web
Guille
1 Jun, 2023, 19:27

If I have a full index with 3 attributes

  • name
  • firstname
  • lastname

How should I write the search query?

Query.search("","text")

Documentation doesn't show any information related to this or at least I didn't find anything.

What I'm basically trying to do is search a text in name, firstname and lastname attributes

TL;DR
The user was discussing the difference between using an equal query and a search query in Appwrite. The user was trying to understand how to add multiple attributes to a full-text index. It was suggested to concatenate the desired attributes into a single field and add a search index to it. This way, the user can search all the attributes at once. No solution was provided for searching multiple attributes directly with a full-text index.
Binyamin
1 Jun, 2023, 19:35

This is a feature that has been requested in the past, as you can't search using logic or condition. As of that any multiple Query.search will result logic and and it will never give the results you're looking for.

The best workaround would be to create another attribute that will hold all other attributes together and add search index on it. You can see the Issue and Steven suggesting about that here https://discord.com/channels/564160730845151244/1103011908518416444/1103026744363069480

Guille
1 Jun, 2023, 22:07

Thank you @Binyamin What would be the use cases for full index with more than one attribute?

Binyamin
1 Jun, 2023, 22:12

For example in your use case you can have a collection like this

  • name
  • first name
  • last name
  • search The only one with search index And you can create function that triggered whenever a new document is created or updated

Then you can do something like this

TypeScript
const document = JSON.parse(req.variables.APPWRITE_FUNCTION_EVENT_DATA ?? '{}') ?? {};

await databases.updateDocument(document.$id,{
 'search' :`${document.name} ${document.firstName} ${document.lastName} `,
});

Now you can search your search field and you'll be able to search all fields. For example to get either John or Jane:

TypeScript
Query.search("search","John Jane")
Guille
2 Jun, 2023, 01:11

Thanks again @Binyamin I understood the workaround, I just have a doubt that when can be necessary to add 3 attributes in fulltext index and how it will be used?

Binyamin
2 Jun, 2023, 01:12

Ohh I though this is way you've ment

Binyamin
2 Jun, 2023, 01:13

You said in your question that you want to be able to search name, firstname and lastname attributes. Did you meant one by one? And you wanted to search for wildcard or something like that? If you want to search part of name like

TypeScript
SELECT email FROM users WHERE NAME LIKE '%name";

Then in version 1.3.0 Appwrite added two queries for that startsWith and endsWith So, to achieve the query noted before you can do something like this.

TypeScript
[
  Query.endWith('fisrt_name','name');
]
Guille
5 Jun, 2023, 19:27

yes, it's what I want, my second question was related about the cases when I need to add 2 or more attributes to a fulltext index

I understand for example if I have two key index I can do

TypeScript
[
  Query.qual("attribute1","value1"),
  Query.qual("attribute2","value2")
]

but it doesn't work the same way for a fulltext index right? regardless of the logic "or/and" condition

Binyamin
5 Jun, 2023, 20:15

Ohh, I see When you're using the equal query is like you're doing this

TypeScript
  SELECT * FROM collection_name WHERE attribute1='value1' AND attribute2='value2'

When you do it with search you'll get something like this

TypeScript
  SELECT * FROM collection_name WHERE MATCH (attribute1) AGAINST ('search values' IN BOOLEAN MODE);

The different is that full text search works with MariaDB algorithm for extracting pieces of text from a large strings. You read it about it here: https://mariadb.com/kb/en/full-text-index-overview/

Also, a side note. The different approach between equal and full text, is that full text is meant for data the need to be extracted from a large text and data that is not indexable.

Guille
7 Jun, 2023, 16:35

Thank you @Guille now I understand better!

Guille
7 Jun, 2023, 16:35

[SOLVED] Query.Search and multiples attributes

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