DEV Community

Discussion on: Appwrite x DEV Hackathon Help Thread

Collapse
 
tqbit profile image
tq-bit • Edited

Hello guys,

I'm struggling with Appwrite indexes. Here's my use case:

  • I have a database table with three columns: id, username and is_public
  • I would like to use the Web SDK to query:
    • All items from a single user (username = user.name)
    • AND only items of this user that were made public (is_public = true)

For this, I tried to create a search index. I tried a few names:

  • is_public-username <- Received error message in the browser's console: AppwriteException: Index not found: user_id,is_public - Link to image
  • is_public,username <- Does not qualify as a valid index key - Link to image
  • Any other index name I tried would not work
  • If I only try and query for a single column, everything works as I would expect it. - Link to image

Here's the part of the FE code that I use to specify the query:

const response = await appwriteClient.database.listDocuments(
        RECIPES_COLLECTION_ID,
        [Query.equal('user_id', userId), Query.equal('is_public', true)]
);
Enter fullscreen mode Exit fullscreen mode

Unfortunatly, I could not find any explanation on what I could do. So my question is:

  • What name must I give my search index to consider both query parameters using the web SDK?
  • Am I doing something wrong about the query building? I tried and follow the explanations on indexes and query building with no success
Collapse
 
gewenyu99 profile image
Vincent Ge • Edited

Hi, you were reaaallllly close to getting it right. The issue you were having with is_public,username <- Does not qualify as a valid index key - Link to image is that you had a comma in the Index Key. We don't allow commas in the name of an index because that really messes with parsing SQL :P

For example, this is from my project, patina-bot

await database.listDocuments("memedex", [sdk.Query.equal("user_id", msg.author.id), sdk.Query.equal("meme_name", memeName)]);
Enter fullscreen mode Exit fullscreen mode

Here's how I would create a two column index:

image

Collapse
 
gewenyu99 profile image
Vincent Ge

And here's what the created index looks like

image

Thread Thread
 
tqbit profile image
tq-bit

thank you so much for your reply. Works like a charm now 🥳