Back

[SOLVED] string & int indexing error

  • 0
  • Databases
  • Flutter
frankenstein
28 Feb, 2023, 08:33

Hello. I'm developing an ecommerce site and real estate both of these I have to query a string using query.equal() and order documents by int that can be price or something else. I'm constantly getting an index error. Only ordering by $createdAt works. This is crucial to my sites. Any help.??

TL;DR
The user was experiencing an index error when trying to query and order documents based on a string attribute and an integer attribute. The solution was to create a separate index for the ordering attribute in the Appwrite Console. By setting the index key to the attribute name and the type to 'key', the 'listDocuments' function should work properly.
Meldiron
28 Feb, 2023, 08:45

Hey there πŸ‘‹

Indexes makes write operations tiny-bit slower, and read operations MUCH faster.

In Appwrite 1.2, you are required to setup index for each query that you make. Appwrite does it to keep your app safe by default. Complex query without index could make your application really slow.

With that said, solution is really simple. Let's say you run listDoucuments and get error like Index not found: category, price.

What you need to do is go to Appwrite Console (web console), enter your collection, and go into indexes tab. There, add a new index:

  • Index key is just a name for you. Can be whatever
  • Set type to key. Thats basic index key with no special functionality
  • Add all attributes mentioned in the exception that you got (category, index)
  • Set order to ASC. Direction doesn' make too much difference, so feel free to use ASC always if you don't want to dig deeper into theory about indexing

Aaand that's it πŸŽ‰ Your listDocuments should now run properly. I am attaching screenshot of the above example:

Meldiron
28 Feb, 2023, 08:46

Oh, and this might get simpler in future. I believe we are researching option of making indexes optional. No promises yet.

frankenstein
28 Feb, 2023, 08:48

Okay I got you

frankenstein
28 Feb, 2023, 08:50

I hae done that, but the issue comes to last attribute for ordering documents

frankenstein
28 Feb, 2023, 08:51

I have 6 string attributes that are on query.equal() the last one is price (int) which is desc

Meldiron
28 Feb, 2023, 08:51

Hmm. Soo the problem is that index creation fails? because of the integer attribute?

frankenstein
28 Feb, 2023, 08:53

Index creation succeeds but when I query I get that index not found indicating that last attributes with error code 400

Meldiron
28 Feb, 2023, 08:54

Could you please share the error message with me?

frankenstein
28 Feb, 2023, 08:59

Okay

frankenstein
28 Feb, 2023, 09:19

i have found a solution: the ordering index shoud be a separate index from the rest queries

frankenstein
28 Feb, 2023, 09:19

so i have created a query index of strings then a seaparate query for ordering

frankenstein
28 Feb, 2023, 09:19

then it has worked

Drake
28 Feb, 2023, 17:11

[SOLVED] string & int indexing error

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