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
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
Thank you @Binyamin What would be the use cases for full index with more than one attribute?
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
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:
Query.search("search","John Jane")
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?
Ohh I though this is way you've ment
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
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.
[
Query.endWith('fisrt_name','name');
]
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
[
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
Ohh, I see When you're using the equal query is like you're doing this
SELECT * FROM collection_name WHERE attribute1='value1' AND attribute2='value2'
When you do it with search you'll get something like this
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.
Thank you @Guille now I understand better!
[SOLVED] Query.Search and multiples attributes
Recommended threads
- Type Mismatch in AppwriteException
There is a discrepancy in the TypeScript type definitions for AppwriteException. The response property is defined as a string in the type definitions, but in pr...
- What Query's are valid for GetDocument?
Documentation shows that Queries are valid here, but doesn't explain which queries are valid. At first I presumed this to be a bug, but before creating a githu...
- Custom emails
What happen if I use a third party email provider to customize my emails and my plan run out of emails/month? Appwrite emails are used as fallback sending emai...