Back

[SOLVED] Create OR query to receive documents where one or multiple attributes fits a search term

  • 0
  • Databases
  • Web
  • Self Hosted
truth
27 Feb, 2023, 22:35

Excellent. so that works, and I would want to use variables.

TL;DR
The user was looking for help in creating an OR query to retrieve documents based on one or multiple attributes that match a search term. They provided some code snippets in GraphQL syntax and asked for assistance in using variables and escaping the search variable in the queries. Other users provided suggestions and explanations about GraphQL syntax and how to structure the query to achieve the desired results. Solution: The final solution provided was to use the `Query.search()` function with the desired attribute and value for each search term. This can be done by passing the function as the variable value in the query.
Drake
27 Feb, 2023, 22:36

right...so if you're passing in variables, you can put Query.search("attr", "value") for the variable value

truth
27 Feb, 2023, 22:38

It's confusing when you switch between different syntax.

truth
27 Feb, 2023, 22:39

I tried this query queries: ["search(\"group_a\", \"hello\")","search(\"group_b\", \"hello\")"] ERROR: Search method requires fulltext index: group_a,group_b

Is there an $or operator?

Drake
27 Feb, 2023, 22:39

no you can't search against multiple attributes like that. that will be treated like AND. you must execute multiple queries

truth
27 Feb, 2023, 22:40

How would I fix this in a single request?

Drake
27 Feb, 2023, 22:40

with graphql you can make muliple api calls in 1

truth
27 Feb, 2023, 22:40

yes

Drake
27 Feb, 2023, 22:45
TypeScript
query search($databaseId: String!, $collectionId: String!, $search1: String, $search2: String) {
      search1: databasesListDocuments (
          databaseId: $databaseId
          collectionId: $collectionId
          queries: [$search1]
      ) {
          total
          documents{
              _id
              _createdAt
              _updatedAt
              _permissions
              data
          }
      }

    search2: databasesListDocuments(
        databaseId: $databaseId
        collectionId: $collectionId
        queries: [$search2]
    ) {
        total
        documents{
            _id
            _createdAt
            _updatedAt
            _permissions
            data
        }
    } 
}
truth
27 Feb, 2023, 23:02

Thanks. I can't figure out how to escape the usage of the search variable in the queries

truth
27 Feb, 2023, 23:04

Why do some Strings! contain a bang?

Drake
27 Feb, 2023, 23:31

what exactly is your code?

Drake
27 Feb, 2023, 23:32

That's graphQL syntax for non-nulable. see https://graphql.org/learn/schema/

truth
28 Feb, 2023, 00:51
TypeScript
      search1: databasesListDocuments (
          databaseId: $databaseId
          collectionId: $collectionId
          queries: ["search(\"group_a\", \"$search1\")"]
      ) {
          total
          documents{
              _id
              _createdAt
              _updatedAt
              _permissions
              data
          }
      }

    search2: databasesListDocuments(
        databaseId: $databaseId
        collectionId: $collectionId
        queries: ["search(\"group_b\", \"$search1\")"]
    ) {
        total
        documents{
            _id
            _createdAt
            _updatedAt
            _permissions
            data
        }
    } 
}```
Drake
28 Feb, 2023, 00:53

This is just your query. What's your actual code in your app?

truth
28 Feb, 2023, 00:54

Do you mean what am I trying to do?

Drake
28 Feb, 2023, 00:55

no...i expect expect this to be in some javascript code if you're making a javacsript based app.

truth
28 Feb, 2023, 00:56

Currently I am playing around with this query in graphql explorer. I have a React based website where I am attempting to display search results with this graphql query

Drake
28 Feb, 2023, 00:58

ok so you would use our SDK and write something like:

TypeScript
import { Client, Graphql, Query } from "appwrite";

const client = new Client()
    .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your Appwrite Endpoint
    .setProject('[PROJECT_ID]');                // Your project ID

const graphql = new Graphql(client);

const query = graphql.query({
    query: `query search($databaseId: String!, $collectionId: String!, $search1: String, $search2: String) {
        search1: databasesListDocuments (
            databaseId: $databaseId
            collectionId: $collectionId
            queries: [$search1]
        ) {
            total
            documents{
                _id
                _createdAt
                _updatedAt
                _permissions
                data
            }
        }
  
      search2: databasesListDocuments(
          databaseId: $databaseId
          collectionId: $collectionId
          queries: [$search2]
      ) {
          total
          documents{
              _id
              _createdAt
              _updatedAt
              _permissions
              data
          }
      } 
  }`,
    variables: {
        databaseId: '...',
        collectionId: '...',
        search1: Query.search('group_a', value),
        search2: Query.search('group_b', value)
    }
});
Drake
28 Feb, 2023, 00:59

in the explorer it would have been something like

truth
28 Feb, 2023, 01:00

Is it too soon to say I love you?

truth
28 Feb, 2023, 01:01

Let me check this out..

truth
28 Feb, 2023, 01:01

Thank you

truth
28 Feb, 2023, 07:09

This worked thank you

Drake
28 Feb, 2023, 17:05

[SOLVED] Create OR query to receive documents where one or multiple attributes fits a search term

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